Last active
February 24, 2021 00:24
-
-
Save PatrickAlphaC/97555edfe78b920dd0de375a64c9e5dd 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.6; | |
import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/ChainlinkClient.sol"; | |
contract APIConsumer is ChainlinkClient { | |
string public password; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
/** | |
* Network: Kovan | |
* Oracle: Chainlink - 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b | |
* Job ID: Chainlink - ?????? | |
* Fee: 0.1 LINK | |
*/ | |
constructor() public { | |
setPublicChainlinkToken(); | |
oracle = 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b; | |
jobId = ""; | |
fee = 0.1 * 10 ** 18; // 0.1 LINK | |
} | |
/** | |
* Create a Chainlink request to retrieve the password | |
*/ | |
function requestEthereumPrice() public returns (bytes32 requestId) | |
{ | |
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector); | |
return sendChainlinkRequestTo(oracle, request, fee); | |
} | |
/** | |
* Receive the response in the form of uint256 | |
*/ | |
function fulfill(bytes32 _requestId, bytes32 _password) public recordChainlinkFulfillment(_requestId) | |
{ | |
// convert it to a string | |
password = bytes32ToString(_password); | |
} | |
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory) { | |
uint8 i = 0; | |
while(i < 32 && _bytes32[i] != 0) { | |
i++; | |
} | |
bytes memory bytesArray = new bytes(i); | |
for (i = 0; i < 32 && _bytes32[i] != 0; i++) { | |
bytesArray[i] = _bytes32[i]; | |
} | |
return string(bytesArray); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment