Skip to content
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

Generic accounts displayed in call info #392

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
124 changes: 94 additions & 30 deletions packages/ui/src/components/CallInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { HiOutlineArrowTopRightOnSquare as LaunchIcon } from 'react-icons/hi2'
import { Link } from './library'
import { usePjsLinks } from '../hooks/usePjsLinks'
import { Alert } from '@mui/material'
import { ApiPromise } from '@polkadot/api'
import { isTypeBalance } from '../utils/isTypeBalance'
import { isTypeAccount } from '../utils/isTypeAccount'

interface Props {
aggregatedData: Omit<AggregatedData, 'from' | 'timestamp'>
Expand All @@ -26,43 +29,97 @@ interface CreateTreeParams {
decimals: number
unit: string
name?: string
api: ApiPromise
typeName?: string
}

const createUlTree = ({ name, args, decimals, unit }: CreateTreeParams) => {
const handleBatchDisplay = ({
value,
decimals,
unit,
api,
key
}: {
value: any[]
decimals: number
unit: string
key: string
api: ApiPromise
}) =>
value.map((call: any, index: number) => {
const name = `${call.section}.${call.method}`
return (
<>
<li key={`${key}-${index}`}>{name}</li>
{createUlTree({
name: `${call.section}.${call.method}`,
args: call.args,
decimals,
unit,
api
})}
</>
)
})

const handleBalanceDisplay = ({
value,
decimals,
unit,
key
}: {
value: any
decimals: number
unit: string
key: string
}) => {
const balance = formatBnBalance(value.replace(/,/g, ''), decimals, {
withThousandDelimiter: true,
tokenSymbol: unit,
numberAfterComma: 4
})

return (
<li key={key}>
{key}: {balance}
</li>
)
}

const getTypeName = (index: number, name: string, value: any, api: ApiPromise) => {
const [palletFromName, methodFromName] = name.split('.')
const pallet = value.section || palletFromName
const method = value.method || methodFromName
const metaArgs = !!pallet && !!method && api.tx[pallet][method].meta.args

return (
(!!metaArgs && metaArgs[index] && (metaArgs[index].toHuman().typeName as string)) || undefined
)
}

const createUlTree = ({ name, args, decimals, unit, api, typeName }: CreateTreeParams) => {
if (!args) return
if (!name) return

const isBalancesTransferAlike = ['balances.transfer', 'balances.transferKeepAlive'].includes(name)
const isProxyCreationDeletion = ['proxy.addProxy', 'proxy.removeProxy'].includes(name)

return (
<ul className="params">
{Object.entries(args).map(([key, value]) => {
// in case the call was a WrapperOpaque<Call> the destination is the value and has no Id
const destAddress = value?.Id || value
// show nice dest
if (
((isBalancesTransferAlike && key === 'dest') ||
(isProxyCreationDeletion && key === 'delegate')) &&
typeof destAddress === 'string'
) {
return (
<li key={key}>
{key}: {<MultisigCompactDisplay address={destAddress} />}
</li>
)
{Object.entries(args).map(([key, value], index) => {
const _typeName = typeName || getTypeName(index, name, value, api)

if (_typeName === 'Vec<RuntimeCall>') {
return handleBatchDisplay({ value, decimals, unit, api, key: `${key}-batch` })
}

// generically show nice value for Balance type
if (isTypeBalance(_typeName)) {
return handleBalanceDisplay({ value, decimals, unit, key })
}

// show nice value
if (isBalancesTransferAlike && key === 'value') {
const balance = formatBnBalance(value.replace(/,/g, ''), decimals, {
withThousandDelimiter: true,
tokenSymbol: unit,
numberAfterComma: 4
})
const destAddress = value?.Id || value
if (isTypeAccount(_typeName) && typeof destAddress === 'string') {
return (
<li key={key}>
{key}: {balance}
{key}: {<MultisigCompactDisplay address={destAddress} />}
</li>
)
}
Expand All @@ -71,7 +128,14 @@ const createUlTree = ({ name, args, decimals, unit }: CreateTreeParams) => {
<li key={key}>
{key}:{' '}
{typeof value === 'object'
? createUlTree({ name, args: value, decimals, unit })
? createUlTree({
name,
args: value,
decimals,
unit,
api,
typeName: _typeName
})
: value}
</li>
)
Expand Down Expand Up @@ -108,7 +172,7 @@ const CallInfo = ({
withProxyFiltered = true
}: Props) => {
const { args, name } = withProxyFiltered ? filterProxyProxy(aggregatedData) : aggregatedData
const { chainInfo } = useApi()
const { chainInfo, api } = useApi()
const decimals = useMemo(() => chainInfo?.tokenDecimals || 0, [chainInfo])
const unit = useMemo(() => chainInfo?.tokenSymbol || '', [chainInfo])
const { getDecodeUrl } = usePjsLinks()
Expand Down Expand Up @@ -140,11 +204,11 @@ const CallInfo = ({
annoyance.
</AlertStyled>
)}
{args && Object.keys(args).length > 0 && (
{args && !!api && Object.keys(args).length > 0 && (
<Expander
expanded={expanded}
title="Params"
content={createUlTree({ name, args, decimals, unit })}
content={createUlTree({ name, args, decimals, unit, api })}
/>
)}
{children}
Expand Down
3 changes: 1 addition & 2 deletions packages/ui/src/components/EasySetup/ManualExtrinsic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useApi } from '../../contexts/ApiContext'
import paramConversion from '../../utils/paramConversion'
import { getGlobalMaxValue, inputToBn } from '../../utils'
import BN from 'bn.js'
import { isTypeBalance } from '../../utils/isTypeBalance'

interface Props {
extrinsicIndex?: string
Expand Down Expand Up @@ -47,8 +48,6 @@ const initFormState = {

const argIsOptional = (arg: any) => arg.type.toString().startsWith('Option<')

const isTypeBalance = (typeName: string) => ['Balance', 'BalanceOf', 'Amount'].includes(typeName)

const isNumType = (type: string) => paramConversion.num.includes(type)

const parseFloatOrInt = (value: any) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/utils/isTypeAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isTypeAccount = (typeName?: string) =>
!!typeName && ['AccountIdLookupOf'].includes(typeName)
2 changes: 2 additions & 0 deletions packages/ui/src/utils/isTypeBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isTypeBalance = (typeName?: string) =>
!!typeName && ['Balance', 'BalanceOf', 'Amount'].includes(typeName)
Loading