Skip to content

Commit

Permalink
fix(deal): encode addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Oct 17, 2023
1 parent 715f888 commit 41549df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -50,8 +50,8 @@ export const deal = async (

const balanceOfCall = [
{
to: erc20,
data: balanceOfIfc.encodeFunctionData("balanceOf", [recipient]),
to: erc20Address,
data: balanceOfIfc.encodeFunctionData("balanceOf", [recipientAddress]),
},
];

Expand All @@ -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,
]);
Expand All @@ -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,
]);
Expand All @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions test/deal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 41549df

Please sign in to comment.