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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#![no_std]

// re-export basic heap types
extern crate alloc;
pub use alloc::boxed::Box;
pub use alloc::vec::Vec;
pub use alloc::string::String;

pub use elrond_codec;

mod types;
pub mod io;
mod proxy;
pub mod storage;
pub mod err_msg;
pub mod call_data;
pub mod non_zero_util;

pub use types::*;
pub use io::*;
pub use storage::{storage_get, storage_set, BorrowedMutStorage};
pub use finish::SCResult;
pub use call_data::*;
pub use proxy::OtherContractHandle;

use core::ops::{Add, Sub, Mul, Div, Rem, Neg};
use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
use core::ops::{BitAnd, BitOr, BitXor, Shr, Shl};
use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign, ShrAssign, ShlAssign};

/// 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 trait ContractHookApi<BigInt, BigUint>: Sized
where
    BigInt: elrond_codec::Encode + 'static,
    BigUint: elrond_codec::Encode + 'static,
{

    fn get_sc_address(&self) -> Address;

    fn get_owner_address(&self) -> Address;

    fn get_caller(&self) -> Address;

    fn get_balance(&self, address: &Address) -> BigUint;

    fn get_sc_balance(&self) -> BigUint {
        self.get_balance(&self.get_sc_address())
    }
    
    fn storage_store(&self, key: &[u8], value: &[u8]);

    fn storage_load(&self, key: &[u8]) -> Vec<u8>;

    fn storage_load_len(&self, key: &[u8]) -> usize;

    fn storage_store_bytes32(&self, key: &[u8], value: &[u8; 32]);
    
    fn storage_load_bytes32(&self, key: &[u8]) -> [u8; 32];

    fn storage_store_big_uint(&self, key: &[u8], value: &BigUint);
    
    fn storage_load_big_uint(&self, key: &[u8]) -> BigUint;

    fn storage_store_big_int(&self, key: &[u8], value: &BigInt);
    
    fn storage_load_big_int(&self, key: &[u8]) -> BigInt;

    fn storage_store_i64(&self, key: &[u8], value: i64);
    
    fn storage_load_i64(&self, key: &[u8]) -> Option<i64>;

    #[inline]
    fn storage_load_cumulated_validator_reward(&self) -> BigUint {
        self.storage_load_big_uint(storage::protected_keys::ELROND_REWARD_KEY)
    }
    
    fn get_call_value_big_uint(&self) -> BigUint;

    fn send_tx(&self, to: &Address, amount: &BigUint, message: &str);

    fn async_call(&self, to: &Address, amount: &BigUint, data: &[u8]);

    fn get_tx_hash(&self) -> H256;

    fn get_gas_left(&self) -> i64;

    fn get_block_timestamp(&self) -> u64;

    fn get_block_nonce(&self) -> u64;

    fn get_block_round(&self) -> u64;
    
    fn get_block_epoch(&self) -> u64;

    fn sha256(&self, data: &[u8]) -> [u8; 32];

    fn keccak256(&self, data: &[u8]) -> [u8; 32];
}

// deprecated, do not use
macro_rules! get_argument_signed_cast {
    ($method_name:ident, $type:ty) => {
        fn $method_name (&self, arg_id: i32) -> $type {
            let arg_i64 = self.get_argument_i64(arg_id);
            let min = <$type>::MIN as i64;
            let max = <$type>::MAX as i64;
            if arg_i64 < min || arg_i64 > max {
                self.signal_error(err_msg::ARG_OUT_OF_RANGE)
            }
            arg_i64 as $type
        }
  };
}

// deprecated, do not use
macro_rules! get_argument_unsigned_cast {
    ($method_name:ident, $type:ty) => {
        fn $method_name (&self, arg_id: i32) -> $type {
            let arg_i64 = self.get_argument_u64(arg_id);
            let min = <$type>::MIN as u64;
            let max = <$type>::MAX as u64;
            if arg_i64 < min || arg_i64 > max {
                self.signal_error(err_msg::ARG_OUT_OF_RANGE)
            }
            arg_i64 as $type
        }
  };
}

/// Interface to only be used by code generated by the macros.
/// The smart contract code doesn't have access to these methods directly.
pub trait ContractIOApi<BigInt, BigUint> {

    fn get_num_arguments(&self) -> i32;

    fn check_num_arguments(&self, expected: i32) -> bool {
        let nr_args = self.get_num_arguments();
        if nr_args != expected {
            self.signal_error(err_msg::ARG_WRONG_NUMBER);
        }
        true
    }

    fn check_not_payable(&self);

    fn get_argument_len(&self, arg_index: i32) -> usize;

    fn copy_argument_to_slice(&self, arg_index: i32, slice: &mut [u8]);

    fn get_argument_vec(&self, arg_index: i32) -> Vec<u8>;

