neo_solidity/ir/context/builtins/
resolve.rs

1fn resolve_builtin_call(expr: &Expression) -> Option<BuiltinCall> {
2    if let Expression::MemberAccess(_, inner, member) = expr {
3        if let Expression::Variable(base) = inner.as_ref() {
4            let member_name = member.name.as_str();
5            match base.name.as_str() {
6                "Runtime" => return resolve_runtime_member(member_name),
7                "abi" => return resolve_abi_member(member_name),
8                "Storage" => return resolve_storage_member(member_name),
9                "Syscalls" => return resolve_syscalls_member(member_name),
10                "NativeCalls" => return resolve_native_calls_member(member_name),
11                "Neo" => return resolve_neo_member(member_name),
12                _ => {}
13            }
14        }
15    }
16
17    if let Expression::Variable(identifier) = expr {
18        if identifier.name == "ecrecover" {
19            return Some(BuiltinCall::Ecrecover);
20        }
21        if identifier.name == "keccak256" {
22            return Some(BuiltinCall::Keccak256);
23        }
24        if identifier.name == "type" {
25            return Some(BuiltinCall::TypeOf);
26        }
27    }
28
29    None
30}
31
32fn builtin_library_supported_members(base: &str) -> Option<&'static [&'static str]> {
33    match base {
34        "Runtime" => Some(&[
35            "notify",
36            "notifyIndexed",
37            "checkWitness",
38            "gasLeft",
39            "burnGas",
40            "log",
41            "getTime",
42            "getTrigger",
43            "getInvocationCounter",
44            "getCurrentSigners",
45            "getCallFlags",
46            "getScriptContainer",
47            "loadScript",
48            "initializeServices",
49        ]),
50        "abi" => Some(&["encode", "encodePacked", "encodeCall", "encodeWithSignature", "decode"]),
51        "Storage" => Some(&[
52            "find",
53            "put",
54            "get",
55            "remove",
56            "findLocal",
57            "putLocal",
58            "getLocal",
59            "removeLocal",
60            "asReadOnly",
61            "initializeContext",
62            "getContext",
63            "getReadOnlyContext",
64            "getUsage",
65            "putContractMetadata",
66        ]),
67        "Syscalls" => Some(&[
68            "contractCall",
69            "contractCallWithFlags",
70            "getCallFlags",
71            "contractCreate",
72            "contractUpdate",
73            "contractDestroy",
74            "createStandardAccount",
75            "createMultisigAccount",
76            "notify",
77            "getCurrentIndex",
78            "getCurrentHash",
79            "getBlock",
80            "getTransaction",
81            "getTransactionHeight",
82            "getTransactionFromBlock",
83            "getTransactionSigners",
84            "getTransactionVMState",
85            "getExecutingScriptHash",
86            "getCallingScriptHash",
87            "getEntryScriptHash",
88            "getScriptContainer",
89            "loadScript",
90            "getStorageContext",
91            "getReadOnlyStorageContext",
92            "storageAsReadOnly",
93            "storageGet",
94            "storagePut",
95            "storageDelete",
96            "storageFind",
97            "storageGetLocal",
98            "storagePutLocal",
99            "storageDeleteLocal",
100            "storageFindLocal",
101            "checkWitness",
102            "getTime",
103            "gasLeft",
104            "getPlatform",
105            "getTrigger",
106            "getNotifications",
107            "log",
108            "getCurrentSigners",
109            "checkSig",
110            "checkMultisig",
111            "sha256",
112            "ripemd160",
113            "verifyWithECDsa",
114            "murmur32",
115            "keccak256",
116            "recoverSecp256K1",
117            "verifyWithEd25519",
118            "bls12381Serialize",
119            "bls12381Deserialize",
120            "bls12381Equal",
121            "bls12381Add",
122            "bls12381Mul",
123            "bls12381Pairing",
124            "serialize",
125            "deserialize",
126            "itoa",
127            "atoi",
128            "jsonSerialize",
129            "jsonDeserialize",
130            "base64Encode",
131            "base64Decode",
132            "base64UrlEncode",
133            "base64UrlDecode",
134            "base58Encode",
135            "base58Decode",
136            "base58CheckEncode",
137            "base58CheckDecode",
138            "hexEncode",
139            "hexDecode",
140            "memoryCompare",
141            "memorySearch",
142            "stringSplit",
143            "strLen",
144            "iteratorNext",
145            "iteratorValue",
146            "getCurrentRandom",
147            "getNetwork",
148            "getAddressVersion",
149            "burnGas",
150            "getInvocationCounter",
151            "getFeePerByte",
152            "getExecFeeFactor",
153            "getExecPicoFeeFactor",
154            "getStoragePrice",
155            "getMillisecondsPerBlock",
156            "getMaxValidUntilBlockIncrement",
157            "getMaxTraceableBlocks",
158            "getAttributeFee",
159            "isBlocked",
160            "oracleRequest",
161            "getOraclePrice",
162            "getDesignatedByRole",
163            "scriptHashToAddress",
164            "addressToScriptHash",
165            "isValidAddress",
166            "getContractScript",
167            "contractExists",
168        ]),
169        "NativeCalls" => Some(&[
170            "neoTotalSupply",
171            "neoBalanceOf",
172            "neoTransfer",
173            "neoDecimals",
174            "neoSymbol",
175            "vote",
176            "getCandidates",
177            "registerCandidate",
178            "unregisterCandidate",
179            "getGasPerBlock",
180            "getRegisterPrice",
181            "setRegisterPrice",
182            "setGasPerBlock",
183            "getAccountState",
184            "unclaimedGas",
185            "getCandidateVote",
186            "getCommittee",
187            "getCommitteeAddress",
188            "isCommittee",
189            "getNextBlockValidators",
190            "getAllCandidates",
191            "isValidator",
192            "gasTotalSupply",
193            "gasBalanceOf",
194            "gasTransfer",
195            "gasDecimals",
196            "gasSymbol",
197            "deployContract",
198            "updateContract",
199            "destroyContract",
200            "getContract",
201            "listContracts",
202            "hasMethod",
203            "getMinimumDeploymentFee",
204            "setMinimumDeploymentFee",
205            "getContractById",
206            "isContract",
207            "getFeePerByte",
208            "setFeePerByte",
209            "getExecFeeFactor",
210            "getExecPicoFeeFactor",
211            "setExecFeeFactor",
212            "getStoragePrice",
213            "getMillisecondsPerBlock",
214            "setMillisecondsPerBlock",
215            "getMaxValidUntilBlockIncrement",
216            "setMaxValidUntilBlockIncrement",
217            "getMaxTraceableBlocks",
218            "setMaxTraceableBlocks",
219            "getAttributeFee",
220            "setAttributeFee",
221            "setStoragePrice",
222            "blockAccount",
223            "unblockAccount",
224            "isBlocked",
225            "getBlockedAccounts",
226            "recoverFund",
227            "setWhitelistFeeContract",
228            "removeWhitelistFeeContract",
229            "getWhitelistFeeContracts",
230            "requestOracleData",
231            "getOraclePrice",
232            "setOraclePrice",
233            "oracleFinish",
234            "oracleVerify",
235            "designateAsRole",
236            "getDesignatedByRole",
237            "currentIndex",
238            "currentHash",
239            "getBlock",
240            "getTransaction",
241            "getTransactionHeight",
242            "getTransactionFromBlock",
243            "getTransactionSigners",
244            "getTransactionVMState",
245            "notaryVerify",
246            "notaryBalanceOf",
247            "notaryExpirationOf",
248            "notaryLockDepositUntil",
249            "notaryWithdraw",
250            "notaryGetMaxNotValidBeforeDelta",
251            "notarySetMaxNotValidBeforeDelta",
252            "notaryOnNEP17Payment",
253            "treasuryVerify",
254            "treasuryOnNEP17Payment",
255            "treasuryOnNEP11Payment",
256            "isNativeContract",
257            "getNativeContractName",
258            "getAllNativeContracts",
259            "estimateNativeCallGas",
260            "batchNativeCalls",
261            "getNetworkConfiguration",
262            "getNativeContractManifest",
263            "safeNativeCall",
264            "externalNativeCall",
265        ]),
266        "Neo" => Some(&[
267            "verifySignature",
268            "callContract",
269            "deployContract",
270            "getNeoBalance",
271            "getGasBalance",
272            "getGasPrice",
273            "getStoragePrice",
274            "getCommittee",
275            "getValidators",
276            "isCommittee",
277            "isValidator",
278            "getRandom",
279        ]),
280        _ => None,
281    }
282}
283
284fn resolve_runtime_member(member: &str) -> Option<BuiltinCall> {
285    match member {
286        "notify" => Some(BuiltinCall::RuntimeNotify),
287        "checkWitness" => Some(BuiltinCall::RuntimeCheckWitness),
288        "gasLeft" => Some(BuiltinCall::Syscall("System.Runtime.GasLeft".to_string())),
289        "burnGas" => Some(BuiltinCall::Syscall("System.Runtime.BurnGas".to_string())),
290        "log" => Some(BuiltinCall::Syscall("System.Runtime.Log".to_string())),
291        "getTime" => Some(BuiltinCall::Syscall("System.Runtime.GetTime".to_string())),
292        "getTrigger" => Some(BuiltinCall::Syscall("System.Runtime.GetTrigger".to_string())),
293        "getInvocationCounter" => Some(BuiltinCall::Syscall(
294            "System.Runtime.GetInvocationCounter".to_string(),
295        )),
296        "getCurrentSigners" => Some(BuiltinCall::Syscall(
297            "System.Runtime.CurrentSigners".to_string(),
298        )),
299        "getCallFlags" => Some(BuiltinCall::Syscall(
300            "System.Contract.GetCallFlags".to_string(),
301        )),
302        "getScriptContainer" => Some(BuiltinCall::Syscall(
303            "System.Runtime.GetScriptContainer".to_string(),
304        )),
305        "loadScript" => Some(BuiltinCall::Syscall("System.Runtime.LoadScript".to_string())),
306        _ => None,
307    }
308}
309
310fn resolve_abi_member(member: &str) -> Option<BuiltinCall> {
311    match member {
312        "encode" => Some(BuiltinCall::AbiEncode),
313        "encodePacked" => Some(BuiltinCall::AbiEncodePacked),
314        "encodeCall" => Some(BuiltinCall::AbiEncodeCall),
315        "encodeWithSignature" => Some(BuiltinCall::AbiEncodeWithSignature),
316        "decode" => Some(BuiltinCall::AbiDecode),
317        _ => None,
318    }
319}
320
321fn resolve_storage_member(member: &str) -> Option<BuiltinCall> {
322    match member {
323        "find" => Some(BuiltinCall::StorageFind),
324        "put" => Some(BuiltinCall::StoragePut),
325        "get" => Some(BuiltinCall::StorageGet),
326        "remove" => Some(BuiltinCall::StorageDelete),
327        "findLocal" => Some(BuiltinCall::Syscall("System.Storage.Local.Find".to_string())),
328        "putLocal" => Some(BuiltinCall::Syscall("System.Storage.Local.Put".to_string())),
329        "getLocal" => Some(BuiltinCall::Syscall("System.Storage.Local.Get".to_string())),
330        "removeLocal" => Some(BuiltinCall::Syscall("System.Storage.Local.Delete".to_string())),
331        "initializeContext" => Some(BuiltinCall::Syscall("System.Storage.GetContext".to_string())),
332        "getContext" => Some(BuiltinCall::Syscall("System.Storage.GetContext".to_string())),
333        "getReadOnlyContext" => Some(BuiltinCall::Syscall(
334            "System.Storage.GetReadOnlyContext".to_string(),
335        )),
336        "asReadOnly" => Some(BuiltinCall::Syscall("System.Storage.AsReadOnly".to_string())),
337        _ => None,
338    }
339}
340
341fn resolve_neo_member(member: &str) -> Option<BuiltinCall> {
342    match member {
343        "verifySignature" => Some(BuiltinCall::VerifySignature),
344        "callContract" => Some(BuiltinCall::ContractCall),
345        "deployContract" => Some(BuiltinCall::DeployContract),
346        "getNeoBalance" => Some(BuiltinCall::NativeCall {
347            contract: NativeContract::Neo,
348            method: "balanceOf".to_string(),
349        }),
350        "getGasBalance" => Some(BuiltinCall::NativeCall {
351            contract: NativeContract::Gas,
352            method: "balanceOf".to_string(),
353        }),
354        "getGasPrice" => Some(BuiltinCall::NativeCall {
355            contract: NativeContract::Policy,
356            method: "getFeePerByte".to_string(),
357        }),
358        "getStoragePrice" => Some(BuiltinCall::NativeCall {
359            contract: NativeContract::Policy,
360            method: "getStoragePrice".to_string(),
361        }),
362        "getCommittee" => Some(BuiltinCall::NativeCall {
363            contract: NativeContract::Neo,
364            method: "getCommittee".to_string(),
365        }),
366        "getRandom" => Some(BuiltinCall::Syscall("System.Runtime.GetRandom".to_string())),
367        _ => None,
368    }
369}