0

Let's say I have a constant variable in huff:

#define constant myAddress = 0x0000000000000000000000000000000000000000

How would I make a macro to read it? Since it doesn't have a storage slot, I wouldn't use sload...

#define macro GET_ADDRESS() = takes (0) returns (0) {
    // No storage slot
    [VALUE_LOCATION]   
    sload                

    0x00 mstore
    0x20 0x00 return
}

1 Answer 1

0

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()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.