Skip to content

Instantly share code, notes, and snippets.

@talbaneth
Last active March 29, 2022 07:05
Show Gist options
  • Save talbaneth/f50c2548c767e5b0fb9ae380c6f07145 to your computer and use it in GitHub Desktop.
Save talbaneth/f50c2548c767e5b0fb9ae380c6f07145 to your computer and use it in GitHub Desktop.
flash-killer-keeper review
const fs = require('fs');
const fetch = require('node-fetch'); // TB - unused?
const ethers = require("ethers");
// configuration
const WS_RPC = 'http://127.0.0.1:8545/';
// const WS_RPC = 'ws://192.168.1.111:8546/';
const CHAINLOG = "0xdA0Ab1e0017DEbCd72Be8599041a2aa3bA7e740F";
const MAX_GAS_LIMIT = ethers.utils.parseUnits('10000000', 'wei');
const GAS_BUMP = ethers.utils.parseUnits('200000', 'wei');
//
// DO NOT CHANGE BELOW LINE // TB - what line does this refer to?
//
// block mutex
let onBlockMutex = false;
// wallet
// const provider = new ethers.providers.JsonRpcProvider(WS_RPC); // TB - can remove
const provider = new ethers.providers.WebSocketProvider(WS_RPC);
const privateKey = fs.readFileSync('./secrets/private.key');
const signer = new ethers.Wallet(privateKey.toString('utf8'), provider);
// contracts
const chainLogABI = JSON.parse(fs.readFileSync('./abi/ChainLog.abi').toString());
const chainlog = new ethers.Contract(CHAINLOG, chainLogABI, signer);
const flashKillerABI = JSON.parse(fs.readFileSync('./abi/FlashKiller.abi').toString());
const flashMintABI = JSON.parse(fs.readFileSync('./abi/FlashMint.abi').toString());
async function checkFlashKiller(flashKiller, _overrides) {
try {
const flashAddress = await flashKiller.flash();
const flashMint = new ethers.Contract(flashAddress, flashMintABI, signer);
const max = await flashMint.max();
if (max.eq(0)) {
console.log("FlashMint already disabled.");
return;
}
_overrides.gasLimit = await flashKiller.estimateGas.kill(_overrides); // TB - can you explain why you are giving the override here?
if (_overrides.gasLimit.gt(MAX_GAS_LIMIT)) {
console.log(
'checkFlashKiller: gasLimit too high: ' +
_overrides.gasLimit + ' > ' + MAX_GAS_LIMIT
);
return;
}
await flashKiller.callStatic.kill(_overrides);
// everything beyond here is a real call
console.log('checkFlashKiller: flashKiller.kill()');
const tx = await flashKiller.kill(_overrides);
console.log(tx);
const receipt = await tx.wait();
console.log(receipt);
} catch (err) {
// standard path lands here, uncomment to debug
// console.error(err);
return;
}
}
async function onBlock(blockNumber) {
console.log('onBlock: block number ' + blockNumber);
if (onBlockMutex) {
console.log('onBlock: already processing a block, exiting...');
return;
}
provider.off("block");
onBlockMutex = true;
const flashKillerAddress = await chainlog.getAddress(
ethers.utils.formatBytes32String('FLASH_KILLER')
);
const flashKiller = new ethers.Contract(
flashKillerAddress, flashKillerABI, signer
);
// TB - Consider taking this debug code to a separate function (simple since it only uses provider). Then you only need to comment out the function call.
// debugging gas price
//
// let fee = await provider.getFeeData()
// console.log(
// 'gasPrice: ' + ethers.utils.formatUnits(fee.gasPrice, "gwei")
// );
// console.log(
// 'maxFeePerGas: ' + ethers.utils.formatUnits(fee.maxFeePerGas, "gwei")
// );
// console.log(
// 'maxPriorityFeePerGas: ' + ethers.utils.formatUnits(fee.maxPriorityFeePerGas, "gwei")
// );
// All overrides are optional
let overrides = {
gasLimit: GAS_BUMP.mul(5) // TB - can we define this as the amount we want and avoid the multipication?
};
await checkFlashKiller(flashKiller, overrides);
onBlockMutex = false;
return provider.on("block", onBlock);
}
provider.on("block", onBlock);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment