【问题标题】:What are the functions setPublicChainlinkToken & setChainlinkToken in Chainlink API call?Chainlink API 调用中的 setPublicChainlinkToken 和 setChainlinkToken 函数是什么?
【发布时间】:2021-10-06 11:38:05
【问题描述】:

我正在遵循 https://docs.chain.link/docs/advanced-tutorial/ 上的 Chainlink 文档教程,从我的智能合约中进行 API 调用。但是,我仍然无法理解 APIConsumer 的构造函数中调用的函数setPublicChainlinkToken()。 我正在尝试通过 API 调用获取城市的温度。但是我的合同在编译时出现错误:

APIConsumer hit a require or revert statement somewhere in its constructor

上述错误非常笼统,我无法理解问题所在。下面是我的合约代码和我用来部署它的脚本。

我需要在部署脚本中传递哪些参数?

APIConsumer 合约

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

import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";

/**
 * THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
 * PLEASE DO NOT USE THIS CODE IN PRODUCTION.
 */
contract APIConsumer is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
    uint256 public temperature;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    constructor () public {
        setPublicChainlinkToken(); // Do I need to pass any params for this?
        // if (_link == address(0)) {
        //     setPublicChainlinkToken();
        // } else {
        //     setChainlinkToken(_link);
        // }
        // setPublicChainlinkToken();
        oracle = <oracle id>; // Removed oracle id and jobid values for post
        jobId = <job id>;
        fee = 0.1 * 10 ** 18; // 0.1 LINK (Varies by network and job)
    }
    
    /**
     * Create a Chainlink request to retrieve API response, find the target
     * data, then multiply by 1000000000000000000 (to remove decimal places from data).
     */
    function requestVolumeData() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        
        // Set the URL to perform the GET request on
        // "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"
        
        request.add("get", "http://api.weatherstack.com/current?access_key=7940b0c1136901badcb304724132b234&query=Mumbai");
        
        // Set the path to find the desired data in the API response, where the response format is:
        // {"RAW":
        //   {"ETH":
        //    {"USD":
        //     {
        //      "VOLUME24HOUR": xxx.xxx,
        //     }
        //    }
        //   }
        //  }
        request.add("path", "current.temperature");
        
        // Multiply the result by 1000000000000000000 to remove decimals
        // int timesAmount = 10**18;
        // request.addInt("times", timesAmount);
        
        // Sends the request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of uint256
     */ 
    function fulfill(bytes32 _requestId, uint256 _temperature) public recordChainlinkFulfillment(_requestId)
    {
        temperature = _temperature;
    }

    // function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}

要部署的 Javascript 脚本:

const APIConsumer = artifacts.require("APIConsumer");
module.exports = async (deployer, network, [defaultAccount]) => {
  try {
    await deployer.deploy(APIConsumer);
  } catch (err) {
    console.error(err);
  }
};

【问题讨论】:

    标签: blockchain solidity smartcontracts chainlink


    【解决方案1】:

    setChainlinkToken 是一个函数,它告诉预言机合约它应该使用什么来接受 LINK 付款。它指向一个 ERC677 代币供合约使用。

    你必须知道LINK令牌地址是什么才能使用这个功能。

    setPublicChainlinkToken() 是一种在不知道其地址的情况下设置 LINK 令牌地址的方法。有一个链上合约(在特定链上)有一个指向“链接代币合约”注册表的指针,该注册表指向不同链上 LINK 代币的地址。所以这个函数通过查看这个查找表来获取地址,然后用这个地址调用setChainlinkToken函数。


    然后您会收到您指定的错误,因为您正在与之交互的预言机合约不知道 LINK 令牌的地址是什么。

            // if (_link == address(0)) {
            //     setPublicChainlinkToken();
            // } else {
            //     setChainlinkToken(_link);
            // }
            // setPublicChainlinkToken();
    

    【讨论】:

      猜你喜欢
      • 2022-03-10
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多