Skip to content

Commit

Permalink
chore: up
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 10, 2025
1 parent cd3a33d commit 88dcd26
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
35 changes: 34 additions & 1 deletion src/actions/public/simulateCalls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test('behavior: with mutation calls + asset changes', async () => {
const account = '0xdead000000000000000042069420694206942069' as const
const { assetChanges, results } = await simulateCalls(mainnetClient, {
account,
assetChanges: true,
traceAssetChanges: true,
calls: [
{
to: accounts[1].address,
Expand Down Expand Up @@ -384,3 +384,36 @@ test('behavior: stress', async () => {
calls,
})
})

test('behavior: account not provided with traceAssetChanges', async () => {
await expect(() =>
simulateCalls(mainnetClient, {
traceAssetChanges: true,
calls: [
{
to: accounts[1].address,
value: parseEther('1'),
},
{
to: accounts[2].address,
value: parseEther('1'),
},
{
abi: wagmiContractConfig.abi,
functionName: 'mint',
to: wagmiContractConfig.address,
},
{
abi: erc20Abi,
functionName: 'transfer',
to: '0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE',
args: [accounts[1].address, parseEther('1')],
},
],
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[BaseError: \`account\` is required when \`traceAssetChanges\` is true
Version: [email protected]]
`)
})
35 changes: 17 additions & 18 deletions src/actions/public/simulateCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export type SimulateCallsParameters<
> = Omit<SimulateBlocksParameters, 'blocks' | 'returnFullTransactions'> & {
/** Account attached to the calls (msg.sender). */
account?: account | undefined
/** Whether to trace asset changes. */
assetChanges?: boolean | undefined
/** Calls to simulate. */
calls: Calls<Narrow<calls>>
/** State overrides. */
stateOverrides?: StateOverride | undefined
/** Whether to trace asset changes. */
traceAssetChanges?: boolean | undefined
}

export type SimulateCallsReturnType<
Expand All @@ -57,8 +57,8 @@ export type SimulateCallsReturnType<
assetChanges: readonly {
token: {
address: Address
decimals: number
symbol: string
decimals?: number | undefined
symbol?: string | undefined
}
value: { pre: bigint; post: bigint; diff: bigint }
}[]
Expand Down Expand Up @@ -130,11 +130,11 @@ export async function simulateCalls<
parameters: SimulateCallsParameters<calls, account>,
): Promise<SimulateCallsReturnType<calls>> {
const {
assetChanges,
blockNumber,
blockTag,
calls,
stateOverrides,
traceAssetChanges,
traceTransfers,
validation,
} = parameters
Expand All @@ -143,8 +143,10 @@ export async function simulateCalls<
? parseAccount(parameters.account)
: undefined

if (assetChanges && !account)
throw new BaseError('`account` is required when `assetChanges` is true')
if (traceAssetChanges && !account)
throw new BaseError(
'`account` is required when `traceAssetChanges` is true',
)

// Derive bytecode to extract ETH balance via a contract call.
const getBalanceData = account
Expand All @@ -161,7 +163,7 @@ export async function simulateCalls<
: undefined

// Fetch ERC20/721 addresses that were "touched" from the calls.
const assetAddresses = assetChanges
const assetAddresses = traceAssetChanges
? await Promise.all(
parameters.calls.map(async (call: any) => {
if (!call.data && !call.abi) return
Expand Down Expand Up @@ -190,7 +192,7 @@ export async function simulateCalls<
blockNumber,
blockTag: blockTag as undefined,
blocks: [
...(assetChanges
...(traceAssetChanges
? [
// ETH pre balances
{
Expand Down Expand Up @@ -231,7 +233,7 @@ export async function simulateCalls<
stateOverrides: resultsStateOverrides,
},

...(assetChanges
...(traceAssetChanges
? [
// ETH post balances
{
Expand Down Expand Up @@ -324,7 +326,7 @@ export async function simulateCalls<
validation,
})

const block_results = assetChanges ? blocks[2] : blocks[0]
const block_results = traceAssetChanges ? blocks[2] : blocks[0]
const [
block_ethPre,
block_assetsPre,
Expand All @@ -334,7 +336,7 @@ export async function simulateCalls<
block_decimals,
block_tokenURI,
block_symbols,
] = assetChanges ? blocks : []
] = traceAssetChanges ? blocks : []

// Extract call results from the simulation.
const { calls: block_calls, ...block } = block_results
Expand Down Expand Up @@ -383,14 +385,11 @@ export async function simulateCalls<
decimals: 18,
symbol: 'ETH',
}
if ((!decimals_ && !tokenURI_) || !symbol_)
throw new BaseError(
'cannot simulate asset changes. asset does not comply with ERC20 or ERC721 standard.',
)

return {
address: assetAddresses[i - 1]! as Address,
decimals: Number(decimals_ ?? 1),
symbol: symbol_,
decimals: tokenURI_ || decimals_ ? Number(decimals_ ?? 1) : undefined,
symbol: symbol_ ?? undefined,
}
})()

Expand Down

0 comments on commit 88dcd26

Please sign in to comment.