multiversx_chain_vm/host/context/
tx_input.rs1use num_bigint::BigUint;
2use num_traits::Zero;
3
4use crate::{
5 display_util::*,
6 types::{VMAddress, H256},
7};
8use std::fmt;
9
10use super::{CallType, TxFunctionName};
11
12#[derive(Clone, Debug)]
13pub struct TxInput {
14 pub from: VMAddress,
15 pub to: VMAddress,
16 pub egld_value: BigUint,
17 pub esdt_values: Vec<TxTokenTransfer>,
18 pub func_name: TxFunctionName,
19 pub args: Vec<Vec<u8>>,
20 pub call_type: CallType,
21 pub readonly: bool, pub gas_limit: u64,
23 pub gas_price: u64,
24 pub tx_hash: H256,
25 pub promise_callback_closure_data: Option<Vec<u8>>,
26 pub callback_payments: CallbackPayments,
27}
28
29impl Default for TxInput {
30 fn default() -> Self {
31 TxInput {
32 from: VMAddress::zero(),
33 to: VMAddress::zero(),
34 egld_value: BigUint::zero(),
35 esdt_values: Vec::new(),
36 func_name: TxFunctionName::EMPTY,
37 args: Vec::new(),
38 call_type: CallType::DirectCall,
39 readonly: false,
40 gas_limit: 0,
41 gas_price: 0,
42 tx_hash: H256::zero(),
43 promise_callback_closure_data: None,
44 callback_payments: Default::default(),
45 }
46 }
47}
48
49impl fmt::Display for TxInput {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 write!(f, "TxInput {{ func: {}, args: {:?}, call_value: {}, esdt_value: {:?}, from: 0x{}, to: 0x{}\n}}",
52 self.func_name.as_str(),
53 self.args,
54 self.egld_value,
55 self.esdt_values,
56 address_hex(&self.from),
57 address_hex(&self.to))
58 }
59}
60
61impl TxInput {
62 pub fn add_arg(&mut self, arg: Vec<u8>) {
63 self.args.push(arg);
64 }
65
66 pub fn func_name_from_arg_index(&self, arg_index: usize) -> TxFunctionName {
67 if let Some(arg) = self.args.get(arg_index) {
68 arg.into()
69 } else {
70 TxFunctionName::EMPTY
71 }
72 }
73}
74
75#[derive(Clone, Debug, PartialEq, Eq)]
77pub struct TxTokenTransfer {
78 pub token_identifier: Vec<u8>,
79 pub nonce: u64,
80 pub value: BigUint,
81}
82
83#[derive(Default, Clone, Debug)]
85pub struct CallbackPayments {
86 pub egld_value: BigUint,
87 pub esdt_values: Vec<TxTokenTransfer>,
88}
89
90impl TxInput {
91 pub fn received_egld(&self) -> &BigUint {
93 if !self.callback_payments.egld_value.is_zero() {
94 &self.callback_payments.egld_value
95 } else {
96 &self.egld_value
97 }
98 }
99
100 pub fn received_esdt(&self) -> &[TxTokenTransfer] {
102 if !self.callback_payments.esdt_values.is_empty() {
103 self.callback_payments.esdt_values.as_slice()
104 } else {
105 self.esdt_values.as_slice()
106 }
107 }
108
109 pub fn get_argument_vec_u8(&self, arg_index: i32) -> Vec<u8> {
110 let arg_idx_usize = arg_index as usize;
111 assert!(arg_idx_usize < self.args.len(), "Tx arg index out of range");
112 self.args[arg_idx_usize].clone()
113 }
114}