|
| 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 | +} |
0 commit comments