Skip to content

Commit

Permalink
Merge pull request #374 from Emurgo/ruslan/burn-is-output
Browse files Browse the repository at this point in the history
[10.0.5] Treat burn as part of output in tx-builder
  • Loading branch information
vsubhuman authored May 17, 2022
2 parents 2b0ba84 + 4fb5faa commit 0fc2eb3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion rust/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 rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 7 additions & 1 deletion rust/pkg/cardano_serialization_lib.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
59 changes: 40 additions & 19 deletions rust/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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<Value, JsError> {
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<Value, JsError> {
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
Expand Down Expand Up @@ -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))?) {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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));
}

}
Expand Down

0 comments on commit 0fc2eb3

Please sign in to comment.