Skip to content

Add module address instantiate 2 helper #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/smooth-buses-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wagemos-graz-nextjs": patch
"@abstract-money/core": patch
"@abstract-money/react": patch
---

Add helpers to predict module address
3 changes: 1 addition & 2 deletions examples/wagemos-graz-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.2.14",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "NODE_OPTIONS='--inspect' next dev",
"build": "next build",
"start": "next start",
"typecheck": "tsc --noEmit",
Expand Down Expand Up @@ -48,7 +48,6 @@
"devDependencies": {
"@abstract-money/cli": "workspace:*",
"@keplr-wallet/types": "^0.12.44",
"@tanstack/react-query-devtools": "^5.17.1",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
43 changes: 32 additions & 11 deletions examples/wagemos-graz-nextjs/src/app/authz-osmosis/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@ import {
useCreateAccountMonarchy,
useSignAndBroadcast,
} from '@abstract-money/react'
import { useModuleInstantiate2AddressFromApi } from '@abstract-money/react'
import { useAccount } from 'graz'
import { useCallback, useEffect, useMemo } from 'react'
import React, { useCallback, useEffect, useMemo } from 'react'
import { Button } from '../../components/ui/button'
import { WalletButton } from '../_components/wallet-button'
import { prepareInstantiateMsg } from './_utils/prepare-instantiate-msg'

const GRANTER = 'osmo1jzyqffltm2s5wxmnjyze5hzrpcady0gmpz738n'
const GRANTEE = 'osmo1ak64euh4tyzetkny6t0y0v5tw47n3y6y0ys3md'
const GRANTER = 'osmo18k2uq7srsr8lwrae6zr0qahpn29rsp7tswpc4g'

const CHAIN_NAME = 'osmosis'
const TEST_SAVINGS_ACCOUNT_ID = 'osmosis-48'
const SAVINGS_APP_MODULE_ID = 'abstract:carrot-app'
export default function AuthzPage() {
useEffect(() => {
prepareInstantiateMsg()
}, [])

const { mutate: signAndBroadcast } = useSignAndBroadcast({
args: { chainName: 'osmosis' },
args: { chainName: CHAIN_NAME },
})
const { data: account } = useAccount({ chainId: 'osmosis-1' })

const { data: accountFactory, isLoading } =
useAccountFactoryQueryClientFromApi('osmosis')
useAccountFactoryQueryClientFromApi(CHAIN_NAME)

const { mutate: createAccount } = useCreateAccountMonarchy({
args: { chainName: 'osmosis' },
args: { chainName: CHAIN_NAME },
})

const onCreateAccount = useCallback(async () => {
Expand All @@ -50,14 +53,32 @@ export default function AuthzPage() {
fee: 'auto',
args: {
name: 'funny-squid',
accountId: stringToAccountId(`local-${sequence}`, 'osmosis'),
accountId: stringToAccountId(`local-${sequence}`, CHAIN_NAME),
owner: account.bech32Address,
},
})
}, [accountFactory])

const { data: savingsAppAddress } = useModuleInstantiate2AddressFromApi(
{
accountId: stringToAccountId(TEST_SAVINGS_ACCOUNT_ID, CHAIN_NAME),
moduleId: SAVINGS_APP_MODULE_ID,
},
{
enabled: true,
},
)

console.log('calculated savings app address', savingsAppAddress)

const onGrantAuthzClick = useMemo(() => {
if (!account) return undefined
if (!account) {
console.error('no account')
return undefined
} else if (!savingsAppAddress) {
console.error('no module grantee')
return undefined
}

return () => {
signAndBroadcast({
Expand All @@ -74,18 +95,18 @@ export default function AuthzPage() {
].map((typeUrl) =>
encodeAuthzGrantGenericAuthorizationMsg(
account.bech32Address,
GRANTEE,
savingsAppAddress,
typeUrl,
),
),
encodeAuthzGrantGenericAuthorizationMsg(
account.bech32Address,
GRANTEE,
savingsAppAddress,
BankTransactionTypeUrl.Send,
),
encodeAuthzGrantSendAuthorizationMsg(
account.bech32Address,
GRANTEE,
savingsAppAddress,
{ spendLimit: [{ denom: 'uosmo', amount: '100' }] },
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
ModuleId,
chainIdToName,
getInstantiate2Address,
toSha256,
} from '@abstract-money/core'
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { VersionControlTypes } from '../../../codegen/abstract'
import { WithArgs } from '../../../types/with-args'
import { getVersionControlAddressFromApi } from '../../get-version-control-address-from-api'
import { getAbstractModuleAddressFromVersionControl } from '../../public/get-abstract-module-address-from-version-control'
import { getAppModuleCodeIdFromVersionControl } from '../../public/get-app-module-code-id-from-version-control'
import { getModuleFactoryAddressFromVersionControl } from '../../public/get-module-factory-address-from-version-control'

export type GetModuleInstantiate2AddressFromApi = WithArgs<{
accountId: VersionControlTypes.AccountId
moduleId: ModuleId
version?: string
cosmWasmClient: CosmWasmClient
apiUrl: string
}>

export async function getModuleInstantiate2AddressFromApi({
args: { accountId, cosmWasmClient, apiUrl, moduleId, version },
}: GetModuleInstantiate2AddressFromApi): Promise<string> {
const chainId = await cosmWasmClient.getChainId()
const chainName = chainIdToName(chainId)

const versionControlAddress = await getVersionControlAddressFromApi({
args: {
apiUrl,
chainName,
},
})

const moduleFactoryAddress = await getModuleFactoryAddressFromVersionControl({
args: {
cosmWasmClient,
versionControlAddress,
},
})

const moduleCodeId = await getAppModuleCodeIdFromVersionControl({
args: { moduleId, version, cosmWasmClient, versionControlAddress },
})

const moduleCodeDetails = await cosmWasmClient.getCodeDetails(moduleCodeId)

return await getInstantiate2Address(
moduleFactoryAddress,
moduleCodeDetails.checksum,
{ ...accountId, chainName },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getVersionControlQueryClient } from './get-version-control-query-client

export enum CommonModuleNames {
ACCOUNT_FACTORY = 'account-factory',
MODULE_FACTORY = 'module-factory',
ANS_HOST = 'ans-host',
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'

import {
ModuleId,
formatModuleIdWithVersion,
moduleIdToName,
moduleIdToNamespace,
} from '@abstract-money/core'
import { VersionControlTypes } from '../../codegen/abstract'
import { WithArgs } from '../../types/with-args'
import { versionControlModuleToCodeId } from '../../utils/version-control/version-control-module-to-code-id'
import { getVersionControlQueryClient } from './get-version-control-query-client'

export type GetAppModuleCodeIdFromVersionControl = WithArgs<{
moduleId: `${ModuleId}`
cosmWasmClient: CosmWasmClient
versionControlAddress: string
version?: string
}>

export async function getAppModuleCodeIdFromVersionControl({
args: { moduleId, cosmWasmClient, versionControlAddress, version },
}: GetAppModuleCodeIdFromVersionControl) {
const versionControlQueryClient = getVersionControlQueryClient({
args: {
cosmWasmClient,
versionControlAddress,
},
})

const [moduleAddress] = await versionControlQueryClient
.modules({
infos: [
{
name: moduleIdToName(moduleId),
namespace: moduleIdToNamespace(moduleId),
version: version ? { version } : 'latest',
} satisfies VersionControlTypes.ModuleInfo,
],
})
.then(({ modules }) =>
modules.map(({ module }) => versionControlModuleToCodeId(module)),
)

if (!moduleAddress) {
throw new Error(
`Could not fetch code id for app module ${moduleId} version ${version} from registry ${versionControlAddress}`,
)
}

return moduleAddress
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'

import { WithArgs } from '../../types/with-args'
import {
CommonModuleNames,
getAbstractModuleAddressFromVersionControl,
} from './get-abstract-module-address-from-version-control'

export type GetModuleFactoryAddressFromVersionControlParameters = WithArgs<{
cosmWasmClient: CosmWasmClient
versionControlAddress: string
version?: string
}>
export async function getModuleFactoryAddressFromVersionControl({
args: { cosmWasmClient, versionControlAddress, version },
}: GetModuleFactoryAddressFromVersionControlParameters) {
return getAbstractModuleAddressFromVersionControl({
args: {
moduleName: CommonModuleNames.MODULE_FACTORY,
cosmWasmClient,
versionControlAddress,
version,
},
})
}
11 changes: 11 additions & 0 deletions packages/core/src/clients/decorators/account-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getAccountBaseAddressesFromApi } from '../../actions/account/public/get
import { getBaseToken } from '../../actions/account/public/get-base-token'
import { getManagerQueryClientFromApi } from '../../actions/account/public/get-manager-query-client-from-api'
import { getModuleAddress } from '../../actions/account/public/get-module-address'
import { getModuleInstantiate2AddressFromApi } from '../../actions/account/public/get-module-instantiate2-address-from-api'
import { getModules } from '../../actions/account/public/get-modules'
import { getNamespace } from '../../actions/account/public/get-namespace'
import { getOwner } from '../../actions/account/public/get-owner'
Expand Down Expand Up @@ -48,6 +49,11 @@ export type AccountPublicActions = {
getModules(
args: CutSpecificArgsFromParameter<typeof getModules>,
): ReturnType<typeof getModules>
getModuleInstantiate2AddressFromApi(
args: CutSpecificArgsFromParameter<
typeof getModuleInstantiate2AddressFromApi
>,
): ReturnType<typeof getModuleInstantiate2AddressFromApi>
getNamespace(
args: CutSpecificArgsFromParameter<typeof getNamespace>,
): ReturnType<typeof getNamespace>
Expand Down Expand Up @@ -108,6 +114,11 @@ export function accountPublicActions(
args: { ...args, accountId, cosmWasmClient, apiUrl },
...rest,
}),
getModuleInstantiate2AddressFromApi: ({ args, ...rest }) =>
getModuleInstantiate2AddressFromApi({
args: { ...args, accountId, cosmWasmClient, apiUrl },
...rest,
}),
getNamespace: ({ args, ...rest }) =>
getNamespace({
args: { ...args, accountId, cosmWasmClient, apiUrl },
Expand Down

This file was deleted.

This file was deleted.

Loading