Last active
January 26, 2021 11:40
-
-
Save PatrickAlphaC/c6af0c3c5f42d5a55a50424e827107dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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. | |
* If you have never used Remix, try our example walkthrough: https://docs.chain.link/docs/example-walkthrough | |
* You will need testnet ETH and LINK. | |
* - Kovan ETH faucet: https://faucet.kovan.network/ | |
* - Kovan LINK faucet: https://kovan.chain.link/ | |
*/ | |
pragma solidity ^0.6.0; | |
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/evm-contracts/src/v0.6/ChainlinkClient.sol"; | |
contract APIConsumer is ChainlinkClient { | |
uint256 public volume; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
/** | |
* Network: Kovan | |
* Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e | |
* Chainlink - 29fa9aa13bf1468788b7cc4a500a45b8 | |
* Fee: 0.01 LINK | |
*/ | |
constructor() public { | |
setChainlinkToken(0x70d1F773A9f81C852087B77F6Ae6d3032B02D2AB); | |
oracle = 0xBf87377162512f8098f78f055DFD2aDAc34cbB47; | |
jobId = "6b57e3fe0d904ba48d137b39350c7892"; | |
fee = 0.01 * 10 ** 18; // 0.01 LINK | |
} | |
/** | |
* Create a Chainlink request to retrieve API response, find the target | |
* data, then multiply by 1000000000000000000 (to remove decimal places from data). | |
*/ | |
function requestBTCCNYPrice() 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://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=CNY&apikey=demo"); | |
string[] memory path = new string[](2); | |
path[0] = "Realtime Currency Exchange Rate"; | |
path[1] = "5. Exchange Rate"; | |
request.addStringArray("path", path); | |
// 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 _volume) public recordChainlinkFulfillment(_requestId) | |
{ | |
volume = _volume; | |
} | |
/** | |
* 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