near_sys/
lib.rs

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