You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Chainlink Direct Request任务返回结构体数组?

尝试通过Chainlink Direct Request任务向消费者合约返回长度不确定的自定义结构体数组MyStruct[],但对应的TOML任务配置无法正常工作。已知gas消耗问题,后续会通过API端点断路器处理,当前暂不考虑该问题。

消费者合约伪代码

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol";

contract ConsumerContract is ChainlinkClient, ConfirmedOwner {

    . . .

    struct MyStruct {
        uint a;
        uint b;
        string x;
    }

    MyStruct[] public result;

    function request() public {
    
        Chainlink.Request memory req = buildOperatorRequest(jobId, this.fulfill.selector);      

        // (add parameters)
       
        // Send the request to the Chainlink oracle
        sendOperatorRequest(req, fee);
    }

    function fulfill(bytes32 requestId, bytes[] memory _result) public recordChainlinkFulfillment(requestId) {

        emit RequestFulfilled(requestId, _result);

        // Process the response
        for(uint i = 0; i < _result.length; i++){
            MyStruct memory myStruct = abi.decode(_result[i], (MyStruct));
            result.push(myStruct);
        }

    }

    . . .

}

API端点示例响应(可自行调整格式)

[
   [
      0,
      1,
      "myString-1"
   ],
   [
      2,
      3,
      "myString-2"
   ]
   . . .
]

当前无法正常工作的TOML任务配置

type = "directrequest"
schemaVersion = 1
name = "my-job"
forwardingAllowed = false
maxTaskDuration = "0s"
contractAddress = "XXX"
minContractPaymentLinkJuels = "0"
observationSource = """"

    decode_log   [type="ethabidecodelog"
                  abi="OracleRequest(bytes32 indexed specId, address requester, bytes32 requestId, uint256 payment, address callbackAddr, bytes4 callbackFunctionId, uint256 cancelExpiration, uint256 dataVersion, bytes data)"
                  data="$(jobRun.logData)"
                  topics="$(jobRun.logTopics)"]

    decode_cbor  [type="cborparse" data="$(decode_log.data)"]

    fetch [type="http" method=GET url="$(decode_cbor.url)" allowunrestrictednetworkaccess="true"]

    encode_data [type="ethabiencode"
                abi="(bytes32 requestId, bytes[] _data)"
                data="{\"requestId\": $(decode_log.requestId), \"_data\": [$(fetch)]}"
                ]

    encode_tx  [type="ethabiencode"
                abi="fulfillOracleRequest2(bytes32 requestId, uint256 payment, address callbackAddress, bytes4 callbackFunctionId, uint256 expiration, bytes calldata data)"
                data="{\"requestId\": $(decode_log.requestId), \"payment\": $(decode_log.payment), \"callbackAddress\": $(decode_log.callbackAddr), \"callbackFunctionId\": $(decode_log.callbackFunctionId), \"expiration\": $(decode_log.cancelExpiration), \"data\": $(encode_data)}"
                ]

    submit_tx    [type="ethtx" to="XXX" data="$(encode_tx)"]

    decode_log -> decode_cbor -> fetch -> encode_data -> encode_tx -> submit_tx
"""

问题现状

Chainlink任务流水线可内部完成,但fulfill交易执行失败,推测是消费者合约的fulfill()函数无法将bytes[]响应转换为struct[]导致。查阅Chainlink文档,未找到通过Direct Request任务返回自定义类型(如结构体)的相关信息。仅找到类似问题,但仅涉及单字段结构体,未覆盖结构体数组场景。

内容的提问来源于stack exchange,提问作者Donald

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 16:03:26