Created
June 1, 2023 08:06
-
-
Save dievardump/5caa2e578db16adfdde733cfb2467b80 to your computer and use it in GitHub Desktop.
Hardhat reorg
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
import {getUnnamedAccounts, ethers} from 'hardhat'; | |
async function main() { | |
await network.provider.send('evm_setAutomine', [false]); | |
let snapshot = null; | |
async function mine() { | |
let blockNumber = parseInt(await network.provider.send('eth_blockNumber'), 16); | |
let block = await network.provider.send('eth_getBlockByNumber', [`0x${blockNumber.toString(16)}`, false]); | |
console.log(`${blockNumber}: ${block?.hash}`); | |
if (blockNumber % 10 == 3) { | |
if (!snapshot) { | |
console.log('Taking snapshot at block', blockNumber); | |
snapshot = await network.provider.send('evm_snapshot', []); | |
await network.provider.send('evm_mine'); | |
} else { | |
console.log('Creating new path'); | |
snapshot = null; | |
await network.provider.send('evm_mine'); | |
} | |
} else if (blockNumber % 10 == 5 && snapshot) { | |
console.log('Reverting to last snapshot'); | |
if (!(await network.provider.send('evm_revert', [snapshot]))) { | |
throw new Error('Could not revert'); | |
} | |
} else { | |
await network.provider.send('evm_mine'); | |
} | |
setTimeout(mine, 1000); | |
} | |
mine(); | |
} | |
// We recommend this pattern to be able to use async/await everywhere | |
// and properly handle errors. | |
main().catch((error) => { | |
console.error(error); | |
process.exitCode = 1; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment