near_sdk/environment/mock/
receipt.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use near_primitives_core::types::GasWeight;
use near_vm_runner::logic::mocks::mock_external::MockAction as LogicMockAction;
use near_vm_runner::logic::types::ReceiptIndex;

use crate::{AccountId, Gas, NearToken};

#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Receipt {
    pub receiver_id: AccountId,
    pub receipt_indices: Vec<ReceiptIndex>,
    pub actions: Vec<MockAction>,
}

#[derive(serde::Serialize)]
#[serde(remote = "GasWeight")]
struct GasWeightSer(u64);

#[derive(Debug, Clone, serde::Serialize, PartialEq, Eq)]
pub enum MockAction {
    CreateReceipt {
        receipt_indices: Vec<ReceiptIndex>,
        receiver_id: AccountId,
    },
    CreateAccount {
        receipt_index: ReceiptIndex,
    },
    DeployContract {
        receipt_index: ReceiptIndex,
        code: Vec<u8>,
    },
    FunctionCallWeight {
        receipt_index: ReceiptIndex,
        method_name: Vec<u8>,
        args: Vec<u8>,
        attached_deposit: NearToken,
        prepaid_gas: Gas,
        #[serde(with = "GasWeightSer")]
        gas_weight: GasWeight,
    },
    Transfer {
        receipt_index: ReceiptIndex,
        deposit: NearToken,
    },
    Stake {
        receipt_index: ReceiptIndex,
        stake: NearToken,
        public_key: near_crypto::PublicKey,
    },
    DeleteAccount {
        receipt_index: ReceiptIndex,
        beneficiary_id: AccountId,
    },
    DeleteKey {
        receipt_index: ReceiptIndex,
        public_key: near_crypto::PublicKey,
    },
    AddKeyWithFunctionCall {
        receipt_index: ReceiptIndex,
        public_key: near_crypto::PublicKey,
        nonce: u64,
        allowance: Option<NearToken>,
        receiver_id: AccountId,
        method_names: Vec<String>,
    },
    AddKeyWithFullAccess {
        receipt_index: ReceiptIndex,
        public_key: near_crypto::PublicKey,
        nonce: u64,
    },
    YieldCreate {
        data_id: near_primitives::hash::CryptoHash,
        receiver_id: AccountId,
    },
    YieldResume {
        data: Vec<u8>,
        data_id: near_primitives::hash::CryptoHash,
    },
}

impl MockAction {
    pub fn receipt_index(&self) -> Option<ReceiptIndex> {
        match self {
            MockAction::CreateReceipt { .. } => None,
            MockAction::CreateAccount { receipt_index } => Some(*receipt_index),
            MockAction::DeployContract { receipt_index, .. } => Some(*receipt_index),
            MockAction::FunctionCallWeight { receipt_index, .. } => Some(*receipt_index),
            MockAction::Transfer { receipt_index, .. } => Some(*receipt_index),
            MockAction::Stake { receipt_index, .. } => Some(*receipt_index),
            MockAction::DeleteAccount { receipt_index, .. } => Some(*receipt_index),
            MockAction::DeleteKey { receipt_index, .. } => Some(*receipt_index),
            MockAction::AddKeyWithFunctionCall { receipt_index, .. } => Some(*receipt_index),
            MockAction::AddKeyWithFullAccess { receipt_index, .. } => Some(*receipt_index),
            MockAction::YieldCreate { .. } => None,
            MockAction::YieldResume { .. } => None,
        }
    }
}

fn map_vec_str(vec_str: Vec<Vec<u8>>) -> Vec<String> {
    vec_str
        .into_iter()
        .map(|element| {
            let string: String = String::from_utf8(element).unwrap();
            string
        })
        .collect()
}

impl From<LogicMockAction> for MockAction {
    fn from(value: LogicMockAction) -> Self {
        match value {
            LogicMockAction::CreateReceipt { receipt_indices, receiver_id } => {
                Self::CreateReceipt { receipt_indices, receiver_id }
            }
            LogicMockAction::CreateAccount { receipt_index } => {
                Self::CreateAccount { receipt_index }
            }
            LogicMockAction::DeployContract { receipt_index, code } => {
                Self::DeployContract { receipt_index, code }
            }
            LogicMockAction::FunctionCallWeight {
                receipt_index,
                method_name,
                args,
                attached_deposit,
                prepaid_gas,
                gas_weight,
            } => Self::FunctionCallWeight {
                receipt_index,
                method_name,
                args,
                attached_deposit: NearToken::from_yoctonear(attached_deposit),
                prepaid_gas: Gas::from_gas(prepaid_gas),
                gas_weight,
            },
            LogicMockAction::Transfer { receipt_index, deposit } => {
                MockAction::Transfer { receipt_index, deposit: NearToken::from_yoctonear(deposit) }
            }
            LogicMockAction::Stake { receipt_index, stake, public_key } => MockAction::Stake {
                receipt_index,
                stake: NearToken::from_yoctonear(stake),
                public_key,
            },
            LogicMockAction::DeleteAccount { receipt_index, beneficiary_id } => {
                Self::DeleteAccount { receipt_index, beneficiary_id }
            }
            LogicMockAction::DeleteKey { receipt_index, public_key } => {
                Self::DeleteKey { receipt_index, public_key }
            }
            LogicMockAction::AddKeyWithFunctionCall {
                receipt_index,
                public_key,
                nonce,
                allowance,
                receiver_id,
                method_names,
            } => Self::AddKeyWithFunctionCall {
                receipt_index,
                public_key,
                nonce,
                allowance: allowance.map(NearToken::from_yoctonear),
                receiver_id,
                method_names: map_vec_str(method_names),
            },
            LogicMockAction::AddKeyWithFullAccess { receipt_index, public_key, nonce } => {
                Self::AddKeyWithFullAccess { receipt_index, public_key, nonce }
            }
            LogicMockAction::YieldCreate { data_id, receiver_id } => {
                Self::YieldCreate { data_id, receiver_id }
            }
            LogicMockAction::YieldResume { data, data_id } => Self::YieldResume { data, data_id },
        }
    }
}