From 710329c4e0022b45603c4de1a1067fa069aef6a5 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Wed, 20 Nov 2024 13:31:18 -0300 Subject: [PATCH 1/2] feat: implement read-params interface --- balius-runtime/src/ledgers/mock.rs | 8 ++++++++ balius-runtime/src/ledgers/mod.rs | 7 +++++++ balius-runtime/src/ledgers/u5c.rs | 26 ++++++++++++++++++++++++++ wit/balius.wit | 2 ++ 4 files changed, 43 insertions(+) diff --git a/balius-runtime/src/ledgers/mock.rs b/balius-runtime/src/ledgers/mock.rs index b537a3c..67cf44f 100644 --- a/balius-runtime/src/ledgers/mock.rs +++ b/balius-runtime/src/ledgers/mock.rs @@ -1,3 +1,5 @@ +use serde_json::json; + use crate::wit::balius::app::ledger as wit; #[derive(Clone)] @@ -37,4 +39,10 @@ impl Ledger { ) -> Result { todo!() } + + pub async fn read_params(&mut self) -> Result { + let json = json!({ "param1": 4 }); + let bytes = serde_json::to_vec(&json).unwrap(); + Ok(bytes.into()) + } } diff --git a/balius-runtime/src/ledgers/mod.rs b/balius-runtime/src/ledgers/mod.rs index 5eb3855..07fcc3b 100644 --- a/balius-runtime/src/ledgers/mod.rs +++ b/balius-runtime/src/ledgers/mod.rs @@ -44,4 +44,11 @@ impl wit::Host for Ledger { Ledger::U5C(ledger) => ledger.search_utxos(pattern, start, max_items).await, } } + + async fn read_params(&mut self) -> Result { + match self { + Ledger::Mock(ledger) => ledger.read_params().await, + Ledger::U5C(ledger) => ledger.read_params().await, + } + } } diff --git a/balius-runtime/src/ledgers/u5c.rs b/balius-runtime/src/ledgers/u5c.rs index 4b094fb..da9a22b 100644 --- a/balius-runtime/src/ledgers/u5c.rs +++ b/balius-runtime/src/ledgers/u5c.rs @@ -119,4 +119,30 @@ impl Ledger { let utxos = self.queries.match_utxos(pattern, start, max_items).await?; Ok(utxos.into()) } + + pub async fn read_params(&mut self) -> Result { + let req = utxorpc::spec::query::ReadParamsRequest::default(); + let res = self + .queries + .read_params(req) + .await + .map_err(|err| wit::LedgerError::Upstream(format!("{:?}", err)))? + .into_inner(); + + let params = res + .values + .and_then(|v| v.params) + .ok_or(wit::LedgerError::Upstream( + "unexpected response from read_params".to_string(), + ))?; + + match params { + utxorpc::spec::query::any_chain_params::Params::Cardano(params) => { + Ok(serde_json::to_vec(¶ms).unwrap()) + } + _ => Err(wit::LedgerError::Upstream( + "unexpected response from read_params".to_string(), + )), + } + } } diff --git a/wit/balius.wit b/wit/balius.wit index 54a44d3..b10e611 100644 --- a/wit/balius.wit +++ b/wit/balius.wit @@ -11,6 +11,7 @@ interface kv { interface ledger { type cbor = list; + type json = list; variant ledger-error { upstream(string), @@ -49,6 +50,7 @@ interface ledger { read-utxos: func(refs: list) -> result, ledger-error>; search-utxos: func(pattern: utxo-pattern, start: option, max-items: u32) -> result; + read-params: func() -> result; } interface submit { From c064f6c535973fb22636bca8605c8f5901d128d4 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Wed, 20 Nov 2024 13:33:49 -0300 Subject: [PATCH 2/2] add to builder --- balius-sdk/src/txbuilder/build.rs | 13 ++++++++----- balius-sdk/src/txbuilder/mod.rs | 8 ++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/balius-sdk/src/txbuilder/build.rs b/balius-sdk/src/txbuilder/build.rs index 6582560..ab12481 100644 --- a/balius-sdk/src/txbuilder/build.rs +++ b/balius-sdk/src/txbuilder/build.rs @@ -51,6 +51,13 @@ impl crate::txbuilder::Ledger for ExtLedgerFacade { fn search_utxos(&self, pattern: &UtxoPattern) -> Result { todo!() } + + fn read_params(&self) -> Result { + let bytes = crate::wit::balius::app::ledger::read_params()?; + + serde_json::from_slice(&bytes) + .map_err(|_| BuildError::LedgerError("failed to parse params json".to_string())) + } } pub fn build(tx: T, ledger: L) -> Result @@ -60,11 +67,7 @@ where { let mut ctx = BuildContext { network: primitives::NetworkId::Testnet, - pparams: PParams { - min_fee_a: 4, - min_fee_b: 3, - min_utxo_value: 2, - }, + pparams: ledger.read_params()?, estimated_fee: 1, ledger: Box::new(ledger), tx_body: None, diff --git a/balius-sdk/src/txbuilder/mod.rs b/balius-sdk/src/txbuilder/mod.rs index 93a8a3f..e7adf82 100644 --- a/balius-sdk/src/txbuilder/mod.rs +++ b/balius-sdk/src/txbuilder/mod.rs @@ -46,16 +46,12 @@ impl From for BuildError { pub use pallas_codec as codec; pub use pallas_primitives::conway as primitives; - -pub struct PParams { - pub min_fee_a: u64, - pub min_fee_b: u64, - pub min_utxo_value: u64, -} +pub use utxorpc_spec::utxorpc::v1alpha::cardano::PParams; pub trait Ledger { fn read_utxos(&self, refs: &[dsl::TxoRef]) -> Result; fn search_utxos(&self, pattern: &dsl::UtxoPattern) -> Result; + fn read_params(&self) -> Result; } pub struct BuildContext {