Use a Chainlink Oracle in your smart contract

Phemex Trading

The Chainlink ecosystem system provides an API which allows you to connect your smart contract to a suite of decentralized network Oracles. These Oracles provide all types of data that you can integrate into your contract. For example you can connect your smart contract to weather data, stock prices, shipping information, sports information, the list goes on. This is how it works:

  • Data providers sell their data in the Chainlink environment. This is a great way for a trusted data provider to make additional revenue.
  • Node operators provide Oracle computational power and infrastructure to service smart contract block chains. Node operators earn revenue for providing this service.
  • Developers integrate Oracles into their contract code. When an Oracle is used the contract is charged a fee in Link tokens.

Sample Contract with Chainlink Oracle

Below is a simple Solidity smart contract that gets the price of ETH from a trusted Oracle. Read the comments in the code to determine how it works.

Start off by funding your wallet.

  • Kovan ETH faucet: https://faucet.kovan.network/
  • Kovan LINK faucet: https://kovan.chain.link/
  1. Copy the code below in Remix and deploy to the test network.

pragma solidity ^0.6.0;

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


//contracts are like classes
//this Chainlink example inherits from ChainlinkClient

contract ChainlinkExample is ChainlinkClient {
    
    //define state variables stored on the block chain
    uint256 public currentPrice;
    address public owner;
    address public Oracle;
    bytes32 public jobId;
    uint256 public fee; 
    
    
    //constructor is run at the time of contract creating
    constructor() public {
        setPublicChainlinkToken();
        owner = msg.sender;
        Oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
        jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }
    
    //function below creates a Chainlink API request to get a price
    //only the owner of the contract can call this function
    function requestPrice() public onlyOwner returns (bytes32 requestId)
    {
        //create a variable and store it temporarily in memory
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        //set the url to perform the GET request
        request.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
        //set the path to find the requred data in the api response
        request.add("path", "USD");
        //multiply the results by 100 to remove decimals
        request.addInt("times", 100);
        //send the request
        return sendChainlinkRequestTo(Oracle, request, fee);
    }
    
    function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) 
    {
        currentPrice = _price;
    }
    
    modifier onlyOwner() {
        require(msg.sender ==owner);
        _;
    }
    
}

Try it in Remix

2. Use Injected Web3.

3. Deploy the contract an you will see your executed contract at the bottom of Remix.

metamask smart contract solidity Use a Chainlink Oracle in your smart contract

4. Before you can interact with your contract you need to fund your contract with Link tokens. To send it tokens copy the contract address, and send it Link tokens using MetaMask.

metamask smart contract solidity

5. Once your contract has Link tokens you can use the pricing functions and retrieve the price of ETH in the test environment. If you try to use the requestPrice function when you do not have Link tokens in your contract you will see an error on the screen.

metamask smart contract solidity

Visit the Chain Link Market place to see all it has to offer. https://market.link/?network=1

If you need help writing a solidity smart contract click here.

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Click here for more information about how to use the Ethereum test network and how to obtain test ETH.

Next Review – Chainlink price feeds on Binance Smart Chain

Leave a Reply