【问题标题】:Chainlink v0.8 API Large ResponsesChainlink v0.8 API 大响应
【发布时间】:2021-11-15 12:11:16
【问题描述】:

我正在关注Large Responses Chainlink 教程,但在尝试调用 requestOracleData() 函数时收到错误消息。使用 v0.6 时一切正常,但我需要使用 v0.8 才能收到大量响应。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

/**
 * @title MyContract is an example contract which requests data from
 * the Chainlink network
 * @dev This contract is designed to work on multiple networks, including
 * local test networks
 */
contract MyContract is ChainlinkClient, ERC721 {
    using Chainlink for Chainlink.Request;

    // address constant ORACLE = 0xF405B99ACa8578B9eb989ee2b69D518aaDb90c1F;
    // bytes32 constant JOB_ID = bytes32("7c4b968028f74b2eabd7d428f03ba45c");

    address constant RINKEBY_ORACLE =
        0x3A56aE4a2831C3d3514b5D7Af5578E45eBDb7a40;
    bytes32 constant RINKEBY_JOB_ID =
        bytes32("187bb80e5ee74a139734cac7475f3c6e");

    uint256 constant FEE = 0.1 * 10**18;

    bytes32 internal keyHash;
    uint256 public fee;
    uint256 public tokenCounter;

    // bytes public data;

    bytes32 public data;
    string public image_url;

    struct Clip {
        string name;
        string url;
    }

    Clip[] public clips;

    mapping(bytes32 => string) public requestIdToClipName;
    mapping(bytes32 => address) public requestIdToSender;
    mapping(bytes32 => string) public requestIdToTokenURI;
    event requestedCollectible(bytes32 indexed requestId);

    mapping(bytes32 => uint256) public requestToTokenId;
    mapping(uint256 => string) public tokenIdToName;
    mapping(uint256 => string) public tokenIdToImgUrl;

    constructor(address _link) public ERC721("Tests", "TST") {
        if (_link == address(0)) {
            setPublicChainlinkToken();
        } else {
            setChainlinkToken(_link);
        }
        setChainlinkOracle(0x3A56aE4a2831C3d3514b5D7Af5578E45eBDb7a40);
        tokenCounter = 0;
    }


    function getChainlinkToken() public view returns (address) {
        return chainlinkTokenAddress();
    }

    function createRequestTo(
        string memory _url,
        string memory _path,
        string memory clipName
    ) public returns (bytes32 requestId) {
        Chainlink.Request memory req = buildChainlinkRequest(
            RINKEBY_JOB_ID,
            address(this),
            this.fulfill.selector
        );
        req.add("get", _url);
        req.add("path", _path);
        
        requestOracleData(req, FEE);

        requestId = sendChainlinkRequestTo(RINKEBY_ORACLE, req, FEE);
        requestIdToClipName[requestId] = clipName;
        requestIdToSender[requestId] = msg.sender;
        emit requestedCollectible(requestId);
        return requestId;
    }

    function fulfill(bytes32 _requestId, bytes32 _data)
        public
        recordChainlinkFulfillment(_requestId)
    {
        address nftOwner = requestIdToSender[_requestId];
        string memory name = requestIdToClipName[_requestId];
        uint256 newItemId = clips.length;

        data = _data;
        image_url = bytes32ToString(data);

        clips.push(Clip(name, image_url));

        _safeMint(nftOwner, newItemId);

        tokenIdToName[newItemId] = name;
        tokenIdToImgUrl[newItemId] = image_url;
    }


    function getNumberOfClips() public view returns (uint256) {
        return clips.length;
    }

    /**
     * @notice Allows the owner to withdraw any LINK balance on the contract
     */
    function withdrawLink() public {
        LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
        require(
            link.transfer(msg.sender, link.balanceOf(address(this))),
            "Unable to transfer"
        );
    }

    /**
     * @notice Call this method if no response is received within 5 minutes
     * @param _requestId The ID that was generated for the request to cancel
     * @param _payment The payment specified for the request to cancel
     * @param _callbackFunctionId The bytes4 callback function ID specified for
     * the request to cancel
     * @param _expiration The expiration generated for the request to cancel
     */
    function cancelRequest(
        bytes32 _requestId,
        uint256 _payment,
        bytes4 _callbackFunctionId,
        uint256 _expiration
    ) public {
        cancelChainlinkRequest(
            _requestId,
            _payment,
            _callbackFunctionId,
            _expiration
        );
    }

    function bytes32ToString(bytes32 _bytes32)
        public
        pure
        returns (string memory)
    {
        uint8 i = 0;
        while (i < 32 && _bytes32[i] != 0) {
            i++;
        }
        bytes memory bytesArray = new bytes(i);
        for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
            bytesArray[i] = _bytes32[i];
        }
        return string(bytesArray);
    }
}

这是我得到的错误:

Running 'scripts/request_data.py::main'...
  File "brownie/_cli/run.py", line 49, in main
    return_value, frame = run(
  File "brownie/project/scripts.py", line 103, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File "./scripts/request_data.py", line 18, in main
    apicall = my_contract.createRequestTo(URL, PATH, "test", {"from": dev})
  File "brownie/network/contract.py", line 1693, in __call__
    return self.transact(*args)
  File "brownie/network/contract.py", line 1566, in transact
    return tx["from"].transfer(
  File "brownie/network/account.py", line 642, in transfer
    receipt, exc = self._make_transaction(
  File "brownie/network/account.py", line 725, in _make_transaction
    raise VirtualMachineError(e) from None
  File "brownie/exceptions.py", line 121, in __init__
    raise ValueError(str(exc)) from None
ValueError: Gas estimation failed: 'execution reverted: Must use whitelisted functions'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.

注意:注释掉 requestOracleData() 函数时不会出现此错误。

【问题讨论】:

    标签: chainlink


    【解决方案1】:

    这是因为您在 Rinkeby 上使用的节点操作符未设置为处理大型请求,并且很可能使用 Oracle.sol 代码作为他们的 oracle 合约,它不接受 @987654323 @方法。

    您可以切换到 Kovan 并使用文档中给出的节点,在 market.link 上搜索不同但兼容的节点,或者在 Rinkeby 上托管您自己的与此作业兼容的节点。

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2021-11-15
      • 2022-01-21
      • 2022-01-21
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 2019-02-04
      • 2017-11-24
      相关资源
      最近更新 更多