When you want to push a constant to the stack, you use bracket syntax [MY_CONST]
& the compiler will insert PUSH<N> value
, where N = the number of bytes in the constant’s value.
After you've pushed the constant to the stack, you can return the 32 bytes with 0x20 0x00 return
#define macro GET_ADDRESS() = takes (0) returns (0) {
[PRICE_FEED_ADDRESS] 0x00 mstore
0x20 0x00 return
}
Full example code for just returning a constant as a view function:
// 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()
}