Chainlink

Smart contracts

pragma solidity >=0.4.0 <0.6.0;
contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public view returns (uint) {
        return storedData;
    }
}

Interact with external world

  • Get external data as input (Oracle: external data source)
  • Trigger external events


Chainlink - blockchain middleware

Example: Get ETH price (in USD)



Traditionally (javascript)
const request = require('request');
request('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD', function (error, response, body) {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // 200
  console.log('body:', body); // {"USD":266.86}
});

Why cannot we just make API calls in smart contracts like above?

Problem 1: Smart contracts cannot make API calls

  • Deterministic
  • The call my node makes should be verifiable by other nodes to reach consensus.
  • Say I make an API call like above and get a result; there’s no way for other nodes to verify my call; they makes the same API call, but may get different results or error

How to interact with smart contracts
  • Smart contracts can call functions of other smart contracts or even deploy new contracts
  • Smart contracts can emit events and write those events to logs. You can read the logs to observe the events 
  • You (off-chain nodes) can make transactions/calls to smart contracts to execute contract functions

Solution
  • Instead of making an API call, smart contracts emit the API request as an event together with a callback function