Skip to content

How to make a Chainlinked contract

Thomas edited this page Aug 3, 2018 · 42 revisions

Creating Chainlinked Contracts

Developers wanting to add external data to their contracts will need to import the Chainlinked contract into their project. The Chainlinked contract contains helper methods in order to simplify the process of making a Chainlink request on-chain for external data.

If you have any questions, feel free to ask us on Gitter!

Adding Chainlink to your project

The required contracts can be added to your project by using the following command:

$ yarn add github:smartcontractkit/chainlink

Importing Chainlinked

Once you've imported the Chainlinked.sol contract, your contract can inherit the Chainlinked behavior:

import "chainlink/solidity/contracts/Chainlinked.sol";

contract MyContract is Chainlinked {
...

Requesting data

Here is an example of a Chainlink request, called a run:

  function requestEthereumPrice(string _currency) public onlyOwner {
    ChainlinkLib.Run memory run = newRun(jobId, this, "requestedDataCallback(bytes32,uint256)");
    run.add("url", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY");
    string[] memory path = new string[](1);
    path[0] = _currency;
    run.addStringArray("path", path);
    run.addInt("times", 100);
    requestId = chainlinkRequest(run, LINK(1));
  }

You give the JobID in the constructor of your contract or you can supply it as a parameter for your requesting function.

function requestEthereumPrice(string _currency, bytes32 jobId) public onlyOwner {
  ChainlinkLib.Run memory run = newRun(jobId, this, "requestedDataCallback(bytes32,uint256)");
  ...

Cancelling an unfilfilled request

You may want the functionality to cancel a request, this is implemented through the deployed oracle contract:

  function cancelRequest(uint256 _requestId) 
    public
    ifAgreed(_requestId)
  {
    oracle.cancel(_requestId);
    ...

Receiving data

In the example above, requestedDataCallback is the callback method supplied in requestEthereumPrice.

  function requestedDataCallback(bytes32 _requestId, uint256 _price)
    public
    onlyOracle
    checkRequestId(_requestId)
  {
    emit RequestFulfilled(_requestId, _price);
    currentPrice = _price;
  }

  modifier checkRequestId(bytes32 _requestId) {
    require(requestId == _requestId);
    _;
  }

You can view additional examples of how to create Chainlinked contracts here.

Deploying to the blockchain

In order for a contract to make use of the Chainlink network, you'll need to have the address of the LINK token contract and our oracle contract.

You will also need the JobID as bytes when invoking a new run. We have pre-made jobs available for you. Types with a Multiplier indicate that the value received from the given endpoint will be multiplied by the specified "times" value before being written to the blockchain. The string value of the JobIDs will need to be given to the Run as bytes.

Ropsten

LINK token contract address: "0x20fE562d797A42Dcb3399062AE9546cd06f63280"

Oracle contract address: "0xA39A4cC64C91267EC1376f6861aAC0622352de8F"

Rinkeby

LINK token contract address: "0x01BE23585060835E02B77ef475b0Cc51aA1e0709"

Oracle contract address: "0x32f5009789310dF4F971bf211E15a723269709C3"

Job Specs

Type Job ID Required Params
Bytes32 Ropsten: 0de7b99e32564e1cadf77ec5914f1734
Rinkeby: 7f075add62e5455bb527025930ceb4f5
url(string),
path(array of strings)
Int256 Ropsten: 45f24e7f6da44657aaac1b92ab86cc82
Rinkeby: c8086213e8b9402c995fe37ee0f73043
url(string),
path(array of strings)
Int256 Multiplier Ropsten: aa07b77beabf42e596bd0f45a10aa1c7
Rinkeby: 80f21e24cdfb4e64be00a5408e316043
url(string),
path(array of strings),
times(int)
Uint256 Ropsten: e584d6df5f7046b388009e47a0752ba6
Rinkeby: cbbbd1d623c249299773198d6629b81a
url(string),
path(array of strings)
Uint256 Multiplier Ropsten: d4a0d580aba44bc09e43a7601025d793
Rinkeby: c3ac52b391df47ecab7bf9d4e37d8055
url(string),
path(array of strings),
times(int)