ChainLink Oracle Remix Introduction

Cybergen
5 min readFeb 12, 2021

Hi there ! šŸ˜ƒ Today we will introduce the Oracle technology and see how we can call for information from outside the blockchain in our contract.

However, in order to start slowly we will here only focus on the Solidity part and build our smart contract in the online Remix IDE allowing us to test it at the same time.

āš ļøāš ļø Important note āš ļøāš ļø

You will need to use Google Chrome for this tutorial and have Metamask install on it. If youā€™re not fully set up you can download Chrome here and find a tutorial on how to set up Metamask here.

Alright so now letā€™s get down to it. First we need to go in the remix IDE

Once youā€™re there click on new file, name your file ā€œOracle.solā€ and click on OK to create your new file.

Once weā€™re there, weā€™re set to start writing the code for our Oracle. Here we will not do anything fancy, the idea being to simply use the aggregator interface provided by ChainLink in order to obtain the live price of ETH in USD. In this sense we will first import the aggregator interface of Chainlink in our contract and then create our AskPrice contract as follows :

notes on the code

  • We declare our AggregatorInterface as internal in order to prevent any call from outside the contract or any derivative contracts
  • The use of the Aggregator Interface here simplify the code but given the inner workings of the latestAnswer( ) function we need to create an event for our Oracle in order to be able to make our price request to the Chainlink Oracle.

Once you have copy the code in your remix file, choose the right Solidity compiler version here 0.6.7 and click on compile on the left panel of remix.

Tip : If the compilation is successfull youā€™ll see a green validation sign as above. If itā€™s not the case, Remix will tell you what lines are incorrect.

When the compilation is done, click on the deploy & run transactions icon just below the compiler choose Injected Web3 in Environment and click on deploy to migrate your contract onto the Ropsten test network.

āš ļøāš ļøImportant note āš ļøāš ļø

Make sure that your Metamask wallet is open and that you have some test ETH for the Ropsten test network. If you donā€™t have any ETH go over to this faucet and copy the public address of your metamask wallet in order to receive some ETH and be able to pay the gas fees for our Oracle.sol contract migration onto the network.

Your Metamask wallet will ask you to authorize it the transaction, click on confirm and then after a bit of time your contract should be deployed and you should be able to see its address on the Ropsten network as below :

Now before doing anything, due to the way the Chainlink Oracle is functionning we need to load our contract with testnet Link in order to be able to call our Oracle and get the current price of ETH in USD.

In order to do so, add the testnet Link token onto your MetaMask Wallet by going into add token, choosing personalized token and pasting this address :

0x20fE562d797A42Dcb3399062AE9546cd06f63280

Once you have this new token in your wallet go here to get free link from the Chainlink faucet.

Alright, so now that we have at tour disposal letā€™s load our contract with testnet Link. To do so, go back into remix, copy the adress of your contract and send 10 testnet Link to it with the help your Metamask wallet.

Once the transaction is done go back to Remix and click on the getLatestPrice. Here given the asynchronous nature of the process we have to wait for the response from the Oracle to come back to be able to see the price.

=> With the Chainlink Oracle you need to wait for 3 confirmations of the transactions. To see the state of the transaction verification you can click on the url linked popping up in the console.

Once you have 3 transactions, click on the priceETH button and you should see appear the price of ETH with a lot of zero behind, it is because ethereum doesnā€™t support the commas so we will always have our ETH price multiply by a factor 10 ^n depending on the parameters of the Oracle.

And thatā€™s it ! We successfully built our very first smart contract using an Oracle šŸ„³šŸ„³šŸ„³

So as always donā€™t hesitate to play around with it and ask different oracles for different information, youā€™ll find the list for all the Chainlink Oracle on the Ropsten network here.

Full code for this tutorial can be found here.

Hope you enjoyed this introduction to the Oracle, and stay tuned for the next tutorial were we will build a full fledged application with a user interface asking using an Oracle.

See you soon āœŒļø

--

--

Cybergen

If you want to read more donā€™t hesitate to go check out our website https://cybergen.jimdofree.com/