Skip to content

Instantly share code, notes, and snippets.

@camharris
Created July 7, 2021 00:52
Show Gist options
  • Save camharris/53546695bbfca3c7a86a0c56413f32c9 to your computer and use it in GitHub Desktop.
Save camharris/53546695bbfca3c7a86a0c56413f32c9 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.4.24+commit.e67f0147.js&optimize=true&runs=200&gist=
pragma solidity 0.4.24;
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/ChainlinkClient.sol";
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/vendor/Ownable.sol";
contract ExampleConsumer is ChainlinkClient, Ownable {
uint256 constant private ORACLE_PAYMENT = 1 * LINK;
// Apy standard deviation
string public ApyStd;
event RequestPoolApyStdFulfilled(
bytes32 indexed requestId,
bytes32 indexed apyStd
);
constructor() public Ownable() {
setPublicChainlinkToken();
}
// Create request to oracle
function RequestPoolApyStd(address _oracle, string _jobId, string _pool, string _range)
public
onlyOwner
{
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), this, this.fulfillPoolApyStd.selector);
req.add("pool", _pool);
req.add("range", _range);
sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
}
function fulfillPoolApyStd(bytes32 _requestId, bytes32 _apyStd)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestPoolApyStdFulfilled(_requestId, _apyStd);
ApyStd = string(abi.encodePacked(_apyStd));
}
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");
}
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