    fn get_argument_bytes32(&self, arg_index: i32) -> [u8; 32];
    
    fn get_argument_address(&self, arg_index: i32) -> Address {
        self.get_argument_bytes32(arg_index).into()
    }
    
    fn get_argument_big_int(&self, arg_id: i32) -> BigInt;

    fn get_argument_big_uint(&self, arg_id: i32) -> BigUint;
    
    // signed
    fn get_argument_i64(&self, arg_id: i32) -> i64;
    get_argument_signed_cast!{get_argument_i32, i32} // deprecated, do not use
    get_argument_signed_cast!{get_argument_isize, isize} // deprecated, do not use
    get_argument_signed_cast!{get_argument_i8, i8} // deprecated, do not use

    // unsigned
    fn get_argument_u64(&self, arg_id: i32) -> u64 {
        let bytes = self.get_argument_vec(arg_id);
        if bytes.len() > 8 {
            self.signal_error(err_msg::ARG_OUT_OF_RANGE);
        }
        elrond_codec::bytes_to_number(bytes.as_slice(), false)
    }
    get_argument_unsigned_cast!{get_argument_u32, u32} // deprecated, do not use
    get_argument_unsigned_cast!{get_argument_usize, usize} // deprecated, do not use
    get_argument_unsigned_cast!{get_argument_u8, u8} // deprecated, do not use

    /// deprecated, do not use
    fn get_argument_bool (&self, arg_id: i32) -> bool {
        let arg_i64 = self.get_argument_i64(arg_id);
        match arg_i64 {
            1 => true,
            0 => false,
            _ => self.signal_error(err_msg::ARG_WRONG_NUMBER)
        }
    }
    
    fn finish_slice_u8(&self, slice: &[u8]);

    fn finish_bytes32(&self, bytes: &[u8; 32]);

    fn finish_big_int(&self, b: &BigInt);

    fn finish_big_uint(&self, b: &BigUint);

    fn finish_i64(&self, value: i64);

    fn signal_error(&self, message: &[u8]) -> !;

    fn write_log(&self, topics: &[[u8;32]], data: &[u8]);
}

/// Definition of the BigUint type required by the API.
/// The API doesn't care about the actual BigInt implementation.
/// The Arwen VM provides an implementation directly in the protocol.
/// For debugging we use a different implementation, based on Rust's BigInt.
/// 
/// Since most values in smart contracts will not be signed, as well as for safety,
/// most of the functionality if provided for unsigned integers.
pub trait BigUintApi: 
    Sized +
    From<u64> +
    From<u32> +
    From<usize> +
    Clone +
    Add<Output=Self> + 
    AddAssign + 
    Sub<Output=Self> + 
    SubAssign +
    Mul<Output=Self> +
    MulAssign +
    Div<Output=Self> +
    DivAssign +
    Rem<Output=Self> +
    RemAssign +
    BitAnd<Output=Self> +
    BitAndAssign +
    BitOr<Output=Self> +
    BitOrAssign +
    BitXor<Output=Self> +
    BitXorAssign +
    Shr<usize, Output=Self> +
    ShrAssign<usize> +
    Shl<usize, Output=Self> +
    ShlAssign<usize> +
    PartialEq<Self> +
    Eq +
    PartialOrd<Self> +
    Ord +
    PartialEq<u64> +
    PartialOrd<u64> +
    elrond_codec::Encode +
    elrond_codec::Decode +
{
    fn zero() -> Self {
        0u64.into()
    }

    fn byte_length(&self) -> i32;

    fn copy_to_slice_big_endian(&self, slice: &mut [u8]) -> i32;

    fn copy_to_array_big_endian_pad_right(&self, target: &mut [u8; 32]);

    fn to_bytes_be(&self) -> Vec<u8>;

    fn to_bytes_be_pad_right(&self, nr_bytes: usize) -> Option<Vec<u8>>;

    fn from_bytes_be(bytes: &[u8]) -> Self;
}

// BigInt sign.
pub enum Sign {
    Minus,
    NoSign,
    Plus,
}

/// Definition of the BigInt type required by the API.
pub trait BigIntApi<BigUint>: 
        Sized +
        From<BigUint> +
        From<i64> +
        From<i32> +
        Clone +
        Add<Output=Self> + 
        AddAssign + 
        Sub<Output=Self> + 
        SubAssign +
        Mul<Output=Self> +
        MulAssign +
        Div<Output=Self> +
        DivAssign +
        Rem<Output=Self> +
        RemAssign +
        Neg +
        PartialEq<Self> +
        Eq +
        PartialOrd<Self> +
        Ord +
        PartialEq<i64> +
        PartialOrd<i64> +
        elrond_codec::Encode +
        elrond_codec::Decode +
{
    fn zero() -> Self {
        0i64.into()
    }

    fn abs_uint(&self) -> BigUint;

    fn sign(&self) -> Sign;

    fn to_signed_bytes_be(&self) -> Vec<u8>;

    fn from_signed_bytes_be(bytes: &[u8]) -> Self;
}

