Skip to content

Commit

Permalink
Merge pull request #142 from AbstractSDK/adair/fix-init-2
Browse files Browse the repository at this point in the history
Clarify instantiate2 address prediction methods
  • Loading branch information
adairrr authored Nov 1, 2024
2 parents a374873 + 06aa82e commit a3da75e
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 222 deletions.
7 changes: 7 additions & 0 deletions .changeset/healthy-experts-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@abstract-money/core": minor
"@abstract-money/react": minor
"wagemos-graz-nextjs": patch
---

Rename instantiate2 methods to predict and remove 'base' from AccountAddress retrieval
4 changes: 2 additions & 2 deletions examples/wagemos-graz-nextjs/src/app/authz-osmosis/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
useCreateAccountMonarchy,
useSignAndBroadcast,
} from '@abstract-money/react'
import { useModuleInstantiate2Address } from '@abstract-money/react'
import { usePredictModuleAddress } from '@abstract-money/react'
import { useAccount } from 'graz'
import React, { useCallback, useEffect, useMemo } from 'react'
import { Button } from '../../components/ui/button'
Expand Down Expand Up @@ -50,7 +50,7 @@ export default function AuthzPage() {
})
}, [account])

const { data: savingsAppAddress } = useModuleInstantiate2Address({
const { data: savingsAppAddress } = usePredictModuleAddress({
accountId: stringToAccountId(TEST_SAVINGS_ACCOUNT_ID, CHAIN_NAME),
chainName: CHAIN_NAME,
args: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

### Patch Changes

- [`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851) Thanks [@dalechyn](https://github.com/dalechyn)! - Added `useAccountsBaseAddressesFromApi` hook.
- [`ed50660`](https://github.com/AbstractSDK/abstract.js/commit/ed5066049de491a9a8793211dcb0d07f18424851) Thanks [@dalechyn](https://github.com/dalechyn)! - Added `useAccountsAddressesFromApi` hook.

## 1.1.0

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ModuleId,
chainIdToName,
getInstantiate2Address,
getInstantiate2AddressWithAccountId,
} from '@abstract-money/core'
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { RegistryTypes } from '../../../codegen/abstract'
Expand Down Expand Up @@ -46,9 +46,9 @@ export async function getModuleInstantiate2AddressFromApi({

const moduleCodeDetails = await cosmWasmClient.getCodeDetails(moduleCodeId)

return getInstantiate2Address(
return getInstantiate2AddressWithAccountId(
moduleFactoryAddress,
moduleCodeDetails.checksum,
{ ...accountId, chainName },
accountId,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { RegistryTypes } from '../../codegen/abstract'
import { getRegistryQueryClientFromApi } from './get-registry-query-client-from-api'

export type GetAccountsBaseAddressesFromApiParameters = {
export type GetAccountsAddressesFromApiParameters = {
accountIds: RegistryTypes.AccountId[]
cosmWasmClient: CosmWasmClient
apiUrl: string
Expand All @@ -12,7 +12,7 @@ export async function getAccountAddressesFromApi({
accountIds,
cosmWasmClient,
apiUrl,
}: GetAccountsBaseAddressesFromApiParameters) {
}: GetAccountsAddressesFromApiParameters) {
const registryQueryClient = await getRegistryQueryClientFromApi({
cosmWasmClient,
apiUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
abstractModuleId,
chainIdToName,
getInstantiate2Address,
getInstantiate2AddressWithAccountId,
} from '@abstract-money/core'
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { P, match } from 'ts-pattern'
import { RegistryTypes } from '../../codegen/abstract'
import { getRegistryAddressFromApi } from '../get-registry-address-from-api'
import { getAppModuleCodeIdFromRegistry } from './get-app-module-code-id-from-registry'
import { CommonModuleNames } from './types'

export type GetAccountInstantiate2AddressFromApi = {
cosmWasmClient: CosmWasmClient
apiUrl: string
creator: string
salt:
| {
accountId: RegistryTypes.AccountId
}
| {
custom: string | Uint8Array
}
}

/**
* Retrieve the calculated manager init2 address from the api.
* @param accountId
* @param cosmWasmClient
* @param apiUrl
* @param creator - the creator address
* @param salt - the (optional) salt to use for the address calculation. If not provided, will use the accountId salt.
*/
export async function getAccountInstantiate2AddressFromApi({
cosmWasmClient,
apiUrl,
creator,
salt,
}: GetAccountInstantiate2AddressFromApi): Promise<string> {
const chainId = await cosmWasmClient.getChainId()
const chainName = chainIdToName(chainId)

const registryAddress = await getRegistryAddressFromApi({
apiUrl,
chainName,
})

const accountCodeId = await getAppModuleCodeIdFromRegistry({
moduleId: abstractModuleId(CommonModuleNames.ACCOUNT),
version: 'latest',
cosmWasmClient,
registryAddress,
})

const moduleCodeDetails = await cosmWasmClient.getCodeDetails(accountCodeId)

return await match(salt)
.with(
{ accountId: P.select() },
async (accountId) =>
await getInstantiate2AddressWithAccountId(
creator,
moduleCodeDetails.checksum,
accountId,
),
)
.with(
{ custom: P.select() },
async (customSalt) =>
await getInstantiate2Address(
creator,
moduleCodeDetails.checksum,
customSalt,
),
)
.exhaustive()
}
18 changes: 2 additions & 16 deletions packages/core/src/clients/decorators/account-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { queryModule } from 'src/actions/account/public/query-module'
import { getAccountAddressFromApi } from '../../actions/account/public/get-account-address-from-api'
import { getAccountId } from '../../actions/account/public/get-account-id'
import { getAccountInstantiate2AddressFromApi } from '../../actions/account/public/get-account-instantiate2-address-from-api'
import { getAccountQueryClientFromApi } from '../../actions/account/public/get-account-query-client-from-api'
import { getAccountSettings } from '../../actions/account/public/get-account-settings'
import { getModuleAddress } from '../../actions/account/public/get-module-address'
Expand Down Expand Up @@ -59,16 +58,11 @@ export type AccountPublicActions = {
typeof getModules
>,
): ReturnType<typeof getModules>
getModuleInstantiate2Address(
predictModuleAddress(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getModuleInstantiate2AddressFromApi
>,
): ReturnType<typeof getModuleInstantiate2AddressFromApi>
getAccountInstantiate2Address(
parameters?: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getAccountInstantiate2AddressFromApi
>,
): ReturnType<typeof getAccountInstantiate2AddressFromApi>
getNamespace(
parameters?: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getNamespace
Expand Down Expand Up @@ -164,22 +158,14 @@ export function accountPublicActions(
...parameters,
...extra,
}),
getModuleInstantiate2Address: ({ extra, ...parameters }) =>
predictModuleAddress: ({ extra, ...parameters }) =>
getModuleInstantiate2AddressFromApi({
accountId,
cosmWasmClient,
apiUrl,
...parameters,
...extra,
}),
getAccountInstantiate2Address: ({ extra, ...parameters } = {}) =>
getAccountInstantiate2AddressFromApi({
accountId,
cosmWasmClient,
apiUrl,
...parameters,
...extra,
}),
getNamespace: ({ extra, ...parameters } = {}) =>
getNamespace({
accountId,
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/clients/decorators/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getSimulationResultFromApi } from '../../actions/get-simulation-result-
import { getAbstractModuleAddressFromRegistry } from '../../actions/public/get-abstract-module-address-from-registry'
import { getAbstractModuleVersion } from '../../actions/public/get-abstract-module-version'
import { getAccountAddressesFromApi } from '../../actions/public/get-account-addresses-from-api'
import { getAccountInstantiate2AddressFromApi } from '../../actions/public/get-account-instantiate2-address-from-api'
import { getAccountQueryClient } from '../../actions/public/get-account-query-client'
import { getAnsHostAddressFromRegistry } from '../../actions/public/get-ans-host-address-from-registry'
import { getAnsHostQueryClient } from '../../actions/public/get-ans-host-query-client'
Expand All @@ -27,11 +28,16 @@ type ExtractAndPartializeDecoratedParametersFromParameters<
* Also see {@link AbstractBaseActions} for more public query actions.
*/
export type PublicActions = {
getAccountsBaseAddresses(
getAccountsAddresses(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getAccountAddressesFromApi
>,
): ReturnType<typeof getAccountAddressesFromApi>
predictAccountAddress(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getAccountInstantiate2AddressFromApi
>,
): ReturnType<typeof getAccountInstantiate2AddressFromApi>
getAbstractModuleVersion(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getAbstractModuleVersion
Expand All @@ -57,6 +63,7 @@ export type PublicActions = {
typeof getRemoteHostsFromApi
>,
): ReturnType<typeof getRemoteHostsFromApi>

getSimulationResult(
parameters: ExtractAndPartializeDecoratedParametersFromParameters<
typeof getSimulationResultFromApi
Expand All @@ -74,13 +81,20 @@ export function publicActions(
apiUrl: string,
): PublicActions {
return {
getAccountsBaseAddresses: ({ extra, ...parameters }) =>
getAccountsAddresses: ({ extra, ...parameters }) =>
getAccountAddressesFromApi({
cosmWasmClient,
apiUrl,
...parameters,
...extra,
}),
predictAccountAddress: ({ extra, ...parameters }) =>
getAccountInstantiate2AddressFromApi({
cosmWasmClient,
apiUrl,
...parameters,
...extra,
}),
getAbstractModuleVersion: ({ extra, ...parameters }) =>
getAbstractModuleVersion({
cosmWasmClient,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { ExecuteResult } from '@cosmjs/cosmwasm-stargate'
import { ACCOUNT_ID_CHAIN_DELIMITER, AccountId } from '../account-id'
import {
ACCOUNT_ID_CHAIN_DELIMITER,
AccountId,
accountIdToString,
stringToAccountId,
} from '../account-id'
import { findAbstractAttribute } from '../events'

export function parseCreateAccountExecuteResult(
result: ExecuteResult,
chainName: string,
) {
const seq = Number.parseInt(
findAbstractAttribute(result, 'account_sequence').value,
)
const trace = findAbstractAttribute(result, 'trace').value
const accountId = {
chainName,
seq,
trace:
trace === 'local' || trace === undefined
? 'local'
: { remote: trace.split(ACCOUNT_ID_CHAIN_DELIMITER) },
} satisfies AccountId
const accountIdString = findAbstractAttribute(result, 'account_id').value
const accountId = stringToAccountId(accountIdString, chainName)

const accountAddress = findAbstractAttribute(result, 'account_address').value
return { accountId, accountAddress }
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export const toUint8Array = (text: string) =>
Uint8Array.from(Array.from(text).map((letter) => letter.charCodeAt(0)))

export const toSha256 = async (text: string): Promise<Uint8Array> => {
const encoder = new TextEncoder()
const data = encoder.encode(text)
const data = toUtf8(text)
const hash = await crypto.subtle.digest('SHA-256', data)
return new Uint8Array(hash)
}

export type EncodedMsg = {
readonly typeUrl: string
readonly value: any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { stringToAccountId } from '../account-id'
import { getInstantiate2Address } from './get-instantiate2-address'
import { getInstantiate2AddressWithAccountId } from './get-instantiate2-address'

describe('getInstantiate2AccountAddress', () => {
it('returns the correct address', async () => {
Expand All @@ -11,7 +11,7 @@ describe('getInstantiate2AccountAddress', () => {
'3f6fb5db7e9be94c6699c495535fd55884ac72e2babbcd90b5b41a41cce179ee'
const accountId = stringToAccountId('osmosis-48', 'osmosis')

const result = await getInstantiate2Address(
const result = await getInstantiate2AddressWithAccountId(
moduleFactoryAddress,
checksum,
accountId,
Expand Down
Loading

0 comments on commit a3da75e

Please sign in to comment.