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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::{
    api::{BlockchainApi, ErrorApi, ManagedTypeApi, StorageReadApi},
    storage::{self, StorageKey},
    types::{
        Address, BigUint, BoxedBytes, DctLocalRole, DctTokenData, ManagedAddress,
        ManagedByteArray, ManagedType, TokenIdentifier, Vec, H256,
    },
};
use alloc::boxed::Box;

/// Interface to be used by the actual smart contract code.
///
/// Note: contracts and the api are not mutable.
/// They simply pass on/retrieve data to/from the protocol.
/// When mocking the blockchain state, we use the Rc/RefCell pattern
/// to isolate mock state mutability from the contract interface.
pub struct BlockchainWrapper<A>
where
    A: BlockchainApi + StorageReadApi + ManagedTypeApi + ErrorApi,
{
    pub(crate) api: A,
}

impl<A> BlockchainWrapper<A>
where
    A: BlockchainApi + StorageReadApi + ManagedTypeApi + ErrorApi,
{
    pub(crate) fn new(api: A) -> Self {
        BlockchainWrapper { api }
    }

    #[inline]
    pub fn get_caller_legacy(&self) -> Address {
        self.api.get_caller_legacy()
    }

    #[inline]
    pub fn get_caller(&self) -> ManagedAddress<A> {
        self.api.get_caller()
    }

    #[inline]
    pub fn get_sc_address_legacy(&self) -> Address {
        self.api.get_sc_address_legacy()
    }

    #[inline]
    pub fn get_sc_address(&self) -> ManagedAddress<A> {
        self.api.get_sc_address()
    }

    #[inline]
    pub fn get_owner_address_legacy(&self) -> Address {
        self.api.get_owner_address_legacy()
    }

    #[inline]
    pub fn get_owner_address(&self) -> ManagedAddress<A> {
        self.api.get_owner_address()
    }

    pub fn check_caller_is_owner(&self) {
        if self.get_owner_address() != self.get_caller() {
            self.api
                .signal_error(b"Endpoint can only be called by owner");
        }
    }

    #[inline]
    pub fn get_shard_of_address_legacy(&self, address: &Address) -> u32 {
        self.api.get_shard_of_address_legacy(address)
    }

    #[inline]
    pub fn get_shard_of_address(&self, address: &ManagedAddress<A>) -> u32 {
        self.api.get_shard_of_address(address)
    }

    #[inline]
    pub fn is_smart_contract_legacy(&self, address: &Address) -> bool {
        self.api.is_smart_contract_legacy(address)
    }

    #[inline]
    pub fn is_smart_contract(&self, address: &ManagedAddress<A>) -> bool {
        self.api.is_smart_contract(address)
    }

    #[inline]
    pub fn get_balance_legacy(&self, address: &Address) -> BigUint<A> {
        self.api.get_balance_legacy(address)
    }

    #[inline]
    pub fn get_balance(&self, address: &ManagedAddress<A>) -> BigUint<A> {
        self.api.get_balance(address)
    }

    #[inline]
    pub fn get_sc_balance(&self, token: &TokenIdentifier<A>, nonce: u64) -> BigUint<A> {
        if token.is_moax() {
            self.get_balance(&self.get_sc_address())
        } else {
            self.get_dct_balance(&self.get_sc_address(), token, nonce)
        }
    }

    #[inline]
    pub fn get_state_root_hash_legacy(&self) -> H256 {
        self.api.get_state_root_hash_legacy()
    }

    #[inline]
    pub fn get_state_root_hash(&self) -> ManagedByteArray<A, 32> {
        self.api.get_state_root_hash()
    }

    #[inline]
    pub fn get_tx_hash_legacy(&self) -> H256 {
        self.api.get_tx_hash_legacy()
    }

    #[inline]
    pub fn get_tx_hash(&self) -> ManagedByteArray<A, 32> {
        self.api.get_tx_hash()
    }

    #[inline]
    pub fn get_gas_left(&self) -> u64 {
        self.api.get_gas_left()
    }

    #[inline]
    pub fn get_block_timestamp(&self) -> u64 {
        self.api.get_block_timestamp()
    }

    #[inline]
    pub fn get_block_nonce(&self) -> u64 {
        self.api.get_block_nonce()
    }

    #[inline]
    pub fn get_block_round(&self) -> u64 {
        self.api.get_block_round()
    }

    #[inline]
    pub fn get_block_epoch(&self) -> u64 {
        self.api.get_block_epoch()
    }

    #[inline]
    pub fn get_block_random_seed_legacy(&self) -> Box<[u8; 48]> {
        self.api.get_block_random_seed_legacy()
    }

    #[inline]
    pub fn get_block_random_seed(&self) -> ManagedByteArray<A, 48> {
        self.api.get_block_random_seed()
    }

    #[inline]
    pub fn get_prev_block_timestamp(&self) -> u64 {
        self.api.get_prev_block_timestamp()
    }

    #[inline]
    pub fn get_prev_block_nonce(&self) -> u64 {
        self.api.get_prev_block_nonce()
    }

    #[inline]
    pub fn get_prev_block_round(&self) -> u64 {
        self.api.get_prev_block_round()
    }

    #[inline]
    pub fn get_prev_block_epoch(&self) -> u64 {
        self.api.get_prev_block_epoch()
    }

    #[inline]
    pub fn get_prev_block_random_seed_legacy(&self) -> Box<[u8; 48]> {
        self.api.get_prev_block_random_seed_legacy()
    }

    #[inline]
    pub fn get_prev_block_random_seed(&self) -> ManagedByteArray<A, 48> {
        self.api.get_prev_block_random_seed()
    }

    #[inline]
    pub fn get_current_dct_nft_nonce(
        &self,
        address: &Address,
        token_id: &TokenIdentifier<A>,
    ) -> u64 {
        self.api.get_current_dct_nft_nonce(address, token_id)
    }

    #[inline]
    pub fn get_dct_balance(
        &self,
        address: &ManagedAddress<A>,
        token_id: &TokenIdentifier<A>,
        nonce: u64,
    ) -> BigUint<A> {
        self.api.get_dct_balance(address, token_id, nonce)
    }

    #[inline]
    pub fn get_dct_token_data(
        &self,
        address: &ManagedAddress<A>,
        token_id: &TokenIdentifier<A>,
        nonce: u64,
    ) -> DctTokenData<A> {
        self.api.get_dct_token_data(address, token_id, nonce)
    }

    /// Retrieves validator rewards, as set by the protocol.
    #[inline]
    pub fn get_cumulated_validator_rewards(&self) -> BigUint<A> {
        let raw_handle = self
            .api
            .storage_load_big_uint_raw(storage::protected_keys::DHARITRI_REWARD_KEY);
        BigUint::from_raw_handle(self.api.clone(), raw_handle)
    }

    /// Retrieves local roles for the token, by reading protected storage.
    /// TODO: rewrite using managed types
    pub fn get_dct_local_roles(&self, token_id: &TokenIdentifier<A>) -> Vec<DctLocalRole> {
        let mut roles = Vec::new();

        let mut key = StorageKey::new(
            self.api.clone(),
            storage::protected_keys::DHARITRI_DCT_LOCAL_ROLES_KEY,
        );
        key.append_managed_buffer(token_id.as_managed_buffer());
        let raw_storage = storage::storage_get::<A, BoxedBytes>(self.api.clone(), &key);
        let raw_storage_bytes = raw_storage.as_slice();
        let mut current_index = 0;

        while current_index < raw_storage_bytes.len() {
            // first character before each role is a \n, so we skip it
            current_index += 1;

            // next is the length of the role as string
            let role_len = raw_storage_bytes[current_index];
            current_index += 1;

            // next is role's ASCII string representation
            let end_index = current_index + role_len as usize;
            let role_name = &raw_storage_bytes[current_index..end_index];
            current_index = end_index;

            let dct_local_role = DctLocalRole::from(role_name);
            roles.push(dct_local_role);
        }

        roles
    }
}