Chainlink如何向fulfill函数传递额外参数?
Chainlink请求中传递额外代币参数的实现方案
你遇到的是Chainlink请求中传递自定义关联参数的典型场景,核心问题是Chainlink的buildChainlinkRequest无法直接向fulfill函数传递额外参数,下面提供两种成熟的解决方案:
方案一:通过请求ID映射关联代币地址(通用方案)
这是最常用的实现方式,不需要依赖Oracle的Job配置,通过合约内部的映射存储请求ID与代币地址的绑定关系,在回调时取出使用。
步骤1:添加状态变量
在合约中定义两个映射,分别存储请求ID到代币地址的关联,以及代币到价格的存储:
// 存储请求ID对应的代币地址 mapping(bytes32 => address) public requestIdToToken; // 存储各代币的最新价格 mapping(address => uint256) public tokenPrices;
步骤2:修改请求函数
在发起Chainlink请求时,将生成的requestId与目标代币地址绑定:
function requestAPIValue(string memory queryToken, address token) public returns (bytes32 requestId) { Chainlink.Request memory req = buildChainlinkRequest( jobId32, address(this), this.fulfillValue.selector ); req.add("get", string.concat(url, queryToken)); req.add("path", path); req.addInt(addIntParam, multiply); requestId = sendChainlinkRequest(req, oraclePayment); // 绑定请求ID与代币地址 requestIdToToken[requestId] = token; }
步骤3:修改回调函数
在fulfillValue中通过requestId取出对应的代币地址,完成价格存储:
event RequestValue(bytes32 indexed requestId, uint256 value, address indexed token); function fulfillValue(bytes32 _requestId, uint256 _value) public recordChainlinkFulfillment(_requestId) { address token = requestIdToToken[_requestId]; // 校验请求ID有效性 require(token != address(0), "Request ID not found"); // 更新代币价格 tokenPrices[token] = _value; emit RequestValue(_requestId, _value, token); // 可选:清理映射释放存储 delete requestIdToToken[_requestId]; }
方案二:通过Chainlink Request传递自定义参数(需自定义Job支持)
如果你的Chainlink Oracle是自行部署的,且可以修改Job配置,可以直接在请求中添加自定义参数,让Oracle在回调时返回该参数。
步骤1:在请求中添加代币参数
将代币地址转为十六进制字符串,添加到Chainlink Request中:
function requestAPIValue(string memory queryToken, address token) public returns (bytes32 requestId) { Chainlink.Request memory req = buildChainlinkRequest( jobId32, address(this), this.fulfillValue.selector ); req.add("get", string.concat(url, queryToken)); req.add("path", path); req.addInt(addIntParam, multiply); // 添加自定义代币参数(转十六进制字符串) req.add("token", Strings.toHexString(uint256(uint160(token)), 20)); requestId = sendChainlinkRequest(req, oraclePayment); }
步骤2:修改回调函数签名
确保回调函数的参数与Oracle Job返回的参数匹配,将返回的字节转换为地址:
event RequestValue(bytes32 indexed requestId, uint256 value, address indexed token); function fulfillValue(bytes32 _requestId, uint256 _value, bytes32 tokenBytes) public recordChainlinkFulfillment(_requestId) { address token = address(uint160(uint256(tokenBytes))); tokenPrices[token] = _value; emit RequestValue(_requestId, _value, token); }
注意:此方案需要修改Oracle的Job配置,确保Job在处理完API请求后,将
token参数一并返回给合约。如果使用第三方公开Oracle,通常不支持自定义参数传递,优先选择方案一。
内容的提问来源于stack exchange,提问作者WP Smart Contracts
相关产品推荐
相关产品推荐

