Skip to content

Commit f369b57

Browse files
committed
change-transactionbuilder-creation
1 parent 94c2f73 commit f369b57

File tree

3 files changed

+142
-41
lines changed

3 files changed

+142
-41
lines changed

rust/Cargo.lock

Lines changed: 89 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ clear_on_drop = { version = "0.2", features = ["no_cc"] }
3232
itertools = "0.10.1"
3333
getrandom = { version = "0.2.3", features = ["js"] }
3434
rand = "0.8.4"
35+
derive_builder = "0.10.2"
3536

3637
# non-wasm
3738
[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))'.dependencies]

rust/src/tx_builder.rs

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::*;
22
use super::fees;
33
use super::utils;
44
use std::collections::{BTreeMap, BTreeSet};
5+
use derive_builder::Builder;
56

67
// comes from witsVKeyNeeded in the Ledger spec
78
fn witness_keys_for_cert(cert_enum: &Certificate, keys: &mut BTreeSet<Ed25519KeyHash>) {
@@ -175,6 +176,19 @@ pub struct TransactionBuilder {
175176
prefer_pure_change: bool,
176177
}
177178

179+
#[wasm_bindgen]
180+
#[derive(Clone, Builder, Debug)]
181+
pub struct TransactionBuilderConfig {
182+
linear_fee: fees::LinearFee,
183+
pool_deposit: BigNum, // protocol parameter
184+
key_deposit: BigNum, // protocol parameter
185+
max_value_size: u32, // protocol parameter
186+
max_tx_size: u32, // protocol parameter
187+
coins_per_utxo_word: Coin, // protocol parameter
188+
#[builder(default = "false")]
189+
prefer_pure_change: bool,
190+
}
191+
178192
#[wasm_bindgen]
179193
impl TransactionBuilder {
180194
/// This automatically selects and adds inputs from {inputs} consisting of just enough to cover
@@ -610,21 +624,14 @@ impl TransactionBuilder {
610624
self.prefer_pure_change = prefer_pure_change;
611625
}
612626

613-
pub fn new(
614-
linear_fee: &fees::LinearFee,
615-
pool_deposit: &BigNum, // protocol parameter
616-
key_deposit: &BigNum, // protocol parameter
617-
max_value_size: u32, // protocol parameter
618-
max_tx_size: u32, // protocol parameter
619-
coins_per_utxo_word: &Coin, // protocol parameter
620-
) -> Self {
627+
pub fn new(cfg: &TransactionBuilderConfig) -> Self {
621628
Self {
622-
coins_per_utxo_word: coins_per_utxo_word.clone(),
623-
key_deposit: key_deposit.clone(),
624-
pool_deposit: pool_deposit.clone(),
625-
max_value_size,
626-
max_tx_size,
627-
fee_algo: linear_fee.clone(),
629+
coins_per_utxo_word: cfg.coins_per_utxo_word.clone(),
630+
key_deposit: cfg.key_deposit.clone(),
631+
pool_deposit: cfg.pool_deposit.clone(),
632+
max_value_size: cfg.max_value_size,
633+
max_tx_size: cfg.max_tx_size,
634+
fee_algo: cfg.linear_fee.clone(),
628635
inputs: Vec::new(),
629636
outputs: TransactionOutputs::new(),
630637
fee: None,
@@ -640,7 +647,7 @@ impl TransactionBuilder {
640647
validity_start_interval: None,
641648
mint: None,
642649
inputs_auto_added: false,
643-
prefer_pure_change: false,
650+
prefer_pure_change: cfg.prefer_pure_change,
644651
}
645652
}
646653

@@ -978,14 +985,16 @@ mod tests {
978985
max_val_size: u32,
979986
coins_per_utxo_word: u64,
980987
) -> TransactionBuilder {
981-
TransactionBuilder::new(
982-
linear_fee,
983-
&to_bignum(pool_deposit),
984-
&to_bignum(key_deposit),
985-
max_val_size,
986-
MAX_TX_SIZE,
987-
&to_bignum(coins_per_utxo_word)
988-
)
988+
let cfg = TransactionBuilderConfigBuilder::default()
989+
.linear_fee(linear_fee.clone())
990+
.pool_deposit(to_bignum(pool_deposit))
991+
.key_deposit(to_bignum(key_deposit))
992+
.max_value_size(max_val_size)
993+
.max_tx_size(MAX_TX_SIZE)
994+
.coins_per_utxo_word(to_bignum(coins_per_utxo_word))
995+
.build()
996+
.unwrap();
997+
TransactionBuilder::new(&cfg)
989998
}
990999

9911000
fn create_tx_builder(
@@ -2428,14 +2437,16 @@ mod tests {
24282437
fn tx_builder_cip2_random_improve_when_using_all_available_inputs() {
24292438
// we have a = 1 to test increasing fees when more inputs are added
24302439
let linear_fee = LinearFee::new(&to_bignum(1), &to_bignum(0));
2431-
let mut tx_builder = TransactionBuilder::new(
2432-
&linear_fee,
2433-
&Coin::zero(),
2434-
&to_bignum(0),
2435-
9999,
2436-
9999,
2437-
&to_bignum(0),
2438-
);
2440+
let cfg = TransactionBuilderConfigBuilder::default()
2441+
.linear_fee(linear_fee)
2442+
.pool_deposit(to_bignum(0))
2443+
.key_deposit(to_bignum(0))
2444+
.max_value_size(9999)
2445+
.max_tx_size(9999)
2446+
.coins_per_utxo_word(Coin::zero())
2447+
.build()
2448+
.unwrap();
2449+
let mut tx_builder = TransactionBuilder::new(&cfg);
24392450
const COST: u64 = 1000;
24402451
tx_builder.add_output(&TransactionOutput::new(
24412452
&Address::from_bech32("addr1vyy6nhfyks7wdu3dudslys37v252w2nwhv0fw2nfawemmnqs6l44z").unwrap(),
@@ -2453,14 +2464,16 @@ mod tests {
24532464
fn tx_builder_cip2_random_improve_adds_enough_for_fees() {
24542465
// we have a = 1 to test increasing fees when more inputs are added
24552466
let linear_fee = LinearFee::new(&to_bignum(1), &to_bignum(0));
2456-
let mut tx_builder = TransactionBuilder::new(
2457-
&linear_fee,
2458-
&Coin::zero(),
2459-
&to_bignum(0),
2460-
9999,
2461-
9999,
2462-
&to_bignum(0),
2463-
);
2467+
let cfg = TransactionBuilderConfigBuilder::default()
2468+
.linear_fee(linear_fee)
2469+
.pool_deposit(to_bignum(0))
2470+
.key_deposit(to_bignum(0))
2471+
.max_value_size(9999)
2472+
.max_tx_size(9999)
2473+
.coins_per_utxo_word(Coin::zero())
2474+
.build()
2475+
.unwrap();
2476+
let mut tx_builder = TransactionBuilder::new(&cfg);
24642477
const COST: u64 = 100;
24652478
tx_builder.add_output(&TransactionOutput::new(
24662479
&Address::from_bech32("addr1vyy6nhfyks7wdu3dudslys37v252w2nwhv0fw2nfawemmnqs6l44z").unwrap(),

0 commit comments

Comments
 (0)