Skip to content

Commit

Permalink
feat: pass protocol parameters to balius
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Dec 6, 2024
1 parent fb1fdff commit 81a415d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion firefly-cardanoconnect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ aide = { version = "0.13", features = ["axum"] }
anyhow = "1"
async-trait = "0.1"
axum = { version = "0.7", features = ["macros", "ws"] }
balius-runtime = { git = "https://github.com/SupernaviX/balius.git", rev = "3a899f9" }
balius-runtime = { git = "https://github.com/txpipe/balius.git", rev = "a72601f" }
blockfrost = { git = "https://github.com/SupernaviX/blockfrost-rust.git", rev = "cfa7a5e", default-features = false, features = ["rustls-tls"] }
blockfrost-openapi = "0.1.69"
clap = { version = "4", features = ["derive"] }
Expand All @@ -25,6 +25,8 @@ pallas-traverse = "0.31"
firefly-server = { path = "../firefly-server" }
hex = "0.4"
minicbor = "0.25"
num-rational = "0.4"
num-traits = "0.2"
rand = "0.8"
rand_chacha = "0.3"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
Expand All @@ -37,3 +39,4 @@ tokio = { version = "1", features = ["full"] }
tokio-rusqlite = { version = "0.5" }
tracing = "0.1"
ulid = "1"
utxorpc-spec = "0.12"
6 changes: 5 additions & 1 deletion firefly-cardanoconnect/src/blockfrost.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
pub use blockfrost::Pagination;
use blockfrost::{BlockFrostSettings, BlockfrostAPI, BlockfrostError, BlockfrostResult};
pub use blockfrost_openapi::models::{AddressUtxoContentInner, BlockContent, TxContentCbor};
pub use blockfrost_openapi::models::{AddressUtxoContentInner, BlockContent, EpochParamContent, TxContentCbor};

#[derive(Debug, Clone)]
pub struct BlockfrostClient {
Expand Down Expand Up @@ -53,6 +53,10 @@ impl BlockfrostClient {
Ok(self.api.blocks_txs(hash, pagination).await?)
}

pub async fn epochs_latest_parameters(&self) -> Result<EpochParamContent> {
Ok(self.api.epochs_latest_parameters().await?)
}

pub async fn transactions_cbor(&self, hash: &str) -> Result<TxContentCbor> {
Ok(self.api.transactions_cbor(hash).await?)
}
Expand Down
59 changes: 59 additions & 0 deletions firefly-cardanoconnect/src/contracts/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::collections::{hash_map::Entry, HashMap};

use async_trait::async_trait;
use balius_runtime::ledgers::{CustomLedger, LedgerError, TxoRef, Utxo, UtxoPage, UtxoPattern};
use num_rational::Rational32;
use num_traits::FromPrimitive as _;
use pallas_traverse::MultiEraTx;
use utxorpc_spec::utxorpc::v1alpha::cardano::{CostModel, CostModels, ExPrices, ExUnits, PParams, ProtocolVersion, RationalNumber};

use crate::blockfrost::{BlockfrostClient, Pagination};

Expand Down Expand Up @@ -35,6 +38,50 @@ impl CustomLedger for BlockfrostLedger {
Ok(result)
}

async fn read_params(&mut self) -> Result<Vec<u8>, LedgerError> {
let raw_params = self.client.epochs_latest_parameters().await.map_err(|e| LedgerError::Upstream(e.to_string()))?;
let params = PParams {
coins_per_utxo_byte: raw_params.coins_per_utxo_size.map(|v| v.parse().unwrap()).unwrap_or_default(),
max_tx_size: raw_params.max_tx_size as u64,
min_fee_coefficient: raw_params.min_fee_a as u64,
min_fee_constant: raw_params.min_fee_b as u64,
max_block_body_size: raw_params.max_block_size as u64,
max_block_header_size: raw_params.max_block_header_size as u64,
stake_key_deposit: raw_params.key_deposit.parse().unwrap(),
pool_deposit: raw_params.pool_deposit.parse().unwrap(),
pool_retirement_epoch_bound: raw_params.e_max as u64,
desired_number_of_pools: raw_params.n_opt as u64,
pool_influence: Some(f64_to_rational(raw_params.a0)),
monetary_expansion: Some(f64_to_rational(raw_params.rho)),
treasury_expansion: Some(f64_to_rational(raw_params.tau)),
min_pool_cost: raw_params.min_pool_cost.parse().unwrap(),
protocol_version: Some(ProtocolVersion { major: raw_params.protocol_major_ver as u32, minor: raw_params.protocol_minor_ver as u32 }),
max_value_size: raw_params.max_val_size.map(|v| v.parse().unwrap()).unwrap_or_default(),
collateral_percentage: raw_params.collateral_percent.map(|v| v as u64).unwrap_or_default(),
max_collateral_inputs: raw_params.max_collateral_inputs.map(|v| v as u64).unwrap_or_default(),
cost_models: raw_params.cost_models_raw.flatten().map(|models| {
let extract_model = |name| {
let val = models.get(name)?;
let array = val.as_array()?;
let values = array.iter().map(|v| v.as_i64().unwrap()).collect();
Some(CostModel { values })
};
CostModels {
plutus_v1: extract_model("PlutusV1"),
plutus_v2: extract_model("PlutusV2"),
plutus_v3: extract_model("PlutusV3"),
}
}),
prices: Some(ExPrices {
steps: raw_params.price_step.map(f64_to_rational),
memory: raw_params.price_mem.map(f64_to_rational),
}),
max_execution_units_per_transaction: ex_units(raw_params.max_tx_ex_steps, raw_params.max_tx_ex_mem),
max_execution_units_per_block: ex_units(raw_params.max_block_ex_steps, raw_params.max_block_ex_mem),
};
Ok(serde_json::to_vec(&params).unwrap())
}

async fn search_utxos(
&mut self,
pattern: UtxoPattern,
Expand Down Expand Up @@ -131,3 +178,15 @@ impl<'a> TxDict<'a> {
MultiEraTx::decode(bytes).map_err(|e| LedgerError::Internal(e.to_string()))
}
}

fn f64_to_rational(value: f64) -> RationalNumber {
let ratio = Rational32::from_f64(value).unwrap();
RationalNumber { numerator: *ratio.numer(), denominator: *ratio.denom() as u32 }
}

fn ex_units(step: Option<String>, mem: Option<String>) -> Option<ExUnits> {
Some(ExUnits {
steps: step?.parse().ok()?,
memory: mem?.parse().ok()?,
})
}
6 changes: 3 additions & 3 deletions wasm/simple-tx/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm/simple-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
repository = "https://github.com/SundaeSwap-finance/firefly-cardano"

[dependencies]
balius-sdk = { git = "https://github.com/SupernaviX/balius.git", rev = "3a899f9" }
balius-sdk = { git = "https://github.com/txpipe/balius.git", rev = "a72601f" }
pallas-addresses = "0.31.0"
pallas-traverse = "0.31.0"
serde = { version = "1", features = ["derive"] }
Expand Down

0 comments on commit 81a415d

Please sign in to comment.