-
Notifications
You must be signed in to change notification settings - Fork 1.7k
How to make a Chainlinked contract
Making your contract "Chainlinked" allows the contract to request data from the Chainlink network. Feel free to ask us on Gitter if you have any questions!
The required contracts can be added to your project by using the following command:
$ yarn add github:smartcontractkit/chainlink
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 {
...
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);
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)");
...
Additionally, you may create a request without a JobID by building the tasks in the contract. Take note of the newSpec
function used below:
function requestEthereumPrice(string _currency) public {
string[] memory tasks = new string[](4);
tasks[0] = "httpget";
tasks[1] = "jsonparse";
tasks[2] = "ethint256";
tasks[3] = "ethtx";
ChainlinkLib.Spec memory spec = newSpec(tasks, this, "fulfill(bytes32,uint256)");
spec.add("url", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,JPY");
string[] memory path = new string[](1);
path[0] = _currency;
spec.addStringArray("path", path);
chainlinkRequest(spec, LINK(1));
}
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);
...
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.
In order for a contract to make use of the Ropsten network, you'll need to have the address of the deployed Ropsten LINK token, and our oracle contract. The Ropsten LINK token's address is "0x20fE562d797A42Dcb3399062AE9546cd06f63280"
and the oracle contract's address is "0x5be84B6381d45579Ed04A887B8473F76699E0389"
.
You will also need the JobID
as bytes when invoking a new run. We have pre-made jobs available for you. Types with "x100" indicate that the value received from the given endpoint will be multiplied by 100 before being written to the blockchain. The string value of the JobIDs will need to be given to the Run as bytes.
Type | Job ID | Required Params |
---|---|---|
Uint256 | 5eadba6f077e4ca8ac88cc370eb66a5f |
url (string),path (array of strings) |
Uint256x100 | d41a0bdf968a43aca8822cf81a2c1fa7 |
url (string),path (array of strings) |
Bytes32 | 2660763707434f35bb58a9db33ee1803 |
url (string),path (array of strings) |
Int256 | cdda8f68cb3e4108a83977c9c600d3a7 |
url (string),path (array of strings) |
Int256x100 | 9a888d17f766458d99dfea62588c8280 |
url (string),path (array of strings) |