nesdie 0.2.0

no_std SDK for NEAR protocol
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use crate::types::Vec;
use crate::{sys, AccountId, Balance, Gas};
use core::mem::size_of;

/// Register used internally for atomic operations. This register is safe to use by the user,
/// since it only needs to be untouched while methods of `Environment` execute, which is guaranteed
/// guest code is not parallel.
const ATOMIC_OP_REGISTER: u64 = core::u64::MAX - 1;
/// Register used to record evicted values from the storage.
const EVICTED_REGISTER: u64 = core::u64::MAX - 2;

/// Key used to store the state of the contract.
const STATE_KEY: &[u8] = b"STATE";

/// A simple macro helper to read blob value coming from host's method.
macro_rules! try_method_into_register {
    ( $method:ident, $v:expr ) => {{
        unsafe { sys::$method(ATOMIC_OP_REGISTER) };
        read_register(ATOMIC_OP_REGISTER, $v).unwrap_or_else(|_| abort())
    }};
}

/// Same as `try_method_into_register` but expects the data.
macro_rules! method_into_register {
    ( $method:ident, $v:expr ) => {{
        try_method_into_register!($method, $v)
    }};
}

/// Index for a batch promise from within the runtime. Used to combine promises within a contract.
pub struct PromiseIndex(pub u64);

/// Aborts the current contract execution without a custom message.
/// To include a message, use [`panic_str`].
pub fn abort() -> ! {
    // Use wasm32 unreachable call to avoid including the `panic` external function in Wasm.
    #[cfg(target_arch = "wasm32")]
    {
        core::arch::wasm32::unreachable()
    }
    #[cfg(not(target_arch = "wasm32"))]
    unsafe {
        sys::panic()
    }
}

/// Reads the content of the `register_id`. If register is not used or the buffer is not large
/// enough, an error will be returned.
#[allow(clippy::result_unit_err)]
pub fn read_register(register_id: u64, buf: &mut [u8]) -> Result<usize, ()> {
    let len = register_len(register_id).ok_or(())? as usize;
    if buf.len() < len {
        return Err(());
    }
    unsafe { sys::read_register(register_id, buf.as_ptr() as _) };
    Ok(len)
}

/// Returns the size of the register. If register is not used returns `None`.
pub fn register_len(register_id: u64) -> Option<u64> {
    let len = unsafe { sys::register_len(register_id) };
    if len == core::u64::MAX {
        None
    } else {
        Some(len)
    }
}

// ###############
// # Context API #
// ###############

// TODO eval this API before releasing
/// The id of the account that owns the current contract.
pub fn current_account_id() -> AccountId {
    let mut a = Vec::<u8, 64>::new();
    // Resize buffer to max length before reading bytes into it.
    a.resize(64, 0).unwrap_or_else(|_| abort());
    let len = method_into_register!(current_account_id, a.as_mut());
    // Update length for size written
    unsafe {
        a.set_len(len);
        // Fine to cast as account id, should be validated by runtime
        AccountId::new_raw(a)
    }
}

/// Current block index.
pub fn block_index() -> u64 {
    unsafe { sys::block_index() }
}

/// Current block timestamp, i.e, number of non-leap-nanoseconds since January 1, 1970 0:00:00 UTC.
pub fn block_timestamp() -> u64 {
    unsafe { sys::block_timestamp() }
}

/// Current epoch height.
pub fn epoch_height() -> u64 {
    unsafe { sys::epoch_height() }
}

/// Current total storage usage of this smart contract that this account would be paying for.
pub fn storage_usage() -> u64 {
    unsafe { sys::storage_usage() }
}

// #################
// # Economics API #
// #################
/// The balance attached to the given account. This includes the attached_deposit that was
/// attached to the transaction
pub fn account_balance() -> Balance {
    let data = [0u8; size_of::<Balance>()];
    unsafe { sys::account_balance(data.as_ptr() as u64) };
    Balance::from_le_bytes(data)
}

/// The balance locked for potential validator staking.
pub fn account_locked_balance() -> Balance {
    let data = [0u8; size_of::<Balance>()];
    unsafe { sys::account_locked_balance(data.as_ptr() as u64) };
    Balance::from_le_bytes(data)
}

