I want to Get Price in Soldity from External api , I Using the Chainlink For this :
// 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
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"
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
.
What's the Problem? how can I Get the Current Price?
Kovan Testnet Oracle:0x58bbdbfb6fca3129b91f0dbe372098123b38b5e9 URL:https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=IRR&to_currency=USD&apikey=K41HVINGOVEW3HHR
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);
since it could help you to change the format of the response