Skip to content

Commit

Permalink
Display packet data in hex
Browse files Browse the repository at this point in the history
  • Loading branch information
blckngm committed Nov 8, 2023
1 parent 1a1c5c1 commit 7dc5106
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions examples/sudt-transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! An example cli for sending/receiving SUDT with the sudt transfer module.

use std::{collections::HashMap, fs, path::PathBuf, pin::pin, time::Duration};
use std::{collections::HashMap, fmt, fs, path::PathBuf, pin::pin, time::Duration};

use anyhow::{bail, ensure, Context, Result};
use bytes::Bytes;
Expand Down Expand Up @@ -161,15 +161,15 @@ async fn consume_ack(
let pd = FungibleTokenPacketData::decode(&p.packet.packet.data[..])?;
if pd.sender == sender {
if p.packet.ack.as_deref() != Some(&[1]) {
println!("skipping packet {pd:?}");
println!("skipping packet {pd}");
continue;
}
break (p, pd);
} else {
println!("skipping packet {pd:?}");
println!("skipping packet {pd}");
}
};
println!("consuming packet ack\n{pd:?}\n{:?}", p.packet.packet);
println!("consuming packet ack\n{pd}\n{:?}", p.packet.packet);

let sudt_type_hash = hex::decode(pd.denom).context("decode base denom")?;
let (sudt_transfer_dep, st_cell, st_cell_amount, _) =
Expand Down Expand Up @@ -374,9 +374,12 @@ async fn send(
let channel = IbcChannelCell::get_latest(&client, &config.sdk_config).await?;
let data = FungibleTokenPacketData {
amount: amount.try_into().context("amount overflow")?,
sender: user_lock_script.calc_script_hash().as_bytes()[..20].to_vec(),
sender: user_lock_script.calc_script_hash().as_bytes()[..20]
.to_vec()
.into(),
receiver: hex::decode(receiver.strip_prefix("0x").unwrap_or(&receiver))
.context("receiver")?,
.context("receiver")?
.into(),
denom: hex::encode(sudt_type_script.calc_script_hash().as_slice()),
}
.encode_to_vec();
Expand Down Expand Up @@ -457,10 +460,10 @@ async fn receive(
if pd.receiver == receiver {
break (p, pd);
} else {
println!("skipping packet {pd:?}");
println!("skipping packet {pd}");
}
};
println!("receiving packet\n{pd:?}\n{:?}", p.packet.packet);
println!("receiving packet\n{pd}\n{:?}", p.packet.packet);

let base_denom = pd.denom.split('/').last().context("get base denom")?;
let sudt_type_hash = hex::decode(base_denom).context("decode base denom")?;
Expand Down Expand Up @@ -687,10 +690,21 @@ pub struct FungibleTokenPacketData {
pub amount: u64,
/// For ckb address, this should be ckb_blake2b(packed lock script)[..20]
#[prost(bytes, tag = "3")]
pub sender: Vec<u8>,
pub sender: Bytes,
/// For ckb address, this should be ckb_blake2b(packed lock script)[..20]
#[prost(bytes, tag = "4")]
pub receiver: Vec<u8>,
pub receiver: Bytes,
}

impl fmt::Display for FungibleTokenPacketData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FungibleTokenPacketData")
.field("denom", &self.denom)
.field("amount", &self.amount)
.field("sender", &format_args!("{:#x}", self.sender))
.field("receiver", &format_args!("{:#x}", self.receiver))
.finish()
}
}

/// Balance and sign tx. The sighash dep will be added. This won't add any new
Expand Down

0 comments on commit 7dc5106

Please sign in to comment.