numbat_wasm_debug/
tx_context.rs1use crate::{blockchain_mock::*, TxInput, TxOutput};
2use alloc::{rc::Rc, vec::Vec};
3use core::cell::RefCell;
4use numbat_wasm::types::Address;
5use std::collections::HashMap;
6
7#[derive(Debug)]
8pub struct TxContext {
9 pub blockchain_info_box: Box<BlockchainTxInfo>,
10 pub tx_input_box: Box<TxInput>,
11 pub tx_output_cell: Rc<RefCell<TxOutput>>,
12}
13
14impl TxContext {
15 pub fn new(blockchain_info: BlockchainTxInfo, tx_input: TxInput, tx_output: TxOutput) -> Self {
16 TxContext {
17 blockchain_info_box: Box::new(blockchain_info),
18 tx_input_box: Box::new(tx_input),
19 tx_output_cell: Rc::new(RefCell::new(tx_output)),
20 }
21 }
22
23 pub fn into_output(self) -> TxOutput {
24 let ref_cell = Rc::try_unwrap(self.tx_output_cell).unwrap();
25 ref_cell.replace(TxOutput::default())
26 }
27
28 pub fn dummy() -> Self {
29 TxContext {
30 blockchain_info_box: Box::new(BlockchainTxInfo {
31 previous_block_info: BlockInfo::new(),
32 current_block_info: BlockInfo::new(),
33 contract_balance: 0u32.into(),
34 contract_dcdt: HashMap::new(),
35 contract_owner: None,
36 }),
37 tx_input_box: Box::new(TxInput {
38 from: Address::zero(),
39 to: Address::zero(),
40 call_value: 0u32.into(),
41 dcdt_value: 0u32.into(),
42 dcdt_token_identifier: Vec::new(),
43 func_name: Vec::new(),
44 args: Vec::new(),
45 gas_limit: 0,
46 gas_price: 0,
47 tx_hash: b"dummy...........................".into(),
48 }),
49 tx_output_cell: Rc::new(RefCell::new(TxOutput::default())),
50 }
51 }
52}
53
54impl Clone for TxContext {
55 fn clone(&self) -> Self {
56 TxContext {
57 blockchain_info_box: self.blockchain_info_box.clone(),
58 tx_input_box: self.tx_input_box.clone(),
59 tx_output_cell: Rc::clone(&self.tx_output_cell),
60 }
61 }
62}