使用ChainlinkClient v0.6开发NFT智能合约时遇TypeError:struct Chainlink.Request memory中找不到或无法通过参数依赖查找访问成员“add”的问题排查
解决Chainlink v0.6中
Chainlink.Request的add方法不存在的错误 嘿,这个问题我之前也踩过坑!根源是你搞混了Chainlink不同版本的API用法——你现在用的是Chainlink v0.6版本的ChainlinkClient,这个版本里的Chainlink.Request结构体根本没有add这个成员方法,add、addInt这类简洁的调用方式是v0.7及以后版本才引入的,v0.6得换一种写法。
在v0.6版本中,所有给请求添加参数的操作都得通过Chainlink全局合约的静态方法来完成,而不是直接调用request对象的方法。我给你把代码修正好了,直接就能用:
// SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; import "chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol"; contract phoneNumber is ChainlinkClient{ uint256 public phone_no; address private oracle; bytes32 private jobId; uint256 private fee; constructor(uint256 _initial_phone_no){ setPublicChainlinkToken(); oracle = 0x7AFe1118Ea78C1eae84ca8feE5C65Bc76CcF879e; jobId = "6d1bfe27e7034b1d87b5270556b17277"; fee = 0.1 * 10 ** 18; // 0.1 LINK phone_no = _initial_phone_no; } function requestVolumeData() public returns (bytes32 requestId) { Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.update_phone_no.selector); // Set the URL to perform the GET request on Chainlink.add(request, "get", "http://localhost:3000/images/phone.json"); Chainlink.add(request, "path", "phone.new"); // Multiply the result by 1000000000000000000 to remove decimals int timesAmount = 10**18; Chainlink.addInt(request, "times", timesAmount); // Sends the request return sendChainlinkRequestTo(oracle, request, fee); } function update_phone_no(bytes32 _requestId, uint256 new_phone_no) public recordChainlinkFulfillment(_requestId) { phone_no = new_phone_no; } function withdrawLink() external { LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress()); require(linkToken.transfer(msg.sender, linkToken.balanceOf(address(this))), "Unable to transfer"); } }
再给你划几个关键注意点:
- v0.6版本中,添加字符串参数用
Chainlink.add(request, key, value),添加整数参数用Chainlink.addInt(request, key, value),还有对应无符号整数的Chainlink.addUint,都是全局调用的形式 - 千万别把v0.6和v0.7+的API混着用,v0.7+为了简化开发,把这些方法直接集成到了
Chainlink.Request结构体中,所以你原来的写法在v0.6里才会报找不到方法的错误 - 如果之后想升级到更高版本的ChainlinkClient,记得把Solidity版本升到0.7及以上,同时调整一些语法细节(比如函数可见性的写法)
内容的提问来源于stack exchange,提问作者cherry
相关产品推荐
相关产品推荐

