From 41549df9c9ecdb458e55394cb4522614a5275b73 Mon Sep 17 00:00:00 2001 From: Rubilmax Date: Tue, 17 Oct 2023 19:16:02 +0200 Subject: [PATCH] fix(deal): encode addresses --- src/helpers.ts | 14 +++++++------- test/deal.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index 8c78c9a..0a1d6f6 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -29,7 +29,7 @@ const getAddress = async (address: AddressLike) => { const awaited = await address; if (typeof awaited === "string") return awaited.toLowerCase(); - return await awaited.getAddress(); + return awaited.getAddress(); }; export const deal = async ( @@ -50,8 +50,8 @@ export const deal = async ( const balanceOfCall = [ { - to: erc20, - data: balanceOfIfc.encodeFunctionData("balanceOf", [recipient]), + to: erc20Address, + data: balanceOfIfc.encodeFunctionData("balanceOf", [recipientAddress]), }, ]; @@ -67,11 +67,11 @@ export const deal = async ( let balanceOfSlot = getBalanceOfSlot(dealSlot.type, dealSlot.slot, recipientAddress); const storageBefore = !cached - ? await hre!.network.provider.send("eth_getStorageAt", [erc20, balanceOfSlot]) + ? await hre!.network.provider.send("eth_getStorageAt", [erc20Address, balanceOfSlot]) : null; await hre!.network.provider.send(hre!.network.config.rpcEndpoints.setStorageAt, [ - erc20, + erc20Address, balanceOfSlot, hexAmount, ]); @@ -83,7 +83,7 @@ export const deal = async ( if (balance === hexAmount) return true; await hre!.network.provider.send(hre!.network.config.rpcEndpoints.setStorageAt, [ - erc20, + erc20Address, balanceOfSlot, storageBefore, ]); @@ -108,7 +108,7 @@ export const deal = async ( success = await trySlot(); } - if (!success) throw Error(`Could not brute-force storage slot for ERC20 at: ${erc20}`); + if (!success) throw Error(`Could not brute-force storage slot for ERC20 at: ${erc20Address}`); if (cached) return; diff --git a/test/deal.test.ts b/test/deal.test.ts index 112449c..4d0fc5b 100644 --- a/test/deal.test.ts +++ b/test/deal.test.ts @@ -40,6 +40,28 @@ describe("Integration tests examples", function () { assert.equal(await usdcContract.balanceOf(user), 1, "balanceOf"); }); + it("Should deal USDC using a contract", async function () { + const usdcContract = await this.hre.ethers.getContractAt( + ["function balanceOf(address) external view returns (uint256)"], + usdc + ); + + await deal(usdcContract, user, 1); + + assert.equal(await usdcContract.balanceOf(user), 1, "balanceOf"); + }); + + it("Should deal USDC to recipient using a contract", async function () { + const usdcContract = await this.hre.ethers.getContractAt( + ["function balanceOf(address) external view returns (uint256)"], + usdc + ); + + await deal(usdc, usdcContract, 1); + + assert.equal(await usdcContract.balanceOf(usdcContract), 1, "balanceOf"); + }); + it("Should deal cbETH & re-use cache", async function () { await deal(cbETH, user, 1);