From 2075578ef0da88bbaf208607a8fcbb21f2c92453 Mon Sep 17 00:00:00 2001 From: dzung pham Date: Fri, 27 Oct 2023 11:16:37 +0700 Subject: [PATCH] support withdrawal validator --- rust/src/tx_builder.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/rust/src/tx_builder.rs b/rust/src/tx_builder.rs index 569d322..073d2a1 100644 --- a/rust/src/tx_builder.rs +++ b/rust/src/tx_builder.rs @@ -1061,10 +1061,13 @@ impl TransactionBuilder { pub fn set_withdrawals(&mut self, withdrawals: &Withdrawals) { self.withdrawals = Some(withdrawals.clone()); - for (withdrawal, _coin) in &withdrawals.0 { - self.inputs - .add_required_signer(&withdrawal.payment_cred().to_keyhash().unwrap()) - } + } + + pub fn get_index_of_withdrawal(&mut self, reward_address: &RewardAddress) -> usize { + let binding = self.withdrawals.clone().unwrap(); + let mut withdrawals: Vec<_> = binding.0.iter().collect(); + withdrawals.sort_by(|(key1, _), (key2, _)| key1.cmp(key2)); + withdrawals.iter().position(|i| i.0 == reward_address).unwrap() } pub fn get_auxiliary_data(&self) -> Option { @@ -4043,6 +4046,18 @@ mod tests { .is_err()); } + #[test] + fn test_withdrawals() { + let mut tx_builder = create_reallistic_tx_builder(); + let address = Address::from_bech32("stake_test17rv54kh022y8jp0pgafd2lupjs9jzcace40r9y7cnmwrgyq80zvgk").unwrap(); + let reward_address = RewardAddress::from_address(&address).unwrap(); + let mut withdrawals = Withdrawals::new(); + withdrawals.insert(&reward_address, &BigNum::zero()); + tx_builder.set_withdrawals(&withdrawals); + let index = tx_builder.get_index_of_withdrawal(&reward_address); + assert_eq!(index , 0); + } + #[test] fn build_tx_add_change_nfts_not_enough_ada() { let mut tx_builder = create_tx_builder_with_fee_and_val_size(