1

我目前正在使用 Chainlink 的示例 HTTP GET 代码两次从 CoinGecko API 提取 Uniswap 价格和 Uniswap 供应(见下文)。我想在相同的“请求”和“履行”函数中从 API 中提取价格和供应,以便我可以在“履行”函数中计算市值(market_cap = 供应 * 价格)。(我意识到我可以从 API 中提取市值,但出于开发原因,我想计算它。)

有没有办法改变下面的代码来做到这一点?Chainlink 允许这样做吗?

pragma solidity ^0.6.0;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/ChainlinkClient.sol";

contract APIConsumer is ChainlinkClient {
  
    uint256 public uniPrice;
    uint256 public uniSupply;
    //uint256 public uniMarketCap;
 
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    /**
     * Network: Kovan
     * Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e
     * Chainlink - 29fa9aa13bf1468788b7cc4a500a45b8
     * Fee: 0.1 LINK
     */
    constructor() public {
        setPublicChainlinkToken();
        oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
        jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }

//dont forget to fund contract with link

    function requestUniPrice() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillUniPrice.selector);

        request.add("get", "https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&ids=uniswap%2C%20sushi%2C%20pancakeswap-token&order=market_cap_desc&per_page=100&page=1&sparkline=false");
        
        request.add("path","0.current_price");

        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    function requestUniSupply() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillUniSupply.selector);

        request.add("get", "https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&ids=uniswap%2C%20sushi%2C%20pancakeswap-token&order=market_cap_desc&per_page=100&page=1&sparkline=false");
        
        request.add("path","0.circulating_supply");

        return sendChainlinkRequestTo(oracle, request, fee);
    }

    function fulfillUniPrice(bytes32 _requestId, uint256 _uniPrice) public recordChainlinkFulfillment(_requestId)
    {
        uniPrice = _uniPrice;
    }
    
    function fulfillUniSupply(bytes32 _requestId, uint256 _uniSupply) public recordChainlinkFulfillment(_requestId)
    {
        uniSupply = _uniSupply;
    }

    function withdrawLink() external {
        LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress());
        require(linkToken.transfer(msg.sender, linkToken.balanceOf(address(this))), "Unable to transfer");
    }
4

1 回答 1

0

不,没有,您必须提出多个请求。

目前,EthTX 核心适配器只能将不大于 32 字节的单个值写入区块链。如果需要多个值,则需要通过多次请求返回数据。

参考:Chainlink 文档常见问题解答

于 2021-06-08T18:32:58.933 回答