Skip to content

Commit

Permalink
Update remote execution
Browse files Browse the repository at this point in the history
  • Loading branch information
adairrr committed Jun 13, 2024
1 parent 5899de2 commit 711365a
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .changeset/cuddly-pigs-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@abstract-money/core": minor
"@abstract-money/react": minor
---

Update remote execution
11 changes: 6 additions & 5 deletions examples/wagemos-cosmoskit-nextjs/src/app/remote/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { accountIdToString } from '@abstract-money/core'
import {
useAccounts,
useCreateRemoteAccount,
useExecuteOnRemote,
useExecuteOnRemoteManager,
useRemoteAccountIds,
useRemoteHosts,
} from '@abstract-money/react'
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type ExecuteOnRemoteParameters = Omit<
* @param fee
* @param memo
*/
export async function executeOnRemote({
export async function executeOnRemoteManager({
accountId,
signingCosmWasmClient,
apiUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function executeOnRemoteModule({
},
}

return executeOnRemote({
return executeOnRemoteManager({
accountId,
signingCosmWasmClient,
apiUrl,
Expand Down
64 changes: 64 additions & 0 deletions packages/core/src/actions/account/wallet/execute-remote.ts
Original file line number Diff line number Diff line change
@@ -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<ProxyTypes.CosmosMsgForEmpty>
}
>,
'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,
})
}
27 changes: 21 additions & 6 deletions packages/core/src/clients/decorators/account-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -56,16 +57,21 @@ export type AccountWalletActions = {
typeof executeOnModule
>,
): ReturnType<typeof executeOnModule>
executeOnRemote(
executeOnRemoteManager(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof executeOnRemote
typeof executeOnRemoteManager
>,
): ReturnType<typeof executeOnRemote>
): ReturnType<typeof executeOnRemoteManager>
executeOnRemoteModule(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof executeOnRemoteModule
>,
): ReturnType<typeof executeOnRemoteModule>
executeRemote(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof executeRemote
>,
): ReturnType<typeof executeRemote>
sendFundsToRemote(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof sendFundsToRemote
Expand Down Expand Up @@ -178,8 +184,8 @@ export function accountWalletActions(
...parameters,
...extra,
}),
executeOnRemote: ({ extra, ...parameters }) =>
executeOnRemote({
executeOnRemoteManager: ({ extra, ...parameters }) =>
executeOnRemoteManager({
accountId,
signingCosmWasmClient,
apiUrl,
Expand All @@ -196,6 +202,15 @@ export function accountWalletActions(
...parameters,
...extra,
}),
executeRemote: ({ extra, ...parameters }) =>
executeRemote({
accountId,
signingCosmWasmClient,
apiUrl,
sender,
...parameters,
...extra,
}),
sendFundsToRemote: ({ extra, ...parameters }) =>
sendFundsToRemote({
accountId,
Expand Down
13 changes: 7 additions & 6 deletions packages/react/src/hooks/account/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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<AccountWalletClient['executeOnRemoteManager']>[0]
>

export type UseExecuteOnRemoteManagerParameters = {
accountId: AccountId | undefined
chainName: string | undefined
mutation?: UseMutationParameters<
Awaited<ReturnType<AccountWalletClient['executeOnRemoteManager']>>,
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import { useConfig } from '../../../contexts'
import { ExtractArgsFromParameters } from '../../../types/args'
import { UseMutationParameters } from '../../../types/queries'

type ExecuteOnRemoteAccountMutation = ExtractArgsFromParameters<
Parameters<AccountWalletClient['executeOnRemote']>[0]
type ExecuteRemoteAccountMutation = ExtractArgsFromParameters<
Parameters<AccountWalletClient['executeRemote']>[0]
>

export type UseExecuteOnRemoteParameters = {
export type UseExecuteRemoteParameters = {
accountId: AccountId | undefined
chainName: string | undefined
mutation?: UseMutationParameters<
Awaited<ReturnType<AccountWalletClient['executeOnRemote']>>,
Awaited<ReturnType<AccountWalletClient['executeRemote']>>,
unknown,
ExecuteOnRemoteAccountMutation
ExecuteRemoteAccountMutation
>
}

export function useExecuteOnRemote({
export function useExecuteRemote({
accountId,
chainName,
mutation,
}: UseExecuteOnRemoteParameters) {
}: UseExecuteRemoteParameters) {
const config = useConfig()
const walletClient = config.useAccountWalletClient({
accountId,
Expand All @@ -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)
}

0 comments on commit 711365a

Please sign in to comment.