首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在huff中返回常数变量

如何在huff中返回常数变量
EN

Ethereum用户
提问于 2022-08-03 17:43:41
回答 1查看 48关注 0票数 0

假设我在愤怒中有一个常数变量:

代码语言:javascript
复制
#define constant myAddress = 0x0000000000000000000000000000000000000000

我将如何制作一个宏来读取它?因为它没有储存槽,所以我不会用.

代码语言:javascript
复制
#define macro GET_ADDRESS() = takes (0) returns (0) {
    // No storage slot
    [VALUE_LOCATION]   
    sload                

    0x00 mstore
    0x20 0x00 return
}
EN

回答 1

Ethereum用户

发布于 2022-08-03 18:04:02

当您想要将一个常量推送到堆栈时,您可以使用括号语法[MY_CONST] &编译器将插入PUSH<N> value,其中N=该常数的值中的字节数。

将常量推到堆栈后,可以使用0x20 0x00 return返回32个字节

代码语言:javascript
复制
#define macro GET_ADDRESS() = takes (0) returns (0) {
    [PRICE_FEED_ADDRESS] 0x00 mstore
    0x20 0x00 return
}

只返回常量作为视图函数的完整示例代码:

代码语言:javascript
复制
// 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()
}
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/132948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档