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

pass client to waitForTransactionReceipt #3929

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
27 changes: 0 additions & 27 deletions packages/core/src/actions/getClient.test-d.ts

This file was deleted.

55 changes: 53 additions & 2 deletions packages/core/src/actions/getClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { config } from '@wagmi/test'
import { expect, test } from 'vitest'
import { chain, config } from '@wagmi/test'
import { expect, expectTypeOf, test } from 'vitest'

import { http } from 'viem'
import { waitForTransactionReceipt } from '../actions/waitForTransactionReceipt.js'
import { createConfig } from '../createConfig.js'
import { optimism, optimismSepolia } from '../exports/chains.js'
import { getClient } from './getClient.js'

test('default', () => {
Expand All @@ -15,3 +19,50 @@ test('behavior: unconfigured chain', () => {
}),
).toBeUndefined()
})

test('default', () => {
const client = getClient(config)
expectTypeOf(client.chain).toEqualTypeOf<typeof config['chains'][number]>()
expectTypeOf(client.transport.type).toEqualTypeOf<'http'>()
})

test('parameters: chainId', () => {
const client = getClient(config, {
chainId: chain.mainnet.id,
})
expectTypeOf(client.chain).toEqualTypeOf<typeof chain.mainnet>()
expectTypeOf(client.chain).not.toEqualTypeOf<typeof chain.mainnet2>()
expectTypeOf(client.transport.type).toEqualTypeOf<'http'>()
})

test('behavior: viem actions', () => {
const viemActionChains = [optimismSepolia, optimism] as const
const viemActionsTransports = {
[optimismSepolia.id]: http(),
[optimism.id]: http(),
} as const
const configObject = {
chains: viemActionChains,
transports: viemActionsTransports,
} as const

const config = createConfig(configObject)

const client = getClient(config)

waitForTransactionReceipt(
{
...config,
client,
},
{ hash: '0x…' },
)
})

test('behavior: unconfigured chain', () => {
const client = getClient(config, {
// @ts-expect-error
chainId: 123456,
})
expectTypeOf(client).toEqualTypeOf<undefined>()
})
17 changes: 14 additions & 3 deletions playgrounds/vite-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useBalance,
useBlockNumber,
useChainId,
useClient,
useConnect,
useConnections,
useConnectorClient,
Expand All @@ -24,6 +25,7 @@ import {
import { optimism } from 'wagmi/chains'

import { wagmiContractConfig } from './contracts'
import { ChainIds, config } from './wagmi'

function App() {
useAccountEffect({
Expand Down Expand Up @@ -138,7 +140,7 @@ function SwitchChain() {
<button
disabled={chainId === chain.id}
key={chain.id}
onClick={() => switchChain({ chainId: chain.id })}
onClick={() => switchChain({ chainId: chain.id as ChainIds })}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

un-related. Saw an error, fixed an error.

type="button"
>
{chain.name}
Expand Down Expand Up @@ -246,6 +248,8 @@ function ConnectorClient() {

function SendTransaction() {
const { data: hash, error, isPending, sendTransaction } = useSendTransaction()
const { data: connectorClientData } = useConnectorClient()
const client = useClient()

async function submit(e: FormEvent<HTMLFormElement>) {
e.preventDefault()
Expand All @@ -255,8 +259,15 @@ function SendTransaction() {
sendTransaction({ to, value: parseEther(value) })
}

const { isLoading: isConfirming, isSuccess: isConfirmed } =
const { isLoading: isConfirming, data: receipt } =
useWaitForTransactionReceipt({
config: {
...config,
// Additionally, we can pass a client to the config
// to use a different client than the one passed to the hook
client,
},
chainId: connectorClientData?.chain?.id,
Comment on lines +264 to +270
Copy link
Contributor Author

Choose a reason for hiding this comment

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

assuming this will help someone in the future when they run into / search this issue.

hash,
})

Expand All @@ -278,7 +289,7 @@ function SendTransaction() {
</form>
{hash && <div>Transaction Hash: {hash}</div>}
{isConfirming && 'Waiting for confirmation...'}
{isConfirmed && 'Transaction confirmed.'}
{`Transaction ${receipt?.status}`}
{error && (
<div>Error: {(error as BaseError).shortMessage || error.message}</div>
)}
Expand Down
5 changes: 4 additions & 1 deletion playgrounds/vite-react/src/wagmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const indexedDBStorage = {
},
}

const chains = [mainnet, sepolia, optimism, celo] as const
export type ChainIds = typeof chains[number]['id']

export const config = createConfig({
chains: [mainnet, sepolia, optimism, celo],
chains,
connectors: [
walletConnect({
projectId: import.meta.env.VITE_WC_PROJECT_ID,
Expand Down
Loading