Last active
October 12, 2022 13:13
-
-
Save chiro-hiro/325e8fc42ef2af47b40d97564af72686 to your computer and use it in GitHub Desktop.
Hardhat: Unlock any signer on mainnet forking
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
// MIT License | |
// Copyright (c) 2021 Dũng Trần a.k.a Chiro Hiro <chiro8x@gmail.com> | |
import { Signer } from '@ethersproject/abstract-signer' | |
import { Contract } from '@ethersproject/contracts' | |
import hre from 'hardhat' | |
interface IKeyValues { | |
[key: string]: string | |
} | |
interface ISignerCache { | |
[key: string]: Signer | |
} | |
const signers: ISignerCache = {} | |
const signerAlias: IKeyValues = {} | |
export async function unlockSigner(alias: string): Promise<Signer> | |
export async function unlockSigner(alias: string, address: string): Promise<Signer> | |
export async function unlockSigner(...params: string[]): Promise<Signer> { | |
if (params.length === 1) { | |
const [alias] = params | |
if (typeof alias !== 'string') throw new Error('Alias was not a string') | |
if (typeof signerAlias[alias] === 'undefined') throw new Error('Alias was not defined') | |
if (typeof signers[signerAlias[alias]] === 'undefined') throw new Error('Signer was not unblocked') | |
// We will get unlocked signer by alias | |
return signers[signerAlias[alias]] | |
} else if (params.length === 2) { | |
const [alias, address] = params | |
// Prevent case sensitive | |
const key = address.toLowerCase() | |
if (typeof signers[address] === 'undefined') { | |
if (typeof signerAlias[alias] === 'undefined') { | |
signerAlias[alias] = key | |
} | |
await hre.network.provider.request({ | |
method: 'hardhat_impersonateAccount', | |
params: [address] | |
}) | |
signers[address] = await hre.ethers.provider.getSigner(address) | |
} | |
return signers[address] | |
} | |
throw new Error('Number of parameters was not match') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment