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 sha3_256(value_len: u64, value_ptr: u64, register_id: u64);
44    pub fn sha3_384(value_len: u64, value_ptr: u64, register_id: u64);
45    pub fn sha3_512(value_len: u64, value_ptr: u64, register_id: u64);
46    pub fn ripemd160(value_len: u64, value_ptr: u64, register_id: u64);
47    pub fn ecrecover(
48        hash_len: u64,
49        hash_ptr: u64,
50        sig_len: u64,
51        sig_ptr: u64,
52        v: u64,
53        malleability_flag: u64,
54        register_id: u64,
55    ) -> u64;
56    pub fn ed25519_verify(
57        sig_len: u64,
58        sig_ptr: u64,
59        msg_len: u64,
60        msg_ptr: u64,
61        pub_key_len: u64,
62        pub_key_ptr: u64,
63    ) -> u64;
64    pub fn p256_verify(
65        sig_len: u64,
66        sig_ptr: u64,
67        msg_len: u64,
68        msg_ptr: u64,
69        pub_key_len: u64,
70        pub_key_ptr: u64,
71    ) -> u64;
72    pub fn ml_dsa_verify(
73        sig_len: u64,
74        sig_ptr: u64,
75        msg_len: u64,
76        msg_ptr: u64,
77        pub_key_len: u64,
78        pub_key_ptr: u64,
79    ) -> u64;
80    // #####################
81    // # Miscellaneous API #
82    // #####################
83    pub fn value_return(value_len: u64, value_ptr: u64);
84    pub fn panic() -> !;
85    pub fn panic_utf8(len: u64, ptr: u64) -> !;
86    pub fn log_utf8(len: u64, ptr: u64);
87    pub fn log_utf16(len: u64, ptr: u64);
88    pub fn abort(msg_ptr: u32, filename_ptr: u32, line: u32, col: u32) -> !;
89    // ################
90    // # Promises API #
91    // ################
92    pub fn promise_create(
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_then(
103        promise_index: u64,
104        account_id_len: u64,
105        account_id_ptr: u64,
106        function_name_len: u64,
107        function_name_ptr: u64,
108        arguments_len: u64,
109        arguments_ptr: u64,
110        amount_ptr: u64,
111        gas: u64,
112    ) -> u64;
113    pub fn promise_and(promise_idx_ptr: u64, promise_idx_count: u64) -> u64;
114    pub fn promise_batch_create(account_id_len: u64, account_id_ptr: u64) -> u64;
115    pub fn promise_batch_then(promise_index: u64, account_id_len: u64, account_id_ptr: u64) -> u64;
116    // #######################
117    // # Promise API actions #
118    // #######################
119    #[cfg(feature = "deterministic-account-ids")]
120    pub fn promise_set_refund_to(promise_index: u64, account_id_len: u64, account_id_ptr: u64);
121    #[cfg(feature = "deterministic-account-ids")]
122    pub fn promise_batch_action_state_init(
123        promise_index: u64,
124        code_len: u64,
125        code_ptr: u64,
126        amount_ptr: u64,
127    ) -> u64;
128    #[cfg(feature = "deterministic-account-ids")]
129    pub fn promise_batch_action_state_init_by_account_id(
130        promise_index: u64,
131        account_id_len: u64,
132        account_id_ptr: u64,
133        amount_ptr: u64,
134    ) -> u64;
135    #[cfg(feature = "deterministic-account-ids")]
136    pub fn set_state_init_data_entry(
137        promise_index: u64,
138        action_index: u64,
139        key_len: u64,
140        key_ptr: u64,
141        value_len: u64,
142        value_ptr: u64,
143    );
144    pub fn promise_batch_action_create_account(promise_index: u64);
145    pub fn promise_batch_action_deploy_contract(promise_index: u64, code_len: u64, code_ptr: u64);
146    pub fn promise_batch_action_function_call(
147        promise_index: u64,
148        function_name_len: u64,
149        function_name_ptr: u64,
150        arguments_len: u64,
151        arguments_ptr: u64,
152        amount_ptr: u64,
153        gas: u64,
154    );
155    pub fn promise_batch_action_function_call_weight(
156        promise_index: u64,
157        function_name_len: u64,
158        function_name_ptr: u64,
159        arguments_len: u64,
160        arguments_ptr: u64,
161        amount_ptr: u64,
162        gas: u64,
163        weight: u64,
164    );
165    pub fn promise_batch_action_transfer(promise_index: u64, amount_ptr: u64);
166    pub fn promise_batch_action_stake(
167        promise_index: u64,
168        amount_ptr: u64,
169        public_key_len: u64,
170        public_key_ptr: u64,
171    );
172    pub fn promise_batch_action_add_key_with_full_access(
173        promise_index: u64,
174        public_key_len: u64,
175        public_key_ptr: u64,
176        nonce: u64,
177    );
178    pub fn promise_batch_action_add_key_with_function_call(
179        promise_index: u64,
180        public_key_len: u64,
181        public_key_ptr: u64,
182        nonce: u64,
183        allowance_ptr: u64,
184        receiver_id_len: u64,
185        receiver_id_ptr: u64,
186        function_names_len: u64,
187        function_names_ptr: u64,
188    );
189    pub fn promise_batch_action_transfer_to_gas_key(
190        promise_index: u64,
191        public_key_len: u64,
192        public_key_ptr: u64,
193        amount_ptr: u64,
194    );
195    pub fn promise_batch_action_add_gas_key_with_full_access(
196        promise_index: u64,
197        public_key_len: u64,
198        public_key_ptr: u64,
199        num_nonces: u64,
200    );
201    pub fn promise_batch_action_add_gas_key_with_function_call(
202        promise_index: u64,
203        public_key_len: u64,
204        public_key_ptr: u64,
205        num_nonces: u64,
206        allowance_ptr: u64,
207        receiver_id_len: u64,
208        receiver_id_ptr: u64,
209        method_names_len: u64,
210        method_names_ptr: u64,
211    );
212    pub fn promise_batch_action_delete_key(
213        promise_index: u64,
214        public_key_len: u64,
215        public_key_ptr: u64,
216    );
217    pub fn promise_batch_action_delete_account(
218        promise_index: u64,
219        beneficiary_id_len: u64,
220        beneficiary_id_ptr: u64,
221    );
222    // #########################
223    // # Global Contract API   #
224    // #########################
225    pub fn promise_batch_action_deploy_global_contract(
226        promise_index: u64,
227        code_len: u64,
228        code_ptr: u64,
229    );
230    pub fn promise_batch_action_deploy_global_contract_by_account_id(
231        promise_index: u64,
232        code_len: u64,
233        code_ptr: u64,
234    );
235    pub fn promise_batch_action_use_global_contract(
236        promise_index: u64,
237        code_hash_len: u64,
238        code_hash_ptr: u64,
239    );
240    pub fn promise_batch_action_use_global_contract_by_account_id(
241        promise_index: u64,
242        account_id_len: u64,
243        account_id_ptr: u64,
244    );
245    // ##########################
246    // # Universal Accounts API #
247    // ##########################
248    pub fn universal_state_init_to_account_id(
249        state_init_len: u64,
250        state_init_ptr: u64,
251        register_id: u64,
252    ) -> u64;
253    pub fn promise_batch_action_universal_state_init(
254        promise_index: u64,
255        state_init_len: u64,
256        state_init_ptr: u64,
257        amount_ptr: u64,
258    );
259    pub fn promise_yield_create(
260        function_name_len: u64,
261        function_name_ptr: u64,
262        arguments_len: u64,
263        arguments_ptr: u64,
264        gas: u64,
265        gas_weight: u64,
266        register_id: u64,
267    ) -> u64;
268    pub fn promise_yield_resume(
269        data_id_len: u64,
270        data_id_ptr: u64,
271        payload_len: u64,
272        payload_ptr: u64,
273    ) -> u32;
274    pub fn promise_yield_create_with_id(
275        function_name_len: u64,
276        function_name_ptr: u64,
277        arguments_len: u64,
278        arguments_ptr: u64,
279        amount_ptr: u64,
280        gas: u64,
281        gas_weight: u64,
282        yield_id_len: u64,
283        yield_id_ptr: u64,
284    ) -> u64;
285    pub fn promise_yield_resume_with_yield_id(
286        yield_id_len: u64,
287        yield_id_ptr: u64,
288        payload_len: u64,
289        payload_ptr: u64,
290    ) -> u32;
291    // #######################
292    // # Promise API results #
293    // #######################
294    pub fn promise_results_count() -> u64;
295    pub fn promise_result(result_idx: u64, register_id: u64) -> u64;
296    pub fn promise_return(promise_id: u64);
297    // ###############
298    // # Storage API #
299    // ###############
300    pub fn storage_write(
301        key_len: u64,
302        key_ptr: u64,
303        value_len: u64,
304        value_ptr: u64,
305        register_id: u64,
306    ) -> u64;
307    pub fn storage_read(key_len: u64, key_ptr: u64, register_id: u64) -> u64;
308    pub fn storage_remove(key_len: u64, key_ptr: u64, register_id: u64) -> u64;
309    pub fn storage_has_key(key_len: u64, key_ptr: u64) -> u64;
310    pub fn storage_iter_prefix(prefix_len: u64, prefix_ptr: u64) -> u64;
311    pub fn storage_iter_range(start_len: u64, start_ptr: u64, end_len: u64, end_ptr: u64) -> u64;
312    pub fn storage_iter_next(iterator_id: u64, key_register_id: u64, value_register_id: u64)
313    -> u64;
314    // ###############
315    // # Validator API #
316    // ###############
317    pub fn validator_stake(account_id_len: u64, account_id_ptr: u64, stake_ptr: u64);
318    pub fn validator_total_stake(stake_ptr: u64);
319    // #############
320    // # Alt BN128 #
321    // #############
322    pub fn alt_bn128_g1_multiexp(value_len: u64, value_ptr: u64, register_id: u64);
323    pub fn alt_bn128_g1_sum(value_len: u64, value_ptr: u64, register_id: u64);
324    pub fn alt_bn128_pairing_check(value_len: u64, value_ptr: u64) -> u64;
325
326    // #############
327    // # BLS12-381 #
328    // #############
329    pub fn bls12381_p1_sum(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
330    pub fn bls12381_p2_sum(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
331    pub fn bls12381_g1_multiexp(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
332    pub fn bls12381_g2_multiexp(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
333    pub fn bls12381_map_fp_to_g1(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
334    pub fn bls12381_map_fp2_to_g2(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
335    pub fn bls12381_pairing_check(value_len: u64, value_ptr: u64) -> u64;
336    pub fn bls12381_p1_decompress(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
337    pub fn bls12381_p2_decompress(value_len: u64, value_ptr: u64, register_id: u64) -> u64;
338}
339
340/// Alias for [`block_index`] function. Returns the height of the current block.
341///
342/// # Safety
343///
344/// This function relies on the external implementation of [`block_index`].
345#[inline]
346pub unsafe fn block_height() -> u64 {
347    unsafe { block_index() }
348}