Skip to content

Commit

Permalink
add skip and token queries
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jun 25, 2024
1 parent c3c6f23 commit e0cda70
Show file tree
Hide file tree
Showing 10 changed files with 1,424 additions and 219 deletions.
600 changes: 404 additions & 196 deletions packages/state/contracts/Cw20Base.ts

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions packages/state/query/queries/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
CryptographicMultisigAccount,
Cw3MultisigAccount,
MultisigAccount,
PolytoneProxies,
} from '@dao-dao/types'
import { ListItemsResponse } from '@dao-dao/types/contracts/DaoCore.v2'
import { Threshold } from '@dao-dao/types/contracts/DaoProposalSingle.common'
import { BaseAccount } from '@dao-dao/types/protobuf/codegen/cosmos/auth/v1beta1/auth'
import { LegacyAminoPubKey } from '@dao-dao/types/protobuf/codegen/cosmos/crypto/multisig/keys'
Expand Down Expand Up @@ -69,30 +71,32 @@ export const fetchAccountList = async (
),
])

// If this is a DAO, get its polytone proxies and registered ICAs (which is a
// chain the DAO has indicated it has an ICA on by storing an item in its KV).
const [polytoneProxies, registeredIcas] = isDao
? await Promise.all([
queryClient.fetchQuery(
const mainAccount: Account = {
chainId,
address,
type: isPolytoneProxy ? AccountType.Polytone : AccountType.Native,
}

const [polytoneProxies, registeredIcas] = await Promise.all([
mainAccount.type !== AccountType.Polytone
? queryClient.fetchQuery(
polytoneQueries.proxies(queryClient, { chainId, address })
),
queryClient.fetchQuery(
)
: ({} as PolytoneProxies),
// If this is a DAO, get its registered ICAs (which is a chain the DAO has
// indicated it has an ICA on by storing an item in its KV).
isDao
? queryClient.fetchQuery(
daoDaoCoreQueries.listAllItems(queryClient, {
chainId,
contractAddress: address,
args: {
prefix: ICA_CHAINS_TX_PREFIX,
},
})
),
])
: []

const mainAccount: Account = {
chainId,
address,
type: isPolytoneProxy ? AccountType.Polytone : AccountType.Native,
}
)
: ([] as ListItemsResponse),
])

const allAccounts: Account[] = [
// Main account.
Expand Down
67 changes: 67 additions & 0 deletions packages/state/query/queries/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QueryClient, queryOptions, skipToken } from '@tanstack/react-query'

import { ChainId } from '@dao-dao/types'
import { ModuleAccount } from '@dao-dao/types/protobuf/codegen/cosmos/auth/v1beta1/auth'
import { Metadata } from '@dao-dao/types/protobuf/codegen/cosmos/bank/v1beta1/bank'
import { DecCoin } from '@dao-dao/types/protobuf/codegen/cosmos/base/v1beta1/coin'
import {
cosmWasmClientRouter,
Expand Down Expand Up @@ -282,6 +283,64 @@ export const fetchWasmContractAdmin = async ({
)
}

/**
* Fetch the on-chain metadata for a denom if it exists. Returns null if denom
* not found. This likely exists for token factory denoms.
*/
export const fetchDenomMetadata = async ({
chainId,
denom,
}: {
chainId: string
denom: string
}): Promise<{
metadata: Metadata
preferredSymbol: string
preferredDecimals: number
} | null> => {
const client = await cosmosProtoRpcClientRouter.connect(chainId)
try {
const { metadata } = await client.bank.v1beta1.denomMetadata({ denom })

if (metadata) {
const { base, denomUnits, symbol, display } = metadata

// If display is equal to the base, use the symbol denom unit if
// available. This fixes the case where display was not updated even
// though a nonzero exponent was created.
const searchDenom = display === base ? symbol : display

const displayDenom =
denomUnits.find(({ denom }) => denom === searchDenom) ??
denomUnits.find(({ denom }) => denom === display) ??
denomUnits.find(({ exponent }) => exponent > 0) ??
denomUnits[0]

return {
metadata,
// If factory denom, extract symbol at the end.
preferredSymbol:
(displayDenom
? displayDenom.denom.startsWith('factory/')
? displayDenom.denom.split('/').pop()!
: displayDenom.denom
: metadata.symbol) || denom,
preferredDecimals: displayDenom?.exponent ?? 0,
}
}
} catch (err) {
// If denom not found, return null.
if (err instanceof Error && err.message.includes('key not found')) {
return null
}

// Rethrow other errors.
throw err
}

return null
}

export const chainQueries = {
/**
* Fetch the module address associated with the specified name.
Expand Down Expand Up @@ -356,4 +415,12 @@ export const chainQueries = {
queryKey: ['chain', 'wasmContractAdmin', options],
queryFn: () => fetchWasmContractAdmin(options),
}),
/**
* Fetch the on-chain metadata for a denom if it exists.
*/
denomMetadata: (options: Parameters<typeof fetchDenomMetadata>[0]) =>
queryOptions({
queryKey: ['chain', 'denomMetadata', options],
queryFn: () => fetchDenomMetadata(options),
}),
}
Loading

0 comments on commit e0cda70

Please sign in to comment.