From 1078fae09363757fbbe93e77d210f8c9d73db6a1 Mon Sep 17 00:00:00 2001 From: adairrr <32375605+adairrr@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:09:57 -0400 Subject: [PATCH 1/2] useClaimNamespace, useSendFunds --- .changeset/hip-feet-serve.md | 6 ++ .../src/actions/account/wallet/execute.ts | 15 +++-- .../src/actions/account/wallet/withdraw.ts | 30 +++++++++- .../src/clients/decorators/account-wallet.ts | 20 ++++++- .../account/wallet/use-claim-namespace.ts | 55 +++++++++++++++++++ .../account/wallet/use-install-modules.ts | 6 +- .../src/hooks/account/wallet/use-withdraw.ts | 23 +++++--- 7 files changed, 132 insertions(+), 23 deletions(-) create mode 100644 .changeset/hip-feet-serve.md create mode 100644 packages/react/src/hooks/account/wallet/use-claim-namespace.ts diff --git a/.changeset/hip-feet-serve.md b/.changeset/hip-feet-serve.md new file mode 100644 index 00000000..41e3e8c3 --- /dev/null +++ b/.changeset/hip-feet-serve.md @@ -0,0 +1,6 @@ +--- +"@abstract-money/core": minor +"@abstract-money/react": minor +--- + +Add useClaimnamespace and re-export send funds diff --git a/packages/core/src/actions/account/wallet/execute.ts b/packages/core/src/actions/account/wallet/execute.ts index 8c5015af..bf875405 100644 --- a/packages/core/src/actions/account/wallet/execute.ts +++ b/packages/core/src/actions/account/wallet/execute.ts @@ -8,13 +8,10 @@ import { CosmosMsgForEmpty } from '../../../codegen/abstract/cosmwasm-codegen/Ac import { WithCosmWasmSignOptions } from '../../../types/parameters' import { getAccountAddressFromApi } from '../public/get-account-address-from-api' -export type ExecuteParameters = Omit< - WithCosmWasmSignOptions< - BaseAccountWalletParameters & { - msgs: MaybeArray - } - >, - 'funds' +export type ExecuteParameters = WithCosmWasmSignOptions< + BaseAccountWalletParameters & { + msgs: MaybeArray + } > /** @@ -26,6 +23,7 @@ export type ExecuteParameters = Omit< * @param msgs * @param fee * @param memo + * @param funds - funds FROM the wallet */ export async function execute({ accountId, @@ -35,6 +33,7 @@ export async function execute({ msgs, fee, memo, + funds, }: ExecuteParameters) { const account = await getAccountAddressFromApi({ accountId, @@ -55,7 +54,7 @@ export async function execute({ sender: sender, contract: account, msg: toUtf8(JSON.stringify(_msg)), - funds: [], + funds, }), } diff --git a/packages/core/src/actions/account/wallet/withdraw.ts b/packages/core/src/actions/account/wallet/withdraw.ts index 26ca606d..1b890d60 100644 --- a/packages/core/src/actions/account/wallet/withdraw.ts +++ b/packages/core/src/actions/account/wallet/withdraw.ts @@ -4,14 +4,31 @@ import { getAccountAddressFromApi } from '../public/get-account-address-from-api import { execute } from './execute' import { BaseAccountWalletParameters } from './types' -export type WithdrawParameters = WithCosmWasmSignOptions< +export type SendFundsParameters = WithCosmWasmSignOptions< BaseAccountWalletParameters & { assets: Asset[] recipient: string } > -export async function withdraw({ +/** + * @deprecated use `SendFundsParameters` instead + */ +export type WithdrawParameters = SendFundsParameters + +/** + * Send funds or withdraw funds from the account. + * @param fee + * @param memo + * @param accountId + * @param signingCosmWasmClient + * @param apiUrl + * @param sender + * @param assets + * @param recipient + * @param funds - funds included from the WALLET. + */ +export async function sendFunds({ fee, memo, accountId, @@ -20,7 +37,8 @@ export async function withdraw({ sender, assets, recipient, -}: WithdrawParameters) { + funds, +}: SendFundsParameters) { const account = await getAccountAddressFromApi({ accountId, cosmWasmClient: signingCosmWasmClient, @@ -41,5 +59,11 @@ export async function withdraw({ msgs: transferMsgs, fee, memo, + funds, }) } + +/** + * @deprecated use `sendFunds` instead + */ +export const withdraw = sendFunds diff --git a/packages/core/src/clients/decorators/account-wallet.ts b/packages/core/src/clients/decorators/account-wallet.ts index 62c5e172..78cf5acd 100644 --- a/packages/core/src/clients/decorators/account-wallet.ts +++ b/packages/core/src/clients/decorators/account-wallet.ts @@ -19,7 +19,7 @@ import { updateInfo } from '../../actions/account/wallet/update-info' import { updateOwnership } from '../../actions/account/wallet/update-ownership' import { updateStatus } from '../../actions/account/wallet/update-status' import { upgradeModules } from '../../actions/account/wallet/upgrade-modules' -import { withdraw } from '../../actions/account/wallet/withdraw' +import { sendFunds, withdraw } from '../../actions/account/wallet/withdraw' import { RegistryTypes } from '../../codegen/abstract/index' import { ExtractAndPartializeParameters } from '../../types/parameters' @@ -54,11 +54,20 @@ export type AccountWalletActions = { typeof deposit >, ): ReturnType + /** + * @deprecated + * @see sendFunds + */ withdraw( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof withdraw >, ): ReturnType + sendFunds( + parameters: ExtractAndPartializeDecoratedParametersFromParameters< + typeof sendFunds + >, + ): ReturnType execute( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof execute @@ -283,6 +292,15 @@ export function accountWalletActions( ...parameters, ...extra, }), + sendFunds: ({ extra, ...parameters }) => + sendFunds({ + accountId, + signingCosmWasmClient, + apiUrl, + sender, + ...parameters, + ...extra, + }), updateStatus: ({ extra, ...parameters }) => updateStatus({ accountId, diff --git a/packages/react/src/hooks/account/wallet/use-claim-namespace.ts b/packages/react/src/hooks/account/wallet/use-claim-namespace.ts new file mode 100644 index 00000000..d3c3808c --- /dev/null +++ b/packages/react/src/hooks/account/wallet/use-claim-namespace.ts @@ -0,0 +1,55 @@ +import { AccountWalletClient } from '@abstract-money/core/clients' +import { AccountId } from '@abstract-money/core/utils' +import { ExecuteResult } from '@cosmjs/cosmwasm-stargate' +import { DeliverTxResponse } from '@cosmjs/stargate' +import { useMutation } from '@tanstack/react-query' +import { useConfig } from '../../../contexts' +import { ExtractArgsFromParameters } from '../../../types/args' +import { + UseMutationParameters, + UseMutationReturnType, +} from '../../../types/queries' + +type ClaimNamespaceMutation = ExtractArgsFromParameters< + Parameters[0] +> + +export type UseClaimNamespaceParameters = { + accountId: AccountId | undefined + chainName: string | undefined + mutation?: UseMutationParameters< + ExecuteResult, + unknown, + ClaimNamespaceMutation + > +} + +/** + * Execute a msg as the account. + * @param accountId + * @param chainName + * @param mutation + */ +export function useClaimNamespace({ + accountId, + chainName, + mutation, +}: UseClaimNamespaceParameters): UseMutationReturnType< + ExecuteResult, + unknown, + ClaimNamespaceMutation +> { + const config = useConfig() + const accountClient = config.useAccountWalletClient({ + chainName, + accountId, + }) + return useMutation( + ['claimNamespace', chainName, accountId], + ({ args, ...cosmWasmSignOptions }) => { + if (!accountClient) throw new Error('client is not defined') + return accountClient.claimNamespace({ ...cosmWasmSignOptions, ...args }) + }, + mutation, + ) +} diff --git a/packages/react/src/hooks/account/wallet/use-install-modules.ts b/packages/react/src/hooks/account/wallet/use-install-modules.ts index 33fdd18b..ee2c8927 100644 --- a/packages/react/src/hooks/account/wallet/use-install-modules.ts +++ b/packages/react/src/hooks/account/wallet/use-install-modules.ts @@ -1,6 +1,6 @@ import { AccountWalletClient } from '@abstract-money/core/clients' import { AccountId } from '@abstract-money/core/utils' -import { DeliverTxResponse } from '@cosmjs/stargate' +import { ExecuteResult } from '@cosmjs/cosmwasm-stargate' import { useMutation } from '@tanstack/react-query' import { useConfig } from '../../../contexts' import { ExtractArgsFromParameters } from '../../../types/args' @@ -16,7 +16,7 @@ type ExecuteMutation = ExtractArgsFromParameters< export type UseExecuteParameters = { accountId: AccountId | undefined chainName: string | undefined - mutation?: UseMutationParameters + mutation?: UseMutationParameters } /** @@ -30,7 +30,7 @@ export function useInstallModules({ chainName, mutation, }: UseExecuteParameters): UseMutationReturnType< - DeliverTxResponse, + ExecuteResult, unknown, ExecuteMutation > { diff --git a/packages/react/src/hooks/account/wallet/use-withdraw.ts b/packages/react/src/hooks/account/wallet/use-withdraw.ts index 769c6418..eb55327e 100644 --- a/packages/react/src/hooks/account/wallet/use-withdraw.ts +++ b/packages/react/src/hooks/account/wallet/use-withdraw.ts @@ -10,24 +10,26 @@ import { } from '../../../types/queries' type WithdrawMutation = ExtractArgsFromParameters< - Parameters[0] + Parameters[0] > -export type UseWithdrawParameters = { +export type UseSendFundsParameters = { accountId: AccountId | undefined chainName: string | undefined mutation?: UseMutationParameters } +export type UseWithdrawParameters = UseSendFundsParameters + /** - * Hook to withdraw to an Account. - * @param options withdraw options. + * Hook to send funds from an Account. + * @param options send funds options. */ -export function useWithdraw({ +export function useSendFunds({ accountId, chainName, mutation, -}: UseWithdrawParameters): UseMutationReturnType< +}: UseSendFundsParameters): UseMutationReturnType< DeliverTxResponse, unknown, WithdrawMutation @@ -38,11 +40,16 @@ export function useWithdraw({ accountId, }) return useMutation( - ['withdraw', chainName, accountId], + ['sendFunds', chainName, accountId], ({ args, ...cosmWasmSignOptions }) => { if (!accountClient) throw new Error('accountClient is not defined') - return accountClient.withdraw({ ...args, ...cosmWasmSignOptions }) + return accountClient.sendFunds({ ...args, ...cosmWasmSignOptions }) }, mutation, ) } + +/** + * @deprecated use `useSendFunds` instead. + */ +export const useWithdraw = useSendFunds From 511b01c719aca050dde2b982d08209ed9b10da65 Mon Sep 17 00:00:00 2001 From: adairrr <32375605+adairrr@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:12:07 -0400 Subject: [PATCH 2/2] Proper exports --- packages/react/src/hooks/account/wallet/index.ts | 4 +++- .../hooks/account/wallet/use-claim-namespace.ts | 2 +- .../hooks/account/wallet/use-install-modules.ts | 14 +++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/react/src/hooks/account/wallet/index.ts b/packages/react/src/hooks/account/wallet/index.ts index d8039067..a0790aea 100644 --- a/packages/react/src/hooks/account/wallet/index.ts +++ b/packages/react/src/hooks/account/wallet/index.ts @@ -1,3 +1,4 @@ +export * from './use-claim-namespace' export * from './use-create-remote-account' export * from './use-create-sub-account' export * from './use-deposit' @@ -6,8 +7,9 @@ export * from './use-execute' export * from './use-execute-on-remote' export * from './use-execute-on-remote-module' export * from './use-execute-remote' -export * from './use-send-funds-to-remote' +export * from './use-install-modules' export * from './use-request-remote-funds' +export * from './use-send-funds-to-remote' export * from './use-update-info' export * from './use-update-settings' export * from './use-update-status' diff --git a/packages/react/src/hooks/account/wallet/use-claim-namespace.ts b/packages/react/src/hooks/account/wallet/use-claim-namespace.ts index d3c3808c..bda132eb 100644 --- a/packages/react/src/hooks/account/wallet/use-claim-namespace.ts +++ b/packages/react/src/hooks/account/wallet/use-claim-namespace.ts @@ -25,7 +25,7 @@ export type UseClaimNamespaceParameters = { } /** - * Execute a msg as the account. + * Claim a namespace from version control on the account. * @param accountId * @param chainName * @param mutation diff --git a/packages/react/src/hooks/account/wallet/use-install-modules.ts b/packages/react/src/hooks/account/wallet/use-install-modules.ts index ee2c8927..27b50f6c 100644 --- a/packages/react/src/hooks/account/wallet/use-install-modules.ts +++ b/packages/react/src/hooks/account/wallet/use-install-modules.ts @@ -9,14 +9,18 @@ import { UseMutationReturnType, } from '../../../types/queries' -type ExecuteMutation = ExtractArgsFromParameters< +type InstallModulesMutation = ExtractArgsFromParameters< Parameters[0] > -export type UseExecuteParameters = { +export type UseInstallModulesParameters = { accountId: AccountId | undefined chainName: string | undefined - mutation?: UseMutationParameters + mutation?: UseMutationParameters< + ExecuteResult, + unknown, + InstallModulesMutation + > } /** @@ -29,10 +33,10 @@ export function useInstallModules({ accountId, chainName, mutation, -}: UseExecuteParameters): UseMutationReturnType< +}: UseInstallModulesParameters): UseMutationReturnType< ExecuteResult, unknown, - ExecuteMutation + InstallModulesMutation > { const config = useConfig() const accountClient = config.useAccountWalletClient({