diff --git a/.changeset/cuddly-pigs-rule.md b/.changeset/cuddly-pigs-rule.md new file mode 100644 index 00000000..2a3be957 --- /dev/null +++ b/.changeset/cuddly-pigs-rule.md @@ -0,0 +1,6 @@ +--- +"@abstract-money/core": minor +"@abstract-money/react": minor +--- + +Update remote execution diff --git a/examples/wagemos-cosmoskit-nextjs/src/app/remote/page.tsx b/examples/wagemos-cosmoskit-nextjs/src/app/remote/page.tsx index 376b365a..b4d86f7c 100644 --- a/examples/wagemos-cosmoskit-nextjs/src/app/remote/page.tsx +++ b/examples/wagemos-cosmoskit-nextjs/src/app/remote/page.tsx @@ -3,7 +3,7 @@ import { accountIdToString } from '@abstract-money/core' import { useAccounts, useCreateRemoteAccount, - useExecuteOnRemote, + useExecuteOnRemoteManager, useRemoteAccountIds, useRemoteHosts, } from '@abstract-money/react' @@ -54,10 +54,11 @@ export default function RemotePage() { chainName: firstAccount?.chainName, }) - const { mutate: execRemote, isLoading: isExecuting } = useExecuteOnRemote({ - accountId: firstAccount, - chainName: firstAccount?.chainName, - }) + const { mutate: execRemote, isLoading: isExecuting } = + useExecuteOnRemoteManager({ + accountId: firstAccount, + chainName: firstAccount?.chainName, + }) const { data: remoteAccountIds } = useRemoteAccountIds({ accountId: firstAccount, diff --git a/packages/core/src/actions/account/wallet/execute-on-remote.ts b/packages/core/src/actions/account/wallet/execute-on-remote-manager.ts similarity index 96% rename from packages/core/src/actions/account/wallet/execute-on-remote.ts rename to packages/core/src/actions/account/wallet/execute-on-remote-manager.ts index cb9d13eb..b239bd07 100644 --- a/packages/core/src/actions/account/wallet/execute-on-remote.ts +++ b/packages/core/src/actions/account/wallet/execute-on-remote-manager.ts @@ -29,7 +29,7 @@ export type ExecuteOnRemoteParameters = Omit< * @param fee * @param memo */ -export async function executeOnRemote({ +export async function executeOnRemoteManager({ accountId, signingCosmWasmClient, apiUrl, diff --git a/packages/core/src/actions/account/wallet/execute-on-remote-module.ts b/packages/core/src/actions/account/wallet/execute-on-remote-module.ts index 8d9c47af..87e20237 100644 --- a/packages/core/src/actions/account/wallet/execute-on-remote-module.ts +++ b/packages/core/src/actions/account/wallet/execute-on-remote-module.ts @@ -2,7 +2,7 @@ import { ManagerTypes } from '../../../codegen/abstract' import { ModuleType } from '../../../codegen/gql/graphql' import { WithCosmWasmSignOptions } from '../../../types/parameters' import { encodeModuleMsg } from '../../../utils/modules/encode-module-msg' -import { executeOnRemote } from './execute-on-remote' +import { executeOnRemoteManager } from './execute-on-remote-manager' import { BaseWalletParameters } from './types' type Base64EncodedJson = string @@ -52,7 +52,7 @@ export async function executeOnRemoteModule({ }, } - return executeOnRemote({ + return executeOnRemoteManager({ accountId, signingCosmWasmClient, apiUrl, diff --git a/packages/core/src/actions/account/wallet/execute-remote.ts b/packages/core/src/actions/account/wallet/execute-remote.ts new file mode 100644 index 00000000..6071a3b8 --- /dev/null +++ b/packages/core/src/actions/account/wallet/execute-remote.ts @@ -0,0 +1,64 @@ +import { + ManagerTypes, + ProxyExecuteMsgBuilder, + ProxyTypes, +} from '../../../codegen/abstract' +import { ModuleType } from '../../../codegen/gql/graphql' +import { WithCosmWasmSignOptions } from '../../../types/parameters' +import { MaybeArray } from '../../../types/utils' +import { abstractModuleId } from '../../../utils/modules/abstract-module-id' +import { encodeModuleMsg } from '../../../utils/modules/encode-module-msg' +import { CommonModuleNames } from '../../public/types' +import { executeOnRemoteManager } from './execute-on-remote-manager' +import { executeOnRemoteModule } from './execute-on-remote-module' +import { BaseWalletParameters } from './types' + +export type ExecuteOnRemoteParameters = Omit< + WithCosmWasmSignOptions< + BaseWalletParameters & { + hostChainName: string + msgs: MaybeArray + } + >, + 'funds' +> + +/** + * Execute a message on a remote account as the admin of the Account. Must be called by the owner. + * This message will execute on the proxy of the account. + * @param accountId + * @param signingCosmWasmClient + * @param apiUrl + * @param sender + * @param hostChainName + * @param msgs + * @param fee + * @param memo + */ +export async function executeRemote({ + accountId, + signingCosmWasmClient, + apiUrl, + sender, + msgs, + hostChainName, + fee, + memo, +}: ExecuteOnRemoteParameters) { + const proxyMsg: ProxyTypes.ExecuteMsg = ProxyExecuteMsgBuilder.moduleAction({ + msgs: Array.isArray(msgs) ? msgs : [msgs], + }) + + return executeOnRemoteModule({ + accountId, + signingCosmWasmClient, + apiUrl, + sender, + moduleId: abstractModuleId(CommonModuleNames.PROXY), + moduleType: ModuleType.AccountBase, + moduleMsg: proxyMsg, + hostChainName, + fee, + memo, + }) +} diff --git a/packages/core/src/clients/decorators/account-wallet.ts b/packages/core/src/clients/decorators/account-wallet.ts index 217c0017..fd3413c1 100644 --- a/packages/core/src/clients/decorators/account-wallet.ts +++ b/packages/core/src/clients/decorators/account-wallet.ts @@ -5,8 +5,9 @@ import { createSubAccount } from '../../actions/account/wallet/create-sub-accoun import { deposit } from '../../actions/account/wallet/deposit' import { execute } from '../../actions/account/wallet/execute' import { executeOnModule } from '../../actions/account/wallet/execute-on-module' -import { executeOnRemote } from '../../actions/account/wallet/execute-on-remote' +import { executeOnRemoteManager } from '../../actions/account/wallet/execute-on-remote-manager' import { executeOnRemoteModule } from '../../actions/account/wallet/execute-on-remote-module' +import { executeRemote } from '../../actions/account/wallet/execute-remote' 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' @@ -56,16 +57,21 @@ export type AccountWalletActions = { typeof executeOnModule >, ): ReturnType - executeOnRemote( + executeOnRemoteManager( parameters: ExtractAndPartializeDecoratedParametersFromParameters< - typeof executeOnRemote + typeof executeOnRemoteManager >, - ): ReturnType + ): ReturnType executeOnRemoteModule( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof executeOnRemoteModule >, ): ReturnType + executeRemote( + parameters: ExtractAndPartializeDecoratedParametersFromParameters< + typeof executeRemote + >, + ): ReturnType sendFundsToRemote( parameters: ExtractAndPartializeDecoratedParametersFromParameters< typeof sendFundsToRemote @@ -178,8 +184,8 @@ export function accountWalletActions( ...parameters, ...extra, }), - executeOnRemote: ({ extra, ...parameters }) => - executeOnRemote({ + executeOnRemoteManager: ({ extra, ...parameters }) => + executeOnRemoteManager({ accountId, signingCosmWasmClient, apiUrl, @@ -196,6 +202,15 @@ export function accountWalletActions( ...parameters, ...extra, }), + executeRemote: ({ extra, ...parameters }) => + executeRemote({ + accountId, + signingCosmWasmClient, + apiUrl, + sender, + ...parameters, + ...extra, + }), sendFundsToRemote: ({ extra, ...parameters }) => sendFundsToRemote({ accountId, diff --git a/packages/react/src/hooks/account/wallet/index.ts b/packages/react/src/hooks/account/wallet/index.ts index e81e1cf3..36d0cd30 100644 --- a/packages/react/src/hooks/account/wallet/index.ts +++ b/packages/react/src/hooks/account/wallet/index.ts @@ -1,10 +1,11 @@ -export * from './use-deposit' -export * from './use-create-sub-account' -export * from './use-execute-on-remote' -export * from './use-execute-on-remote-module' export * from './use-create-remote-account' +export * from './use-create-sub-account' +export * from './use-deposit' export * from './use-execute' +export * from './use-execute-on-remote-manager' +export * from './use-execute-on-remote-module' +export * from './use-execute-remote' +export * from './use-send-funds-to-remote' +export * from './use-update-settings' export * from './use-upgrade-module' export * from './use-withdraw' -export * from './use-update-settings' -export * from './use-send-funds-to-remote' diff --git a/packages/react/src/hooks/account/wallet/use-execute-on-remote-manager.ts b/packages/react/src/hooks/account/wallet/use-execute-on-remote-manager.ts new file mode 100644 index 00000000..908bfbc7 --- /dev/null +++ b/packages/react/src/hooks/account/wallet/use-execute-on-remote-manager.ts @@ -0,0 +1,39 @@ +import { AccountId, AccountWalletClient } from '@abstract-money/core' +import { useMutation } from '@tanstack/react-query' +import { useConfig } from '../../../contexts' +import { ExtractArgsFromParameters } from '../../../types/args' +import { UseMutationParameters } from '../../../types/queries' + +type ExecuteOnRemoteManagerutation = ExtractArgsFromParameters< + Parameters[0] +> + +export type UseExecuteOnRemoteManagerParameters = { + accountId: AccountId | undefined + chainName: string | undefined + mutation?: UseMutationParameters< + Awaited>, + unknown, + ExecuteOnRemoteManagerutation + > +} + +export function useExecuteOnRemoteManager({ + accountId, + chainName, + mutation, +}: UseExecuteOnRemoteManagerParameters) { + const config = useConfig() + const walletClient = config.useAccountWalletClient({ + accountId, + chainName: chainName, + }) + + return useMutation(({ args, ...cosmWasmSignOptions }) => { + if (!walletClient) throw new Error('walletClient is not defined') + return walletClient.executeOnRemoteManager({ + ...cosmWasmSignOptions, + ...args, + }) + }, mutation) +} diff --git a/packages/react/src/hooks/account/wallet/use-execute-on-remote.ts b/packages/react/src/hooks/account/wallet/use-execute-remote.ts similarity index 64% rename from packages/react/src/hooks/account/wallet/use-execute-on-remote.ts rename to packages/react/src/hooks/account/wallet/use-execute-remote.ts index 42ce6971..41279344 100644 --- a/packages/react/src/hooks/account/wallet/use-execute-on-remote.ts +++ b/packages/react/src/hooks/account/wallet/use-execute-remote.ts @@ -4,25 +4,25 @@ import { useConfig } from '../../../contexts' import { ExtractArgsFromParameters } from '../../../types/args' import { UseMutationParameters } from '../../../types/queries' -type ExecuteOnRemoteAccountMutation = ExtractArgsFromParameters< - Parameters[0] +type ExecuteRemoteAccountMutation = ExtractArgsFromParameters< + Parameters[0] > -export type UseExecuteOnRemoteParameters = { +export type UseExecuteRemoteParameters = { accountId: AccountId | undefined chainName: string | undefined mutation?: UseMutationParameters< - Awaited>, + Awaited>, unknown, - ExecuteOnRemoteAccountMutation + ExecuteRemoteAccountMutation > } -export function useExecuteOnRemote({ +export function useExecuteRemote({ accountId, chainName, mutation, -}: UseExecuteOnRemoteParameters) { +}: UseExecuteRemoteParameters) { const config = useConfig() const walletClient = config.useAccountWalletClient({ accountId, @@ -31,6 +31,6 @@ export function useExecuteOnRemote({ return useMutation(({ args, ...cosmWasmSignOptions }) => { if (!walletClient) throw new Error('walletClient is not defined') - return walletClient.executeOnRemote({ ...cosmWasmSignOptions, ...args }) + return walletClient.executeRemote({ ...cosmWasmSignOptions, ...args }) }, mutation) }