Skip to main content

near_sys/
lib.rs

1#![no_std]
2
3#[cfg_attr(target_arch = "wasm32", link(wasm_import_module = "env"))]
4unsafe extern "C" {
5    // #############
6    // # Registers #
7    // #############
8    pub fn read_register(register_id: u64, ptr: u64);
9    pub fn register_len(register_id: u64) -> u64;
10    pub fn write_register(register_id: u64, data_len: u64, data_ptr: u64);
11    // ###############
12    // # Context API #
13    // ###############
14    pub fn current_account_id(register_id: u64);
15    #[cfg(feature = "deterministic-account-ids")]
16    pub fn current_contract_code(register_id: u64) -> u64;
17    #[cfg(feature = "deterministic-account-ids")]
18    pub fn refund_to_account_id(register_id: u64);
19    pub fn signer_account_id(register_id: u64);
20    pub fn signer_account_pk(register_id: u64);
21    pub fn predecessor_account_id(register_id: u64);
22    pub fn input(register_id: u64);
23    pub fn chain_id(register_id: u64);
24    pub fn block_index() -> u64;
25    pub fn block_timestamp() -> u64;
26    pub fn epoch_height() -> u64;
27    pub fn storage_usage() -> u64;
28    // #################
29    // # Economics API #
30    // #################
31    pub fn account_balance(balance_ptr: u64);
32    pub fn account_locked_balance(balance_ptr: u64);
33    pub fn attached_deposit(balance_ptr: u64);
34    pub fn prepaid_gas() -> u64;
35    pub fn used_gas() -> u64;
36    // ############
37    // # Math API #
38    // ############
39    pub fn random_seed(register_id: u64);
40    pub fn sha256(value_len: u64, value_ptr: u64, register_id: u64);
41    pub fn keccak256(value_len: u64, value_ptr: u64, register_id: u64);
42    pub fn keccak512(value_len: u64, value_ptr: u64, register_id: u64);
43    pub fn ripemd160(value_len: u64, value_ptr: u64, register_id: u64);
44    pub fn ecrecover(
45        hash_len: u64,
46        hash_ptr: u64,
47        sig_len: u64,
48        sig_ptr: u64,
49        v: u64,
50        malleability_flag: u64,
51        register_id: u64,
52    ) -> u64;
53    pub fn ed25519_verify(
54        sig_len: u64,
55        sig_ptr: u64,
56        msg_len: u64,
57        msg_ptr: u64,
58        pub_key_len: u64,
59        pub_key_ptr: u64,
60    ) -> u64;
61    pub fn p256_verify(
62        sig_len: u64,
63        sig_ptr: u64,
64        msg_len: u64,
65        msg_ptr: u64,
66        pub_key_len: u64,
67        pub_key_ptr: u64,
68    ) -> u64;
69    // #####################
70    // # Miscellaneous API #
71    // #####################
72    pub fn value_return(value_len: u64, value_ptr: u64);
73    pub fn panic() -> !;
74    pub fn panic_utf8(len: u64, ptr: u64) -> !;
75    pub fn log_utf8(len: u64, ptr: u64);
76    pub fn log_utf16(len: u64, ptr: u64);
77    pub fn abort(msg_ptr: u32, filename_ptr: u32, line: u32, col: u32) -> !;
78    // ################
79    // # Promises API #
80    // ################
81    pub fn promise_create(
82        account_id_len: u64,
83        account_id_ptr: u64,
84        function_name_len: u64,
85        function_name_ptr: u64,
86        arguments_len: u64,
87        arguments_ptr: u64,
88        amount_ptr: u64,
89        gas: u64,
90    ) -> u64;
91    pub fn promise_then(
92        promise_index: u64,
93        account_id_len: u64,
94        account_id_ptr: u64,
95        function_name_len: u64,
96        function_name_ptr: u64,
97        arguments_len: u64,
98        arguments_ptr: u64,
99        amount_ptr: u64,
100        gas: u64,
101    ) -> u64;
102    pub fn promise_and(promise_idx_ptr: u64, promise_idx_count: u64) -> u64;
103    pub fn promise_batch_create(account_id_len: u64, account_id_ptr: u64) -> u64;
104    pub fn promise_batch_then(promise_index: u64, account_id_len: u64, account_id_ptr: u64) -> u64;
105    // #######################
106    // # Promise API actions #
107    // #######################
108    #[cfg(feature = "deterministic-account-ids")]
109    pub fn promise_set_refund_to(promise_index: u64, account_id_len: u64, account_id_ptr: u64);
110    #[cfg(feature = "deterministic-account-ids")]
111    pub fn promise_batch_action_state_init(
112        promise_index: u64,
113        code_len: u64,
114        code_ptr: u64,
115        amount_ptr: u64,
116    ) -> u64;
117    #[cfg(feature = "deterministic-account-ids")]
118    pub fn promise_batch_action_state_init_by_account_id(
119        promise_index: u64,
120        account_id_len: u64,
121        account_id_ptr: u64,
122        amount_ptr: u64,
123    ) -> u64;
124    #[cfg(feature = "deterministic-account-ids")]
125    pub fn set_state_init_data_entry(
126        promise_index: u64,
127        action_index: u64,
128        key_len: u64,
129        key_ptr: u64,
130        value_len: u64,
131        value_ptr: u64,
132    );
133    pub fn promise_batch_action_create_account(promise_index: u64);
134    pub fn promise_batch_action_deploy_contract(promise_index: u64, code_len: u64, code_ptr: u64);
135    pub fn promise_batch_action_function_call(
136        promise_index: u64,
137        function_name_len: u64,
138        function_name_ptr: u64,
139        arguments_len: u64,
140        arguments_ptr: u64,
141        amount_ptr: u64,
142        gas: u64,
143    );
144    pub fn promise_batch_action_function_call_weight(
145        promise_index: u64,
146        function_name_len: u64,
147        function_name_ptr: u64,
148        arguments_len: u64,
149        arguments_ptr: u64,
150        amount_ptr: u64,
151        gas: u64,
152        weight: u64,
153    );
154    pub fn promise_batch_action_transfer(promise_index: u64, amount_ptr: u64);
155    pub fn promise_batch_action_stake(
156        promise_index: u64,
157        amount_ptr: u64,
158        public_key_len: u64,
159        public_key_ptr: u64,
160    );
161    pub fn promise_batch_action_add_key_with_full_access(
162        promise_index: u64,
163        public_key_len: u64,
164        public_key_ptr: u64,
165        nonce: u64,
166    );
167    pub fn promise_batch_action_add_key_with_function_call(
168        promise_index: u64,
169        public_key_len: u64,
170        public_key_ptr: u64,
171        nonce: u64,
172        allowance_ptr: u64,
173        receiver_id_len: u64,
174        receiver_id_ptr: u64,
175        function_names_len: u64,
176        function_names_ptr: u64,
177    );
178    pub fn promise_batch_action_transfer_to_gas_key(
179        promise_index: u64,
180        public_key_len: u64,
181        public_key_ptr: u64,
182        amount_ptr: u64,
183    );
184    pub fn promise_batch_action_add_gas_key_with_full_access(
185        promise_index: u64,
186        public_key_len: u64,
187        public_key_ptr: u64,
188        num_nonces: u64,
189    );
190    pub fn promise_batch_action_add_gas_key_with_function_call(
191        promise_index: u64,
192        public_key_len: u64,
193        public_key_ptr: u64,
194        num_nonces: u64,
195        allowance_ptr: u64,
196        receiver_id_len: u64,
197        receiver_id_ptr: u64,
198        method_names_len: u64,
199        method_names_ptr: u64,
200    );
201    pub fn promise_batch_action_delete_key(
202        promise_index: u64,
203        public_key_len: u64,
204        public_key_ptr: u64,
205    );
206    pub fn promise_batch_action_delete_account(
207        promise_index: u64,
208        beneficiary_id_len: u64,
209        beneficiary_id_ptr: u64,
210    );
211    // #########################
212    // # Global Contract API   #
213    // #########################
214    pub fn promise_batch_action_deploy_global_contract(
215        promise_index: u64,
216        code_len: u64,
217        code_ptr: u64,
218    );
219    pub fn promise_batch_action_deploy_global_contract_by_account_id(
220        promise_index: u64,
221        code_len: u64,
222        code_ptr: u64,
223    );
224    pub fn promise_batch_action_use_global_contract(
225        promise_index: u64,
226        code_hash_len: u64,
227        code_hash_ptr: u64,
228    );
229    pub fn promise_batch_action_use_global_contract_by_account_id(
230        promise_index: u64,
231        account_id_len: u64,
232        account_id_ptr: u64,
233    );
234    pub fn promise_yield_create(
235        function_name_len: u64,
236        function_name_ptr: u64,
237        arguments_len: u64,
238        arguments_ptr: u64,
239        gas: u64,
240        gas_weight: u64,
241        register_id: u64,
242    ) -> u64;
243    pub fn promise_yield_resume(
244        data_id_len: u64,
245        data_id_ptr: u64,
246        payload_len: u64,
247        payload_ptr: u64,
248    ) -> u32;
249    pub fn promise_yield_create_with_id(
250        function_name_len: u64,
251        function_name_ptr: u64,
252        arguments_len: u64,
253        arguments_ptr: u64,
254        amount_ptr: u64,
255        gas: u64,
256        gas_weight: u64,
257        yield_id_len: u64,
258        yield_id_ptr: u64,
259    ) -> u64;
260    pub fn promise_yield_resume_with_yield_id(
261        yield_id_len: u64,
262        yield_id_ptr: u64,
263        payload_len: u64,
264        payload_ptr: u64,
265    ) -> u32;
266    // #######################
267    // # Promise API results #
268    // #######################
269    pub fn promise_results_count() -> u64;
270    pub fn promise_result(result_idx: u64, register_id: u64) -> u64;
271    pub fn promise_return(promise_id: u64);
272    // ###############
273    // # Storage API #
274    // ###############
275    pub fn storage_write(
276        key_len: u64,
277        key_ptr: u64,
278        value_len: u64,
279        value_ptr: u64,
280        register_id: u64,
281    ) -> u64;
282    pub fn storage_read(key_len: u64, key_ptr: u64, register_id: u64) -> u64;
283    pub fn storage_remove(key_len: u64, key_ptr: u64, register_id: u64) -> u64;
284    pub fn storage_has_key(key_len: u64, key_ptr: u64) -> u64;
285    pub fn storage_iter_prefix(prefix_len: u64, prefix_ptr: u64) -> u64;
286    pub fn storage_iter_range(start_len: u64, start_ptr: u64, end_len: u64, end_ptr: u64) -> u64;
287    pub fn storage_iter_next(iterator_id: u64, key_register_id: u64, value_register_id: u64)
288    -> u64;
289    // ###############
290    // # Validator API #
291    // ###############
292    pub fn validator_stake(account_id_len: u64, account_id_ptr: u64, stake_ptr: u64);
293    pub fn validator_total_stake(stake_ptr: u64);
294    // #############
295    // # Alt BN128 #
296    // #############
297    pub fn alt_bn128_g1_multiexp(value_len: u64, value_ptr: u64, register_id: u64);
298    pub fn alt_bn128_g1_sum(value_len: u64, value_ptr: u64, register_id: u64);
299    pub fn alt_bn128_pairing_check(value_len: u64, value_ptr: u64) -> u64;
300
301    // #############
302    // # BLS12-381 #
303    // #############
304    pub fn bls12381_p1_sum(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
305    pub fn bls12381_p2_sum(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
306    pub fn bls12381_g1_multiexp(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
307    pub fn bls12381_g2_multiexp(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
308    pub fn bls12381_map_fp_to_g1(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
309    pub fn bls12381_map_fp2_to_g2(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
310    pub fn bls12381_pairing_check(value_len: u64, value_ptr: u64) -> u64;
311    pub fn bls12381_p1_decompress(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
312    pub fn bls12381_p2_decompress(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
313}
314
315/// Alias for [`block_index`] function. Returns the height of the current block.
316///
317/// # Safety
318///
319/// This function relies on the external implementation of [`block_index`].
320#[inline]
321pub unsafe fn block_height() -> u64 {
322    unsafe { block_index() }
323}