Created
July 27, 2021 13:43
-
-
Save yuyasugano/eac5d31e53489b41e02e1660efdef2d5 to your computer and use it in GitHub Desktop.
techflare staging smart contract example
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.7; | |
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol"; | |
contract TechFlareConsumer is ChainlinkClient { | |
uint256 public value; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
/** | |
* Network: Kovan | |
* Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e | |
* Chainlink - 29fa9aa13bf1468788b7cc4a500a45b8 | |
* Fee: 0.1 LINK | |
*/ | |
constructor() public { | |
setPublicChainlinkToken(); | |
oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e; | |
jobId = "29fa9aa13bf1468788b7cc4a500a45b8"; | |
fee = 0.1 * 10 ** 18; // 0.1 LINK | |
} | |
/** | |
* Create a Chainlink request to retrieve API response, find the target | |
* data, then multiply by 1000000000000000000 (to remove decimal places from data). | |
************************************************************************************ | |
* STOP! * | |
* THIS FUNCTION WILL FAIL IF THIS CONTRACT DOES NOT OWN LINK * | |
* ---------------------------------------------------------- * | |
* Learn how to obtain testnet LINK and fund this contract: * | |
* ------- https://docs.chain.link/docs/acquire-link -------- * | |
* ---- https://docs.chain.link/docs/fund-your-contract ----- * | |
* * | |
************************************************************************************/ | |
function requestAPIData() 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://3t9jjasu58.execute-api.ap-northeast-1.amazonaws.com/Stage/chainlink?n=btc&p=h"); | |
// Set the path to find the desired data in the API response: | |
request.add("path", "sma.5"); | |
// 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 _value) public recordChainlinkFulfillment(_requestId) | |
{ | |
value = _value; | |
} | |
/** | |
* 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