Created
November 29, 2021 17:18
-
-
Save ccashwell/88021971769178119e6b37db71a66b74 to your computer and use it in GitHub Desktop.
HardHat Test Helpers
This file contains hidden or 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 * as hre from "hardhat"; | |
import { RequestArguments } from "hardhat/types"; | |
import { BigNumber } from "@ethersproject/abi/node_modules/@ethersproject/bignumber"; | |
import { Block } from "@ethersproject/abstract-provider"; | |
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; | |
// Mine the next block | |
export async function mineBlock(): Promise<void> { | |
await rpc({ method: "evm_mine" }); | |
} | |
// Advance to a given block (must be later than current block) | |
export async function advanceToBlock(blockNumber: number): Promise<void> { | |
await rpc({ method: "evm_mineBlockNumber", params: [blockNumber] }); | |
} | |
// Mine N sequential blocks | |
export async function advanceBlocks(blocks: number): Promise<void> { | |
for (let i = await blockNumber(); i < blocks; i++) await mineBlock(); | |
} | |
// Get the current block number | |
export async function blockNumber(): Promise<number> { | |
return parseInt((await rpc({ method: "eth_blockNumber" })) as string); | |
} | |
// Advance the clock by N seconds then mine a block at that time | |
export async function increaseTime( | |
seconds: number, | |
mine = true | |
): Promise<void> { | |
await rpc({ method: "evm_increaseTime", params: [seconds] }); | |
if (mine) await rpc({ method: "evm_mine" }); | |
} | |
// Set the next block to be mined at a specific timestamp (unix format) | |
export async function setNextBlockTimestamp(timestamp: number): Promise<void> { | |
await rpc({ method: "evm_setNextBlockTimestamp", params: [timestamp] }); | |
} | |
// Get the latest block (including txs and metadata) | |
export async function lastBlock(): Promise<Block> { | |
return await hre.ethers.provider.getBlock("latest"); | |
} | |
// Sends a raw RPC request to the provider | |
export async function rpc(request: RequestArguments): Promise<unknown> { | |
return await hre.network.provider.request(request); | |
} | |
// Get all signer accounts | |
export async function getAccounts(): Promise<SignerWithAddress[]> { | |
return await hre.ethers.getSigners(); | |
} | |
// Give some ETH to the given address | |
export async function giveEther( | |
address: string, | |
amount: BigNumber | |
): Promise<void> { | |
await rpc({ | |
method: "hardhat_setBalance", | |
params: [address, amount.toHexString()], | |
}); | |
} | |
// Get a signer that impersonates a given address | |
export async function impersonate(address: string): Promise<SignerWithAddress> { | |
await rpc({ | |
method: "hardhat_impersonateAccount", | |
params: [address], | |
}); | |
return hre.ethers.getSigner(address); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment