From 0dbbcdd9c2d86356a8064f7f89b476beb18ebde4 Mon Sep 17 00:00:00 2001 From: hotwater Date: Thu, 11 Apr 2024 20:58:27 +0300 Subject: [PATCH 1/2] feat: upgrade module query --- .../actions/account/wallet/upgrade-module.ts | 36 ++++++++++++++++++ packages/core/src/actions/index.ts | 1 + .../src/clients/decorators/account-wallet.ts | 14 +++++++ .../react/src/hooks/account/wallet/index.ts | 1 + .../account/wallet/use-upgrade-module.ts | 37 +++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 packages/core/src/actions/account/wallet/upgrade-module.ts create mode 100644 packages/react/src/hooks/account/wallet/use-upgrade-module.ts diff --git a/packages/core/src/actions/account/wallet/upgrade-module.ts b/packages/core/src/actions/account/wallet/upgrade-module.ts new file mode 100644 index 00000000..478839a1 --- /dev/null +++ b/packages/core/src/actions/account/wallet/upgrade-module.ts @@ -0,0 +1,36 @@ +import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' +import { ManagerClient, VersionControlTypes } from '../../../codegen/abstract' +import { WithCosmWasmSignOptions } from '../../../types/parameters' +import { getManagerClientFromApi } from './get-manager-client-from-api' + +export type UpgradeModuleParameters = WithCosmWasmSignOptions< + { + accountId: VersionControlTypes.AccountId + signingCosmWasmClient: SigningCosmWasmClient + apiUrl: string + sender: string + } & Omit< + Parameters[0], + 'accountId' + > & { subAccountId?: number } +> + +export async function upgradeModule({ + accountId, + subAccountId, + signingCosmWasmClient, + apiUrl, + sender, + fee, + memo, + funds, + ...rest +}: UpgradeModuleParameters) { + const managerClient = await getManagerClientFromApi({ + accountId, + signingCosmWasmClient, + sender, + apiUrl, + }) + return managerClient.upgrade(rest, fee, memo, funds) +} diff --git a/packages/core/src/actions/index.ts b/packages/core/src/actions/index.ts index 5384927d..d7ad02fc 100644 --- a/packages/core/src/actions/index.ts +++ b/packages/core/src/actions/index.ts @@ -30,6 +30,7 @@ export * from './account/wallet/execute' export * from './account/wallet/get-manager-client-from-api' export * from './account/wallet/get-proxy-client-from-api' export * from './account/wallet/remove-namespace' +export * from './account/wallet/upgrade-module' export * from './account/wallet/withdraw' export * from './public/get-abstract-module-address-from-version-control' diff --git a/packages/core/src/clients/decorators/account-wallet.ts b/packages/core/src/clients/decorators/account-wallet.ts index aed8669e..0fc8bfdd 100644 --- a/packages/core/src/clients/decorators/account-wallet.ts +++ b/packages/core/src/clients/decorators/account-wallet.ts @@ -6,6 +6,7 @@ import { execute } from '../../actions/account/wallet/execute' import { getManagerClientFromApi } from '../../actions/account/wallet/get-manager-client-from-api' import { getProxyClientFromApi } from '../../actions/account/wallet/get-proxy-client-from-api' import { revokeNamespace } from '../../actions/account/wallet/remove-namespace' +import { upgradeModule } from '../../actions/account/wallet/upgrade-module' import { withdraw } from '../../actions/account/wallet/withdraw' import { VersionControlTypes } from '../../codegen/abstract/index' import { ExtractAndOmitParameters } from '../../types/parameters' @@ -49,6 +50,11 @@ export type AccountWalletActions = { typeof revokeNamespace >, ): ReturnType + upgradeModule( + parameters: ExtractAndOmitDecoratedParametersFromParameters< + typeof upgradeModule + >, + ): ReturnType withdraw( parameters: ExtractAndOmitDecoratedParametersFromParameters< typeof withdraw @@ -95,6 +101,14 @@ export function accountWalletActions( apiUrl, sender, }), + upgradeModule: (parameters) => + upgradeModule({ + ...parameters, + accountId, + signingCosmWasmClient, + apiUrl, + sender, + }), withdraw: (parameters) => withdraw({ ...parameters, diff --git a/packages/react/src/hooks/account/wallet/index.ts b/packages/react/src/hooks/account/wallet/index.ts index 3a8d89f2..62238087 100644 --- a/packages/react/src/hooks/account/wallet/index.ts +++ b/packages/react/src/hooks/account/wallet/index.ts @@ -1,4 +1,5 @@ export * from './use-deposit' export * from './use-create-sub-account' export * from './use-execute' +export * from './use-upgrade-module' export * from './use-withdraw' diff --git a/packages/react/src/hooks/account/wallet/use-upgrade-module.ts b/packages/react/src/hooks/account/wallet/use-upgrade-module.ts new file mode 100644 index 00000000..35e0de67 --- /dev/null +++ b/packages/react/src/hooks/account/wallet/use-upgrade-module.ts @@ -0,0 +1,37 @@ +import { AccountWalletClient } from '@abstract-money/core/clients' +import { AccountId } from '@abstract-money/core/utils' +import { useMutation } from '@tanstack/react-query' +import { useConfig } from '../../../contexts' +import { ExtractArgsFromParameters } from '../../../types/args' +import { UseMutationParameters } from '../../../types/queries' + +type UpgradeModuleMutation = ExtractArgsFromParameters< + Parameters[0] +> + +export type UseUpgradeModuleParameters = { + accountId: AccountId | undefined + chainName: string | undefined + mutation?: UseMutationParameters< + Awaited>, + unknown, + UpgradeModuleMutation + > +} + +export function useUpgradeModule({ + accountId, + chainName, + mutation, +}: UseUpgradeModuleParameters) { + const config = useConfig() + const walletClient = config.useAccountWalletClient({ + chainName, + accountId, + }) + + return useMutation(({ args, ...cosmWasmSignOptions }) => { + if (!walletClient) throw new Error('walletClient is not defined') + return walletClient.upgradeModule({ ...cosmWasmSignOptions, ...args }) + }, mutation) +} From 5f849f4de8aace7b8bc21b20a876236376042b38 Mon Sep 17 00:00:00 2001 From: hotwater Date: Thu, 11 Apr 2024 20:58:54 +0300 Subject: [PATCH 2/2] chore: changesets --- .changeset/good-pets-cover.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/good-pets-cover.md diff --git a/.changeset/good-pets-cover.md b/.changeset/good-pets-cover.md new file mode 100644 index 00000000..6dfbac4a --- /dev/null +++ b/.changeset/good-pets-cover.md @@ -0,0 +1,6 @@ +--- +"@abstract-money/core": patch +"@abstract-money/react": patch +--- + +Added query and action to upgrade a module.