slotseek-viem is a JavaScript library for viem that assists with finding the storage slots for the balanceOf and allowance mappings in an ERC20 token contract, and the permit2 allowance mapping. It also provides a way to generate mock data that can be used to override the state of a contract in an eth_call or eth_estimateGas call.
This is a port of the original slotseek library from ethers.js to viem.
The main use case for this library is to estimate gas costs of transactions that would fail if the address did not have the required balance or approval.
For example, estimating the gas a transaction will consume when swapping, before the user has approved the contract to spend their tokens.
- Find storage slots for
balanceOfandallowancemappings in an ERC20 token contract, and permit2 allowance mapping - Generates mock data that can be used to override the state of a contract in an
eth_call/eth_estimateGascall - Supports vyper storage layouts
- Built for viem with full TypeScript support
The library uses a brute force approach to find the storage slot of the balanceOf and allowance mappings in an ERC20 token contract. It does this by using a user-provided address that we know has a balance or approval, and then iterates through the storage slots of the contract via the eth_getStorageAt JSON-RPC method until it finds the slot where the storage value matches the user's balance or approval.
This is not a perfect method, and there are more efficient ways to find the storage slot outside of just interacting directly with the contract over RPC. But it's difficult to do so without needing to setup more tools/infra, especially for multi-chain support and gas estimation at runtime. Also, there are not many tools to help with this in javascript.
npm install @d3or/slotseek-viem
# or
yarn add @d3or/slotseek-viem- Add caching options to reduce the number of RPC calls and reduce the time it takes to find the same slot again
import { createPublicClient, http, parsePrivateKey, privateKeyToAddress, getAddress } from "viem";
import { base } from "viem/chains";
import { generateMockBalanceData } from "@d3or/slotseek-viem";
async function fakeUserBalance() {
// Setup - Base RPC
const client = createPublicClient({
chain: base,
transport: http("YOUR_RPC_URL")
});
// Constants
const tokenAddress = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"; // USDC on Base
const holderAddress = "0x0000c3Caa36E2d9A8CD5269C976eDe05018f0000"; // USDC holder
const mockAddress = privateKeyToAddress(parsePrivateKey("0x..." /* generate random */)); // Address to fake balance for
const mockBalanceAmount = "1000000000000"; // 1 million USDC (6 decimal places), optional. If not provided, defaults to the balance of the holder
// Generate mock balance data
const data = await generateMockBalanceData(client, {
tokenAddress,
holderAddress,
mockAddress,
mockBalanceAmount,
});
// Prepare state diff object
const stateDiff = {
[tokenAddress]: {
stateDiff: {
[data.slot]: data.balance,
},
},
};
// Make the eth_call with state overrides, or eth_estimateGas
const balanceOfResponse = await client.request({
method: 'eth_call',
params: [
{
from: mockAddress,
to: tokenAddress,
data: encodeFunctionData({
abi: [parseAbiItem('function balanceOf(address) view returns (uint256)')],
functionName: 'balanceOf',
args: [mockAddress]
}),
},
"latest",
stateDiff,
],
});
// Decode and log the result
const balance = decodeFunctionResult({
abi: [parseAbiItem('function balanceOf(address) view returns (uint256)')],
functionName: 'balanceOf',
data: balanceOfResponse
});
console.log(
`Mocked balance for ${mockAddress}: ${formatUnits(balance, 6)} USDC`
);
}
fakeUserBalance().catch(console.error);This can also be used to fake approvals, by using the generateMockApprovalData function instead of generateMockBalanceData.
import { createPublicClient, http, parsePrivateKey, privateKeyToAddress } from "viem";
import { base } from "viem/chains";
import { generateMockApprovalData } from "@d3or/slotseek-viem";
async function fakeUserApproval() {
// Setup
const client = createPublicClient({
chain: base,
transport: http("YOUR_RPC_URL")
});
// Constants
const tokenAddress = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"; // USDC on Base
const ownerAddress = "0x0000c3Caa36E2d9A8CD5269C976eDe05018f0000"; // USDC holder
const spenderAddress = "0x000000000022D473030F116dDEE9F6B43aC78BA3"; // Spender address
const mockAddress = privateKeyToAddress(parsePrivateKey("0x..." /* generate random */)); // Address to fake balance for
const mockApprovalAmount = "1000000000000"; // 1 million USDC (6 decimal places)
// Generate mock approval data
const mockApprovalData = await generateMockApprovalData(client, {
tokenAddress,
ownerAddress,
spenderAddress,
mockAddress,
mockApprovalAmount,
});
// Prepare state diff object
const stateDiff = {
[tokenAddress]: {
stateDiff: {
[mockApprovalData.slot]: mockApprovalData.approval,
},
},
};
// Make the eth_call with state overrides, or eth_estimateGas
const allowanceResponse = await client.request({
method: 'eth_call',
params: [
{
from: mockAddress,
to: tokenAddress,
data: encodeFunctionData({
abi: [parseAbiItem('function allowance(address,address) view returns (uint256)')],
functionName: 'allowance',
args: [mockAddress, spenderAddress]
}),
},
"latest",
stateDiff,
],
});
// Decode and log the result
const allowance = decodeFunctionResult({
abi: [parseAbiItem('function allowance(address,address) view returns (uint256)')],
functionName: 'allowance',
data: allowanceResponse
});
console.log(
`Mocked allowance for ${mockAddress}: ${formatUnits(allowance, 6)} USDC`
);
}
fakeUserApproval().catch(console.error);You can also override both the balance and the allowance at the same time by providing both the balance and approval fields in the state diff object.
import { createPublicClient, http } from "viem";
import { base } from "viem/chains";
import { getErc20BalanceStorageSlot } from "@d3or/slotseek-viem";
async function findStorageSlot() {
// Setup - Base RPC
const client = createPublicClient({
chain: base,
transport: http("https://mainnet.base.org")
});
// Constants
const tokenAddress = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"; // USDC on Base
const holderAddress = "0x0000c3Caa36E2d9A8CD5269C976eDe05018f0000"; // USDC holder
const maxSlots = 100; // Max slots to search
// Find the storage slot for the balance of the holder
// or for approvals, use getErc20AllowanceStorageSlot
const { slot, balance, isVyper } = await getErc20BalanceStorageSlot(
client,
tokenAddress,
holderAddress,
maxSlots
);
console.log(
`User has balance of ${formatUnits(balance, 6)} USDC stored at slot #${Number(slot)}`
);
}
findStorageSlot().catch(console.error);import { createPublicClient, http, pad, toHex } from "viem";
import { base } from "viem/chains";
import { computePermit2AllowanceStorageSlot } from "@d3or/slotseek-viem";
async function findStorageSlot() {
// Setup - Base RPC
const client = createPublicClient({
chain: base,
transport: http("https://mainnet.base.org")
});
// Constants
const tokenAddress = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"; // USDC on Base
const mockAddress = "0x0000c3Caa36E2d9A8CD5269C976eDe05018f0000"; // USDC holder to mock approval for
const spenderAddress = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
// Compute storage slot of where the allowance would be held
const { slot } = computePermit2AllowanceStorageSlot(mockAddress, tokenAddress, spenderAddress)
const permit2Contract = '0x000000000022d473030f116ddee9f6b43ac78ba3'
// Prepare state diff object
const stateDiff = {
[permit2Contract]: {
stateDiff: {
[slot]: pad(
toHex(BigInt("1461501637330902918203684832716283019655932142975")),
{ size: 32 }
)
},
},
};
// Function selector for allowance(address,address,address)
const allowanceCalldata = encodeFunctionData({
abi: [parseAbiItem('function allowance(address,address,address) view returns (uint256)')],
functionName: 'allowance',
args: [mockAddress, tokenAddress, spenderAddress]
});
const allowanceResponse = await client.request({
method: 'eth_call',
params: [
{
to: permit2Contract,
data: allowanceCalldata,
},
"latest",
stateDiff
],
});
// convert the response to a BigNumber
const approvalAmount = decodeFunctionResult({
abi: [parseAbiItem('function allowance(address,address,address) view returns (uint256)')],
functionName: 'allowance',
data: allowanceResponse
});
console.log(
`Mocked balance for ${mockAddress}: ${formatUnits(approvalAmount, 6)} USDC`
);
}
findStorageSlot().catch(console.error);