Last active
October 11, 2022 04:23
-
-
Save zeuslawyer/e1081afe1adebcdd5e9372efaa418a6d to your computer and use it in GitHub Desktop.
Client Contract for Oracle/Operator contract associated with my test CL Node for External Adapters
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.8.7; | |
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol"; | |
import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol"; | |
/** | |
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE. | |
* DO NOT USE THIS CODE IN PRODUCTION. | |
*/ | |
contract ConsumerContract is ChainlinkClient, ConfirmedOwner { | |
using Chainlink for Chainlink.Request; | |
uint256 private constant ORACLE_PAYMENT = 1 * LINK_DIVISIBILITY; // 1 * 10**18 | |
string public lastRetrievedInfo; | |
event RequestForInfoFulfilled( | |
bytes32 indexed requestId, | |
string indexed response | |
); | |
/** | |
* Goerli | |
*@dev LINK address in Goerli network: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB | |
* @dev Check https://docs.chain.link/docs/link-token-contracts/ for LINK address for the right network | |
*/ | |
constructor() ConfirmedOwner(msg.sender) { | |
setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB); | |
} | |
function requestInfo( | |
address _oracle, | |
string memory _jobId, | |
string memory number, | |
string memory infoType | |
) public onlyOwner { | |
Chainlink.Request memory req = buildOperatorRequest( | |
stringToBytes32(_jobId), | |
this.fulfillRequestInfo.selector | |
); | |
req.add("number", number); | |
req.add("infoType", infoType); | |
sendOperatorRequestTo(_oracle, req, ORACLE_PAYMENT); | |
} | |
function fulfillRequestInfo(bytes32 _requestId, string memory _info) | |
public | |
recordChainlinkFulfillment(_requestId) | |
{ | |
emit RequestForInfoFulfilled(_requestId, _info); | |
lastRetrievedInfo = _info; | |
} | |
/* | |
========= UTILITY FUNCTIONS ========== | |
*/ | |
function contractBalances() | |
public | |
view | |
returns (uint256 eth, uint256 link) | |
{ | |
eth = address(this).balance; | |
LinkTokenInterface linkContract = LinkTokenInterface( | |
chainlinkTokenAddress() | |
); | |
link = linkContract.balanceOf(address(this)); | |
} | |
function getChainlinkToken() public view returns (address) { | |
return chainlinkTokenAddress(); | |
} | |
function withdrawLink() public onlyOwner { | |
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress()); | |
require( | |
link.transfer(msg.sender, link.balanceOf(address(this))), | |
"Unable to transfer Link" | |
); | |
} | |
function withdrawBalance() public onlyOwner { | |
payable(msg.sender).transfer(address(this).balance); | |
} | |
function cancelRequest( | |
bytes32 _requestId, | |
uint256 _payment, | |
bytes4 _callbackFunctionId, | |
uint256 _expiration | |
) public onlyOwner { | |
cancelChainlinkRequest( | |
_requestId, | |
_payment, | |
_callbackFunctionId, | |
_expiration | |
); | |
} | |
function stringToBytes32(string memory source) | |
private | |
pure | |
returns (bytes32 result) | |
{ | |
bytes memory tempEmptyStringTest = bytes(source); | |
if (tempEmptyStringTest.length == 0) { | |
return 0x0; | |
} | |
assembly { | |
// solhint-disable-line no-inline-assembly | |
result := mload(add(source, 32)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment