Skip to content

Commit

Permalink
add support for domain wallet balance
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Feb 18, 2025
1 parent 86b553d commit aaa23ce
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions explorer/src/components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,29 @@ export const Swap: FC = () => {
const loadWalletBalance = useCallback(async () => {
if (!api || !actingAccount) return

if (actingAccount.type === WalletType.subspace) {
const balance = await api.query.system.account(actingAccount.address)
setWalletBalance(
formatUnitsToNumber((balance.toJSON() as { data: { free: string } }).data.free),
)
switch (true) {
case from === 'consensus': {
if (actingAccount.type === WalletType.subspace) {
const balance = await api.query.system.account(actingAccount.address)
setWalletBalance(
formatUnitsToNumber((balance.toJSON() as { data: { free: string } }).data.free),
)
}
break
}
case from && from.startsWith('domain'): {
const domainApi = domainsApis[from.replace('domainId', '')]
if (!domainApi) return
const balance = await domainApi.api.query.system.account(actingAccount.address)
setWalletBalance(
formatUnitsToNumber((balance.toJSON() as { data: { free: string } }).data.free),
)
break
}
default:
break
}
}, [api, actingAccount])
}, [api, actingAccount, domainsApis, from])

const maxAmount = useMemo(
() =>
Expand All @@ -282,6 +298,10 @@ export const Swap: FC = () => {
[from, to, amount, receiver],
)

const isWalletBalanceEnough = useMemo(() => {
return walletBalance > AMOUNT_TO_SUBTRACT_FROM_MAX_AMOUNT_FOR_XDM + parseFloat(amount || '0')
}, [walletBalance, amount])

useEffect(() => {
loadWalletBalance()
}, [api, actingAccount, loadWalletBalance])
Expand Down Expand Up @@ -347,12 +367,18 @@ export const Swap: FC = () => {
{!actingAccount ? (
<WalletButton />
) : (
<button
className='block rounded-full bg-grayDarker px-5 py-3 text-[13px] font-semibold leading-4 text-white dark:bg-primaryAccent'
type='submit'
>
Send token
</button>
<>
{!isWalletBalanceEnough ? (
<span className='text-red-500'>Insufficient wallet balance</span>
) : (
<button
className='block rounded-full bg-grayDarker px-5 py-3 text-[13px] font-semibold leading-4 text-white dark:bg-primaryAccent'
type='submit'
>
Send token
</button>
)}
</>
)}
</div>
</>
Expand Down

0 comments on commit aaa23ce

Please sign in to comment.