Skip to content

Commit

Permalink
parse sudt amount should unpack first
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Jun 6, 2024
1 parent 79859e0 commit 0fc108a
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/transaction/builder/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ impl SudtTransactionBuilder {
}
}

#[test]
fn test_parse_u128_from_sudt_tx_output_data() {
use crate::Address;
use std::str::FromStr;

let network_info = NetworkInfo::testnet();
let configuration =
TransactionBuilderConfiguration::new_with_network(network_info.clone()).unwrap();

let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r").unwrap();

let iterator = InputIterator::new_with_address(&[sender.clone()], &network_info);

let test_sudt_amount = 9999_u128;
let mut builder = SudtTransactionBuilder::new(configuration, iterator, &sender, false).unwrap();

builder.add_output(&sender, test_sudt_amount);

let outputs_sudt_amount: u128 = builder
.tx
.outputs_data
.iter()
.map(|data| parse_u128(data.raw_data().as_ref()))
.collect::<Result<Vec<u128>, TxBuilderError>>()
.map(|u128_vec| u128_vec.iter().sum())
.expect("parse u128 failed");
assert_eq!(outputs_sudt_amount, test_sudt_amount);
}

fn parse_u128(data: &[u8]) -> Result<u128, TxBuilderError> {
if data.len() > std::mem::size_of::<u128>() {
return Err(TxBuilderError::Other(anyhow!(
Expand Down Expand Up @@ -138,7 +167,7 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
let outputs_sudt_amount: u128 = tx
.outputs_data
.iter()
.map(|data| parse_u128(data.as_slice()))
.map(|data| parse_u128(data.raw_data().as_ref()))
.collect::<Result<Vec<u128>, TxBuilderError>>()
.map(|u128_vec| u128_vec.iter().sum())?;

Expand Down

0 comments on commit 0fc108a

Please sign in to comment.