/// CallableContract is the means by which the debugger calls methods in the contract.
pub trait CallableContract {
    fn call(&self, fn_name: &'static str);

    fn clone_contract(&self) -> Box<dyn CallableContract>;
}

/// Handy way of casting to a contract proxy trait.
/// Would make more sense to be in elrond-wasm-derive, but Rust "cannot export macro_rules! macros from a `proc-macro` crate type currently".
#[macro_export]
macro_rules! contract_proxy {
    ($s:expr, $address:expr, $proxy_trait:ident) => {
      $s.contract_proxy($address) as Box<dyn $proxy_trait<BigInt, BigUint>>
  };
}

/// Getting all imports needed for a smart contract.
#[macro_export]
macro_rules! imports {
    () => {
        use elrond_wasm::{Box, Vec, String, Queue, VarArgs, BorrowedMutStorage};
        use elrond_wasm::{SCError, SCResult, SCResult::Ok, SCResult::Err};
        use elrond_wasm::{H256, Address, ErrorMessage};
        use elrond_wasm::{ContractHookApi, ContractIOApi, BigIntApi, BigUintApi, OtherContractHandle, AsyncCallResult, AsyncCallError};
        use elrond_wasm::elrond_codec::{Encode, Decode, DecodeError};
        use elrond_wasm::io::*;
        use elrond_wasm::non_zero_util::*;
        use elrond_wasm::err_msg;
        use core::ops::{Add, Sub, Mul, Div, Rem};
        use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
        use core::ops::{BitAnd, BitOr, BitXor, Shr, Shl};
        use core::ops::{BitAndAssign, BitOrAssign, BitXorAssign, ShrAssign, ShlAssign};
  };
}

/// Compact way of returning a static error message.
#[macro_export]
macro_rules! sc_error {
    ($s:expr) => {
        elrond_wasm::SCResult::Err(elrond_wasm::SCError::Static($s.as_bytes()))
    }
}

/// Equivalent of the ? operator for SCResult.
#[macro_export]
macro_rules! sc_try {
    ($s:expr) => {
        match $s {
            elrond_wasm::SCResult::Ok(t) => t,
            elrond_wasm::SCResult::Err(e) => { return elrond_wasm::SCResult::Err(e); }
        }
        
    }
}

/// Allows us to write Solidity style `require!(<condition>, <error_msg>)` and avoid if statements.
/// 
/// It can only be used in a function that returns `SCResult<_>` where _ can be any type.
/// 
/// ```rust
/// # use elrond_wasm::{*, SCResult::Ok};
/// # pub trait ExampleContract<BigInt, BigUint>: ContractHookApi<BigInt, BigUint>
/// # where
/// #     BigInt: elrond_codec::Encode + 'static,
/// #     BigUint: elrond_codec::Encode + 'static,
/// # {
/// fn only_callable_by_owner(&self) -> SCResult<()> {
///     require!(self.get_caller() == self.get_owner_address(), "Caller must be owner");
///     Ok(())
/// }
/// # }
/// ```
#[macro_export]
macro_rules! require {
	($expression:expr, $error_msg:expr) => {
		if (!($expression)) {
			return sc_error!($error_msg)
		}
	};
}

/// Very compact way of not allowing anyone but the owner to call a function.
/// 
/// It can only be used in a function that returns `SCResult<_>` where _ can be any type.
/// 
/// ```rust
/// # use elrond_wasm::{*, SCResult::Ok};
/// # pub trait ExampleContract<BigInt, BigUint>: ContractHookApi<BigInt, BigUint>
/// # where
/// #     BigInt: elrond_codec::Encode + 'static,
/// #     BigUint: elrond_codec::Encode + 'static,
/// # {
/// fn only_callable_by_owner(&self) -> SCResult<()> {
///     only_owner!(self, "Caller must be owner");
///     Ok(())
/// }
/// # }
/// ```
#[macro_export]
macro_rules! only_owner {
	($trait_self: expr, $error_msg:expr) => {
		if ($trait_self.get_caller() != $trait_self.get_owner_address()) {
			return sc_error!($error_msg)
		}
	};
}

/// Compact way to represent the BorrowedMutStorage type.
#[macro_export]
macro_rules! mut_storage (
    ($t:ty) => (
        BorrowedMutStorage<'_, T, BigInt, BigUint, $t>
    )
);

/// Converts usize to NonZeroUsize or returns SCError.
#[macro_export]
macro_rules! non_zero_usize {
	($input: expr, $error_msg:expr) => {
        if let Some(nz) = NonZeroUsize::new($input) {
            nz
        } else {
			return sc_error!($error_msg)
		}
	};
}