/// The balance that was attached to the call that will be immediately deposited before the
/// contract execution starts
pub fn attached_deposit() -> Balance {
    let data = [0u8; size_of::<Balance>()];
    unsafe { sys::attached_deposit(data.as_ptr() as u64) };
    Balance::from_le_bytes(data)
}

/// The amount of gas attached to the call that can be used to pay for the gas fees.
pub fn prepaid_gas() -> Gas {
    unsafe { sys::prepaid_gas() }
}

/// The gas that was already burnt during the contract execution (cannot exceed `prepaid_gas`)
pub fn used_gas() -> Gas {
    unsafe { sys::used_gas() }
}

// ############
// # Math API #
// ############
// /// Get random seed from the register.
// pub fn random_seed() -> Vec<u8> {
//     method_into_register!(random_seed)
// }

/// Hashes the random sequence of bytes using sha256.
pub fn sha256(value: &[u8]) -> [u8; 32] {
    unsafe { sys::sha256(value.len() as _, value.as_ptr() as _, ATOMIC_OP_REGISTER) };
    let mut hash = [0u8; 32];

    read_register(ATOMIC_OP_REGISTER, &mut hash).unwrap_or_else(|_| abort());
    hash
}

/// Hashes the random sequence of bytes using keccak256.
pub fn keccak256(value: &[u8]) -> [u8; 32] {
    unsafe { sys::keccak256(value.len() as _, value.as_ptr() as _, ATOMIC_OP_REGISTER) };
    let mut hash = [0u8; 32];

    read_register(ATOMIC_OP_REGISTER, &mut hash).unwrap_or_else(|_| abort());
    hash
}

/// Hashes the random sequence of bytes using keccak512.
pub fn keccak512(value: &[u8]) -> [u8; 64] {
    unsafe { sys::keccak512(value.len() as _, value.as_ptr() as _, ATOMIC_OP_REGISTER) };
    let mut hash = [0u8; 64];

    read_register(ATOMIC_OP_REGISTER, &mut hash).unwrap_or_else(|_| abort());
    hash
}

// ###############
// # Validator API #
// ###############

/// For a given account return its current stake. If the account is not a validator, returns 0.
pub fn validator_stake(account_id: &str) -> Balance {
    let data = [0u8; size_of::<Balance>()];
    unsafe {
        sys::validator_stake(
            account_id.len() as _,
            account_id.as_ptr() as _,
            data.as_ptr() as u64,
        )
    };
    Balance::from_le_bytes(data)
}

/// Returns the total stake of validators in the current epoch.
pub fn validator_total_stake() -> Balance {
    let data = [0u8; size_of::<Balance>()];
    unsafe { sys::validator_total_stake(data.as_ptr() as u64) };
    Balance::from_le_bytes(data)
}

// #####################
// # Miscellaneous API #
// #####################
/// Sets the blob of data as the return value of the contract.
pub fn value_return(value: &[u8]) {
    unsafe { sys::value_return(value.len() as _, value.as_ptr() as _) }
}

/// Terminates the execution of the program with the UTF-8 encoded message.
pub fn panic_str(message: &str) -> ! {
    unsafe { sys::panic_utf8(message.len() as _, message.as_ptr() as _) }
}
/// Log the UTF-8 encodable message.
pub fn log_str(message: &str) {
    #[cfg(all(debug_assertions, not(target_arch = "wasm32")))]
    eprintln!("{}", message);
    unsafe { sys::log_utf8(message.len() as _, message.as_ptr() as _) }
}

// ###############
// # Storage API #
// ###############

