Skip to content

Commit

Permalink
feat: implement read-params interface (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Nov 20, 2024
1 parent 864ab73 commit 1dff78f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
8 changes: 8 additions & 0 deletions balius-runtime/src/ledgers/mock.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde_json::json;

use crate::wit::balius::app::ledger as wit;

#[derive(Clone)]
Expand Down Expand Up @@ -37,4 +39,10 @@ impl Ledger {
) -> Result<wit::UtxoPage, wit::LedgerError> {
todo!()
}

pub async fn read_params(&mut self) -> Result<wit::Json, wit::LedgerError> {
let json = json!({ "param1": 4 });
let bytes = serde_json::to_vec(&json).unwrap();
Ok(bytes.into())
}
}
7 changes: 7 additions & 0 deletions balius-runtime/src/ledgers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ impl wit::Host for Ledger {
}
}
}

async fn read_params(&mut self) -> Result<wit::Json, wit::LedgerError> {
match self {
Ledger::Mock(ledger) => ledger.read_params().await,
Ledger::U5C(ledger) => ledger.read_params().await,
}
}
}
26 changes: 26 additions & 0 deletions balius-runtime/src/ledgers/u5c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<wit::Json, wit::LedgerError> {
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(&params).unwrap())
}
_ => Err(wit::LedgerError::Upstream(
"unexpected response from read_params".to_string(),
)),
}
}
}
13 changes: 8 additions & 5 deletions balius-sdk/src/txbuilder/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ impl crate::txbuilder::Ledger for ExtLedgerFacade {
}
Ok(utxos.into())
}

fn read_params(&self) -> Result<PParams, BuildError> {
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<T, L>(tx: T, ledger: L) -> Result<primitives::Tx, BuildError>
Expand All @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions balius-sdk/src/txbuilder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,12 @@ impl From<crate::wit::balius::app::ledger::LedgerError> 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<dsl::UtxoSet, BuildError>;
fn search_utxos(&self, pattern: &dsl::UtxoPattern) -> Result<dsl::UtxoSet, BuildError>;
fn read_params(&self) -> Result<PParams, BuildError>;
}

pub struct BuildContext {
Expand Down
2 changes: 2 additions & 0 deletions wit/balius.wit
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface kv {

interface ledger {
type cbor = list<u8>;
type json = list<u8>;

variant ledger-error {
upstream(string),
Expand Down Expand Up @@ -49,6 +50,7 @@ interface ledger {

read-utxos: func(refs: list<txo-ref>) -> result<list<utxo>, ledger-error>;
search-utxos: func(pattern: utxo-pattern, start: option<string>, max-items: u32) -> result<utxo-page, ledger-error>;
read-params: func() -> result<json, ledger-error>;
}

interface submit {
Expand Down

0 comments on commit 1dff78f

Please sign in to comment.