Skip to content

How to make a Chainlinked contract

Thomas edited this page Aug 30, 2018 · 42 revisions

This information is out of date! Please refer to our new documentation site, here.

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

Start off by adding Chainlink to your project, which is hosted on GitHub.

$ yarn add github:smartcontractkit/chainlink

Connect your smart contract to the Chainlink Network

Import Chainlinked.sol into your contract so you can inherit the Chainlinked behavior.

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

contract MyContract is Chainlinked {
  constructor()
    public
  {
    setLinkToken(0x20fE562d797A42Dcb3399062AE9546cd06f63280);
    setOracle(0xB68145133973411b7B3F2726A625FE3f3808240D);
    ...

Or if using Remix, simply use the Github URL as your import path.

import "github.com/smartcontractkit/chainlink/solidity/contracts/Chainlinked.sol";

contract MyContract is Chainlinked {
  constructor()
    public
  {
    setLinkToken(0x20fE562d797A42Dcb3399062AE9546cd06f63280);
    setOracle(0xB68145133973411b7B3F2726A625FE3f3808240D);
    ...

Requesting data

Here is an example of a Chainlink request from Solidity, 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));
}

For details on which parameters to specify, see our adapters page.

Receiving data

In the example above requestedDataCallback is the callback method specified in the requestEthereumPrice method.

Be sure to add the checkChainlinkFulfillment modifier to ensure that only Chainlink can call this method. This modifier is included with the Chainlinked contract.

function requestedDataCallback(bytes32 _requestId, uint256 _reportedPrice)
  public checkChainlinkFulfillment(_requestId)
{
  calculatePayout(_reportedPrice); // run your calculation with the reported data
  ...

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 address: "0x20fE562d797A42Dcb3399062AE9546cd06f63280"

Oracle address: "0xB68145133973411b7B3F2726A625FE3f3808240D"

Rinkeby

LINK token address: "0x01BE23585060835E02B77ef475b0Cc51aA1e0709"

Oracle address: "0x1b12694E8651389c5BB3C094E8fb7e5609b663E7"

Job Specs

Type Job ID Required Params
Bytes32 Ropsten: ae18a03a2f5746ca967c403cf53e1318
Rinkeby: 9152e6787fcb417abeb848369a6b206a
url(string),
path(array of strings)
Int256 Ropsten: 6094e28f1b0a45fcbdcbb0e37646b776
Rinkeby: 6da41cedf4de4b24b0b49e34d1f32371
url(string),
path(array of strings)
Int256 Multiplier Ropsten: 69cf308f8cee429c88eb25644d9f1c1d
Rinkeby: 2c95c513b0cb4c72b085740459d1951e
url(string),
path(array of strings),
times(int)
Uint256 Ropsten: e032d814b62a48779d47d135c13ebd7e
Rinkeby: 04ddb59dfe004f37b1ef4c8391bd957e
url(string),
path(array of strings)
Uint256 Multiplier Ropsten: 8b05126a278f4f0abe02dd482aa802f8
Rinkeby: b4eaae96efc041d78458960456872047
url(string),
path(array of strings),
times(int)