Skip to content

Instantly share code, notes, and snippets.

@BlockmanCodes
Created August 11, 2023 01:31
Show Gist options
  • Save BlockmanCodes/733a9c47f9e5fb91e5d18fcb32fc51c5 to your computer and use it in GitHub Desktop.
Save BlockmanCodes/733a9c47f9e5fb91e5d18fcb32fc51c5 to your computer and use it in GitHub Desktop.
Hardhat: impersonate signer when forking
const SwapRouterArtifact = require('@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json')
const erc20Abi = [
{
"inputs": [
{"internalType": "address", "name": "spender", "type": "address"},
{"internalType": "uint256", "name": "amount", "type": "uint256"}
],
"name": "approve",
"outputs": [{"internalType": "bool", "name": "", "type": "bool"}],
"stateMutability": "nonpayable", "type": "function"
},
{
"inputs": [{"internalType": "address", "name": "account", "type": "address"}],
"name": "balanceOf",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function"
}
]
const hardhat = require("hardhat")
const provider = hardhat.ethers.provider
const RECIPIENT = '0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B'
const wethAddress = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
const usdcAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const routerAddress = '0xE592427A0AEce92De3Edee1F18E0157C05861564'
const weth = new hardhat.ethers.Contract(wethAddress, erc20Abi, provider)
const usdc = new hardhat.ethers.Contract(usdcAddress, erc20Abi, provider)
const router = new hardhat.ethers.Contract(routerAddress, SwapRouterArtifact.abi, provider)
const logBalances = async () => {
const wethBalance = await weth.balanceOf(RECIPIENT)
const usdcBalance = await usdc.balanceOf(RECIPIENT)
console.log('wethBalance', hardhat.ethers.utils.formatUnits(wethBalance, 18))
console.log('usdcBalance', hardhat.ethers.utils.formatUnits(usdcBalance, 6))
}
const main = async () => {
const signer = await hardhat.ethers.getImpersonatedSigner(RECIPIENT);
console.log('------------ Time 0')
await logBalances()
const tokensIn = '10'
const amountIn = hardhat.ethers.utils.parseUnits(tokensIn, 18)
const gasLimit = 1_000_000
await weth.connect(signer).approve(router.address, amountIn)
const params = {
tokenIn: weth.address,
tokenOut: usdc.address,
recipient: RECIPIENT,
deadline: Math.floor(Date.now() / 1000 + (10 * 60)),
amountIn,
fee: 500,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
}
const txInput = await router.connect(signer).exactInputSingle(params, { gasLimit })
await txInput.wait()
console.log('------------ Time 1')
await logBalances()
}
/*
node scripts/01_impersonateSigner.js
*/
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
require("@nomiclabs/hardhat-waffle");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.18",
networks: {
hardhat: {
forking: {
url: <YOUR_INFURA_URL>
}
}
}
};
{
"name": "YouTubeTutorials",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@nomiclabs/hardhat-waffle": "^2.0.6",
"@types/sinon-chai": "^3.2.9",
"@uniswap/sdk": "^3.0.3",
"@uniswap/sdk-core": "^4.0.2",
"@uniswap/v3-core": "^1.0.1",
"@uniswap/v3-periphery": "^1.4.3",
"@uniswap/v3-sdk": "^3.9.0",
"ethereum-waffle": "^4.0.10",
"ethers": "^5.7.2",
"hardhat": "^2.14.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment