Skip to content

Commit

Permalink
Fix balance check (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut authored Apr 5, 2024
1 parent 0abd643 commit f0bd82d
Show file tree
Hide file tree
Showing 11 changed files with 13,981 additions and 4,096 deletions.
4 changes: 2 additions & 2 deletions packages/ui/node-metadata.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
"lint:types": "tsc --pretty",
"lint:fix": "yarn run lint --fix",
"codegen": "graphql-codegen --config graphql.config.json",
"generate:type-from-defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --endpoint ./node-metadata.json --package src/interfaces --input ./src/interfaces",
"generate:types-from-chain": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --endpoint wss://rococo-rpc.polkadot.io --output ./src/interfaces",
"generate:type-from-defs": "dlx ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --endpoint ./node-metadata.json --package src/interfaces --input ./src/interfaces",
"generate:types-from-chain": "dlx ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --endpoint wss://rpc.ibp.network/polkadot --output ./src/interfaces",
"test": "cypress open",
"test:ci": "cypress run --browser chrome --headless"
},
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 @@ -91,7 +91,7 @@ export const networkList: Record<string, NetworkInfo> = {
hydradx: {
chainId: 'hydradx',
explorerNetworkName: 'hydradx',
rpcUrl: 'wss://hydradx-rpc.dwellir.com',
rpcUrl: 'wss://rpc.helikon.io/hydradx',
httpGraphqlUrl: HTTP_GRAPHQL_URL,
logo: hydradxSVG
},
Expand Down
19 changes: 11 additions & 8 deletions packages/ui/src/hooks/useGetBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import { useEffect, useState } from 'react'
import { useApi } from '../contexts/ApiContext'
import { DeriveBalancesAccount } from '@polkadot/api-derive/types'
import { formatBnBalance } from '../utils/formatBnBalance'
import { Balance } from '@polkadot/types/interfaces/runtime'
import BN from 'bn.js'
import { FrameSystemAccountInfo } from '@polkadot/types/lookup'

interface useGetBalanceProps {
address?: string
numberAfterComma?: number
}

export const useGetBalance = ({ address, numberAfterComma = 4 }: useGetBalanceProps) => {
const { api, chainInfo } = useApi()
const [balance, setBalance] = useState<Balance | null>(null)
const [balance, setBalance] = useState<BN | null>(null)
const [balanceFormatted, setFormattedBalance] = useState<string | null>(null)

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

let unsubscribe: () => void

api.derive.balances
.account(address, (info: DeriveBalancesAccount) => {
setBalance(info.freeBalance)
api.query.system
.account(address, ({ data: { free, frozen } }: FrameSystemAccountInfo) => {
const transferable = free.sub(frozen)

setBalance(transferable)
setFormattedBalance(
formatBnBalance(info.freeBalance, chainInfo?.tokenDecimals, {
formatBnBalance(transferable, chainInfo?.tokenDecimals, {
numberAfterComma,
tokenSymbol: chainInfo?.tokenSymbol
})
)
})
.then((unsub) => {
unsubscribe = unsub
unsubscribe = unsub as unknown as () => void
})
.catch(console.error)

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/hooks/useGetMultisigTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const useGetMultisigTx = ({
}

if (forceAsMulti && !extrinsicToCall) {
console.error(
console.warn(
'The extrinsic call is required when multisig.asMulti is called',
extrinsicToCall
)
Expand Down
Loading

0 comments on commit f0bd82d

Please sign in to comment.