-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: `useWatchAsset` hook * test: add tests * docs: add docs * nit: use `getConnectorClient` * fix: types * test: fix * test: fix * test: fix * chore: tweaks * chore: changeset --------- Co-authored-by: Tom Meagher <tom@meagher.co>
- Loading branch information
Showing
27 changed files
with
577 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@wagmi/core": minor | ||
--- | ||
|
||
Added `watchAsset` action. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wagmi": minor | ||
--- | ||
|
||
Added `useWatchAsset` hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { config } from '@wagmi/test' | ||
import { expect, test } from 'vitest' | ||
|
||
import { connect } from './connect.js' | ||
import { disconnect } from './disconnect.js' | ||
import { watchAsset } from './watchAsset.js' | ||
|
||
const connector = config.connectors[0]! | ||
|
||
test('default', async () => { | ||
await connect(config, { connector }) | ||
await expect( | ||
watchAsset(config, { | ||
type: 'ERC20', | ||
options: { | ||
address: '0x0000000000000000000000000000000000000000', | ||
symbol: 'NULL', | ||
decimals: 18, | ||
}, | ||
}), | ||
).resolves.toMatchInlineSnapshot('true') | ||
await disconnect(config, { connector }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
type WatchAssetErrorType as viem_WatchAssetErrorType, | ||
type WatchAssetParameters as viem_WatchAssetParameters, | ||
type WatchAssetReturnType as viem_WatchAssetReturnType, | ||
watchAsset as viem_watchAsset, | ||
} from 'viem/actions' | ||
|
||
import type { Config } from '../createConfig.js' | ||
import type { BaseErrorType, ErrorType } from '../errors/base.js' | ||
import type { ConnectorParameter } from '../types/properties.js' | ||
import type { Compute } from '../types/utils.js' | ||
import { getAction } from '../utils/getAction.js' | ||
import { | ||
type GetConnectorClientErrorType, | ||
getConnectorClient, | ||
} from './getConnectorClient.js' | ||
|
||
export type WatchAssetParameters = Compute< | ||
viem_WatchAssetParameters & ConnectorParameter | ||
> | ||
|
||
export type WatchAssetReturnType = viem_WatchAssetReturnType | ||
|
||
export type WatchAssetErrorType = | ||
// getConnectorClient() | ||
| GetConnectorClientErrorType | ||
// base | ||
| BaseErrorType | ||
| ErrorType | ||
// viem | ||
| viem_WatchAssetErrorType | ||
|
||
/** https://wagmi.sh/core/api/actions/watchAsset */ | ||
export async function watchAsset( | ||
config: Config, | ||
parameters: WatchAssetParameters, | ||
): Promise<WatchAssetReturnType> { | ||
const { connector, ...rest } = parameters | ||
|
||
const client = await getConnectorClient(config, { connector }) | ||
|
||
const action = getAction(client, viem_watchAsset, 'watchAsset') | ||
return action(rest as viem_WatchAssetParameters) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { config } from '@wagmi/test' | ||
import { expect, test } from 'vitest' | ||
|
||
import { watchAssetMutationOptions } from './watchAsset.js' | ||
|
||
test('default', () => { | ||
expect(watchAssetMutationOptions(config)).toMatchInlineSnapshot(` | ||
{ | ||
"mutationFn": [Function], | ||
"mutationKey": [ | ||
"watchAsset", | ||
], | ||
} | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { MutationOptions } from '@tanstack/query-core' | ||
|
||
import { | ||
type WatchAssetErrorType, | ||
type WatchAssetParameters, | ||
type WatchAssetReturnType, | ||
watchAsset, | ||
} from '../actions/watchAsset.js' | ||
import type { Config } from '../createConfig.js' | ||
import type { Compute } from '../types/utils.js' | ||
import type { Mutate, MutateAsync } from './types.js' | ||
|
||
export function watchAssetMutationOptions(config: Config) { | ||
return { | ||
mutationFn(variables) { | ||
return watchAsset(config, variables) | ||
}, | ||
mutationKey: ['watchAsset'], | ||
} as const satisfies MutationOptions< | ||
WatchAssetData, | ||
WatchAssetErrorType, | ||
WatchAssetVariables | ||
> | ||
} | ||
|
||
export type WatchAssetData = WatchAssetReturnType | ||
|
||
export type WatchAssetVariables = Compute<WatchAssetParameters> | ||
|
||
export type WatchAssetMutate<context = unknown> = Mutate< | ||
WatchAssetData, | ||
WatchAssetErrorType, | ||
WatchAssetVariables, | ||
context | ||
> | ||
|
||
export type WatchAssetMutateAsync<context = unknown> = MutateAsync< | ||
WatchAssetData, | ||
WatchAssetErrorType, | ||
WatchAssetVariables, | ||
context | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import type { WatchAssetErrorType } from '@wagmi/core' | ||
import type { WatchAssetVariables } from '@wagmi/core/query' | ||
import { expectTypeOf, test } from 'vitest' | ||
|
||
import { useWatchAsset } from './useWatchAsset.js' | ||
|
||
const tokenInfo = { | ||
address: '0x0000000000000000000000000000000000000000', | ||
symbol: 'NULL', | ||
decimals: 18, | ||
} | ||
const contextValue = { foo: 'bar' } as const | ||
|
||
test('context', () => { | ||
const { context, data, error, watchAsset, variables } = useWatchAsset({ | ||
mutation: { | ||
onMutate(variables) { | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
return contextValue | ||
}, | ||
onError(error, variables, context) { | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
expectTypeOf(error).toEqualTypeOf<WatchAssetErrorType>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>() | ||
}, | ||
onSuccess(data, variables, context) { | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
expectTypeOf(data).toEqualTypeOf<boolean>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue>() | ||
}, | ||
onSettled(data, error, variables, context) { | ||
expectTypeOf(data).toEqualTypeOf<boolean | undefined>() | ||
expectTypeOf(error).toEqualTypeOf<WatchAssetErrorType | null>() | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>() | ||
}, | ||
}, | ||
}) | ||
|
||
expectTypeOf(data).toEqualTypeOf<boolean | undefined>() | ||
expectTypeOf(error).toEqualTypeOf<WatchAssetErrorType | null>() | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables | undefined>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>() | ||
|
||
watchAsset( | ||
{ type: 'ERC20', options: tokenInfo }, | ||
{ | ||
onError(error, variables, context) { | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
expectTypeOf(error).toEqualTypeOf<WatchAssetErrorType>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>() | ||
}, | ||
onSuccess(data, variables, context) { | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
expectTypeOf(data).toEqualTypeOf<boolean>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue>() | ||
}, | ||
onSettled(data, error, variables, context) { | ||
expectTypeOf(data).toEqualTypeOf<boolean | undefined>() | ||
expectTypeOf(error).toEqualTypeOf<WatchAssetErrorType | null>() | ||
expectTypeOf(variables).toEqualTypeOf<WatchAssetVariables>() | ||
expectTypeOf(context).toEqualTypeOf<typeof contextValue | undefined>() | ||
}, | ||
}, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { connect, disconnect } from '@wagmi/core' | ||
import { config } from '@wagmi/test' | ||
import { renderHook, waitFor } from '@wagmi/test/react' | ||
import { expect, test } from 'vitest' | ||
|
||
import { useWatchAsset } from './useWatchAsset.js' | ||
|
||
const connector = config.connectors[0]! | ||
|
||
const tokenInfo = { | ||
address: '0x0000000000000000000000000000000000000000', | ||
symbol: 'NULL', | ||
decimals: 18, | ||
} | ||
|
||
test('default', async () => { | ||
await connect(config, { connector }) | ||
|
||
const { result } = renderHook(() => useWatchAsset()) | ||
|
||
result.current.watchAsset({ type: 'ERC20', options: tokenInfo }) | ||
await waitFor(() => expect(result.current.isSuccess).toBeTruthy()) | ||
|
||
expect(result.current.data).toEqual(true) | ||
|
||
await disconnect(config, { connector }) | ||
}) |
Oops, something went wrong.