Last active
September 26, 2021 14:00
-
-
Save loicdrbx/663093889d5bdadd76eeff3683c7094a 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.7+commit.b8d736ae.js&optimize=false&runs=200&gist=
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.7; | |
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol"; | |
/** | |
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY. | |
* PLEASE DO NOT USE THIS CODE IN PRODUCTION. | |
*/ | |
contract APIConsumer is ChainlinkClient { | |
using Chainlink for Chainlink.Request; | |
uint256 public volume; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
/** | |
* Network: Kovan | |
* Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel | |
* Node) | |
* Job ID: d5270d1c311941d0b08bead21fea7747 | |
* Fee: 0.1 LINK | |
*/ | |
constructor() public { | |
setPublicChainlinkToken(); | |
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8; | |
jobId = "d5270d1c311941d0b08bead21fea7747"; | |
fee = 0.1 * 10 ** 18; // (Varies by network and job) | |
} | |
/** | |
* Create a Chainlink request to retrieve API response, find the target | |
* data, then multiply by 1000000000000000000 (to remove decimal places from data). | |
*/ | |
function requestVolumeData() 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://strain-api.free.beeceptor.com/person1"); | |
// Set the path to find the desired data in the API response, where the response format is: | |
// {"strain": | |
// { | |
// "score": xxx.xxx, | |
// } | |
// } | |
request.add("path", "strain.score"); | |
// 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; | |
} | |
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment