Skip to content

Instantly share code, notes, and snippets.

@thodges-gh
Forked from PatrickAlphaC/chainlinkVRFDemo.sol
Last active May 12, 2020 20:19
Show Gist options
  • Save thodges-gh/8f4460c5a90f7d1b2e79cbf638b83ac4 to your computer and use it in GitHub Desktop.
Save thodges-gh/8f4460c5a90f7d1b2e79cbf638b83ac4 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.17+commit.d19bba13.js&optimize=false&gist=
pragma solidity ^0.6.0;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract GetData is ChainlinkClient {
uint256 public currentPrice;
address public owner;
constructor() public {
// Sets the address of the LINK token when deployed to a public blockchain
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(
address _oracle,
string calldata _jobId,
uint256 _payment
)
external
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(_jobId), address(this), this.fulfill.selector);
// The HttpGet adapter takes a param with the key "get" to specify 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
// for the JsonParse adapter to store the desired key
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 to the given oracle address with payment
sendChainlinkRequestTo(_oracle, req, _payment);
}
// When the URL finishes, the response is routed to this function
function fulfill(bytes32 _requestId, uint256 _price)
external
recordChainlinkFulfillment(_requestId) // Ensures only the oracle can call this function
{
currentPrice = _price;
}
// 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");
}
// 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))
}
}
// Ensures only the owner can call a function
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
pragma solidity ^0.4.24;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/interfaces/AggregatorInterface.sol";
contract ReferenceFeed {
AggregatorInterface internal ref;
constructor(address _aggregator) public {
ref = AggregatorInterface(_aggregator);
}
function getLatestPrice() public view returns (int256) {
return ref.latestAnswer();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment