Skip to content

Commit

Permalink
Add comments describing helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Sep 12, 2023
1 parent c0edb27 commit aef4026
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions test/lib/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ export const getBalances = async (
return Promise.all(balances);
};

/**
* Helper function that sends a transaction and check for balance deltas
*
* @param tokensForBalanceCheck Token addresses to check balance deltas
* @param client Client that will perform transactions
* @param clientAddress Account address that will have token balance checked
* @param to Contract Address that will be called
* @param data Transaction encoded data
* @param value ETH value in case of ETH transfer
* @returns Transaction recepit, balance deltas and gas used
*/
export async function sendTransactionGetBalances(
tokensForBalanceCheck: Address[],
client: Client & PublicActions & TestActions & WalletActions,
Expand Down Expand Up @@ -148,7 +159,7 @@ export const setTokenBalance = async (
const slotBytes = pad(toBytes(slot));
const accountAddressBytes = pad(toBytes(accountAddress));

let index;
let index: Address;
if (isVyperMapping) {
index = keccak256(concat([slotBytes, accountAddressBytes])); // slot, key
} else {
Expand Down Expand Up @@ -186,6 +197,7 @@ export async function findTokenBalanceSlot(
[BigInt((Math.random() * 10000).toFixed())],
);
for (let i = 0; i < 999; i++) {
// encode probed slot
const slotBytes = pad(toBytes(i));
const accountAddressBytes = pad(toBytes(accountAddress));
let probedSlot: Address;
Expand All @@ -194,32 +206,39 @@ export async function findTokenBalanceSlot(
} else {
probedSlot = keccak256(concat([accountAddressBytes, slotBytes])); // key, slot
}

// remove padding for JSON RPC
probedSlot = trim(probedSlot);

// get storage value
const prev = (await client.getStorageAt({
address: tokenAddress,
slot: probedSlot,
})) as Hex;
// make sure the probe will change the slot value
const probe = prev === probeA ? probeB : probeA;

// set storage slot to new probe
const probe = prev === probeA ? probeB : probeA;
await client.setStorageAt({
address: tokenAddress,
index: probedSlot,
value: probe,
});

// check if balance changed
const balance = await getErc20Balance(
tokenAddress,
client,
accountAddress,
);

// reset to previous value
await client.setStorageAt({
address: tokenAddress,
index: probedSlot,
value: prev,
});

// return slot if balance changed
if (balance === hexToBigInt(probe)) return i;
}
throw new Error('Balance slot not found!');
Expand Down

0 comments on commit aef4026

Please sign in to comment.