contract GetData is ChainlinkClient { uint256 public currentPrice; address public owner; // The address of an oracle address ORACLE = 0x83F00b902cbf06E316C95F51cbEeD9D2572a349a; // The address of the http get job string constant JOB = "c179a8180e034cf5a341488406c32827"; // When you call a job, you have to make a payment in LINK token // So be sure to send this contract's address some LINK uint256 constant private ORACLE_PAYMENT = 1 * LINK;
// The constructor constructor() public { setPublicChainlinkToken(); owner = msg.sender; }
// This is where the magic happens // And it will be the big orange button on the side of remix function requestEthereumPrice() public onlyOwner { // We build the API call to send to the node // noting that the result will come back through the `fulfill` method Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(JOB), address(this), this.fulfill.selector); // Here is where we enter the URL req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD"); // The response is in JSON, and the value is in the USD keyword, so we add this path req.add("path", "USD"); // Solidity can't handle decimals, so we have to multiply by 100 to get a whole number req.addInt("times", 100); // Then we send the request! sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT); }
// When the URL finishes, the response is routed to this function function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) { currentPrice = _price; } // Don't worry about this for now modifier onlyOwner() { require(msg.sender == owner); _; } // A helper funciton to make the string a bytes32 function stringToBytes32(string memory source) private pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); if (tempEmptyStringTest.length == 0) { return 0x0; } assembly { // solhint-disable-line no-inline-assembly result := mload(add(source, 32)) } } // Allows the owner to withdraw their LINK on this contract function withdrawLink() external onlyOwner() { LinkTokenInterface _link = LinkTokenInterface(chainlinkTokenAddress()); require(_link.transfer(msg.sender, _link.balanceOf(address(this))), "Unable to transfer"); } }
// MyContract inherits the ChainlinkClient contract to gain the // functionality of creating Chainlink requests contract ChainlinkExample is ChainlinkClient { // Stores the answer from the Chainlink oracle uint256 public currentPrice; // This is where you'll save each response from the // nodes you've chosen in a list, and then you can // take the median of those nodes uint256 public index; uint256[3] public currentPriceList; address public owner; // An "Oracle" you'll learn about soon :) // You'll have to add a little more LINK in due to this one address ORACLE1 = 0x11db7845a757041F5Dad3fAf81d70ebAd394e9A2; bytes32 constant JOBID1 = 0xcb08d1dbcc1b0621b4e8d4aea6bafc79f456e12332c782b4258c7e83bcedb74c; // LinkPool yessir! address ORACLE2 = 0x83F00b902cbf06E316C95F51cbEeD9D2572a349a; bytes32 JOBID2 = stringToBytes32("c179a8180e034cf5a341488406c32827"); // Alpha Vantage WOOO! address ORACLE3 = 0xB36d3709e22F7c708348E225b20b13eA546E6D9c; bytes32 JOBID3 = stringToBytes32("b1440eefbbff416c8b99062a128ca075"); uint256 constant private ORACLE_PAYMENT = 1 * LINK;
constructor() public { // Set the address for the LINK token for the network setPublicChainlinkToken(); owner = msg.sender; currentPriceList[0] = 0; currentPriceList[1] = 0; currentPriceList[2] = 0; }
// Creates a Chainlink request with the uint256 multiplier job function requestEthereumPrice(address _address, bytes32 _jobID) public onlyOwner { // newRequest takes a JobID, a callback address, and callback function as input Chainlink.Request memory req = buildChainlinkRequest(_jobID, address(this), this.fulfill.selector); // Adds a URL with the key "get" to the request parameters req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD"); // Uses input param (dot-delimited string) as the "path" in the request parameters req.add("path", "USD"); // Adds an integer with the key "times" to the request parameters req.addInt("times", 100); // Sends the request with the amount of payment specified to the oracle sendChainlinkRequestTo(_address, req, ORACLE_PAYMENT); } // fulfill receives a uint256 data type function fulfill(bytes32 _requestId, uint256 _price) public // Use recordChainlinkFulfillment to ensure only the requesting oracle can fulfill recordChainlinkFulfillment(_requestId) { // This is where the magic happens // Once we have all 3 responses, we can calculate the median value! currentPriceList[index] = _price; // This ensures the array never goes past 3, we just keep rotating responses index = (index + 1) % 3; currentPrice = median(); }
function multipleData() public{ requestEthereumPrice(ORACLE1, JOBID1); requestEthereumPrice(ORACLE2, JOBID2); requestEthereumPrice(ORACLE3, JOBID3); } // cancelRequest allows the owner to cancel an unfulfilled request function cancelRequest( bytes32 _requestId, uint256 _payment, bytes4 _callbackFunctionId, uint256 _expiration ) public onlyOwner { cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration); } // withdrawLink allows the owner to withdraw any extra LINK on the contract function withdrawLink() public onlyOwner { LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress()); require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer"); } modifier onlyOwner() { require(msg.sender == owner); _; } // helper function function stringToBytes32(string memory source) private pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); if (tempEmptyStringTest.length == 0) { return 0x0; } assembly { // solhint-disable-line no-inline-assembly result := mload(add(source, 32)) } } // Our sort of lame approach to getting the median function median() public view returns(uint256){ if (currentPriceList[0] > currentPriceList[1]){ if(currentPriceList[0] > currentPriceList[2]){ if(currentPriceList[1] > currentPriceList[2]){ return currentPriceList[1]; } else {return currentPriceList[2];} } else {return currentPriceList[0];} } else if (currentPriceList[1] > currentPriceList[2]){ return currentPriceList[2]; } return currentPriceList[1]; } // Allows the owner to withdraw their LINK on this contract function withdrawLink() external onlyOwner() { LinkTokenInterface _link = LinkTokenInterface(chainlinkTokenAddress()); require(_link.transfer(msg.sender, _link.balanceOf(address(this))), "Unable to transfer"); } }
// MyContract inherits the ChainlinkClient contract to gain the // functionality of creating Chainlink requests contract ChainlinkExample is ChainlinkClient { // Stores the answer from the Chainlink oracle uint256 public currentPrice; address public owner; address preCoordinatorOracleAddress = 0x11db7845a757041F5Dad3fAf81d70ebAd394e9A2; bytes32 constant private Service_Agreement_ID = 0xcb08d1dbcc1b0621b4e8d4aea6bafc79f456e12332c782b4258c7e83bcedb74c; uint256 constant private ORACLE_PAYMENT = 5 * LINK;
constructor() public { // Set the address for the LINK token for the network setPublicChainlinkToken(); owner = msg.sender; }
// Creates a Chainlink request with the uint256 multiplier job function requestEthereumPrice(address _address, bytes32 _jobID) public onlyOwner { // newRequest takes a JobID, a callback address, and callback function as input Chainlink.Request memory req = buildChainlinkRequest(_jobID, address(this), this.fulfill.selector); // Adds a URL with the key "get" to the request parameters req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD"); // Uses input param (dot-delimited string) as the "path" in the request parameters req.add("path", "USD"); // Adds an integer with the key "times" to the request parameters req.addInt("times", 100); // Sends the request with the amount of payment specified to the oracle sendChainlinkRequestTo(_address, req, ORACLE_PAYMENT); } // fulfill receives a uint256 data type function fulfill(bytes32 _requestId, uint256 _price) public // Use recordChainlinkFulfillment to ensure only the requesting oracle can fulfill recordChainlinkFulfillment(_requestId) { currentPrice = _price; } //0x11db7845a757041F5Dad3fAf81d70ebAd394e9A2, 0xcb08d1dbcc1b0621b4e8d4aea6bafc79f456e12332c782b4258c7e83bcedb74c function getData() public{ requestEthereumPrice(0x11db7845a757041F5Dad3fAf81d70ebAd394e9A2, 0xcb08d1dbcc1b0621b4e8d4aea6bafc79f456e12332c782b4258c7e83bcedb74c); } // cancelRequest allows the owner to cancel an unfulfilled request function cancelRequest( bytes32 _requestId, uint256 _payment, bytes4 _callbackFunctionId, uint256 _expiration ) public onlyOwner { cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration); }
// withdrawLink allows the owner to withdraw any extra LINK on the contract function withdrawLink() public onlyOwner { LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress()); require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer"); } modifier onlyOwner() { require(msg.sender == owner); _; } function stringToBytes32(string memory source) private pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); if (tempEmptyStringTest.length == 0) { return 0x0; } assembly { // solhint-disable-line no-inline-assembly result := mload(add(source, 32)) } } }