首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回函数中的参数

返回函数中的参数
EN

Ethereum用户
提问于 2023-02-03 10:37:17
回答 1查看 61关注 0票数 1

我有一个从ChainlinkClient继承来向API发送请求的契约,该契约有三个主要功能:

代码语言:javascript
复制
function onTokenTransfer(
    address _sender,
    uint256 _fee,
    bytes calldata _data
) public {
    require(_fee >= fee, "NOT ENOUGH FUNDS");
    bytes memory data = _data[4:];
    uint256 _id = abi.decode(data, (uint256));
    requestVolumeData(_id);
}
代码语言:javascript
复制
function requestVolumeData(uint256 _id) public returns (bytes32 requestId) {
    Chainlink.Request memory req = buildChainlinkRequest(
        jobId,
        address(this),
        this.fulfill.selector
    );

    // do somthing with _id
    req.add(
        "get",
        "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"
    );
    req.add("path", "RAW,ETH,USD,VOLUME24HOUR"); 
    int256 timesAmount = 10 ** 18;
    req.addInt("times", timesAmount);
    return sendChainlinkRequest(req, fee);
}
代码语言:javascript
复制
function fulfill(
    bytes32 _requestId,
    uint256 _volume
) public recordChainlinkFulfillment(_requestId) {
    emit RequestVolume(_requestId, _volume);
    volume = _volume;
}

当用户使用link.transferAndCall使用_id调用我的契约时,onTokenTransfer函数使用相同的_id调用requestVolumeData,该函数向节点发出请求,节点将请求的数据返回给fulfill函数。

My的问题是:如何在实现函数中获得requestVolumeData中使用的_id的值?

我的结构和(id=>struct)映射:

代码语言:javascript
复制
struct Collab {
    address promoter;
    address client;
    string apiUrl;
    uint256 amount;

}

mapping(uint256 => Collab) public collabById;
EN

回答 1

Ethereum用户

回答已采纳

发布于 2023-02-03 10:54:49

您可以将_id存储到以下状态变量中:

代码语言:javascript
复制
uint256 id;

function requestVolumeData(uint256 _id) public returns (bytes32 requestId) {



    Collab memory collab;

    // You can set the values that you are having 

    collab.apiUrl = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD";
    
    collab.promoter = msg.sender; // Or any other address that goes here

    collab.client = msg.sender; // Or any other address that goes here

    collab.amount = 0; // Setting this 0 for now, you can leave that too because by-default it's 0 only

    id = _id;

    collabById[id] = collab;

    Chainlink.Request memory req = buildChainlinkRequest(
        jobId,
        address(this),
        this.fulfill.selector
    );

    // do somthing with _id
    req.add(
        "get",
        "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"
    );
    req.add("path", "RAW,ETH,USD,VOLUME24HOUR"); 
    int256 timesAmount = 10 ** 18;
    req.addInt("times", timesAmount);
    return sendChainlinkRequest(req, fee);
}

然后,在fulfill()函数中,可以使用id状态变量访问最新的D4值:

代码语言:javascript
复制
function fulfill(
    bytes32 _requestId,
    uint256 _volume
) public recordChainlinkFulfillment(_requestId) {
    emit RequestVolume(_requestId, _volume);
    volume = _volume;
    // You can set the remaining values of the mapping struct here
    collabById[id].amount = volume;
}
票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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