Created
May 10, 2020 20:07
-
-
Save PatrickAlphaC/93ac60b85ba27e6d86fc8300ec4cbc82 to your computer and use it in GitHub Desktop.
Get data using the precoordinator tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.0; | |
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol"; | |
// 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)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment