简体   繁体   English

使用 chainlink 从外部 api 获取十进制数

[英]get decimal number in solidity from external api with chainlink

I want to Get Price in Soldity from External api, I Using the Chainlink For this:我想从外部 api 获取 Soldity 价格,我为此使用 Chainlink:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/ChainlinkClient.sol";

contract Fiat is ChainlinkClient {
    
    using Chainlink for Chainlink.Request;

    uint256 public price;
    
    bytes32 private jobId;
    uint256 private fee;
    
    constructor() {
        setPublicChainlinkToken();
        jobId = "83ba9ddc927946198fbd0bf1bd8a8c25";
        fee = 0.1 * 10 ** 18; // (Varies by network and job)
    }
    
    /**
     * Create a Chainlink request to retrieve API response, find the target price
     * data, then multiply by 100 (to remove decimal places from price).
     */
    function findExhangeRateFiatToBaseFiat(string memory _url
     , address _oracle) public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        
        // Set the URL to perform the GET request on
        // NOTE: If this oracle gets more than 5 requests from this job at a time, it will not return. 
        request.add("get", _url);
        
        string[] memory path = new string[](2);
        path[0] = "Realtime Currency Exchange Rate";
        path[1] = "5. Exchange Rate";
        request.addStringArray("path", path);
        
        // Multiply the result by 10000000000 to remove decimals
        request.addInt("times", 10000000000);
        
        // Sends the request
        return sendChainlinkRequestTo(_oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of uint256
     */ 
    function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId)
    {
        price = _price;
    }

}

Now I Have a Problem:现在我有一个问题:

When I Send Request to This Api: https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=IRR&to_currency=USD&apikey=K41HVINGOVEW3HHR当我向这个 Api 发送请求时: https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=IRR&to_currency=USD&apikey=K41HVINGOVEW3HHR

it Show me This Result:它告诉我这个结果:

{
    "Realtime Currency Exchange Rate": {
        "1. From_Currency Code": "IRR",
        "2. From_Currency Name": "Iranian Rial",
        "3. To_Currency Code": "USD",
        "4. To_Currency Name": "United States Dollar",
        "5. Exchange Rate": "0.00002381",
        "6. Last Refreshed": "2022-02-09 11:45:08",
        "7. Time Zone": "UTC",
        "8. Bid Price": "0.00002381",
        "9. Ask Price": "0.00002381"
    }
}

and I want this Result: "5. Exchange Rate": "0.00002381"我想要这个结果: "5. Exchange Rate": "0.00002381"

but when I Call this Api and Call the price in Remix it shows me this Result 238100 Actually it should show me this result 0.00002381 .但是当我调用这个 Api 并调用 Remix 中的price时,它显示了这个结果238100实际上它应该显示这个结果0.00002381

What's the Problem?有什么问题? how can I Get the Current Price?我怎样才能得到当前价格?

Kovan Tes.net Oracle :0x58bbdbfb6fca3129b91f0dbe372098123b38b5e9 URL : https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=IRR&to_currency=USD&apikey=K41HVINGOVEW3HHR Kovan TES.NET Oracle :0x58BBDBDBFB6FCA3129B91F0DBE372098123B38B5E9 URL8883.WRENGY__8105635270888888888888888888888888BPHERTING = CROUNGENTING frounptry functery = Co = Co = Co = Co = Co = CO = CO = CO = CO = CO = cocey = CO = CO =

floats or doubles does not exist in solidity, so in order to convert it to a number it get rid of the decimals, i'm not really sure of how decimals are managed in this case but you could check that and then you can work in a similar way as you would work when dealing with eth, gwei, wei, also you should check how this line of code affects the response request.addInt("times", 10000000000); floats 或 double 不存在于 solidity 中,所以为了将它转换为数字,它去掉了小数点,我不太确定在这种情况下小数点是如何管理的,但你可以检查一下,然后你可以在与处理 eth、gwei、wei 时的工作方式类似,您还应该检查这行代码如何影响响应request.addInt("times", 10000000000); since it could help you to change the format of the response因为它可以帮助您更改响应的格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM