Chainlink Developers

Welcome to the Chainlink documentation site. You'll find comprehensive guides and documentation to help you start working with Chainlink as quickly as possible, as well as support if you get stuck. Click here for an introductory walkthrough on how to create a Chainlink request on the Ropsten test network!

Request data using Chainlink

Chainlink will provide your smart contract with access to any external API you want to connect your smart contract with. You can begin by trying a Chainlink out on an Ethereum testnet.

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.

Requirements

  • Your contract will need to be funded with LINK in order to create a request. To obtain testnet LINK, visit the faucets below:
  • You will need a Job Spec ID that's associated with the type of data you want. View the Addresses and Job Specs for information specific to your network.

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

Add Chainlink to your project

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

With NPM:

$ npm install github:smartcontractkit/chainlink --save

With Yarn:

$ yarn add github:smartcontractkit/chainlink

Create your Chainlinked contract

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

pragma solidity ^0.4.24;

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

contract MyContract is Chainlinked {
  constructor() public {
    setLinkToken(0x20fE562d797A42Dcb3399062AE9546cd06f63280);
    setOracle(0x51DE85B0cD5B3684865ECfEedfBAF12777cd0Ff8);
  }
  // Additional functions here:
  
}
pragma solidity ^0.4.24;

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

contract MyContract is Chainlinked {
  constructor() public {
    setLinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709);
    setOracle(0xa8DC9e5D99DF8790D700C885e5124573fA1720a3);
  }
  // Additional functions here:
  
}
pragma solidity ^0.4.24;

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

contract MyContract is Chainlinked {
  constructor() public {
    setLinkToken(0xa36085F69e2889c224210F603D836748e7dC0088);
    setOracle(0x4bd72fA5Bb512c1E1f5B65E6E09509C93aC01b45);
  }
  // Additional functions here:
  
}

Requesting data

When a request is created on-chain from your smart contract for the available job specs (helpers below), you will need to specify parameters about the data you want Chainlink to retrieve. For details on which parameters to specify, see our Adapters page.

bytes32 constant GET_BYTES32_JOB = bytes32("3b41e94eb2bf4d05987addf45b2c6932");
bytes32 constant POST_BYTES32_JOB = bytes32("fe1786c160024298a87200617fe78fab");
bytes32 constant INT256_JOB = bytes32("92c5bb9294af41d2a59fb3d1709c7be3");
bytes32 constant INT256_MUL_JOB = bytes32("18f98fe54d7f4866aca37f008c0a665e");
bytes32 constant UINT256_JOB = bytes32("438fe46facb8488b831143f6c011d389");
bytes32 constant UINT256_MUL_JOB = bytes32("587948181f6248dbbb8599f949f977c6");
bytes32 constant GET_BYTES32_JOB = bytes32("ccc41cccdce8492398ac2d5558debf55");
bytes32 constant POST_BYTES32_JOB = bytes32("954572881f024f89bc230e572dc5d8ea");
bytes32 constant INT256_JOB = bytes32("796add02d7b44142b42e675e21ab0ad0");
bytes32 constant INT256_MUL_JOB = bytes32("913f3f4e8aca42498d6741dfe9cb8cf2");
bytes32 constant UINT256_JOB = bytes32("82d69822e1094857b8416053d0b032bb");
bytes32 constant UINT256_MUL_JOB = bytes32("73fda4e2b40d48f3b22d4f839cd5691b");
bytes32 constant GET_BYTES32_JOB = bytes32("04134837cdb346338e818771224083a7");
bytes32 constant POST_BYTES32_JOB = bytes32("beb8b0e29ca44120930bce6bad6e227b");
bytes32 constant INT256_JOB = bytes32("86dd1ce9c8ec4129934a283639fdc567");
bytes32 constant INT256_MUL_JOB = bytes32("21643e1c73474eb180eb6699c7c9a202");
bytes32 constant UINT256_JOB = bytes32("061aed65dc05456b86f868cde4b0164a");
bytes32 constant UINT256_MUL_JOB = bytes32("31b8de0942884aaaab295569c0b2ec31");

Specifying parameters is accomplished by using methods within ChainlinkLib.Run which takes a key (as a string) and value. We have the following method available for different value types:

  • add: Adds a string
  • addBytes: Adds bytes
  • addInt: Adds a int256
  • addUint: Adds a uint256
  • addStringArray: Adds an array of strings

In the examples below, we specify a "get" key for the HttpGet adapter, a "path" key for the JsonParse adapter, and a "times" key for the Multiply adapter.

Note: we use 1 ether below as a short-hand way of getting the value 1000000000000000000. It's important to know that 1 ether is not being sent, but 1 LINK.

function requestEthereumLastMarket() public returns (bytes32 requestId) {
  ChainlinkLib.Run memory run = newRun(GET_BYTES32_JOB, this, this.fulfillEthereumLastMarket.selector);
  run.add("get", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
  run.add("path", "RAW.ETH.USD.LASTMARKET");
  requestId = chainlinkRequest(run, 1 ether);
  }
function requestEthereumChange(string _currency) public returns (bytes32 requestId) {
  ChainlinkLib.Run memory run = newRun(INT256_MUL_JOB, this, this.fulfillEthereumChange.selector);
  run.add("get", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD,EUR,JPY");
  string[] memory path = new string[](4);
  path[0] = "RAW";
  path[1] = "ETH";
  path[2] = _currency;
  path[3] = "CHANGEPCTDAY";
  run.addStringArray("path", path);
  run.addInt("times", 1000000000);
  requestId = chainlinkRequest(run, 1 ether);
}
function requestEthereumPrice(string _currency) public returns (bytes32 requestId) {
  ChainlinkLib.Run memory run = newRun(UINT256_MUL_JOB, this, this.fulfillEthereumPrice.selector);
  run.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY");
  run.add("path", _currency);
  run.addInt("times", 100);
  requestId = chainlinkRequest(run, 1 ether);
}

Receiving data

The request examples above each specify a callback function to be triggered when the Chainlink network responds with an answer.

Below are examples of how your contract can receive the data from the Chainlink network:

bytes32 public market;

function fulfillEthereumLastMarket(bytes32 _requestId, bytes32 _market)
  public checkChainlinkFulfillment(_requestId)
{
  market = _market;
}
int256 public change;

function fulfillEthereumChange(bytes32 _requestId, int256 _change)
  public checkChainlinkFulfillment(_requestId)
{
  change = _change;
}
uint256 public price;

function fulfillEthereumPrice(bytes32 _requestId, uint256 _price)
  public checkChainlinkFulfillment(_requestId)
{
  price = _price;
}

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

With your Chainlinked contract written, you're now ready to deploy!