Last active
August 11, 2021 07:51
-
-
Save OnahProsperity/7229a6d29c688c5408f5cb7f2a2d5fdd to your computer and use it in GitHub Desktop.
ChainLinkAlarmCLock
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 | |
interface IERC20 { | |
function balanceOf(address owner) external view returns (uint); | |
function transfer(address to, uint value) external returns (bool); | |
function transferFrom(address from, address to, uint value) external returns (bool); | |
} | |
abstract contract Context { | |
function _msgSender() internal view virtual returns (address) { | |
return msg.sender; | |
} | |
function _msgData() internal view virtual returns (bytes calldata) { | |
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 | |
return msg.data; | |
} | |
} | |
pragma solidity >=0.6.2; | |
interface ISmartSwapRouter02 { | |
function factory() external pure returns (address); | |
function WETH() external pure returns (address); | |
function addLiquidity( | |
address tokenA, | |
address tokenB, | |
uint amountADesired, | |
uint amountBDesired, | |
uint amountAMin, | |
uint amountBMin, | |
address to, | |
uint deadline | |
) external returns (uint amountA, uint amountB, uint liquidity); | |
function addLiquidityETH( | |
address token, | |
uint amountTokenDesired, | |
uint amountTokenMin, | |
uint amountETHMin, | |
address to, | |
uint deadline | |
) external payable returns (uint amountToken, uint amountETH, uint liquidity); | |
function removeLiquidity( | |
address tokenA, | |
address tokenB, | |
uint liquidity, | |
uint amountAMin, | |
uint amountBMin, | |
address to, | |
uint deadline | |
) external returns (uint amountA, uint amountB); | |
function removeLiquidityETH( | |
address token, | |
uint liquidity, | |
uint amountTokenMin, | |
uint amountETHMin, | |
address to, | |
uint deadline | |
) external returns (uint amountToken, uint amountETH); | |
function removeLiquidityWithPermit( | |
address tokenA, | |
address tokenB, | |
uint liquidity, | |
uint amountAMin, | |
uint amountBMin, | |
address to, | |
uint deadline, | |
bool approveMax, uint8 v, bytes32 r, bytes32 s | |
) external returns (uint amountA, uint amountB); | |
function removeLiquidityETHWithPermit( | |
address token, | |
uint liquidity, | |
uint amountTokenMin, | |
uint amountETHMin, | |
address to, | |
uint deadline, | |
bool approveMax, uint8 v, bytes32 r, bytes32 s | |
) external returns (uint amountToken, uint amountETH); | |
function swapExactTokensForTokens( | |
uint amountIn, | |
uint amountOutMin, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external returns (uint[] memory amounts); | |
function swapTokensForExactTokens( | |
uint amountOut, | |
uint amountInMax, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external returns (uint[] memory amounts); | |
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) | |
external | |
payable | |
returns (uint[] memory amounts); | |
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline) | |
external | |
returns (uint[] memory amounts); | |
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) | |
external | |
returns (uint[] memory amounts); | |
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline) | |
external | |
payable | |
returns (uint[] memory amounts); | |
function removeLiquidityETHSupportingFeeOnTransferTokens( | |
address token, | |
uint liquidity, | |
uint amountTokenMin, | |
uint amountETHMin, | |
address to, | |
uint deadline | |
) external returns (uint amountETH); | |
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens( | |
address token, | |
uint liquidity, | |
uint amountTokenMin, | |
uint amountETHMin, | |
address to, | |
uint deadline, | |
bool approveMax, uint8 v, bytes32 r, bytes32 s | |
) external returns (uint amountETH); | |
function swapExactTokensForTokensSupportingFeeOnTransferTokens( | |
uint amountIn, | |
uint amountOutMin, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external; | |
function swapExactETHForTokensSupportingFeeOnTransferTokens( | |
uint amountOutMin, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external payable; | |
function swapExactTokensForETHSupportingFeeOnTransferTokens( | |
uint amountIn, | |
uint amountOutMin, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external; | |
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB); | |
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut); | |
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn); | |
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts); | |
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts); | |
} | |
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/ChainlinkClient.sol"; | |
// import "./ChainlinkClient.sol"; | |
pragma solidity 0.8.6; | |
contract rigelAutoPrice is ChainlinkClient, Context{ | |
IERC20 token; | |
using Chainlink for Chainlink.Request; | |
// ["1000000","1000000",["0x10249e900b919fdee9e2ed38b4cd83c4df857254","0x9f0227a21987c1ffab1785ba3eba60578ec1501b"],0,"0x2289Bc372bc6a46DD3eBC070FC5B7b7A49597A4E","1627348233"] | |
ISmartSwapRouter02 smartSwap; | |
uint256 public eventsCharges; | |
uint256 oraclePayment; | |
bool public alarmDone; | |
uint256 public number; | |
bytes32 _jobId; | |
address _oracle; | |
address owner; | |
address _reciver; | |
mapping(address => userData) public Data; | |
struct userData { | |
uint amountIn; | |
uint amountOutMin; | |
address[] path; | |
uint expectedTime; | |
address to; | |
uint deadline; | |
} | |
event periods(address indexed _sender, uint _period); | |
constructor() { | |
// ISmartSwapRouter02 _router, ISmartSwapPair _swapPair | |
smartSwap = ISmartSwapRouter02(0x00749e00Af4359Df5e8C156aF6dfbDf30dD53F44); | |
// set the Oracle contract address | |
// setChainlinkOracle(0x46cC5EbBe7DA04b45C0e40c061eD2beD20ca7755); | |
// set LINKToken contract address | |
setChainlinkToken(0x84b9B910527Ad5C03A9Ca831909E21e236EA7b06); | |
_oracle = 0x46cC5EbBe7DA04b45C0e40c061eD2beD20ca7755; | |
_jobId = "842e5f3cbcd34f76bf416d92b47ed416"; | |
owner = _msgSender(); | |
token = IERC20(0x9f0227A21987c1fFab1785BA3eBa60578eC1501B); | |
_reciver = 0xd6bC7F57755a4dECe5C863794c7F37718D10064A; | |
alarmDone = false; | |
number = 5; | |
eventsCharges = 9e18; | |
// setPublicChainlinkToken(); | |
oraclePayment = 0.1e18; | |
} | |
receive() external payable { } | |
modifier onlyOwner() { | |
require(_msgSender() == owner,"RGP: YOU ARE NOT THE OWNER."); | |
_; | |
} | |
// function delayStart( address _oracle) public { | |
// Chainlink.Request memory req = buildChainlinkRequest(_jobId, address(this), this.fulfill.selector); | |
// req.addUint("until", block.timestamp + 5 minutes); | |
// sendChainlinkRequestTo(_oracle, req, oraclePayment); | |
// } | |
// function fulfill(bytes32 _requestId, address _user) public recordChainlinkFulfillment(_requestId) { | |
// /* additional computation here */ | |
// require(_oracle == _msgSender(), "Caller must be the specified Oracle address"); | |
// userData storage _userData = Data[_user]; | |
// // smartSwap.swapExactTokensForTokens( | |
// // _userData.amountIn, | |
// // _userData.amountOutMin, | |
// // _userData.path, | |
// // _userData.to, | |
// // _userData.expectedTime + 20 minutes | |
// // ); | |
// token.transferFrom(_userData.to, _reciver, 2e18); | |
// } | |
function setPeriod(userData memory _data, uint _time) external returns (bool){ | |
Chainlink.Request memory req = buildChainlinkRequest(_jobId, address(this), this.fulfillAlarm.selector); | |
req.addUint("until", block.timestamp + _time); | |
sendChainlinkRequestTo(_oracle, req, oraclePayment); | |
userData storage _userData = Data[_msgSender()]; | |
_userData.path = [_data.path[0], _data.path[1]]; | |
_userData.amountIn = _data.amountIn; | |
_userData.amountOutMin = _data.amountOutMin; | |
_userData.expectedTime = block.timestamp + _time; | |
_userData.deadline = _data.deadline; | |
_data.to = _msgSender(); | |
token.transferFrom(_msgSender(), address(this), eventsCharges); | |
return true; | |
} | |
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); | |
return sendChainlinkRequestTo(_oracle, request, oraclePayment); | |
} | |
/** | |
* Receive the response in the form of uint256 | |
*/ | |
function fulfillAlarm(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId) | |
{ | |
// alarmDone = true; | |
// number = 8; | |
token.transferFrom(owner, _reciver, 2e18); | |
} | |
function setCharges(uint256 _newCharges) public onlyOwner returns(bool) { | |
eventsCharges = _newCharges; | |
return true; | |
} | |
function changeRouterContractAddress(address _smartSwap) external onlyOwner { | |
smartSwap = ISmartSwapRouter02(_smartSwap); | |
} | |
function changeOracle(address _newOracle, bytes32 _newJobID) public onlyOwner { | |
_oracle = _newOracle; | |
_jobId = _newJobID; | |
} | |
/** | |
* 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"); | |
} | |
function sendEther(address payable _to, uint _amount) public { | |
require(msg.sender == owner, "Must own this wallet to send Ether from it"); | |
require(address(this).balance >= _amount, "Cannot send more than this wallet holds"); | |
_to.transfer(_amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment