diff --git a/package-lock.json b/package-lock.json index 569022e1..7b5f55b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "cardano-serialization-lib", - "version": "10.0.4", + "version": "10.0.5-beta.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2d677be1..180677aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cardano-serialization-lib", - "version": "10.0.4", + "version": "10.0.5-beta.1", "description": "(De)serialization functions for the Cardano blockchain along with related utility functions", "scripts": { "rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; wasm-pack pack) && npm run js:flowgen", diff --git a/rust/Cargo.lock b/rust/Cargo.lock index d64f5bce..341d259a 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -52,7 +52,7 @@ checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" [[package]] name = "cardano-serialization-lib" -version = "10.0.4" +version = "10.0.5-beta.1" dependencies = [ "bech32", "cbor_event", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 77d31a5b..b8040f9c 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cardano-serialization-lib" -version = "10.0.4" +version = "10.0.5-beta.1" edition = "2018" authors = ["EMURGO"] license = "MIT" diff --git a/rust/pkg/cardano_serialization_lib.js.flow b/rust/pkg/cardano_serialization_lib.js.flow index bd79a739..3cf74034 100644 --- a/rust/pkg/cardano_serialization_lib.js.flow +++ b/rust/pkg/cardano_serialization_lib.js.flow @@ -5485,11 +5485,17 @@ declare export class TransactionBuilder { get_implicit_input(): Value; /** - * Return explicit input plus implicit input plus mint minus burn + * Return explicit input plus implicit input plus mint * @returns {Value} */ get_total_input(): Value; + /** + * Return explicit output plus deposit plus burn + * @returns {Value} + */ + get_total_output(): Value; + /** * does not include fee * @returns {Value} diff --git a/rust/src/tx_builder.rs b/rust/src/tx_builder.rs index 84541a8a..d284369c 100644 --- a/rust/src/tx_builder.rs +++ b/rust/src/tx_builder.rs @@ -330,9 +330,7 @@ impl TransactionBuilder { pub fn add_inputs_from(&mut self, inputs: &TransactionUnspentOutputs, strategy: CoinSelectionStrategyCIP2) -> Result<(), JsError> { let available_inputs = &inputs.0.clone(); let mut input_total = self.get_total_input()?; - let mut output_total = self - .get_explicit_output()? - .checked_add(&Value::new(&self.get_deposit()?))? + let mut output_total = self.get_total_output()? .checked_add(&Value::new(&self.min_fee()?))?; match strategy { CoinSelectionStrategyCIP2::LargestFirst => { @@ -969,13 +967,20 @@ impl TransactionBuilder { }).unwrap_or((Value::zero(), Value::zero())) } - /// Return explicit input plus implicit input plus mint minus burn + /// Return explicit input plus implicit input plus mint pub fn get_total_input(&self) -> Result { - let (mint_value, burn_value) = self.get_mint_as_values(); + let (mint_value, _) = self.get_mint_as_values(); self.get_explicit_input()? .checked_add(&self.get_implicit_input()?)? - .checked_add(&mint_value)? - .checked_sub(&burn_value) + .checked_add(&mint_value) + } + + /// Return explicit output plus deposit plus burn + pub fn get_total_output(&self) -> Result { + let (_, burn_value) = self.get_mint_as_values(); + self.get_explicit_output()? + .checked_add(&Value::new(&self.get_deposit()?))? + .checked_add(&burn_value) } /// does not include fee @@ -1020,10 +1025,7 @@ impl TransactionBuilder { let data_hash = None; let input_total = self.get_total_input()?; - - let output_total = self - .get_explicit_output()? - .checked_add(&Value::new(&self.get_deposit()?))?; + let output_total = self.get_total_output()?; use std::cmp::Ordering; match &input_total.partial_cmp(&output_total.checked_add(&Value::new(&fee))?) { @@ -4157,7 +4159,7 @@ mod tests { } #[test] - fn total_input_with_mint_and_burn() { + fn total_input_output_with_mint_and_burn() { let mut tx_builder = create_tx_builder_with_fee(&create_linear_fee(0, 1)); let spend = root_key_15() .derive(harden(1852)) @@ -4208,22 +4210,41 @@ mod tests { ); } + tx_builder.add_output( + &TransactionOutputBuilder::new() + .with_address(&byron_address()) + .next().unwrap() + .with_coin(&to_bignum(42)) + .build().unwrap() + ).unwrap(); + let total_input_before_mint = tx_builder.get_total_input().unwrap(); + let total_output_before_mint = tx_builder.get_total_output().unwrap(); assert_eq!(total_input_before_mint.coin, to_bignum(300)); - let ma1 = total_input_before_mint.multiasset.unwrap(); - assert_eq!(ma1.get(&policy_id1).unwrap().get(&name).unwrap(), to_bignum(360)); - assert_eq!(ma1.get(&policy_id2).unwrap().get(&name).unwrap(), to_bignum(360)); - + assert_eq!(total_output_before_mint.coin, to_bignum(42)); + let ma1_input = total_input_before_mint.multiasset.unwrap(); + let ma1_output = total_output_before_mint.multiasset; + assert_eq!(ma1_input.get(&policy_id1).unwrap().get(&name).unwrap(), to_bignum(360)); + assert_eq!(ma1_input.get(&policy_id2).unwrap().get(&name).unwrap(), to_bignum(360)); + assert!(ma1_output.is_none()); + + // Adding mint tx_builder.add_mint_asset(&mint_script1, &name, Int::new_i32(40)); + + // Adding burn tx_builder.add_mint_asset(&mint_script2, &name, Int::new_i32(-40)); let total_input_after_mint = tx_builder.get_total_input().unwrap(); + let total_output_after_mint = tx_builder.get_total_output().unwrap(); assert_eq!(total_input_after_mint.coin, to_bignum(300)); - let ma2 = total_input_after_mint.multiasset.unwrap(); - assert_eq!(ma2.get(&policy_id1).unwrap().get(&name).unwrap(), to_bignum(400)); - assert_eq!(ma2.get(&policy_id2).unwrap().get(&name).unwrap(), to_bignum(320)); + assert_eq!(total_output_before_mint.coin, to_bignum(42)); + let ma2_input = total_input_after_mint.multiasset.unwrap(); + let ma2_output = total_output_after_mint.multiasset.unwrap(); + assert_eq!(ma2_input.get(&policy_id1).unwrap().get(&name).unwrap(), to_bignum(400)); + assert_eq!(ma2_input.get(&policy_id2).unwrap().get(&name).unwrap(), to_bignum(360)); + assert_eq!(ma2_output.get(&policy_id2).unwrap().get(&name).unwrap(), to_bignum(40)); } }