Last active
August 23, 2021 02:42
-
-
Save camharris/aec35e5012aee1d00b8f596e43314bf3 to your computer and use it in GitHub Desktop.
kovan alarm clock contract
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
/** 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/ | |
*/ | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.0; | |
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol"; | |
contract AlarmClockSample is ChainlinkClient { | |
bool public alarmDone; | |
address private oracle; | |
bytes32 private jobId; | |
uint256 private fee; | |
/** | |
* Network: Kovan | |
* Oracle: Chainlink - 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b | |
* Job ID: Chainlink - 982105d690504c5d9ce374d040c08654 | |
* Fee: 0.1 LINK | |
*/ | |
constructor() public { | |
setPublicChainlinkToken(); | |
oracle = 0xF405B99ACa8578B9eb989ee2b69D518aaDb90c1F; | |
jobId = "a13ad0518c9d4ffdbdbd5bf745aefe02"; | |
fee = 0.1 * 10 ** 18; // 0.1 LINK | |
alarmDone = false; | |
} | |
/** | |
* Create a Chainlink request to start an alarm and after | |
* the time in seconds is up, return throught the fulfillAlarm | |
* function | |
*/ | |
function requestAlarmClock(uint256 durationInSeconds) public returns (bytes32 requestId) | |
{ | |
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillAlarm.selector); | |
// This will return in 90 seconds | |
request.addUint("until", block.timestamp + durationInSeconds); | |
alarmDone = false; | |
return sendChainlinkRequestTo(oracle, request, fee); | |
} | |
/** | |
* Receive the response in the form of uint256 | |
*/ | |
function fulfillAlarm(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId) | |
{ | |
alarmDone = true; | |
} | |
/** | |
* 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