Skip to content

Commit

Permalink
Update keys to include account id
Browse files Browse the repository at this point in the history
  • Loading branch information
adairrr committed Jun 17, 2024
1 parent 5e118ba commit 1c784b5
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/lemon-chicken-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@abstract-money/core": patch
"@abstract-money/react": patch
---

Update keys to include account id
5 changes: 5 additions & 0 deletions .changeset/old-wolves-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@abstract-money/react": patch
---

Add chainName to query key
11 changes: 8 additions & 3 deletions packages/core/src/clients/create-account-public-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { VersionControlTypes } from '../codegen/abstract/index'
import type { Evaluate } from '../types/utils'
import { ABSTRACT_API_URL } from '../utils'
import {
ABSTRACT_API_URL,
accountIdToString,
versionControlAccountIdToString,
} from '../utils'
import { type Client } from './create-client'
import { PublicClientConfig, createPublicClient } from './create-public-client'
import {
Expand All @@ -17,9 +21,10 @@ export type AccountPublicClient = Evaluate<Client<AccountPublicActions>>
export function createAccountPublicClient(
parameters: AccountPublicClientConfig,
): AccountPublicClient {
const accountIdString = versionControlAccountIdToString(parameters.accountId)
const {
key = 'account-public',
name = 'AccountPublic Client',
key = `account-public-${accountIdString}`,
name = `AccountPublic Client (${accountIdString})`,
accountId,
cosmWasmClient,
apiUrl = ABSTRACT_API_URL,
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/clients/create-account-wallet-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VersionControlTypes } from '../codegen/abstract/index'
import type { Evaluate } from '../types/utils'
import { ABSTRACT_API_URL } from '../utils'
import { ABSTRACT_API_URL, versionControlAccountIdToString } from '../utils'
import { type Client } from './create-client'
import { WalletClientConfig, createWalletClient } from './create-wallet-client'
import {
Expand All @@ -17,9 +17,11 @@ export type AccountWalletClient = Evaluate<Client<AccountWalletActions>>
export function createAccountWalletClient(
parameters: AccountWalletClientConfig,
): AccountWalletClient {
const accountIdString = versionControlAccountIdToString(parameters.accountId)
// TODO: the sender may need to be included in the URL
const {
key = 'account-wallet',
name = 'AccountWallet Client',
key = `account-wallet-${accountIdString}`,
name = `AccountWallet Client (${accountIdString})`,
accountId,
signingCosmWasmClient,
sender,
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/hooks/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './use-accounts-base-addresses-from-api'
export * from './use-account-factory-query-client-from-api'
export * from './use-account-factory-config-from-api'
export * from './use-remote-hosts'
export type MaybeChainName = string | undefined
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import React from 'react'
import { useConfig } from '../../contexts'
import { WithArgs } from '../../types/args'
import { UseQueryParameters, useQuery } from '../../types/queries'
import { MaybeChainName } from './index'

type QueryFnData = Awaited<ReturnType<PublicClient['getAbstractModuleVersion']>>

type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly [
'abstractModuleVersion',
MaybeChainName,
PublicClient | undefined,
UseAbstractModuleVersionParameters['args'],
Parameters<PublicClient['getAbstractModuleVersion']>[0]['extra'],
Expand All @@ -37,14 +39,15 @@ export function useAbstractModuleVersion({
chainName,
})
const queryKey = React.useMemo(
() => ['abstractModuleVersion', publicClient, args, extra] as const,
[publicClient, args, extra],
() =>
['abstractModuleVersion', chainName, publicClient, args, extra] as const,
[publicClient, chainName, args, extra],
)

const enabled = Boolean(publicClient && args && (query.enabled ?? true))

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
({ queryKey: [_, publicClient, args, extra] }) => {
({ queryKey: [_, _chainName, publicClient, args, extra] }) => {
if (!publicClient) throw new Error('No client')
if (!args) throw new Error('No args')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UseQueryReturnType,
useQuery,
} from '../../types/queries'
import { MaybeChainName } from './index'

type QueryFnData = Awaited<
ReturnType<
Expand All @@ -21,6 +22,7 @@ type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly [
'accountFactoryConfigFromApi',
MaybeChainName,
PublicClient | undefined,
(
| NonNullable<
Expand Down Expand Up @@ -53,14 +55,15 @@ export function useAccountFactoryConfigFromApi({
chainName,
})
const queryKey = React.useMemo(
() => ['accountFactoryConfigFromApi', publicClient, extra] as const,
[publicClient, extra],
() =>
['accountFactoryConfigFromApi', chainName, publicClient, extra] as const,
[publicClient, chainName, extra],
)

const enabled = Boolean(publicClient && (query.enabled ?? true))

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
async ({ queryKey: [_, publicClient, extra] }) => {
async ({ queryKey: [_, _chainName, publicClient, extra] }) => {
if (!publicClient) throw new Error('No client')

const accountFactoryQueryClient =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UseQueryReturnType,
useQuery,
} from '../../types/queries'
import { MaybeChainName } from './index'

type QueryFnData = Awaited<
ReturnType<PublicClient['getAccountFactoryQueryClientFromApi']>
Expand All @@ -17,6 +18,7 @@ type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly [
'accountFactoryQueryClientFromApi',
MaybeChainName,
PublicClient | undefined,
NonNullable<
Parameters<PublicClient['getAccountFactoryQueryClientFromApi']>[0]
Expand Down Expand Up @@ -45,14 +47,20 @@ export function useAccountFactoryQueryClientFromApi({
chainName,
})
const queryKey = React.useMemo(
() => ['accountFactoryQueryClientFromApi', publicClient, extra] as const,
[publicClient, extra],
() =>
[
'accountFactoryQueryClientFromApi',
chainName,
publicClient,
extra,
] as const,
[chainName, publicClient, extra],
)

const enabled = Boolean(publicClient && (query.enabled ?? true))

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
({ queryKey: [_, publicClient, extra] }) => {
({ queryKey: [_, _chainName, publicClient, extra] }) => {
if (!publicClient) throw new Error('No client')

return publicClient.getAccountFactoryQueryClientFromApi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
UseQueryReturnType,
useQuery,
} from '../../types/queries'
import { MaybeChainName } from './index'

type QueryFnData = Awaited<ReturnType<PublicClient['getAccountsBaseAddresses']>>

type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly [
'accountBaseAddresses',
MaybeChainName,
PublicClient | undefined,
WithArgs<Parameters<PublicClient['getAccountsBaseAddresses']>[0]>['args'],
(
Expand Down Expand Up @@ -49,16 +51,23 @@ export function useAccountsBaseAddressesFromApi({
chainName,
})
const queryKey = React.useMemo(
() => ['accountBaseAddresses', accountPublicClient, args, extra] as const,
[accountPublicClient, args, extra],
() =>
[
'accountBaseAddresses',
chainName,
accountPublicClient,
args,
extra,
] as const,
[accountPublicClient, chainName, args, extra],
)

const enabled = Boolean(
accountPublicClient && args && (query.enabled ?? true),
)

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
({ queryKey: [_, accountPublicClient, args, extra] }) => {
({ queryKey: [_, _chainName, accountPublicClient, args, extra] }) => {
if (!accountPublicClient || !args) throw new Error('No client or args')

return accountPublicClient.getAccountsBaseAddresses({ extra, ...args })
Expand Down
16 changes: 11 additions & 5 deletions packages/react/src/hooks/public/use-cosm-wasm-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import {
UseQueryReturnType,
useQuery,
} from '../../types/queries'
import { MaybeChainName } from './index'

type QueryFnData = Awaited<ReturnType<PublicClient['getCosmWasmClient']>>

type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly ['cosmWasmClient', PublicClient | undefined]
type QueryKey = readonly [
'cosmWasmClient',
MaybeChainName,
PublicClient | undefined,
]
type QueryResult = UseQueryReturnType<QueryData, QueryError>

type QueryOptions = UseQueryParameters<
Expand All @@ -35,13 +40,14 @@ export function useCosmWasmClient({
const publicClient = config.usePublicClient({ chainName })

const queryKey = React.useMemo(
() => ['cosmWasmClient', publicClient] as const,
[publicClient],
() => ['cosmWasmClient', chainName, publicClient] as const,
[chainName, publicClient],
)

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
({ queryKey: [_, publicClient] }) => {
if (!publicClient) throw new Error('client is not defined')
({ queryKey: [_, chainName, publicClient] }) => {
if (!publicClient)
throw new Error(`client is not defined for ${chainName}`)

return publicClient.getCosmWasmClient()
},
Expand Down
8 changes: 5 additions & 3 deletions packages/react/src/hooks/public/use-remote-hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
UseQueryReturnType,
useQuery,
} from '../../types/queries'
import { MaybeChainName } from './index'

type QueryFnData = Awaited<ReturnType<PublicClient['getRemoteHosts']>>

type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly [
'getRemoteHosts',
MaybeChainName,
PublicClient | undefined,
(
| NonNullable<Parameters<PublicClient['getRemoteHosts']>[0]>['extra']
Expand Down Expand Up @@ -42,14 +44,14 @@ export function useRemoteHosts({
chainName,
})
const queryKey = React.useMemo(
() => ['getRemoteHosts', publicClient, extra] as const,
[publicClient, extra],
() => ['getRemoteHosts', chainName, publicClient, extra] as const,
[chainName, publicClient, extra],
)

const enabled = Boolean(publicClient && (query.enabled ?? true))

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
({ queryKey: [_, publicClient, extra] }) => {
({ queryKey: [_, _chainName, publicClient, extra] }) => {
if (!publicClient) throw new Error('No client')

return publicClient.getRemoteHosts(extra ? { extra } : undefined)
Expand Down
10 changes: 6 additions & 4 deletions packages/react/src/hooks/public/use-smart-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
UseQueryReturnType,
useQuery,
} from '../../types/queries'
import { MaybeChainName } from './index'
import { useCosmWasmClient } from './use-cosm-wasm-client'

type QueryFnData = Record<string, unknown>
Expand All @@ -14,6 +15,7 @@ type QueryError = unknown
type QueryData = QueryFnData
type QueryKey = readonly [
'smartQuery',
MaybeChainName,
CosmWasmClient | undefined,
string,
Record<string, unknown>,
Expand Down Expand Up @@ -43,14 +45,14 @@ export function useSmartQuery<TData = QueryData>({
const { data: cosmWasmClient, isSuccess } = useCosmWasmClient({ chainName })

const queryKey = React.useMemo(
() => ['smartQuery', cosmWasmClient, address, queryMsg] as const,
[cosmWasmClient, address, queryMsg],
() => ['smartQuery', chainName, cosmWasmClient, address, queryMsg] as const,
[cosmWasmClient, chainName, address, queryMsg],
)

const queryFn = React.useCallback<QueryFunction<QueryFnData, QueryKey>>(
async ({ queryKey: [_, cosmWasmClient] }) => {
async ({ queryKey: [_, _chainName, cosmWasmClient] }) => {
if (!cosmWasmClient) {
throw new Error('client is not defined')
throw new Error(`client is not defined for ${chainName}`)
}
return cosmWasmClient.queryContractSmart(address, queryMsg)
},
Expand Down

0 comments on commit 1c784b5

Please sign in to comment.