Skip to content

Commit

Permalink
fix: multicall return type (#436)
Browse files Browse the repository at this point in the history
* fix: fix multicall return type

* chore: changeset
  • Loading branch information
jxom committed Apr 30, 2023
1 parent 95cc5c6 commit 72ed656
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tall-pianos-cough.md
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed an issue where `multicall`'s return type was incorrectly flattening when `allowFailure: false`.
16 changes: 16 additions & 0 deletions contracts/src/GH434.sol
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.13;

contract GH434 {
function foo() public pure returns (uint256 a, bool b) {
return (42069, true);
}

function bar() public pure returns (string memory) {
return "hi";
}

function baz() public pure returns (uint256) {
return 69420;
}
}
47 changes: 47 additions & 0 deletions src/actions/public/multicall.test.ts
Expand Up @@ -6,16 +6,20 @@

import { mainnet } from '@wagmi/chains'
import { describe, expect, test } from 'vitest'
import gh434 from '../../../contracts/out/GH434.sol/GH434.json'
import { createPublicClient, http } from '../../clients/index.js'
import {
accounts,
address,
anvilChain,
deploy,
initialBlockNumber,
localHttpUrl,
publicClient,
usdcContractConfig,
} from '../../_test/index.js'
import { gh434ABI } from '../../_test/generated.js'
import type { Hex } from '../../types/index.js'
import { baycContractConfig, wagmiContractConfig } from '../../_test/abis.js'

import { multicall } from './multicall.js'
Expand Down Expand Up @@ -666,3 +670,46 @@ test('batchSize on client', async () => {
contracts,
})
})

describe('GitHub repros', () => {
test('https://github.com/wagmi-dev/viem/issues/434', async () => {
const { contractAddress } = await deploy({
abi: gh434ABI,
bytecode: gh434.bytecode.object as Hex,
account: accounts[0].address,
})

expect(
await multicall(publicClient, {
allowFailure: false,
blockNumber: initialBlockNumber,
contracts: [
{
abi: gh434ABI,
address: contractAddress!,
functionName: 'foo',
},
{
abi: gh434ABI,
address: contractAddress!,
functionName: 'bar',
},
{
abi: gh434ABI,
address: contractAddress!,
functionName: 'baz',
},
],
}),
).toMatchInlineSnapshot(`
[
[
42069n,
true,
],
"hi",
69420n,
]
`)
})
})
2 changes: 1 addition & 1 deletion src/actions/public/multicall.ts
Expand Up @@ -185,7 +185,7 @@ export async function multicall<
),
)

return results.flat().flatMap(({ returnData, success }, i) => {
return results.flat().map(({ returnData, success }, i) => {
const calls = chunkedCalls.flat()
const { callData } = calls[i]
const { abi, address, functionName, args } = contracts[i]
Expand Down

1 comment on commit 72ed656

@vercel
Copy link

@vercel vercel bot commented on 72ed656 Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.