Skip to content

Commit

Permalink
[MIN-1126] fix stake withdrawal fee calculation (#35)
Browse files Browse the repository at this point in the history
* fix stake withdrawal fee calculation

* remove redudant code

* remove import

* remove whitespace

* only add stake withdraw for pub key
  • Loading branch information
baymac authored Jun 28, 2024
1 parent 2c1615c commit de7239a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,21 @@ impl Withdrawals {
}
}

impl From<&Withdrawals> for RequiredSignersSet {
fn from(withdrawals: &Withdrawals) -> Self {
withdrawals.0.iter().fold(BTreeSet::new(), |mut set, w| {
let payment_cred = (*w.0).payment_cred();
let kind = payment_cred.kind();
if kind == StakeCredKind::Key {
if let Some(keyhash) = payment_cred.to_keyhash() {
set.insert(keyhash);
}
}
set
})
}
}

impl serde::Serialize for Withdrawals {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
47 changes: 47 additions & 0 deletions rust/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ fn count_needed_vkeys(tx_builder: &TransactionBuilder) -> usize {
if let Some(scripts) = &tx_builder.mint_scripts {
input_hashes.extend(RequiredSignersSet::from(scripts));
}
if let Some(withdrawals) = &tx_builder.withdrawals {
input_hashes.extend(RequiredSignersSet::from(withdrawals));
}
input_hashes.len()
}

Expand Down Expand Up @@ -2233,6 +2236,50 @@ mod tests {
let _final_tx = tx_builder.build(); // just test that it doesn't throw
}

#[test]
fn build_tx_withdraw() {
let mut tx_builder: TransactionBuilder = create_default_tx_builder();
let payment_addr = Address::from_bech32("addr_test1qrev489ndc4n5rvcscce4ug2nqrcg3f93u9nq0z6lfrqgwpcc8ypmmlrlz4sgak8azdp8jeqv5psgvdlk7jvl5ht8ruquf04vw").unwrap();
let input = TransactionInput::new(
&TransactionHash::from_hex(
"ecc0d58b2a997056cbb3c70b8ea166f3f29305bb1bc14fade0ebc5298093dca2",
)
.unwrap(),
2,
);

tx_builder.add_input(
&payment_addr,
&input,
&Value::new(&BigNum::from_str("5736075744").unwrap()),
);


let stake_addr = Address::from_bech32("stake_test1uquvrjqaal3l32cywmr73xsnevsx2qcyxxlm0fx06t4n37qpxp68z").unwrap();

let mut withdrawals = Withdrawals::new();
withdrawals.insert(
&RewardAddress::from_address(&stake_addr).unwrap(),
&BigNum::from_str("7175949").unwrap(),
);

tx_builder.set_withdrawals(&withdrawals);
tx_builder.add_change_if_needed(&payment_addr).unwrap();
assert_eq!(
tx_builder
.get_explicit_input()
.unwrap()
.checked_add(&tx_builder.get_implicit_input().unwrap())
.unwrap(),
tx_builder
.get_explicit_output()
.unwrap()
.checked_add(&Value::new(&tx_builder.get_fee_if_set().unwrap()))
.unwrap()
);
assert_eq!(&tx_builder.get_fee_if_set().unwrap(), &BigNum::from_str("183002").unwrap());
}

#[test]
fn build_tx_without_change() {
let mut tx_builder = create_default_tx_builder();
Expand Down

0 comments on commit de7239a

Please sign in to comment.