diff --git a/.changeset/healthy-experts-flash.md b/.changeset/healthy-experts-flash.md new file mode 100644 index 00000000..50e44f73 --- /dev/null +++ b/.changeset/healthy-experts-flash.md @@ -0,0 +1,7 @@ +--- +"@abstract-money/core": minor +"@abstract-money/react": minor +"wagemos-graz-nextjs": patch +--- + +Rename instantiate2 methods to predict and remove 'base' from AccountAddress retrieval diff --git a/examples/wagemos-graz-nextjs/src/app/authz-osmosis/page.tsx b/examples/wagemos-graz-nextjs/src/app/authz-osmosis/page.tsx index b56ba099..88a74633 100644 --- a/examples/wagemos-graz-nextjs/src/app/authz-osmosis/page.tsx +++ b/examples/wagemos-graz-nextjs/src/app/authz-osmosis/page.tsx @@ -12,7 +12,7 @@ import { useCreateAccountMonarchy, useSignAndBroadcast, } from '@abstract-money/react' -import { useModuleInstantiate2Address } from '@abstract-money/react' +import { usePredictModuleAddress } from '@abstract-money/react' import { useAccount } from 'graz' import React, { useCallback, useEffect, useMemo } from 'react' import { Button } from '../../components/ui/button' @@ -50,7 +50,7 @@ export default function AuthzPage() { }) }, [account]) - const { data: savingsAppAddress } = useModuleInstantiate2Address({ + const { data: savingsAppAddress } = usePredictModuleAddress({ accountId: stringToAccountId(TEST_SAVINGS_ACCOUNT_ID, CHAIN_NAME), chainName: CHAIN_NAME, args: { diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index d805595f..7845ba10 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -137,7 +137,7 @@ ### Patch Changes -- [`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851) Thanks [@dalechyn](https://github.com/dalechyn)! - Added `useAccountsBaseAddressesFromApi` hook. +- [`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851) Thanks [@dalechyn](https://github.com/dalechyn)! - Added `useAccountsAddressesFromApi` hook. ## 1.1.0 diff --git a/packages/core/src/actions/account/public/get-account-instantiate2-address-from-api.ts b/packages/core/src/actions/account/public/get-account-instantiate2-address-from-api.ts deleted file mode 100644 index 137a7992..00000000 --- a/packages/core/src/actions/account/public/get-account-instantiate2-address-from-api.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { chainIdToName, getInstantiate2Address } from '@abstract-money/core' -import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate' -import { RegistryTypes } from '../../../codegen/abstract' -import { abstractModuleId } from '../../../utils/modules/abstract-module-id' -import { getRegistryAddressFromApi } from '../../get-registry-address-from-api' -import { getAppModuleCodeIdFromRegistry } from '../../public/get-app-module-code-id-from-registry' -import { getModuleFactoryAddressFromRegistry } from '../../public/get-module-factory-address-from-registry' -import { CommonModuleNames } from '../../public/types' - -export type GetAccountInstantiate2AddressFromApi = { - accountId: RegistryTypes.AccountId - cosmWasmClient: CosmWasmClient - apiUrl: string -} - -/** - * Retrieve the calculated manager init2 address from the api. - * @param accountId - * @param cosmWasmClient - * @param apiUrl - */ -export async function getAccountInstantiate2AddressFromApi({ - accountId, - cosmWasmClient, - apiUrl, -}: GetAccountInstantiate2AddressFromApi) { - const chainId = await cosmWasmClient.getChainId() - const chainName = chainIdToName(chainId) - - const registryAddress = await getRegistryAddressFromApi({ - apiUrl, - chainName, - }) - - const moduleFactoryAddress = await getModuleFactoryAddressFromRegistry({ - cosmWasmClient, - registryAddress, - }) - - const accountCodeId = await getAppModuleCodeIdFromRegistry({ - moduleId: abstractModuleId(CommonModuleNames.ACCOUNT), - version: 'latest', - cosmWasmClient, - registryAddress, - }) - - const moduleCodeDetails = await cosmWasmClient.getCodeDetails(accountCodeId) - - return getInstantiate2Address( - moduleFactoryAddress, - moduleCodeDetails.checksum, - { ...accountId, chainName }, - ) -} diff --git a/packages/core/src/actions/account/public/get-module-instantiate2-address-from-api.ts b/packages/core/src/actions/account/public/get-module-instantiate2-address-from-api.ts index 44652887..fd1542f0 100644 --- a/packages/core/src/actions/account/public/get-module-instantiate2-address-from-api.ts +++ b/packages/core/src/actions/account/public/get-module-instantiate2-address-from-api.ts @@ -1,7 +1,7 @@ import { ModuleId, chainIdToName, - getInstantiate2Address, + getInstantiate2AddressWithAccountId, } from '@abstract-money/core' import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate' import { RegistryTypes } from '../../../codegen/abstract' @@ -46,9 +46,9 @@ export async function getModuleInstantiate2AddressFromApi({ const moduleCodeDetails = await cosmWasmClient.getCodeDetails(moduleCodeId) - return getInstantiate2Address( + return getInstantiate2AddressWithAccountId( moduleFactoryAddress, moduleCodeDetails.checksum, - { ...accountId, chainName }, + accountId, ) } diff --git a/packages/core/src/actions/public/get-account-addresses-from-api.ts b/packages/core/src/actions/public/get-account-addresses-from-api.ts index bcbf9097..24e4d7fa 100644 --- a/packages/core/src/actions/public/get-account-addresses-from-api.ts +++ b/packages/core/src/actions/public/get-account-addresses-from-api.ts @@ -2,7 +2,7 @@ import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate' import { RegistryTypes } from '../../codegen/abstract' import { getRegistryQueryClientFromApi } from './get-registry-query-client-from-api' -export type GetAccountsBaseAddressesFromApiParameters = { +export type GetAccountsAddressesFromApiParameters = { accountIds: RegistryTypes.AccountId[] cosmWasmClient: CosmWasmClient apiUrl: string @@ -12,7 +12,7 @@ export async function getAccountAddressesFromApi({ accountIds, cosmWasmClient, apiUrl, -}: GetAccountsBaseAddressesFromApiParameters) { +}: GetAccountsAddressesFromApiParameters) { const registryQueryClient = await getRegistryQueryClientFromApi({ cosmWasmClient, apiUrl, diff --git a/packages/core/src/actions/public/get-account-instantiate2-address-from-api.ts b/packages/core/src/actions/public/get-account-instantiate2-address-from-api.ts new file mode 100644 index 00000000..70c23471 --- /dev/null +++ b/packages/core/src/actions/public/get-account-instantiate2-address-from-api.ts @@ -0,0 +1,78 @@ +import { + abstractModuleId, + chainIdToName, + getInstantiate2Address, + getInstantiate2AddressWithAccountId, +} from '@abstract-money/core' +import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate' +import { P, match } from 'ts-pattern' +import { RegistryTypes } from '../../codegen/abstract' +import { getRegistryAddressFromApi } from '../get-registry-address-from-api' +import { getAppModuleCodeIdFromRegistry } from './get-app-module-code-id-from-registry' +import { CommonModuleNames } from './types' + +export type GetAccountInstantiate2AddressFromApi = { + cosmWasmClient: CosmWasmClient + apiUrl: string + creator: string + salt: + | { + accountId: RegistryTypes.AccountId + } + | { + custom: string | Uint8Array + } +} + +/** + * Retrieve the calculated manager init2 address from the api. + * @param accountId + * @param cosmWasmClient + * @param apiUrl + * @param creator - the creator address + * @param salt - the (optional) salt to use for the address calculation. If not provided, will use the accountId salt. + */ +export async function getAccountInstantiate2AddressFromApi({ + cosmWasmClient, + apiUrl, + creator, + salt, +}: GetAccountInstantiate2AddressFromApi): Promise { + const chainId = await cosmWasmClient.getChainId() + const chainName = chainIdToName(chainId) + + const registryAddress = await getRegistryAddressFromApi({ + apiUrl, + chainName, + }) + + const accountCodeId = await getAppModuleCodeIdFromRegistry({ + moduleId: abstractModuleId(CommonModuleNames.ACCOUNT), + version: 'latest', + cosmWasmClient, + registryAddress, + }) + + const moduleCodeDetails = await cosmWasmClient.getCodeDetails(accountCodeId) + + return await match(salt) + .with( + { accountId: P.select() }, + async (accountId) => + await getInstantiate2AddressWithAccountId( + creator, + moduleCodeDetails.checksum, + accountId, + ), + ) + .with( + { custom: P.select() }, + async (customSalt) => + await getInstantiate2Address( + creator, + moduleCodeDetails.checksum, + customSalt, + ), + ) + .exhaustive() +} diff --git a/packages/core/src/clients/decorators/account-public.ts b/packages/core/src/clients/decorators/account-public.ts index 7aa1749f..5fb51f84 100644 --- a/packages/core/src/clients/decorators/account-public.ts +++ b/packages/core/src/clients/decorators/account-public.ts @@ -3,7 +3,6 @@ import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate' import { queryModule } from 'src/actions/account/public/query-module' import { getAccountAddressFromApi } from '../../actions/account/public/get-account-address-from-api' import { getAccountId } from '../../actions/account/public/get-account-id' -import { getAccountInstantiate2AddressFromApi } from '../../actions/account/public/get-account-instantiate2-address-from-api' import { getAccountQueryClientFromApi } from '../../actions/account/public/get-account-query-client-from-api' import { getAccountSettings } from '../../actions/account/public/get-account-settings' import { getModuleAddress } from '../../actions/account/public/get-module-address' @@ -59,16 +58,11 @@ export type AccountPublicActions = { typeof getModules >, ): ReturnType - getModuleInstantiate2Address( + predictModuleAddress( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof getModuleInstantiate2AddressFromApi >, ): ReturnType - getAccountInstantiate2Address( - parameters?: ExtractAndPartializeDecoratedParametersFromParameters< - typeof getAccountInstantiate2AddressFromApi - >, - ): ReturnType getNamespace( parameters?: ExtractAndPartializeDecoratedParametersFromParameters< typeof getNamespace @@ -164,7 +158,7 @@ export function accountPublicActions( ...parameters, ...extra, }), - getModuleInstantiate2Address: ({ extra, ...parameters }) => + predictModuleAddress: ({ extra, ...parameters }) => getModuleInstantiate2AddressFromApi({ accountId, cosmWasmClient, @@ -172,14 +166,6 @@ export function accountPublicActions( ...parameters, ...extra, }), - getAccountInstantiate2Address: ({ extra, ...parameters } = {}) => - getAccountInstantiate2AddressFromApi({ - accountId, - cosmWasmClient, - apiUrl, - ...parameters, - ...extra, - }), getNamespace: ({ extra, ...parameters } = {}) => getNamespace({ accountId, diff --git a/packages/core/src/clients/decorators/public.ts b/packages/core/src/clients/decorators/public.ts index bffd290d..c913724e 100644 --- a/packages/core/src/clients/decorators/public.ts +++ b/packages/core/src/clients/decorators/public.ts @@ -3,6 +3,7 @@ import { getSimulationResultFromApi } from '../../actions/get-simulation-result- import { getAbstractModuleAddressFromRegistry } from '../../actions/public/get-abstract-module-address-from-registry' import { getAbstractModuleVersion } from '../../actions/public/get-abstract-module-version' import { getAccountAddressesFromApi } from '../../actions/public/get-account-addresses-from-api' +import { getAccountInstantiate2AddressFromApi } from '../../actions/public/get-account-instantiate2-address-from-api' import { getAccountQueryClient } from '../../actions/public/get-account-query-client' import { getAnsHostAddressFromRegistry } from '../../actions/public/get-ans-host-address-from-registry' import { getAnsHostQueryClient } from '../../actions/public/get-ans-host-query-client' @@ -27,11 +28,16 @@ type ExtractAndPartializeDecoratedParametersFromParameters< * Also see {@link AbstractBaseActions} for more public query actions. */ export type PublicActions = { - getAccountsBaseAddresses( + getAccountsAddresses( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof getAccountAddressesFromApi >, ): ReturnType + predictAccountAddress( + parameters: ExtractAndPartializeDecoratedParametersFromParameters< + typeof getAccountInstantiate2AddressFromApi + >, + ): ReturnType getAbstractModuleVersion( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof getAbstractModuleVersion @@ -57,6 +63,7 @@ export type PublicActions = { typeof getRemoteHostsFromApi >, ): ReturnType + getSimulationResult( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof getSimulationResultFromApi @@ -74,13 +81,20 @@ export function publicActions( apiUrl: string, ): PublicActions { return { - getAccountsBaseAddresses: ({ extra, ...parameters }) => + getAccountsAddresses: ({ extra, ...parameters }) => getAccountAddressesFromApi({ cosmWasmClient, apiUrl, ...parameters, ...extra, }), + predictAccountAddress: ({ extra, ...parameters }) => + getAccountInstantiate2AddressFromApi({ + cosmWasmClient, + apiUrl, + ...parameters, + ...extra, + }), getAbstractModuleVersion: ({ extra, ...parameters }) => getAbstractModuleVersion({ cosmWasmClient, diff --git a/packages/core/src/utils/account-factory/parse-create-account-execute-result.ts b/packages/core/src/utils/account-factory/parse-create-account-execute-result.ts index ca026013..62bf69d0 100644 --- a/packages/core/src/utils/account-factory/parse-create-account-execute-result.ts +++ b/packages/core/src/utils/account-factory/parse-create-account-execute-result.ts @@ -1,23 +1,18 @@ import { ExecuteResult } from '@cosmjs/cosmwasm-stargate' -import { ACCOUNT_ID_CHAIN_DELIMITER, AccountId } from '../account-id' +import { + ACCOUNT_ID_CHAIN_DELIMITER, + AccountId, + accountIdToString, + stringToAccountId, +} from '../account-id' import { findAbstractAttribute } from '../events' export function parseCreateAccountExecuteResult( result: ExecuteResult, chainName: string, ) { - const seq = Number.parseInt( - findAbstractAttribute(result, 'account_sequence').value, - ) - const trace = findAbstractAttribute(result, 'trace').value - const accountId = { - chainName, - seq, - trace: - trace === 'local' || trace === undefined - ? 'local' - : { remote: trace.split(ACCOUNT_ID_CHAIN_DELIMITER) }, - } satisfies AccountId + const accountIdString = findAbstractAttribute(result, 'account_id').value + const accountId = stringToAccountId(accountIdString, chainName) const accountAddress = findAbstractAttribute(result, 'account_address').value return { accountId, accountAddress } diff --git a/packages/core/src/utils/encoding.ts b/packages/core/src/utils/encoding.ts index d3854cd5..8da1ad4e 100644 --- a/packages/core/src/utils/encoding.ts +++ b/packages/core/src/utils/encoding.ts @@ -12,11 +12,11 @@ export const toUint8Array = (text: string) => Uint8Array.from(Array.from(text).map((letter) => letter.charCodeAt(0))) export const toSha256 = async (text: string): Promise => { - const encoder = new TextEncoder() - const data = encoder.encode(text) + const data = toUtf8(text) const hash = await crypto.subtle.digest('SHA-256', data) return new Uint8Array(hash) } + export type EncodedMsg = { readonly typeUrl: string readonly value: any diff --git a/packages/core/src/utils/module-factory/get-instantiate2-address.test.ts b/packages/core/src/utils/module-factory/get-instantiate2-address.test.ts index f94f2795..5445d0fd 100644 --- a/packages/core/src/utils/module-factory/get-instantiate2-address.test.ts +++ b/packages/core/src/utils/module-factory/get-instantiate2-address.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { stringToAccountId } from '../account-id' -import { getInstantiate2Address } from './get-instantiate2-address' +import { getInstantiate2AddressWithAccountId } from './get-instantiate2-address' describe('getInstantiate2AccountAddress', () => { it('returns the correct address', async () => { @@ -11,7 +11,7 @@ describe('getInstantiate2AccountAddress', () => { '3f6fb5db7e9be94c6699c495535fd55884ac72e2babbcd90b5b41a41cce179ee' const accountId = stringToAccountId('osmosis-48', 'osmosis') - const result = await getInstantiate2Address( + const result = await getInstantiate2AddressWithAccountId( moduleFactoryAddress, checksum, accountId, diff --git a/packages/core/src/utils/module-factory/get-instantiate2-address.ts b/packages/core/src/utils/module-factory/get-instantiate2-address.ts index 71bbdb22..40d0c80c 100644 --- a/packages/core/src/utils/module-factory/get-instantiate2-address.ts +++ b/packages/core/src/utils/module-factory/get-instantiate2-address.ts @@ -1,32 +1,48 @@ import { instantiate2Address } from '@cosmjs/cosmwasm-stargate' -import { fromHex } from '@cosmjs/encoding' +import { fromHex, toUtf8 } from '@cosmjs/encoding' import { bech32 } from 'bech32' -import { AccountId, registryAccountIdToString } from '../account-id' +import { RegistryTypes } from '../../codegen/abstract' +import { registryAccountIdToString } from '../account-id' import { toSha256 } from '../encoding' const SALT_POSTFIX = 'abstract' -async function getAccountIdSalt(accountId: AccountId) { +async function getAccountIdSalt(accountId: RegistryTypes.AccountId) { const sha256 = await toSha256(registryAccountIdToString(accountId)) - const encoder = new TextEncoder() - return new Uint8Array([...sha256, ...encoder.encode(SALT_POSTFIX)]) + return new Uint8Array([...sha256, ...toUtf8(SALT_POSTFIX)]) } /** - * Returns the instantiate2 address for the given account factory address, codeIdChecksum and accountId - * @param moduleFactoryAddress - the module factory is the creator + * Returns the instantiate2 address for the given creator address, codeIdChecksum and accountId + * @param creatorAddress - address of the contract creator * @param codeIdChecksum - checksum of the code id of the contract expected to be instantiated * @param accountId */ +export async function getInstantiate2AddressWithAccountId( + creatorAddress: string, + codeIdChecksum: string, + accountId: RegistryTypes.AccountId, +) { + const salt = await getAccountIdSalt(accountId) + + return getInstantiate2Address(creatorAddress, codeIdChecksum, salt) +} + +/** + * Returns the instantiate2 address for the given creator address, codeIdChecksum and salt + * @param creatorAddress - address of the contract creator + * @param codeIdChecksum - checksum of the code id of the contract expected to be instantiated + * @param salt - the salt to use for the address calculation + */ export async function getInstantiate2Address( - moduleFactoryAddress: string, + creatorAddress: string, codeIdChecksum: string, - accountId: AccountId, + salt: string | Uint8Array, ) { const hexedChecksum = fromHex(codeIdChecksum) - const salt = await getAccountIdSalt(accountId) - const prefix = bech32.decode(moduleFactoryAddress).prefix + const saltBytes = typeof salt === 'string' ? toUtf8(salt) : salt + const prefix = bech32.decode(creatorAddress).prefix - return instantiate2Address(hexedChecksum, moduleFactoryAddress, salt, prefix) + return instantiate2Address(hexedChecksum, creatorAddress, saltBytes, prefix) } diff --git a/packages/react/CHANGELOG.md b/packages/react/CHANGELOG.md index 2818e2d0..50ff0f39 100644 --- a/packages/react/CHANGELOG.md +++ b/packages/react/CHANGELOG.md @@ -250,7 +250,7 @@ ### Patch Changes -- [`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851) Thanks [@dalechyn](https://github.com/dalechyn)! - Added `useAccountsBaseAddressesFromApi` hook. +- [`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851) Thanks [@dalechyn](https://github.com/dalechyn)! - Added `useAccountsAddressesFromApi` hook. - Updated dependencies [[`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851)]: - @abstract-money/core@1.1.1 diff --git a/packages/react/src/hooks/account/public/index.ts b/packages/react/src/hooks/account/public/index.ts index 01a335a2..388e59a3 100644 --- a/packages/react/src/hooks/account/public/index.ts +++ b/packages/react/src/hooks/account/public/index.ts @@ -1,7 +1,6 @@ export * from './use-account-address-from-api' -export * from './use-account-instantiate2-address' export * from './use-account-settings' -export * from './use-module-instantiate2-address' +export * from './use-predict-module-address' export * from './use-modules' export * from './use-remote-account-ids' export * from './use-remote-accounts' diff --git a/packages/react/src/hooks/account/public/use-account-instantiate2-address.ts b/packages/react/src/hooks/account/public/use-account-instantiate2-address.ts deleted file mode 100644 index 76e90ffc..00000000 --- a/packages/react/src/hooks/account/public/use-account-instantiate2-address.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { AccountPublicClient } from '@abstract-money/core/clients' -import { AccountId } from '@abstract-money/core/utils' -import { QueryFunction } from '@tanstack/react-query' -import React from 'react' -import { useConfig } from '../../../contexts' -import { WithArgs } from '../../../types/args' -import { - UseQueryParameters, - UseQueryReturnType, - useQuery, -} from '../../../types/queries' - -type QueryFnData = Awaited< - ReturnType -> - -type QueryError = unknown -type QueryData = QueryFnData -type QueryKey = readonly [ - 'accountInstantiate2Address', - AccountPublicClient | undefined, - ( - | NonNullable< - Parameters[0] - >['extra'] - ), -] - -type QueryOptions = UseQueryParameters< - QueryFnData, - QueryError, - TData, - QueryKey -> -type QueryResult = UseQueryReturnType - -export type UseAccountInstantiate2AddressParameters = - WithArgs< - Parameters[0] - > & { - query?: QueryOptions - chainName: string | undefined - accountId: AccountId | undefined - } -export function useAccountInstantiate2Address({ - accountId, - chainName, - extra, - query = {}, -}: UseAccountInstantiate2AddressParameters): QueryResult { - const config = useConfig() - const accountPublicClient = config.useAccountPublicClient({ - accountId, - chainName, - }) - const queryKey = React.useMemo( - () => ['accountInstantiate2Address', accountPublicClient, extra] as const, - [accountPublicClient, extra], - ) - - const enabled = Boolean(accountPublicClient && (query.enabled ?? true)) - - const queryFn = React.useCallback>( - ({ queryKey: [_, accountPublicClient, extra] }) => { - if (!accountPublicClient) throw new Error('No client') - - return accountPublicClient.getAccountInstantiate2Address( - extra ? { extra } : undefined, - ) - }, - [], - ) - - return useQuery({ queryKey, queryFn, ...query, enabled }) -} diff --git a/packages/react/src/hooks/account/public/use-module-instantiate2-address.ts b/packages/react/src/hooks/account/public/use-predict-module-address.ts similarity index 61% rename from packages/react/src/hooks/account/public/use-module-instantiate2-address.ts rename to packages/react/src/hooks/account/public/use-predict-module-address.ts index f4451e0d..7c3134f6 100644 --- a/packages/react/src/hooks/account/public/use-module-instantiate2-address.ts +++ b/packages/react/src/hooks/account/public/use-predict-module-address.ts @@ -11,19 +11,17 @@ import { } from '../../../types/queries' type QueryFnData = Awaited< - ReturnType + ReturnType > type QueryError = unknown type QueryData = QueryFnData type QueryKey = readonly [ - 'moduleInstantiate2Address', + 'predictModuleAddress', AccountPublicClient | undefined, - UseModuleInstantiate2AddressParameters['args'], + UsePredictModuleAddressParameters['args'], ( - | Parameters< - AccountPublicClient['getModuleInstantiate2Address'] - >[0]['extra'] + | Parameters[0]['extra'] | undefined ), ] @@ -36,30 +34,28 @@ type QueryOptions = UseQueryParameters< > type QueryResult = UseQueryReturnType -export type UseModuleInstantiate2AddressParameters = - WithArgs< - Parameters[0] - > & { - query?: QueryOptions - chainName: string | undefined - accountId: AccountId | undefined - } +export type UsePredictModuleAddressParameters = WithArgs< + Parameters[0] +> & { + query?: QueryOptions + chainName: string | undefined + accountId: AccountId | undefined +} -export function useModuleInstantiate2Address({ +export function usePredictModuleAddress({ args, accountId, chainName, extra, query = {}, -}: UseModuleInstantiate2AddressParameters): QueryResult { +}: UsePredictModuleAddressParameters): QueryResult { const config = useConfig() const accountPublicClient = config.useAccountPublicClient({ accountId, chainName, }) const queryKey = React.useMemo( - () => - ['moduleInstantiate2Address', accountPublicClient, args, extra] as const, + () => ['predictModuleAddress', accountPublicClient, args, extra] as const, [accountPublicClient, args, extra], ) @@ -72,7 +68,7 @@ export function useModuleInstantiate2Address({ if (!accountPublicClient) throw new Error('No client') if (!args) throw new Error('No args') - return accountPublicClient.getModuleInstantiate2Address({ + return accountPublicClient.predictModuleAddress({ ...args, ...extra, }) @@ -86,5 +82,10 @@ export function useModuleInstantiate2Address({ /** * @deprecated */ -const useModuleInstantiate2AddressFromApi = useModuleInstantiate2Address -export { useModuleInstantiate2AddressFromApi } +const useModuleInstantiate2AddressFromApi = usePredictModuleAddress +/** + * @deprecated + */ +const useModuleInstantiate2Address = usePredictModuleAddress + +export { useModuleInstantiate2AddressFromApi, useModuleInstantiate2Address } diff --git a/packages/react/src/hooks/public/index.ts b/packages/react/src/hooks/public/index.ts index e682bd12..0ddf0a23 100644 --- a/packages/react/src/hooks/public/index.ts +++ b/packages/react/src/hooks/public/index.ts @@ -1,7 +1,9 @@ export * from './use-cosm-wasm-client' export * from './use-smart-query' export * from './use-abstract-module-version' -export * from './use-accounts-base-addresses-from-api' +export * from './use-accounts-addresses-from-api' export * from './use-remote-hosts' export * from './use-simulate-remote-msg' +export * from './use-predict-account-address' + export type MaybeChainName = string | undefined diff --git a/packages/react/src/hooks/public/use-accounts-base-addresses-from-api.ts b/packages/react/src/hooks/public/use-accounts-addresses-from-api.ts similarity index 67% rename from packages/react/src/hooks/public/use-accounts-base-addresses-from-api.ts rename to packages/react/src/hooks/public/use-accounts-addresses-from-api.ts index 7d12980a..af9bb66e 100644 --- a/packages/react/src/hooks/public/use-accounts-base-addresses-from-api.ts +++ b/packages/react/src/hooks/public/use-accounts-addresses-from-api.ts @@ -10,7 +10,7 @@ import { } from '../../types/queries' import { MaybeChainName } from './index' -type QueryFnData = Awaited> +type QueryFnData = Awaited> type QueryError = unknown type QueryData = QueryFnData @@ -18,11 +18,9 @@ type QueryKey = readonly [ 'accountAddress', MaybeChainName, PublicClient | undefined, - WithArgs[0]>['args'], + WithArgs[0]>['args'], ( - | NonNullable< - Parameters[0] - >['extra'] + | NonNullable[0]>['extra'] | undefined ), ] @@ -33,18 +31,19 @@ type QueryOptions = Omit< > type QueryResult = UseQueryReturnType -export type UseAccountsBaseAddressesFromApiParameters = - WithArgs[0]> & { - chainName?: string | undefined - query?: QueryOptions - } +export type UseAccountsAddressesFromApiParameters = WithArgs< + Parameters[0] +> & { + chainName?: string | undefined + query?: QueryOptions +} -export function useAccountsBaseAddressesFromApi({ +export function useAccountsAddressesFromApi({ chainName, args, extra, query = {}, -}: UseAccountsBaseAddressesFromApiParameters): QueryResult { +}: UseAccountsAddressesFromApiParameters): QueryResult { const config = useConfig() const accountPublicClient = config.usePublicClient({ chainName, @@ -63,7 +62,7 @@ export function useAccountsBaseAddressesFromApi({ ({ queryKey: [_, _chainName, accountPublicClient, args, extra] }) => { if (!accountPublicClient || !args) throw new Error('No client or args') - return accountPublicClient.getAccountsBaseAddresses({ extra, ...args }) + return accountPublicClient.getAccountsAddresses({ extra, ...args }) }, [], ) diff --git a/packages/react/src/hooks/public/use-predict-account-address.ts b/packages/react/src/hooks/public/use-predict-account-address.ts new file mode 100644 index 00000000..a533b255 --- /dev/null +++ b/packages/react/src/hooks/public/use-predict-account-address.ts @@ -0,0 +1,69 @@ +import { PublicClient } from '@abstract-money/core/clients' +import { QueryFunction } from '@tanstack/react-query' +import React from 'react' +import { useConfig } from '../../contexts' +import { WithArgs } from '../../types/args' +import { + UseQueryParameters, + UseQueryReturnType, + useQuery, +} from '../../types/queries' + +type QueryFnData = Awaited> + +type QueryError = unknown +type QueryData = QueryFnData +type QueryKey = readonly [ + 'predictAccountAddress', + PublicClient | undefined, + WithArgs[0]>['args'], + ( + | NonNullable[0]>['extra'] + | undefined + ), +] + +type QueryOptions = UseQueryParameters< + QueryFnData, + QueryError, + TData, + QueryKey +> +type QueryResult = UseQueryReturnType + +export type UsePredictAccountAddressParameters = WithArgs< + Parameters[0] +> & { + query?: QueryOptions + chainName: string | undefined +} + +export function usePredictAccountAddress({ + chainName, + args, + extra, + query = {}, +}: UsePredictAccountAddressParameters): QueryResult { + const config = useConfig() + const publicClient = config.usePublicClient({ + chainName, + }) + const queryKey = React.useMemo( + () => ['predictAccountAddress', publicClient, args, extra] as const, + [publicClient, extra], + ) + + const enabled = Boolean(publicClient && (query.enabled ?? true)) + + const queryFn = React.useCallback>( + ({ queryKey: [_, publicClient, args, extra] }) => { + if (!publicClient) throw new Error('No client') + if (!args) throw new Error('No args') + + return publicClient.predictAccountAddress({ extra, ...args }) + }, + [], + ) + + return useQuery({ queryKey, queryFn, ...query, enabled }) +}