/// Writes key-value into storage.
/// If another key-value existed in the storage with the same key it returns `true`, otherwise `false`.
pub fn storage_write(key: &[u8], value: &[u8]) -> bool {
    match unsafe {
        sys::storage_write(
            key.len() as _,
            key.as_ptr() as _,
            value.len() as _,
            value.as_ptr() as _,
            EVICTED_REGISTER,
        )
    } {
        0 => false,
        1 => true,
        _ => abort(),
    }
}
/// Reads the value stored under the given key.
pub fn storage_read(key: &[u8], buf: &mut [u8]) -> Option<usize> {
    match unsafe { sys::storage_read(key.len() as _, key.as_ptr() as _, ATOMIC_OP_REGISTER) } {
        0 => None,
        1 => Some(read_register(ATOMIC_OP_REGISTER, buf).unwrap_or_else(|_| abort())),
        _ => abort(),
    }
}
/// Removes the value stored under the given key.
/// If key-value existed returns `true`, otherwise `false`.
pub fn storage_remove(key: &[u8]) -> bool {
    match unsafe { sys::storage_remove(key.len() as _, key.as_ptr() as _, EVICTED_REGISTER) } {
        0 => false,
        1 => true,
        _ => abort(),
    }
}
/// Reads the most recent value that was evicted with `storage_write` or `storage_remove` command.
pub fn storage_get_evicted(buf: &mut [u8]) -> Option<usize> {
    read_register(EVICTED_REGISTER, buf).ok()
}
/// Checks if there is a key-value in the storage.
pub fn storage_has_key(key: &[u8]) -> bool {
    match unsafe { sys::storage_has_key(key.len() as _, key.as_ptr() as _) } {
        0 => false,
        1 => true,
        _ => abort(),
    }
}

// ############################################
// # Saving and loading of the contract state #
// ############################################
/// Load the state of the given object.
/// Read raw bytes under the static state key.
pub fn state_read_raw(buf: &mut [u8]) -> Option<usize> {
    storage_read(STATE_KEY, buf)
}

/// Write bytes under the static state key.
pub fn state_write_raw(data: &[u8]) {
    storage_write(STATE_KEY, data);
}

/// Returns `true` if the contract state exists and `false` otherwise.
pub fn state_exists() -> bool {
    storage_has_key(STATE_KEY)
}

//* Promises

// Creates a promise that will execute a method on account with given arguments and attaches
/// the given amount and gas.
pub fn promise_create(
    account_id: &str,
    method_name: &str,
    arguments: &[u8],
    amount: Balance,
    gas: Gas,
) -> PromiseIndex {
    unsafe {
        PromiseIndex(sys::promise_create(
            account_id.len() as _,
            account_id.as_ptr() as _,
            method_name.len() as _,
            method_name.as_ptr() as _,
            arguments.len() as _,
            arguments.as_ptr() as _,
            &amount as *const Balance as _,
            gas,
        ))
    }
}

/// Attaches the callback that is executed after promise pointed by `promise_idx` is complete.
pub fn promise_then(
    promise_idx: PromiseIndex,
    account_id: &str,
    method_name: &str,
    arguments: &[u8],
    amount: Balance,
    gas: Gas,
) -> PromiseIndex {
    unsafe {
        PromiseIndex(sys::promise_then(
            promise_idx.0,
            account_id.len() as _,
            account_id.as_ptr() as _,
            method_name.len() as _,
            method_name.as_ptr() as _,
            arguments.len() as _,
            arguments.as_ptr() as _,
            &amount as *const Balance as _,
            gas,
        ))
    }
}

// TODO consider API, currently requires alloc
// /// Creates a new promise which completes when time all promises passed as arguments complete.
// pub fn promise_and(promise_indices: &[PromiseIndex]) -> PromiseIndex {
//     let mut data = vec![0u8; promise_indices.len() * size_of::<PromiseIndex>()];
//     for i in 0..promise_indices.len() {
//         data[i * size_of::<PromiseIndex>()..(i + 1) * size_of::<PromiseIndex>()]
//             .copy_from_slice(&promise_indices[i].to_le_bytes());
//     }
//     unsafe {
//         PromiseIndex(sys::promise_and(
//             data.as_ptr() as _,
//             promise_indices.len() as _,
//         ))
//     }
// }

/// Create a batch promise and return the index of that promise.
pub fn promise_batch_create(account_id: &str) -> PromiseIndex {
    unsafe {
        PromiseIndex(sys::promise_batch_create(
            account_id.len() as _,
            account_id.as_ptr() as _,
        ))
    }
}

