Skip to content

Commit

Permalink
refactor!: user_lock_script -> module_lock_script (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Flouse <[email protected]>
  • Loading branch information
blckngm and Flouse authored Nov 9, 2023
1 parent a50a120 commit cd205bd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Use `cargo +nightly doc --open` to view the full API docs.

## Configuration

- user_lock_script: module lock script
- module_lock_script
- axon_metadata_type_script
- channel_contract_type_id_args
- channel_id
Expand Down
57 changes: 31 additions & 26 deletions examples/sudt-transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,45 +111,47 @@ async fn main() -> Result<()> {
let pubkey = secp256k1::PublicKey::from_secret_key(&secp, &sk);

let address = AddressPayload::from_pubkey(&pubkey);
let user_lock_script = packed::Script::from(&address);
let sender_lock_script = packed::Script::from(&address);

println!(
"port: {}",
hex::encode(
config
.sdk_config
.user_lock_script()
.module_lock_script()
.calc_script_hash()
.as_slice()
)
);

ensure!(
config.sdk_config.user_lock_script().code_hash()
config.sdk_config.module_lock_script().code_hash()
== packed::Script::from(config.sudt_transfer_contract_type_script.clone())
.calc_script_hash(),
"port id code hash is not sudt transfer contract type script hash",
);

match cli.command {
Commands::Recv => receive(config, sk, user_lock_script).await,
Commands::CreateStCell { sudt } => create_st_cell(config, sk, user_lock_script, sudt).await,
Commands::ConsumeAck => consume_ack(config, sk, user_lock_script).await,
Commands::Recv => receive(config, sk, sender_lock_script).await,
Commands::CreateStCell { sudt } => {
create_st_cell(config, sk, sender_lock_script, sudt).await
}
Commands::ConsumeAck => consume_ack(config, sk, sender_lock_script).await,
Commands::Send {
sudt,
receiver,
amount,
} => send(config, sk, user_lock_script, sudt, receiver, amount).await,
} => send(config, sk, sender_lock_script, sudt, receiver, amount).await,
}
}

async fn consume_ack(
config: Config,
sk: secp256k1::SecretKey,
user_lock_script: packed::Script,
sender_lock_script: packed::Script,
) -> Result<()> {
let client = CkbRpcClient::new(config.ckb_rpc_url.clone());
let sender = user_lock_script.calc_script_hash().as_bytes().slice(..20);
let sender = sender_lock_script.calc_script_hash().as_bytes().slice(..20);

let mut ack_packets = pin!(
PacketCell::subscribe(client.clone(), config.sdk_config.clone())
Expand Down Expand Up @@ -178,7 +180,7 @@ async fn consume_ack(
.await
.context("get sudt dep")?;

let user_input = get_capacity_input(&client, &user_lock_script).await?;
let user_input = get_capacity_input(&client, &sender_lock_script).await?;

let packet_contract_cell = get_latest_cell_by_type_script(
&client,
Expand All @@ -205,7 +207,7 @@ async fn consume_ack(
.input(simple_input(user_input.out_point.into()))
.witness(placeholder_witness.as_bytes().pack());
let tx = add_ibc_envelope(tx, &envelope).build();
let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script, sk)?;
let tx = complete_tx(&config.ckb_rpc_url, &tx, sender_lock_script, sk)?;
send_transaction(&config.ckb_rpc_url, tx)?;

Ok(())
Expand All @@ -228,14 +230,14 @@ fn get_sudt_type_script(config: &Config, sudt: &str) -> Result<packed::Script> {
async fn create_st_cell(
config: Config,
sk: secp256k1::SecretKey,
user_lock_script: packed::Script,
sender_lock_script: packed::Script,
sudt: String,
) -> Result<()> {
let client = CkbRpcClient::new(config.ckb_rpc_url.clone());

let sudt_type_script = get_sudt_type_script(&config, &sudt)?;

let st_cell_lock_script = config.sdk_config.user_lock_script();
let st_cell_lock_script = config.sdk_config.module_lock_script();

let a_sudt_cell = get_latest_cell_by_type_script(&client, sudt_type_script.clone().into())
.await
Expand All @@ -256,7 +258,7 @@ async fn create_st_cell(
.cell_dep(sudt_dep)
.build();

let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script, sk)?;
let tx = complete_tx(&config.ckb_rpc_url, &tx, sender_lock_script, sk)?;
send_transaction(&config.ckb_rpc_url, tx)?;

Ok(())
Expand All @@ -265,7 +267,7 @@ async fn create_st_cell(
async fn send(
config: Config,
sk: secp256k1::SecretKey,
user_lock_script: packed::Script,
sender_lock_script: packed::Script,
sudt: String,
receiver: String,
amount: u128,
Expand All @@ -281,7 +283,7 @@ async fn send(
.out_point
.into(),
);
let st_cell_lock_script = config.sdk_config.user_lock_script();
let st_cell_lock_script = config.sdk_config.module_lock_script();
let sudt_search_filter = ckb_indexer::SearchKeyFilter {
script: Some(sudt_type_script.clone().into()),
script_len_range: {
Expand Down Expand Up @@ -329,7 +331,7 @@ async fn send(
ckb_indexer::SearchKey {
filter: Some(sudt_search_filter),
group_by_transaction: Some(true),
script: user_lock_script.clone().into(),
script: sender_lock_script.clone().into(),
script_search_mode: Some(ckb_indexer::ScriptSearchMode::Exact),
script_type: ckb_indexer::ScriptType::Lock,
with_data: Some(true),
Expand Down Expand Up @@ -374,7 +376,7 @@ 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]
sender: sender_lock_script.calc_script_hash().as_bytes()[..20]
.to_vec()
.into(),
receiver: hex::decode(receiver.strip_prefix("0x").unwrap_or(&receiver))
Expand Down Expand Up @@ -427,24 +429,27 @@ async fn send(

let tx = add_ibc_envelope(tx, &envelope).build();

let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script.clone(), sk)?;
let tx = complete_tx(&config.ckb_rpc_url, &tx, sender_lock_script.clone(), sk)?;

send_transaction(&config.ckb_rpc_url, tx)?;

println!("consuming ack");

consume_ack(config, sk, user_lock_script).await?;
consume_ack(config, sk, sender_lock_script).await?;

Ok(())
}

async fn receive(
config: Config,
sk: secp256k1::SecretKey,
user_lock_script: packed::Script,
receiver_lock_script: packed::Script,
) -> Result<()> {
let client = CkbRpcClient::new(config.ckb_rpc_url.clone());
let receiver = user_lock_script.calc_script_hash().as_bytes().slice(..20);
let receiver = receiver_lock_script
.calc_script_hash()
.as_bytes()
.slice(..20);

let mut recv_packets = pin!(
PacketCell::subscribe(client.clone(), config.sdk_config.clone())
Expand Down Expand Up @@ -502,7 +507,7 @@ async fn receive(
vec![1],
)?;

let user_input = get_capacity_input(&client, &user_lock_script).await?;
let user_input = get_capacity_input(&client, &receiver_lock_script).await?;

// sighash placeholder witness.
let placeholder_witness = packed::WitnessArgs::new_builder()
Expand All @@ -526,7 +531,7 @@ async fn receive(
// sudt output.
.output(
packed::CellOutput::new_builder()
.lock(user_lock_script.clone())
.lock(receiver_lock_script.clone())
.type_(Some(sudt_type_script).pack())
.build_exact_capacity(Capacity::bytes(16).unwrap())
.unwrap(),
Expand All @@ -538,7 +543,7 @@ async fn receive(

let tx = add_ibc_envelope(tx, &envelope).build();

let tx = complete_tx(&config.ckb_rpc_url, &tx, user_lock_script, sk)?;
let tx = complete_tx(&config.ckb_rpc_url, &tx, receiver_lock_script, sk)?;

send_transaction(&config.ckb_rpc_url, tx)?;

Expand Down Expand Up @@ -587,7 +592,7 @@ async fn get_st_cell_by_sudt_type_hash(
.out_point
.into(),
);
let st_cell_lock_script = config.sdk_config.user_lock_script();
let st_cell_lock_script = config.sdk_config.module_lock_script();
let st_cells = client
.get_cells(
ckb_indexer::SearchKey {
Expand Down
14 changes: 7 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use types::*;
#[derive(Serialize, Deserialize, Clone)]
pub struct Config {
/// Its hash is used as port ID. Address (string) or script.
pub user_lock_script: AddressOrScript,
pub module_lock_script: AddressOrScript,

/// Axon metadata cell type script.
pub axon_metadata_type_script: AddressOrScript,
Expand All @@ -28,8 +28,8 @@ pub struct Config {
}

impl Config {
pub fn user_lock_script(&self) -> packed::Script {
self.user_lock_script.script()
pub fn module_lock_script(&self) -> packed::Script {
self.module_lock_script.script()
}

pub fn axon_metadata_type_script(&self) -> packed::Script {
Expand All @@ -49,7 +49,7 @@ impl Config {
}

pub fn port_id(&self) -> [u8; 32] {
self.user_lock_script().calc_script_hash().unpack().0
self.module_lock_script().calc_script_hash().unpack().0
}

pub fn port_id_string(&self) -> String {
Expand All @@ -65,7 +65,7 @@ impl Config {
.0,
open: true,
channel_id: self.channel_id,
port_id: self.user_lock_script().calc_script_hash().unpack().0,
port_id: self.module_lock_script().calc_script_hash().unpack().0,
};
packed::Script::new_builder()
.hash_type(ScriptHashType::Type.into())
Expand All @@ -81,7 +81,7 @@ impl Config {
pub fn packet_cell_lock_script_prefix(&self) -> packed::Script {
let packet_args = PacketArgs {
channel_id: self.channel_id,
port_id: self.user_lock_script().calc_script_hash().unpack().0,
port_id: self.module_lock_script().calc_script_hash().unpack().0,
sequence: 0,
};

Expand All @@ -96,7 +96,7 @@ impl Config {
pub fn packet_cell_lock_script(&self, sequence: u16) -> packed::Script {
let packet_args = PacketArgs {
channel_id: self.channel_id,
port_id: self.user_lock_script().calc_script_hash().unpack().0,
port_id: self.module_lock_script().calc_script_hash().unpack().0,
sequence,
};

Expand Down
6 changes: 3 additions & 3 deletions tests/sudt-transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn test_send_packet() -> Result<()> {
channel_id,
confirmations: 1,
packet_contract_type_id_args: [0u8; 32].into(),
user_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()),
module_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()),
};

let current_channel_state = IbcChannel {
Expand Down Expand Up @@ -225,7 +225,7 @@ fn test_write_ack_packet() -> Result<()> {
channel_id,
confirmations: 1,
packet_contract_type_id_args: packet_contract_type_id_args.into(),
user_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()),
module_lock_script: AddressOrScript::Script(sudt_transfer_lock.into()),
};

let current_channel_state = IbcChannel {
Expand Down Expand Up @@ -404,7 +404,7 @@ fn test_consume_ack_packet() -> Result<()> {
channel_id,
confirmations: 1,
packet_contract_type_id_args: packet_contract_type_id_args.into(),
user_lock_script: AddressOrScript::Script(sudt_transfer_lock.clone().into()),
module_lock_script: AddressOrScript::Script(sudt_transfer_lock.clone().into()),
};

let packet = IbcPacket {
Expand Down
6 changes: 3 additions & 3 deletions tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_send_packet() -> Result<()> {
channel_id,
confirmations: 1,
packet_contract_type_id_args: [0u8; 32].into(),
user_lock_script: AddressOrScript::Script(always_success_lock.into()),
module_lock_script: AddressOrScript::Script(always_success_lock.into()),
};

let current_channel_state = IbcChannel {
Expand Down Expand Up @@ -167,7 +167,7 @@ fn test_write_ack_packet() -> Result<()> {
channel_id,
confirmations: 1,
packet_contract_type_id_args: packet_contract_type_id_args.into(),
user_lock_script: AddressOrScript::Script(always_success_lock.into()),
module_lock_script: AddressOrScript::Script(always_success_lock.into()),
};

let current_channel_state = IbcChannel {
Expand Down Expand Up @@ -286,7 +286,7 @@ fn test_consume_ack_packet() -> Result<()> {
channel_id,
confirmations: 1,
packet_contract_type_id_args: packet_contract_type_id_args.into(),
user_lock_script: AddressOrScript::Script(always_success_lock.into()),
module_lock_script: AddressOrScript::Script(always_success_lock.into()),
};

let packet = IbcPacket {
Expand Down

0 comments on commit cd205bd

Please sign in to comment.