Skip to content

Commit 4c24fca

Browse files
author
Liu Chuankai
committed
feat: 🎸 support dao withdraw phrase1 simple transaction builder
1 parent 25a470f commit 4c24fca

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use ckb_sdk::{
2+
transaction::{
3+
builder::{CkbTransactionBuilder, SimpleTransactionBuilder},
4+
handler::{dao, HandlerContexts},
5+
input::InputIterator,
6+
signer::{SignContexts, TransactionSigner},
7+
TransactionBuilderConfiguration,
8+
},
9+
Address, CkbRpcClient, NetworkInfo,
10+
};
11+
use ckb_types::h256;
12+
use std::{error::Error as StdErr, str::FromStr};
13+
14+
fn main() -> Result<(), Box<dyn StdErr>> {
15+
let network_info = NetworkInfo::testnet();
16+
let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r")?;
17+
18+
let configuration = TransactionBuilderConfiguration::new_with_network(network_info.clone())?;
19+
20+
let iterator = InputIterator::new(vec![(&sender).into()], configuration.network_info());
21+
let mut builder = SimpleTransactionBuilder::new(configuration, iterator);
22+
23+
let mut context = dao::WithdrawPhrase1Context::new(network_info.url.clone());
24+
let input_outpoint = serde_json::from_str::<ckb_jsonrpc_types::OutPoint>(
25+
r#"
26+
{
27+
"tx_hash": "0x2aba579894cdf5f6c4afd3ada52792c4405fe6ba64d05226fb63fa5c1ec6f666",
28+
"index": "0x0"
29+
}
30+
"#,
31+
)
32+
.unwrap();
33+
context.add_input_outpoint(input_outpoint.into(), None);
34+
let mut contexts = HandlerContexts::default();
35+
contexts.add_context(Box::new(context) as Box<_>);
36+
37+
builder.set_change_lock((&sender).into());
38+
let mut tx_with_groups = builder.build(&contexts)?;
39+
40+
let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
41+
println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
42+
43+
let private_keys = vec![h256!(
44+
"0x6c9ed03816e3111e49384b8d180174ad08e29feb1393ea1b51cef1c505d4e36a"
45+
)];
46+
TransactionSigner::new(&network_info).sign_transaction(
47+
&mut tx_with_groups,
48+
&SignContexts::new_sighash_h256(private_keys)?,
49+
)?;
50+
51+
let json_tx = ckb_jsonrpc_types::TransactionView::from(tx_with_groups.get_tx_view().clone());
52+
println!("tx: {}", serde_json::to_string_pretty(&json_tx).unwrap());
53+
54+
let tx_hash = CkbRpcClient::new(network_info.url.as_str())
55+
.send_transaction(json_tx.inner, None)
56+
.expect("send transaction");
57+
// example tx: b615b9cbb566af18dd2d860836b89e07a86dfcc7af510595dcb404f1b19e6d7e
58+
println!(">>> tx {} sent! <<<", tx_hash);
59+
60+
Ok(())
61+
}

‎src/transaction/handler/dao.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,22 @@ pub struct WithdrawPhrase1Context {
6262

6363
impl WithdrawPhrase1Context {
6464
/// add input.
65-
/// If `lock_script` is `None` copy the lock script from input with same
65+
/// If `receiver_lock` is `None` copy the lock script from input with same
6666
/// index, otherwise replace the lock script with the given script.
67-
pub fn add_input(&mut self, input: CellInput, lock_script: Option<Script>) {
68-
self.items.push(DaoPrepareItem { input, lock_script });
67+
pub fn add_input(&mut self, input: CellInput, receiver_lock: Option<Script>) {
68+
self.items.push(DaoPrepareItem {
69+
input,
70+
lock_script: receiver_lock,
71+
});
72+
}
73+
74+
pub fn add_input_outpoint(&mut self, input_outpoint: OutPoint, receiver_lock: Option<Script>) {
75+
self.items.push(DaoPrepareItem {
76+
input: CellInput::new_builder()
77+
.previous_output(input_outpoint)
78+
.build(),
79+
lock_script: receiver_lock,
80+
});
6981
}
7082

7183
pub fn new(rpc_url: String) -> Self {
@@ -136,7 +148,7 @@ impl DaoScriptHandler {
136148
transaction_inputs.push(transaction_input);
137149

138150
tx_data.dedup_header_dep(deposit_header.hash());
139-
tx_data.input(input.clone());
151+
140152
tx_data.output(output);
141153
tx_data.output_data(output_data.pack());
142154
}
@@ -208,6 +220,9 @@ impl ScriptHandler for DaoScriptHandler {
208220
tx_data.set_witness(*index, witness.as_bytes().pack());
209221
}
210222
Ok(true)
223+
} else if let Some(_args) = context.as_any().downcast_ref::<WithdrawPhrase1Context>() {
224+
tx_data.dedup_cell_deps(self.cell_deps.clone());
225+
Ok(true)
211226
} else {
212227
Ok(false)
213228
}

0 commit comments

Comments
 (0)