Last active
December 29, 2020 19:49
-
-
Save fourswordsio/04620b24e9295c871c950fc22cd15645 to your computer and use it in GitHub Desktop.
Based-Consumer-CoinGecko.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
pragma solidity ^0.6.0; | |
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/evm-contracts/src/v0.6/ChainlinkClient.sol"; | |
contract ConsumerBasedMoney is ChainlinkClient { | |
uint256 public price; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
/** | |
* Oracle: FourSwords Kovan Testnet Node | |
* Oracle Address - 0xa42fdfd2e1a7239b76d753803cbb7611004fe068 | |
* Job ID - 235c4499b644416da1cdd8f77d810f33 | |
* Fee: 0.1 LINK | |
*/ | |
/** | |
* Oracle: Divination Mainnet Node | |
* Oracle Address - 0x20b975a6eaa4aed8f76c6ba4250c8b40be5c9881 | |
* Job ID - 12a057dd60bf4e5fb38a35cf40d15c98 | |
* Fee: 0.1 LINK | |
*/ | |
constructor() public { | |
setPublicChainlinkToken(); | |
oracle = 0xA42FdFD2E1a7239B76D753803cbB7611004FE068; | |
jobId = "235c4499b644416da1cdd8f77d810f33"; | |
fee = 0.1 * 10 ** 18; // 0.1 LINK | |
} | |
function requestBasedPrice() public returns (bytes32 requestId) | |
{ | |
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); | |
// Set the URL to perform the GET request on | |
request.add("get", "https://api.coingecko.com/api/v3/simple/price?ids=based-money&vs_currencies=usd"); | |
request.add("path", "BASED-MONEY.USD"); | |
// Multiply the result by 1000000000000000000 to remove decimals | |
int timesAmount = 10**18; | |
request.addInt("times", timesAmount); | |
// Sends the request | |
return sendChainlinkRequestTo(oracle, request, fee); | |
} | |
/** | |
* Receive the response in the form of uint256 | |
*/ | |
function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId) | |
{ | |
price = _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