Skip to content

Commit

Permalink
tsc passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut committed Jan 17, 2025
1 parent 841a728 commit ff2434c
Show file tree
Hide file tree
Showing 20 changed files with 296 additions and 220 deletions.
3 changes: 2 additions & 1 deletion packages/ui/src/components/CallInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Expander from './Expander'
import { styled } from '@mui/material/styles'
import { ReactNode, useMemo } from 'react'
import { ApiDescriptors, IApiContext, useApi } from '../contexts/ApiContext'
import { IApiContext, useApi } from '../contexts/ApiContext'
import { getExtrinsicName } from '../utils/getExtrinsicName'
import { isProxyCall } from '../utils/isProxyCall'
import { formatBigIntBalance } from '../utils/formatBnBalance'
Expand All @@ -14,6 +14,7 @@ import { JSONprint } from '../utils/jsonPrint'
import { Transaction } from 'polkadot-api'
import MultisigCompactDisplay from './MultisigCompactDisplay'
import { ChainInfoHuman } from '../contexts/PeopleChainApiContext'
import { ApiDescriptors } from '../types'

interface Props {
aggregatedData: Omit<CallDataInfoFromChain, 'from' | 'timestamp'>
Expand Down
34 changes: 18 additions & 16 deletions packages/ui/src/components/EasySetup/BalancesTransfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Box, InputAdornment } from '@mui/material'
import { styled } from '@mui/material/styles'
import GenericAccountSelection, { AccountBaseInfo } from '../select/GenericAccountSelection'
import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'
import { useApi } from '../../contexts/ApiContext'
import { isContextIn, isContextOf, useApi } from '../../contexts/ApiContext'
import { useCheckBalance } from '../../hooks/useCheckBalance'
import { inputToBigInt, getGlobalMaxValue } from '../../utils/bnUtils'
import { TextField } from '../library'
Expand All @@ -11,6 +11,7 @@ import { useAccountBaseFromAccountList } from '../../hooks/useAccountBaseFromAcc
import { MultiAddress } from '@polkadot-api/descriptors'
import { Transaction } from 'polkadot-api'
import { useNetwork } from '../../contexts/NetworkContext'
import { noHydrationKeys } from '../../types'

interface Props {
className?: string
Expand All @@ -22,7 +23,8 @@ interface Props {
const BalancesTransfer = ({ className, onSetExtrinsic, onSetErrorMessage, from }: Props) => {
const accountBase = useAccountBaseFromAccountList({ withAccountsFromAddressBook: true })
const [selected, setSelected] = useState<AccountBaseInfo | undefined>()
const { api, chainInfo, apiDescriptor } = useApi()
const ctx = useApi()
const { api, chainInfo, apiDescriptor } = ctx
const [amountString, setAmountString] = useState('')
const [amount, setAmount] = useState<bigint | undefined>()
const [amountError, setAmountError] = useState('')
Expand All @@ -43,7 +45,7 @@ const BalancesTransfer = ({ className, onSetExtrinsic, onSetErrorMessage, from }
}, [amount, amountError, hasEnoughFreeBalance, onSetErrorMessage])

useEffect(() => {
if (!api || !selectedNetwork) {
if (!ctx?.api || !api || !selectedNetwork) {
onSetExtrinsic(undefined)
return
}
Expand All @@ -53,19 +55,19 @@ const BalancesTransfer = ({ className, onSetExtrinsic, onSetErrorMessage, from }
return
}

const extrinsic =
apiDescriptor === 'hydration'
? api.tx.Balances.transfer_keep_alive({
dest: toAddress,
value: amount
})
: api.tx.Balances.transfer_keep_alive({
dest: MultiAddress.Id(toAddress),
value: amount
})

onSetExtrinsic(extrinsic)
}, [amount, api, apiDescriptor, chainInfo, onSetExtrinsic, selectedNetwork, toAddress])
const extrinsic = isContextOf(ctx, 'hydration')
? ctx.api.tx.Balances.transfer_keep_alive({
dest: toAddress,
value: amount
})
: isContextIn(ctx, noHydrationKeys) &&
ctx.api.tx.Balances.transfer_keep_alive({
dest: MultiAddress.Id(toAddress),
value: amount
})

!!extrinsic && onSetExtrinsic(extrinsic)
}, [amount, api, apiDescriptor, chainInfo, ctx, onSetExtrinsic, selectedNetwork, toAddress])

const onAddressDestChange = useCallback((account: AccountBaseInfo) => {
setSelected(account)
Expand Down
64 changes: 38 additions & 26 deletions packages/ui/src/components/modals/ChangeMultisig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ import AccountDisplay from '../AccountDisplay/AccountDisplay'
import ThresholdSelection from '../../pages/Creation/ThresholdSelection'
import SignatorySelection from '../select/SignatorySelection'
import Summary from '../../pages/Creation/Summary'
import {
isContextIn,
isContextOf,
noHydrationKeys_1,
noHydrationKeys_2,
useApi
} from '../../contexts/ApiContext'
import { isContextIn, isContextOf, useApi } from '../../contexts/ApiContext'
import { useAccounts } from '../../contexts/AccountsContext'
import { useSigningCallback } from '../../hooks/useSigningCallback'
import { AccountBadge } from '../../types'
import { AccountBadge, noHydrationKeys_1, noHydrationKeys_2, noHydrationKeys_3 } from '../../types'
import { getIntersection } from '../../utils/arrayUtils'
import GenericAccountSelection, { AccountBaseInfo } from '../select/GenericAccountSelection'
import { useProxyAdditionNeededFunds } from '../../hooks/useProxyAdditionNeededFunds'
Expand Down Expand Up @@ -141,12 +135,18 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
proxy_type: Enum('Any'),
delay: 0
})
: isContextIn(ctx, noHydrationKeys_2) &&
ctx.api.tx.Proxy.add_proxy({
delegate: MultiAddress.Id(newMultisigAddress),
proxy_type: Enum('Any'),
delay: 0
})
: isContextIn(ctx, noHydrationKeys_2)
? ctx.api.tx.Proxy.add_proxy({
delegate: MultiAddress.Id(newMultisigAddress),
proxy_type: Enum('Any'),
delay: 0
})
: isContextIn(ctx, noHydrationKeys_3) &&
ctx.api.tx.Proxy.add_proxy({
delegate: MultiAddress.Id(newMultisigAddress),
proxy_type: Enum('Any'),
delay: 0
})

if (!addProxyTx) return

Expand All @@ -162,12 +162,18 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
force_proxy_type: undefined,
call: addProxyTx.decodedCall
})
: isContextIn(ctx, noHydrationKeys_2) &&
ctx.api.tx.Proxy.proxy({
real: MultiAddress.Id(selectedMultiProxy?.proxy),
force_proxy_type: undefined,
call: addProxyTx.decodedCall
})
: isContextIn(ctx, noHydrationKeys_2)
? ctx.api.tx.Proxy.proxy({
real: MultiAddress.Id(selectedMultiProxy?.proxy),
force_proxy_type: undefined,
call: addProxyTx.decodedCall
})
: isContextIn(ctx, noHydrationKeys_3) &&
ctx.api.tx.Proxy.proxy({
real: MultiAddress.Id(selectedMultiProxy?.proxy),
force_proxy_type: undefined,
call: addProxyTx.decodedCall
})

if (!proxyTx) return

Expand Down Expand Up @@ -257,12 +263,18 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
force_proxy_type: undefined,
call: removeProxyTx.decodedCall
})
: isContextIn(ctx, noHydrationKeys_2) &&
api.tx.Proxy.proxy({
real: MultiAddress.Id(selectedMultiProxy?.proxy),
force_proxy_type: undefined,
call: removeProxyTx.decodedCall
})
: isContextIn(ctx, noHydrationKeys_2)
? ctx.api.tx.Proxy.proxy({
real: MultiAddress.Id(selectedMultiProxy?.proxy),
force_proxy_type: undefined,
call: removeProxyTx.decodedCall
})
: isContextIn(ctx, noHydrationKeys_3) &&
ctx.api.tx.Proxy.proxy({
real: MultiAddress.Id(selectedMultiProxy?.proxy),
force_proxy_type: undefined,
call: removeProxyTx.decodedCall
})

if (!proxyTx) return

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { nodesCoretimeSVG } from './logos/coretimeSVG'
import { polimecSVG } from './logos/polimecSVG'
import usdc from './logos/usdc.svg'
import usdt from './logos/usdt.svg'
import { DESCRIPTORS } from './contexts/ApiContext'
import { DESCRIPTORS } from './types'

export const DAPP_NAME = 'Multix'
export const ICON_SIZE_LARGE = 92
Expand Down
65 changes: 1 addition & 64 deletions packages/ui/src/contexts/ApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,7 @@ import { ethereumChains } from '../utils/ethereumChains'
import { CompatibilityToken, createClient, PolkadotClient, TypedApi } from 'polkadot-api'
import { getWsProvider } from 'polkadot-api/ws-provider/web'
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat'
import {
acala,
bifrostDot,
dot,
dotAssetHub,
hydration,
khala,
ksm,
ksmAssetHub,
paseo,
phala,
polimec,
coretimeDot,
westend
} from '@polkadot-api/descriptors'

export const DESCRIPTORS = {
acala,
bifrostDot,
dot,
dotAssetHub,
hydration,
khala,
ksm,
ksmAssetHub,
paseo,
phala,
polimec,
coretimeDot,
westend
} as const

