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!

Contract Examples

Job Specification IDs

Job Specifications defines the order of the tasks that the Chainlink node must complete for the request. The spec ID is supplied to the newRun method as a bytes32 parameter.

For example, the parameter SPEC_ID below:

ChainlinkLib.Run memory run = newRun(SPEC_ID, this, this.fulfill.selector);

Supplying the SPEC_ID can be accomplished by creating a constant in your contract:

bytes32 constant SPEC_ID = bytes32("3c2e8c3422f0417cb0e24f9a81ab2316");

function requestPrice() public {
  ChainlinkLib.Run memory run = newRun(SPEC_ID, this, this.fulfill.selector);
  ...

As a parameter of the method:

function requestPrice(bytes32 _specId) public {
  ChainlinkLib.Run memory run = newRun(_specId, this, this.fulfill.selector);
  ...

Or as a stored variable:

bytes32 public jobId = 0x3363326538633334323266303431376362306532346639613831616232333136;

function requestPrice() public {
  ChainlinkLib.Run memory run = newRun(jobId, this, this.fulfill.selector);
  ...

Notice the string "3c2e8c3422f0417cb0e24f9a81ab2316" has a value of 0x3363326538633334323266303431376362306532346639613831616232333136 when converted to bytes.

The chainlinkRequest Method

chainlinkRequest returns a bytes32 value that is used as the ID for your request (the first parameter of the callback: _requestId). This value is checked in the checkChainlinkFulfillment modifier (explained in detail below), but can also be used in your contract for various purposes.

The request ID can simply be used as the return value of your request method:

function requestPrice() public returns (bytes32) {
  ChainlinkLib.Run memory run = newRun(SPEC_ID, this, this.fulfill.selector);
  return chainlinkRequest(run, LINK(1));
}

The returned requestId can also be stored in a mapping

mapping(bytes32 => address) public receipts;

function requestPrice(address _to) public {
  ChainlinkLib.Run memory run = newRun(SPEC_ID, this, this.fulfill.selector);
  receipts[chainlinkRequest(run, LINK(1))] = _to;
}

Which allows it to be used in the callback method:

event PayTo(address indexed to, uint256 indexed amount);

function fulfill(bytes32 _requestId, uint256 _price)
  public
  checkChainlinkFulfillment(_requestId)
{
  emit PayTo(receipts[_requestId], _price);
}

The checkChainlinkFulfillment Modifier

The checkChainlinkFulfillment modifier should be added to all callback methods to ensure that the data entering the contract is valid. This includes ensuring that only the oracle contract you specified when creating your Chainlinked contract can call the method and that the same request ID hasn't previously been used.

After your implemented method body has completed, it will emit a log ChainlinkFulfilled with the topic: 0x7cc135e0cebb02c3480ae5d74d377283180a2601f8f644edf7987b009316c63a

The LINK Helper Method

The LINK(uint256) helper method allows you to easily specify whole values of the LINK token. Since token amounts are typically specified as their smallest unit (18 decimals for LINK), this helper method makes it easy to visualize the amount of LINK used for the request.

This:

chainlinkRequest(run, LINK(1));

Is the same as this:

chainlinkRequest(run, 1 ether);

Even though the word ether is present, in Solidity, it's just a conversion of 1 with 18 zeroes appended after it. You can read about Ether units here.

Additionally, you can specify smaller amounts of LINK by using the literal value represented to the 18th decimal place (like wei) or the other available suffixes:

chainlinkRequest(run, 4 szabo);
// or
chainlinkRequest(run, 4000000000000);
// are the same