Skip to content

Commit

Permalink
fix: getClient property passthrough (#3571)
Browse files Browse the repository at this point in the history
* bugfix: batch size didnt work from createConfig

* chroe: remove config

* refactor: make dynamic

* chore: changeset

---------

Co-authored-by: 0xRoy <1997roylee@gmail.com>
  • Loading branch information
tmm and 1997roylee committed Feb 6, 2024
1 parent 387269a commit 7c6618e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-weeks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": patch
---

Fixed `getClient` passthrough properties from `createConfig`.
70 changes: 70 additions & 0 deletions packages/core/src/createConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,73 @@ test('behavior: migrate chainId', async () => {
}
`)
})

test('behavior: properties passed through to Viem Client via getClient', () => {
{
const properties = {
batch: {
multicall: {
batchSize: 102_400,
},
},
cacheTime: 5_000,
pollingInterval: 1_000,
}

const config = createConfig({
chains: [mainnet, optimism],
transports: {
[mainnet.id]: http(),
[optimism.id]: http(),
},
...properties,
})

const {
account: _a,
chain: _c,
extend: _e,
key: _k,
name: _n,
request: _r,
transport: _tr,
uid: _u,
type: _ty,
...rest
} = config.getClient()
expect(rest).toEqual(properties)
}

{
const config = createConfig({
chains: [mainnet, optimism],
transports: {
[mainnet.id]: http(),
[optimism.id]: http(),
},
batch: {
[mainnet.id]: {
multicall: {
batchSize: 1024,
},
},
},
})

const client = config.getClient()
expect(client.batch).toMatchInlineSnapshot(`
{
"multicall": {
"batchSize": 1024,
},
}
`)

const client2 = config.getClient({ chainId: optimism.id })
expect(client2.batch).toMatchInlineSnapshot(`
{
"multicall": true,
}
`)
}
})
16 changes: 14 additions & 2 deletions packages/core/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,29 @@ export function createConfig<
if (rest.client) client = rest.client({ chain })
else {
const chainId = chain.id as chains[number]['id']
const chainIds = chains.map((x) => x.id)
// Grab all properties off `rest` and resolve for use in `createClient`
const properties: Partial<viem_ClientConfig> = {}
const entries = Object.entries(rest) as [keyof typeof rest, any][]

for (const [key, value] of entries) {
if (key === 'client' || key === 'connectors' || key === 'transports')
continue
else {
if (typeof value === 'object') properties[key] = value[chainId]
else properties[key] = value
if (typeof value === 'object') {
// check if value is chainId-specific since some values can be objects
// e.g. { batch: { multicall: { batchSize: 1024 } } }
if (chainId in value) properties[key] = value[chainId]
else {
// check if value is chainId-specific, but does not have value for current chainId
const hasChainSpecificValue = chainIds.some((x) => x in value)
if (hasChainSpecificValue) continue
properties[key] = value
}
} else properties[key] = value
}
}

client = createClient({
...properties,
chain,
Expand Down

0 comments on commit 7c6618e

Please sign in to comment.