From 1dff78fef36a2b8d09f739bc2cf3d545d3de27a9 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Wed, 20 Nov 2024 13:51:10 -0300 Subject: [PATCH] feat: implement read-params interface (#40) --- balius-runtime/src/ledgers/mock.rs | 8 ++++++++ balius-runtime/src/ledgers/mod.rs | 7 +++++++ balius-runtime/src/ledgers/u5c.rs | 26 ++++++++++++++++++++++++++ balius-sdk/src/txbuilder/build.rs | 13 ++++++++----- balius-sdk/src/txbuilder/mod.rs | 8 ++------ wit/balius.wit | 2 ++ 6 files changed, 53 insertions(+), 11 deletions(-) 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 d534ce8..17ff887 100644 --- a/balius-runtime/src/ledgers/mod.rs +++ b/balius-runtime/src/ledgers/mod.rs @@ -59,4 +59,11 @@ impl wit::Host for Ledger { } } } + + 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/balius-sdk/src/txbuilder/build.rs b/balius-sdk/src/txbuilder/build.rs index c07359f..5c71751 100644 --- a/balius-sdk/src/txbuilder/build.rs +++ b/balius-sdk/src/txbuilder/build.rs @@ -74,6 +74,13 @@ impl crate::txbuilder::Ledger for ExtLedgerFacade { } Ok(utxos.into()) } + + 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 @@ -83,11 +90,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()?, total_input: primitives::Value::Coin(0), spent_output: primitives::Value::Coin(0), estimated_fee: 0, diff --git a/balius-sdk/src/txbuilder/mod.rs b/balius-sdk/src/txbuilder/mod.rs index 5cbaa5f..8d103b0 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 { 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 {