Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Last active July 15, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PatrickAlphaC/7cae2cc64026ea69073ee76a32dd0268 to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/7cae2cc64026ea69073ee76a32dd0268 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.6.2+commit.d19bba13.js&optimize=false&gist=7cae2cc64026ea69073ee76a32dd0268
pragma solidity 0.6.6;
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/7a4e19a8ff07db1be0b397465d38d175bc0bb5b5/evm-contracts/src/v0.6/VRFConsumerBase.sol";
contract VRFTestnetD20 is VRFConsumerBase {
uint256[] public d20Results;
bytes32 internal keyHash;
uint256 internal fee;
/**
* @notice Constructor inherits VRFConsumerBase
* @dev Ropsten deployment params:
* @dev _vrfCoordinator: 0xf720CF1B963e0e7bE9F58fd471EFa67e7bF00cfb
* @dev _link: 0x20fE562d797A42Dcb3399062AE9546cd06f63280
*
* @dev After deploying your contract, remember to fill it with Testnet LINK
* @dev Ropsten faucet: https://ropsten.chain.link/
*/
constructor(address _vrfCoordinator, address _link)
VRFConsumerBase(_vrfCoordinator, _link) public
{
vrfCoordinator = _vrfCoordinator;
LINK = LinkTokenInterface(_link);
keyHash = 0xced103054e349b8dfb51352f0f8fa9b5d20dde3d06f9f43cb2b85bc64b238205;
fee = 0.1 * 10 ** 18; // 0.1 LINK
}
/**
* @notice Requests randomness from a user-provided seed
* @dev This is only an example implementation and not necessarily suitable for mainnet.
* @dev You must review your implementation details with extreme care.
*/
function rollDice(uint256 userProvidedSeed) public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) > fee, "Not enough LINK - fill contract with faucet");
bytes32 _requestId = requestRandomness(keyHash, fee, userProvidedSeed);
return _requestId;
}
/**
* @notice Modifier to only allow updates by the VRFCoordinator contract
*/
modifier onlyVRFCoordinator {
require(msg.sender == vrfCoordinator, 'Fulfillment only allowed by VRFCoordinator');
_;
}
/**
* @notice Callback function used by VRF Coordinator
* @dev Important! Add a modifier to only allow this function to be called by the VRFCoordinator
* @dev This is where you do something with randomness!
* @dev The VRF Coordinator will only send this function verified responses.
*/
function fulfillRandomness(bytes32 requestId, uint256 randomness) external override onlyVRFCoordinator {
uint256 d20Result = randomness.mod(20).add(1); // Simplified example
d20Results.push(d20Result);
}
/**
* @notice Convenience function to show the latest roll
*/
function latestRoll() public view returns (uint256 d20result) {
return d20Results[d20Results.length - 1];
}
}
pragma solidity ^0.6.0;
// This is different from the truffle import
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol";
contract GetDataNaive 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");
}
}
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))
}
}
}
pragma solidity ^0.4.24;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/interfaces/AggregatorInterface.sol";
contract Demo {
AggregatorInterface internal ref;
constructor(address _aggregator) public {
ref = AggregatorInterface(_aggregator);
}
function getLatestPrice() public view returns (int256) {
return ref.latestAnswer();
}
}
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;
// 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");
}
}
pragma solidity ^0.5.0;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.5/PreCoordinator.sol";
// You'll have to compile and run this to deploy a service aggreement before you can run
// the getDataPreCoordinator.sol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment