Skip to content

Instantly share code, notes, and snippets.

@kx9x
Last active July 24, 2021 01:29
Show Gist options
  • Save kx9x/c74879378895d5fc84755464b8b6e791 to your computer and use it in GitHub Desktop.
Save kx9x/c74879378895d5fc84755464b8b6e791 to your computer and use it in GitHub Desktop.
This gist shows an autotask that can be used with OpenZeppelin Defender on the free tier without relayer
// Settings edited by user
const YOUR_KEEP3R_ADDRESSS = "";
const YOUR_KEEP3R_PRIVATE_KEY = "";
// Code starts here
const { ethers } = require("ethers");
const privateKey = Buffer.from(YOUR_KEEP3R_PRIVATE_KEY, 'hex');
const UniswapSlidingWindow = "UniswapSlidingWindow";
const HegicPoolKeep3r = "HegicPoolKeep3r";
const YearnV1EarnKeep3r = "YearnV1EarnKeep3r";
const abis = {
UniswapSlidingWindow: [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"KP3R","outputs":[{"internalType":"contract IKeep3rV1","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptGovernance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"add","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address","name":"tokenOut","type":"address"}],"name":"current","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"governance","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lastObservation","outputs":[{"internalType":"uint256","name":"timestamp","type":"uint256"},{"internalType":"uint256","name":"price0Cumulative","type":"uint256"},{"internalType":"uint256","name":"price1Cumulative","type":"uint256"},{"internalType":"uint256","name":"timeElapsed","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"lastUpdated","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"pairObservations","outputs":[{"internalType":"uint256","name":"timestamp","type":"uint256"},{"internalType":"uint256","name":"price0Cumulative","type":"uint256"},{"internalType":"uint256","name":"price1Cumulative","type":"uint256"},{"internalType":"uint256","name":"timeElapsed","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pairs","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingGovernance","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodSize","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"granularity","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_governance","type":"address"}],"name":"setGovernance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"update","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"i","type":"uint256"},{"internalType":"uint256","name":"length","type":"uint256"}],"name":"updateFor","outputs":[{"internalType":"bool","name":"updated","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"pair","type":"address"}],"name":"updatePair","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"work","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"workable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"pair","type":"address"}],"name":"workable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}],
HegicPoolKeep3r: [{"inputs":[{"internalType":"address","name":"_keep3r","type":"address"},{"internalType":"address","name":"_hegicPool","type":"address"},{"internalType":"uint256","name":"_minETHRewards","type":"uint256"},{"internalType":"uint256","name":"_minWBTCRewards","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"DustSent","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"rewards","type":"uint256"}],"name":"ForcedClaimedRewards","type":"event"},{"anonymous":false,"inputs":[],"name":"GovernorAccepted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"hegicPool","type":"address"}],"name":"HegicPoolSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"keep3r","type":"address"}],"name":"Keep3rSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"eth","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"wbtc","type":"uint256"}],"name":"LotsBought","type":"event"},{"anonymous":false,"inputs":[],"name":"ManagerAccepted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_minETHRewards","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_minWBTCRewards","type":"uint256"}],"name":"MinRewardsSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"pendingGovernor","type":"address"}],"name":"PendingGovernorSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"pendingManager","type":"address"}],"name":"PendingManagerSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"rewards","type":"uint256"}],"name":"RewardsClaimedByKeeper","type":"event"},{"inputs":[],"name":"ETH_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"HegicPool","outputs":[{"internalType":"contract IHegicPoolV2","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptGovernor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"acceptManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_eth","type":"uint256"},{"internalType":"uint256","name":"_wbtc","type":"uint256"}],"name":"buyLots","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claimRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"forceClaimRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"governor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"keep3r","outputs":[{"internalType":"contract IKeep3rV1","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minETHRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minWBTCRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingGovernor","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"sendDust","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_hegicPool","type":"address"}],"name":"setHegicPool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_keep3r","type":"address"}],"name":"setKeep3r","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_minETHRewards","type":"uint256"},{"internalType":"uint256","name":"_minWBTCRewards","type":"uint256"}],"name":"setMinRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_pendingGovernor","type":"address"}],"name":"setPendingGovernor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_pendingManager","type":"address"}],"name":"setPendingManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"workable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}],
YearnV1EarnKeep3r: [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"BASE","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"KP3R","outputs":[{"internalType":"contract IKeep3rV1","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"THRESHOLD","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IYERC20","name":"_token","type":"address"}],"name":"shouldRebalance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokens","outputs":[{"internalType":"contract IYERC20[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"work","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"workable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}],
}
class KeeperJob {
constructor(name, addr, workableFunctionName, workFunctionName) {
this.name = name;
this.abi = abis[name];
this.addr = addr;
this.workableFunctionName = workableFunctionName;
this.workFunctionName = workFunctionName;
}
}
const jobs = [
new KeeperJob(UniswapSlidingWindow, "0xCA2E2df6A7a7Cf5bd19D112E8568910a6C2D3885", "workable()", "work"),
new KeeperJob(HegicPoolKeep3r, "0x5DDe926b0A31346f2485900C5e64c2577F43F774", "workable", "claimRewards"),
new KeeperJob(YearnV1EarnKeep3r,"0xe7F4ab593aeC81EcA754Da1B3B7cE0C42a13Ec0C", "workable", "work"),
]
const txLog = {};
for (job in jobs) {
txLog[job] = null;
}
const gasMultiple = 1.00;
// Entrypoint for the Autotask
exports.handler = async function(credentials) {
const provider = ethers.getDefaultProvider('mainnet');
const signer = new ethers.Wallet(privateKey, provider, { speed: 'fastest', YOUR_KEEP3R_ADDRESSS });
for (var i = 0; i < jobs.length; i++)
{
const job = jobs[i];
const contract = new ethers.Contract(job.addr, job.abi, signer);
// Run work if needed
if (await contract[job.workableFunctionName].call()) {
console.log(job.name + " workable");
const gas = await provider.getGasPrice();
var overrideOptions = {
gasLimit: 1500000,
gasPrice: Math.round(gas * gasMultiple)
};
if (txLog[job.name] != null)
{
var receipt = await provider.getTransactionReceipt(txLog[job.name].hash);
if (receipt != null)
{
txLog[job.name] = null;
}
continue;
}
const tx = await contract[job.workFunctionName](overrideOptions);
txLog[job.name] = tx;
console.log(tx.hash);
} else {
console.log(job.name + " not workable");
}
}
}
Copyright 2020 Jesse Liston
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@usmanbaloch21
Copy link

Message: Error running autotask: Unhandled

Logs: 2020-11-05T21:19:54.334Z AUTOTASK START
2020-11-05T21:19:55.351Z ERROR Invoke Error {"errorType":"TypeError","errorMessage":"contract[job.workableFunctionName] is not a function","stack":["TypeError: contract[job.workableFunctionName] is not a function"," at Runtime.exports.handler (/var/task/index.js:44:49)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
2020-11-05T21:19:55.390Z AUTOTASK COMPLETE

I Get this Error while running this any idea why ?

@kx9x
Copy link
Author

kx9x commented Nov 5, 2020

Message: Error running autotask: Unhandled

Logs: 2020-11-05T21:19:54.334Z AUTOTASK START
2020-11-05T21:19:55.351Z ERROR Invoke Error {"errorType":"TypeError","errorMessage":"contract[job.workableFunctionName] is not a function","stack":["TypeError: contract[job.workableFunctionName] is not a function"," at Runtime.exports.handler (/var/task/index.js:44:49)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
2020-11-05T21:19:55.390Z AUTOTASK COMPLETE

I Get this Error while running this any idea why ?

One of the jobs got replaced and I didn't update it. I just updated the code, you can try it out if you want.

@usmanbaloch21
Copy link

Ran without any errors Thanks ALOT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment