Skip to content

Commit

Permalink
Merge pull request #125 from chenyukang/pcn-udt
Browse files Browse the repository at this point in the history
Add support for customizing local devnet
  • Loading branch information
quake authored Jul 22, 2024
2 parents fce8b74 + e205f61 commit 7a06e3b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/transaction/builder/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub struct SudtTransactionBuilder {
input_iter: InputIterator,
/// The identifier of the sUDT
sudt_owner_lock_script: Script,
/// The sUDT type script, by default we will set default type script for `Mainnet` and `Testnet`
/// in the scenario of Dev enviornment, we may need to manually override type script
sudt_type_script: Option<Script>,
/// Whether we are in owner mode
owner_mode: bool,
/// The inner transaction builder
Expand All @@ -51,6 +54,7 @@ impl SudtTransactionBuilder {
configuration,
input_iter,
sudt_owner_lock_script: sudt_owner_lock_script.into(),
sudt_type_script: None,
owner_mode,
tx: TransactionBuilder::default(),
})
Expand All @@ -61,6 +65,11 @@ impl SudtTransactionBuilder {
self.change_lock = lock_script;
}

/// Manually set the sUDT type script
pub fn set_sudt_type_script(&mut self, sudt_type_script: Script) {
self.sudt_type_script = Some(sudt_type_script);
}

/// Add an output cell and output data to the transaction.
pub fn add_output_and_data(&mut self, output: CellOutput, data: packed::Bytes) {
self.tx.output(output);
Expand All @@ -72,6 +81,7 @@ impl SudtTransactionBuilder {
let type_script = build_sudt_type_script(
self.configuration.network_info(),
&self.sudt_owner_lock_script,
self.sudt_type_script.clone(),
);
let output_data = sudt_amount.to_le_bytes().pack();
let dummy_output = CellOutput::new_builder()
Expand Down Expand Up @@ -146,8 +156,10 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
configuration,
mut input_iter,
sudt_owner_lock_script,
sudt_type_script,
owner_mode,
mut tx,
..
} = self;

let change_builder = DefaultChangeBuilder {
Expand All @@ -159,8 +171,11 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
if owner_mode {
inner_build(tx, change_builder, input_iter, &configuration, contexts)
} else {
let sudt_type_script =
build_sudt_type_script(configuration.network_info(), &sudt_owner_lock_script);
let sudt_type_script = build_sudt_type_script(
configuration.network_info(),
&sudt_owner_lock_script,
sudt_type_script,
);
let mut sudt_input_iter = input_iter.clone();
sudt_input_iter.set_type_script(Some(sudt_type_script));

Expand Down Expand Up @@ -195,7 +210,14 @@ impl CkbTransactionBuilder for SudtTransactionBuilder {
}
}

fn build_sudt_type_script(network_info: &NetworkInfo, sudt_owner_lock_script: &Script) -> Script {
fn build_sudt_type_script(
network_info: &NetworkInfo,
sudt_owner_lock_script: &Script,
override_sudt_type_script: Option<Script>,
) -> Script {
if let Some(sudt_type_script) = override_sudt_type_script {
return sudt_type_script;
}
// code_hash from https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0025-simple-udt/0025-simple-udt.md#notes
let code_hash = match network_info.network_type {
NetworkType::Mainnet => {
Expand All @@ -204,6 +226,9 @@ fn build_sudt_type_script(network_info: &NetworkInfo, sudt_owner_lock_script: &S
NetworkType::Testnet => {
h256!("0xc5e5dcf215925f7ef4dfaf5f4b4f105bc321c02776d6e7d52a1db3fcd9d011a4")
}
NetworkType::Dev => {
panic!("You may need to manually set `sudt_type_script` with `set_sudt_type_script` method");
}
_ => panic!("Unsupported network type"),
};

Expand Down
3 changes: 3 additions & 0 deletions src/transaction/handler/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ impl Secp256k1Blake160MultisigAllScriptHandler {
ret.init(network)?;
Ok(ret)
}
pub fn new_with_customize(cell_deps: Vec<CellDep>) -> Self {
Self { cell_deps }
}
}

impl ScriptHandler for Secp256k1Blake160MultisigAllScriptHandler {
Expand Down
3 changes: 3 additions & 0 deletions src/transaction/handler/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ impl Secp256k1Blake160SighashAllScriptHandler {
ret.init(network)?;
Ok(ret)
}
pub fn new_with_customize(cell_deps: Vec<CellDep>) -> Self {
Self { cell_deps }
}
}

impl ScriptHandler for Secp256k1Blake160SighashAllScriptHandler {
Expand Down
7 changes: 7 additions & 0 deletions src/transaction/handler/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ impl SudtHandler {
sudt_script_id,
})
}

pub fn new_with_customize(cell_deps: Vec<CellDep>, sudt_script_id: ScriptId) -> Self {
Self {
cell_deps,
sudt_script_id,
}
}
}

impl ScriptHandler for SudtHandler {
Expand Down
9 changes: 9 additions & 0 deletions src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ impl TransactionBuilderConfiguration {
Self::new_with_network(NetworkInfo::testnet())
}

pub fn new_devnet() -> Result<Self, TxBuilderError> {
Ok(Self {
network: NetworkInfo::devnet(),
script_handlers: vec![],
fee_rate: 1000,
estimate_tx_size: 128000,
})
}

pub fn new_with_network(network: NetworkInfo) -> Result<Self, TxBuilderError> {
let script_handlers = Self::generate_system_handlers(&network)?;
Ok(Self {
Expand Down
7 changes: 7 additions & 0 deletions src/types/network_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ impl NetworkInfo {
url: "https://testnet.ckb.dev".to_string(),
}
}

pub fn devnet() -> Self {
Self {
network_type: NetworkType::Dev,
url: "http://localhost:8114".to_string(),
}
}
}

0 comments on commit 7a06e3b

Please sign in to comment.