假设我在愤怒中有一个常数变量:
#define constant myAddress = 0x0000000000000000000000000000000000000000我将如何制作一个宏来读取它?因为它没有储存槽,所以我不会用.
#define macro GET_ADDRESS() = takes (0) returns (0) {
// No storage slot
[VALUE_LOCATION]
sload
0x00 mstore
0x20 0x00 return
}发布于 2022-08-03 18:04:02
当您想要将一个常量推送到堆栈时,您可以使用括号语法[MY_CONST] &编译器将插入PUSH<N> value,其中N=该常数的值中的字节数。
将常量推到堆栈后,可以使用0x20 0x00 return返回32个字节
#define macro GET_ADDRESS() = takes (0) returns (0) {
[PRICE_FEED_ADDRESS] 0x00 mstore
0x20 0x00 return
}只返回常量作为视图函数的完整示例代码:
// SPDX-License-Identifier: MIT
/* Interface */
#define function getAddress() view returns (address)
/* Constants */
#define constant MY_CONST_ADDRESS = 0x0000000000000000000000000000000000000001
/* Macrocs (Functions) */
#define macro GET_ADDRESS() = takes (0) returns (0) {
[MY_CONST_ADDRESS] 0x00 mstore
0x20 0x00 return
}
#define macro MAIN() = takes (0) returns (0) {
// Identify which function is being called.
0x00 calldataload 0xE0 shr
// We tell our code where to go based on the function selector
// 0x38cc4831 is the function selector of `getAddress`
// Here is some info on quickly getting a func selector: https://ethereum.stackexchange.com/questions/131693/fastest-way-to-find-the-function-in-a-contract-from-its-selector
dup1 0x38cc4831 eq getAddress jumpi
getAddress:
GET_ADDRESS()
}https://ethereum.stackexchange.com/questions/132948
复制相似问题