export const DESCRIPTORS_NOT_HYDRATION_1 = {
acala,
bifrostDot,
dot,
dotAssetHub,
khala,
phala,
paseo,
polimec
} as const

export const DESCRIPTORS_NOT_HYDRATION_2 = {
ksm,
ksmAssetHub,
coretimeDot,
westend
} as const

export type ApiDescriptors = keyof typeof DESCRIPTORS
export type ApiDescriptorsNotHydration_1 = keyof typeof DESCRIPTORS_NOT_HYDRATION_1
export type ApiDescriptorsNotHydration_2 = keyof typeof DESCRIPTORS_NOT_HYDRATION_1

export type Descriptors<Id extends ApiDescriptors> = (typeof DESCRIPTORS)[Id]
type ApiOf<Id extends ApiDescriptors> = TypedApi<Descriptors<Id>>

export const noHydrationKeys_1 = Object.keys(
DESCRIPTORS_NOT_HYDRATION_1
) as ApiDescriptorsNotHydration_1[]

export const noHydrationKeys_2 = Object.keys(
DESCRIPTORS_NOT_HYDRATION_2
) as ApiDescriptorsNotHydration_2[]
import { ApiDescriptors, ApiOf, Descriptors, DESCRIPTORS } from '../types'

type ApiContextProps = {
children: React.ReactNode | React.ReactNode[]
Expand Down
51 changes: 24 additions & 27 deletions packages/ui/src/hooks/useGetAssetBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect, useState } from 'react'
import { useApi } from '../contexts/ApiContext'
import { isContextIn, useApi } from '../contexts/ApiContext'
import { formatBigIntBalance } from '../utils/formatBnBalance'
import { dotAssetHub, DotAssetHubQueries } from '@polkadot-api/descriptors'
import { TypedApi } from 'polkadot-api'
import { DotAssetHubQueries } from '@polkadot-api/descriptors'
import { assetHubKeys } from '../types'

interface useGetBalanceProps {
address?: string
Expand All @@ -15,41 +15,38 @@ export const useGetAssetBalance = ({
numberAfterComma = 4,
assetId
}: useGetBalanceProps) => {
const { api } = useApi()
const ctx = useApi()
const [balance, setBalance] = useState<bigint | null>(null)
const [balanceFormatted, setFormattedBalance] = useState<string | null>(null)
const [assetMetadata, setAssetMetadata] = useState<
DotAssetHubQueries['Assets']['Metadata']['Value'] | null
>(null)

useEffect(() => {
if (!api || !assetId) return
;(api as TypedApi<typeof dotAssetHub>).query.Assets.Metadata.getValue(assetId)
.then(setAssetMetadata)
.catch(console.error)
}, [api, assetId])
if (!ctx?.api || !isContextIn(ctx, assetHubKeys) || !assetId) return
ctx.api.query.Assets.Metadata.getValue(assetId).then(setAssetMetadata).catch(console.error)
}, [ctx, assetId])

useEffect(() => {
if (!api || !address || !assetMetadata || !assetId) return

const unsub = (api as TypedApi<typeof dotAssetHub>).query.Assets.Account.watchValue(
assetId,
address,
'best'
).subscribe((res) => {
const balance = res?.balance || 0n

setBalance(balance)
setFormattedBalance(
formatBigIntBalance(balance, assetMetadata.decimals, {
numberAfterComma,
tokenSymbol: assetMetadata.symbol.asText()
})
)
})
if (!ctx?.api || !isContextIn(ctx, assetHubKeys) || !address || !assetMetadata || !assetId)
return

const unsub = ctx.api.query.Assets.Account.watchValue(assetId, address, 'best').subscribe(
(res) => {
const balance = res?.balance || 0n

setBalance(balance)
setFormattedBalance(
formatBigIntBalance(balance, assetMetadata.decimals, {
numberAfterComma,
tokenSymbol: assetMetadata.symbol.asText()
})
)
}
)

return () => unsub && unsub.unsubscribe()
}, [address, api, assetId, assetMetadata, numberAfterComma])
}, [address, assetId, assetMetadata, ctx, numberAfterComma])

return { balance, balanceFormatted }
}
4 changes: 2 additions & 2 deletions packages/ui/src/hooks/useGetIdentity.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback } from 'react'
import { useIdenityApi } from './useIdentityApi'
import { useIdentityApi } from './useIdentityApi'
import { FixedSizeBinary, TypedApi } from 'polkadot-api'
import { dotPpl, IdentityData, IdentityJudgement } from '@polkadot-api/descriptors'

Expand All @@ -9,7 +9,7 @@ export interface IdentityInfo extends Record<string, any> {
}

export const useGetIdentity = () => {
const { api } = useIdenityApi()
const { api } = useIdentityApi()

const getIdentity = useCallback(
async (address: string) => {
Expand Down
Loading

0 comments on commit ff2434c

Please sign in to comment.