Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: demonstrate callstack in extended client action #2680

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 45 additions & 13 deletions src/clients/createClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assertType, describe, expect, test, vi } from 'vitest'

import { anvilMainnet } from '../../test/src/anvil.js'
import { getChainId } from '../actions/public/getChainId.js'
import { localhost, mainnet } from '../chains/index.js'
import type { EIP1193RequestFn, EIP1474Methods } from '../types/eip1193.js'
import { getAction } from '../utils/getAction.js'
import { createClient } from './createClient.js'
import { type Client, createClient } from './createClient.js'
import { publicActions } from './decorators/public.js'
import { createTransport } from './transports/createTransport.js'
import { custom } from './transports/custom.js'
Expand Down Expand Up @@ -577,29 +576,62 @@ describe('extends', () => {
})

test('action composition', async () => {
const calls: string[] = []
const extended = anvilMainnet
.getClient()
let calls: string[] = []

async function getChainId(_client: Client) {
calls.push('getChainId:base')
return 1337
}

async function estimateGas(client: Client) {
calls.push('estimateGas:base')
await getAction(client, getChainId, 'getChainId')({})
return 1000n
}

const extended = createClient({
chain: localhost,
transport: http(),
})
.extend((client) => ({
async getChainId() {
calls.push('first')
return getAction(client, getChainId, 'getChainId')({})
},
getChainId: () => getChainId(client),
estimateGas: () => estimateGas(client),
}))
.extend((client) => ({
async getChainId() {
calls.push('second')
calls.push('getChainId:first')
return getAction(client, getChainId, 'getChainId')({})
},
async estimateGas() {
calls.push('estimateGas:first')
return getAction(client, estimateGas, 'estimateGas')({})
},
}))
.extend((client) => ({
async getChainId() {
calls.push('third')
calls.push('getChainId:second')
return getAction(client, getChainId, 'getChainId')({})
},
async estimateGas() {
calls.push('estimateGas:second')
return getAction(client, estimateGas, 'estimateGas')({})
},
}))

expect(await extended.getChainId()).toBe(anvilMainnet.chain.id)
expect(calls).toEqual(['third', 'second', 'first'])
expect(await extended.getChainId()).toBe(1337)
expect(calls).toEqual([
'getChainId:second',
'getChainId:first',
'getChainId:base',
])

calls = []
expect(await extended.estimateGas()).toBe(1000n)
expect(calls).toEqual([
'estimateGas:second',
'estimateGas:first',
'estimateGas:base',
'getChainId:base',
])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the functionality I was trying to understand, where I hoped that this would look more like

[
  'estimateGas:second',
  'estimateGas:first',
  'estimateGas:base',
  'getChainId:second',
  'getChainId:first',
  'getChainId:base',
]

Copy link
Member

@jxom jxom Aug 31, 2024

Choose a reason for hiding this comment

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

Could consider binding them (in Viem) to behave like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure of the consequences of that. Would that be a breaking change because it would change the behavior of the entire callstack of extended clients?

I was thinking of writing a separate "compose" decorator wrapper that could handle this.

})
})
Loading