/// Schedule a promise after the provided promise index.
pub fn promise_batch_then(promise_index: PromiseIndex, account_id: &str) -> PromiseIndex {
    unsafe {
        PromiseIndex(sys::promise_batch_then(
            promise_index.0,
            account_id.len() as _,
            account_id.as_ptr() as _,
        ))
    }
}

/// Create account with the batch promise.
pub fn promise_batch_action_create_account(promise_index: PromiseIndex) {
    unsafe { sys::promise_batch_action_create_account(promise_index.0) }
}

/// Deploy contract with the batch promise.
pub fn promise_batch_action_deploy_contract(promise_index: u64, code: &[u8]) {
    unsafe {
        sys::promise_batch_action_deploy_contract(
            promise_index,
            code.len() as _,
            code.as_ptr() as _,
        )
    }
}

/// Call a function within the batch promise.
pub fn promise_batch_action_function_call(
    promise_index: PromiseIndex,
    method_name: &str,
    arguments: &[u8],
    amount: Balance,
    gas: Gas,
) {
    unsafe {
        sys::promise_batch_action_function_call(
            promise_index.0,
            method_name.len() as _,
            method_name.as_ptr() as _,
            arguments.len() as _,
            arguments.as_ptr() as _,
            &amount as *const Balance as _,
            gas,
        )
    }
}

/// Transfer tokens with the promise.
pub fn promise_batch_action_transfer(promise_index: PromiseIndex, amount: Balance) {
    unsafe { sys::promise_batch_action_transfer(promise_index.0, &amount as *const Balance as _) }
}

/// Stake tokens with the promise.
pub fn promise_batch_action_stake(promise_index: PromiseIndex, amount: Balance, public_key: &[u8]) {
    unsafe {
        sys::promise_batch_action_stake(
            promise_index.0,
            &amount as *const Balance as _,
            public_key.len() as _,
            public_key.as_ptr() as _,
        )
    }
}

/// Add full access key with batch promise.
pub fn promise_batch_action_add_key_with_full_access(
    promise_index: PromiseIndex,
    public_key: &[u8],
    nonce: u64,
) {
    unsafe {
        sys::promise_batch_action_add_key_with_full_access(
            promise_index.0,
            public_key.len() as _,
            public_key.as_ptr() as _,
            nonce,
        )
    }
}

/// Add access key with only function call priviledges
pub fn promise_batch_action_add_key_with_function_call(
    promise_index: PromiseIndex,
    public_key: &[u8],
    nonce: u64,
    allowance: Balance,
    receiver_id: &str,
    method_names: &str,
) {
    unsafe {
        sys::promise_batch_action_add_key_with_function_call(
            promise_index.0,
            public_key.len() as _,
            public_key.as_ptr() as _,
            nonce,
            &allowance as *const Balance as _,
            receiver_id.len() as _,
            receiver_id.as_ptr() as _,
            method_names.len() as _,
            method_names.as_ptr() as _,
        )
    }
}

/// Delete access key with batch promise.
pub fn promise_batch_action_delete_key(promise_index: PromiseIndex, public_key: &[u8]) {
    unsafe {
        sys::promise_batch_action_delete_key(
            promise_index.0,
            public_key.len() as _,
            public_key.as_ptr() as _,
        )
    }
}

/// Delete account with batch promise.
pub fn promise_batch_action_delete_account(promise_index: PromiseIndex, beneficiary_id: &str) {
    unsafe {
        sys::promise_batch_action_delete_account(
            promise_index.0,
            beneficiary_id.len() as _,
            beneficiary_id.as_ptr() as _,
        )
    }
}

// #####################################
// # Parameters exposed by the runtime #
// #####################################

/// Price per 1 byte of storage from mainnet genesis config.
/// TODO: will be using the host function when it will be available.
const STORAGE_PRICE_PER_BYTE: Balance = 10_000_000_000_000_000_000;

/// Returns the storage cost per byte.
pub fn storage_byte_cost() -> Balance {
    STORAGE_PRICE_PER_BYTE
}