Loading...
Until the ChainLink was available we could not get any external data in smart contracts. Since smart contracts are immutable and hackproof it is very good if we can get any data from anywhere in smart contract directly. we will see hot to use chainlink to do this.
Head over to remix IDE and create new solidity file. the complete code for this tuto will be :
pragma solidity ^0.6.0;
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";
contract GetAPI is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public volume;
address private oracle;
bytes32 private jobId;
uint256 private fee;
constructor() public {
setPublicChainlinkToken();
oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
fee = 0.1 * 10 ** 18; // (Varies by network and job)
}
function requestVolumeData() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
request.add("get", "https://reqres.in/api/products/3");
request.add("path", "data.id");
int timesAmount = 1;
request.addInt("times", timesAmount);
return sendChainlinkRequestTo(oracle, request, fee);
}
function fulfill(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId)
{
volume = _volume;
}
}
pragma solidity ^0.6.0;
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";
define solidity version
import chainlink client contract
contract GetAPI is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public volume;
address private oracle;
bytes32 private jobId;
uint256 private fee;
constructor() public {
setPublicChainlinkToken();
oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
fee = 0.1 * 10 ** 18; // (Varies by network and job)
}
this is chainlink node operator oracle address , this will be varied as per the networks .eg for mainnet, kovan testnet, rinkeby testnet you will have to choose proper oracle address .
this is varied according to the adapters , suppose we want the return variable as uint256 then this address will do but if you want to read string as return variable jobId will be different.
here is the link for chainlink official docs , you can get your required oracle and jobId from here
function requestVolumeData() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
request.add("get", "https://reqres.in/api/products/3");
request.add("path", "data.id");
int timesAmount = 1;
request.addInt("times", timesAmount);
return sendChainlinkRequestTo(oracle, request, fee);
}
function fulfill(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId)
{
volume = _volume;
}
this is the function we will be most concern of,
request.add("get", "API ADDRESS");
here you will replace the url you want to read the data from
the above url provides random json object. you can directly open it in your browser and check the output it will be like this :
{
"data":{
"id":3,
"name":"true red",
"year":2002,
"color":"#BF1932",
"pantone_value":"19-1664"
},
"support":{
"url":"https://reqres.in/#support-heading",
"text":"To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
Now we will read only uint in this smartbook (other varaible require some different code we will discuss it in next part). so for "id", the path will be data.id right.
so in request.add we provide the path of our desired variable.
timeamount and request.addInt are optional, if you are getting result in decimal supppose 12.47855 then you can multiply it with proper decimal and get uint without error. thats it,
After this much thing you neet to deploy the contract, after compiling for deployment you will get many contract options to deploy , choose the proper one and deploy. after that you will need to fund it with LINK tokens.
there are many faucets available , fund you smart contarct according to the testnet you are using.
and deploy contract
now execute the function requestVolumeData and it will set the variable volume to the output result, this volume variable is defined at the very begining after we defined the contract.
call volume and this is the desired output we want.
In next part we will learn how to read string variables from api.
..........