Last active
March 16, 2021 13:23
-
-
Save alexroan/94b5de0732702de00a3238d3d6e738e6 to your computer and use it in GitHub Desktop.
CoinGeckoConsumer.sol
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
// This example code is designed to quickly deploy an example contract using Remix. | |
pragma solidity ^0.6.0; | |
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/ChainlinkClient.sol"; | |
contract CoinGeckoConsumer is ChainlinkClient { | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
uint256 public ethereumPrice; | |
/** | |
* Network: Kovan | |
* Oracle: | |
* Name: AlphaChain Kovan | |
* Listing URL: https://market.link/nodes/ef076e87-49f4-486b-9878-c4806781c7a0?start=1601380594&end=1601985394 | |
* Address: 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b | |
* Job: | |
* Name: ETH-USD CoinGecko | |
* Listing URL: https://market.link/jobs/78868caf-4a75-4dbf-a4cf-52538a283409 | |
* ID: 9cc0c77e8e6e4f348ef5ba03c636f1f7 | |
* Fee: 0.1 LINK | |
*/ | |
constructor() public { | |
setPublicChainlinkToken(); | |
oracle = 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b; // oracle address | |
jobId = "9cc0c77e8e6e4f348ef5ba03c636f1f7"; //job id | |
fee = 0.1 * 10 ** 18; // 0.1 LINK | |
} | |
/** | |
* Make initial request | |
*/ | |
function requestEthereumPrice() public { | |
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this), this.fulfillEthereumPrice.selector); | |
sendChainlinkRequestTo(oracle, req, fee); | |
} | |
/** | |
* Callback function | |
*/ | |
function fulfillEthereumPrice(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) { | |
ethereumPrice = _price; | |
} | |
/** | |
* Withdraw LINK from this contract | |
* | |
* NOTE: DO NOT USE THIS IN PRODUCTION AS IT CAN BE CALLED BY ANY ADDRESS. | |
* THIS IS PURELY FOR EXAMPLE PURPOSES ONLY. | |
*/ | |
function withdrawLink() external { | |
LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress()); | |
require(linkToken.transfer(msg.sender, linkToken.balanceOf(address(this))), "Unable to transfer"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment