Skip to content

Commit

Permalink
Merge pull request #103 from sidan-lab/feature-update/set-fee
Browse files Browse the repository at this point in the history
Feature update/set fee
  • Loading branch information
twwu123 authored Dec 2, 2024
2 parents 981f8de + 6978cf0 commit f622669
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 22 deletions.
12 changes: 6 additions & 6 deletions packages/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 packages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
version = "0.9.6"
version = "0.9.7"
resolver = "2"
members = [
"sidan-csl-rs",
Expand Down
4 changes: 2 additions & 2 deletions packages/sidan-csl-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sidan-csl-rs"
version = "0.9.6"
version = "0.9.7"
edition = "2021"
license = "Apache-2.0"
description = "Wrapper around the cardano-serialization-lib for easier transaction building, heavily inspired by cardano-cli APIs"
Expand All @@ -10,7 +10,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
getrandom = { version = "0.2", features = ["js"] }
cardano-serialization-lib = "=13.1.0"
cardano-serialization-lib = "=13.2.0"
hex = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
8 changes: 8 additions & 0 deletions packages/sidan-csl-rs/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub fn serialize_tx_body(
// self.add_collateral_return(self.tx_builder_body.change_address.clone());
// }
// }
if tx_builder_body.fee.is_some() {
TxBuilderCore::set_fee(&mut mesh_csl, tx_builder_body.fee.unwrap());
}
mesh_csl.add_change(
tx_builder_body.change_address.clone(),
tx_builder_body.change_datum.clone(),
Expand Down Expand Up @@ -158,6 +161,7 @@ impl TxBuilderCore {
invalid_hereafter: None,
},
signing_key: vec![],
fee: None,
network: None,
},
tx_evaluation_multiplier_percentage: 110,
Expand Down Expand Up @@ -434,6 +438,10 @@ impl TxBuilderCore {
// )
// .unwrap();
// }

fn set_fee(mesh_csl: &mut MeshCSL, fee: String) {
mesh_csl.set_fee(fee);
}
}

impl Default for TxBuilderCore {
Expand Down
5 changes: 5 additions & 0 deletions packages/sidan-csl-rs/src/core/core_csl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ impl MeshCSL {
.set_ttl_bignum(&to_bignum(invalid_hereafter));
}

pub fn set_fee(&mut self, fee: String) {
self.tx_builder
.set_fee(&csl::BigNum::from_str(&fee).expect("Error parsing fee amount"));
}

pub fn add_change(
&mut self,
change_address: String,
Expand Down
1 change: 1 addition & 0 deletions packages/sidan-csl-rs/src/core/tx_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl MeshTxParser {
invalid_hereafter: None,
},
signing_key: vec![],
fee: None,
network: None,
};
let csl_tx = csl::Transaction::from_hex(s).expect("Invalid transaction");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ pub struct TxBuilderBody {
pub certificates: Vec<Certificate>,
pub votes: Vec<Vote>,
pub signing_key: Vec<String>,
pub fee: Option<String>,
pub network: Option<Network>,
}
4 changes: 2 additions & 2 deletions packages/whisky-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whisky-examples"
version = "0.9.6"
version = "0.9.7"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"
Expand All @@ -13,4 +13,4 @@ path = "src/server.rs"
actix-cors = "0.7.0"
actix-web = "4.9.0"
serde = "1.0.209"
whisky = { version = "=0.9.6", path = "../whisky" }
whisky = { version = "=0.9.7", path = "../whisky" }
4 changes: 2 additions & 2 deletions packages/whisky/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whisky"
version = "0.9.6"
version = "0.9.7"
edition = "2021"
license = "Apache-2.0"
description = "The Cardano Rust SDK, inspired by MeshJS"
Expand All @@ -24,7 +24,7 @@ pallas-codec = { version = "0.30.2", features = ["num-bigint"] }
pallas-primitives = "0.30.2"
pallas-traverse = "0.30.2"
maestro-rust-sdk = "1.1.3"
sidan-csl-rs = { version = "=0.9.6", path = "../sidan-csl-rs" }
sidan-csl-rs = { version = "=0.9.7", path = "../sidan-csl-rs" }
reqwest = "0.12.5"
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }

Expand Down
31 changes: 22 additions & 9 deletions packages/whisky/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,7 @@ impl TxBuilder {
///
/// * `Self` - The TxBuilder instance
pub fn invalid_hereafter(&mut self, slot: u64) -> &mut Self {
self.core
.tx_builder_body
.validity_range
.invalid_hereafter = Some(slot);
self.core.tx_builder_body.validity_range.invalid_hereafter = Some(slot);
self
}

Expand Down Expand Up @@ -334,6 +331,22 @@ impl TxBuilder {
self
}

/// ## Transaction building method
///
/// Sets a specific fee amount, if the fee is insufficient, the transaction will fail to build
///
/// ### Arguments
///
/// * `fee` - The fee amount
///
/// ### Returns
///
/// * `Self` - The TxBuilder instance
pub fn set_fee(&mut self, fee: &str) -> &mut Self {
self.core.tx_builder_body.fee = Some(fee.to_string());
self
}

/// ## Transaction building method
///
/// Selects the network to use, primarily to decide which cost models to use for evaluation and calculating script integrity hash
Expand Down Expand Up @@ -424,7 +437,10 @@ impl TxBuilder {
}
Vote::BasicVote(_) => {}
}
self.core.tx_builder_body.votes.push(self.vote_item.clone().unwrap());
self.core
.tx_builder_body
.votes
.push(self.vote_item.clone().unwrap());
self.vote_item = None;
}

Expand Down Expand Up @@ -571,10 +587,7 @@ impl TxBuilder {
address: Some(input.output.address.clone()),
},
});
self.core
.tx_builder_body
.inputs
.push(pub_key_input.clone());
self.core.tx_builder_body.inputs.push(pub_key_input.clone());
self.input_for_evaluation(&input);
}
Ok(())
Expand Down
26 changes: 26 additions & 0 deletions packages/whisky/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,30 @@ mod int_tests {
println!("{}", unsigned_tx);
assert!(mesh.core.mesh_csl.tx_hex != *"");
}

#[test]
fn test_simple_spend_with_set_fee() {
let mut mesh = TxBuilder::new(TxBuilderParam {
evaluator: None,
fetcher: None,
submitter: None,
params: None,
});
let signed_tx = mesh
.tx_in(
"2cb57168ee66b68bd04a0d595060b546edf30c04ae1031b883c9ac797967dd85",
3,
&[Asset::new_from_str("lovelace", "9891607895")],
"addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh",
)
.change_address("addr_test1vru4e2un2tq50q4rv6qzk7t8w34gjdtw3y2uzuqxzj0ldrqqactxh")
.signing_key("51022b7e38be01d1cc581230e18030e6e1a3e949a1fdd2aeae5f5412154fe82b")
.set_fee("500000")
.complete_sync(None)
.unwrap()
.complete_signing().unwrap();

println!("{}", signed_tx);
assert!(mesh.core.mesh_csl.tx_hex != *"");
}
}

0 comments on commit f622669

Please sign in to comment.