|
1 | 1 | <script lang="ts">
|
2 |
| - import { formatUnits, parseUnits } from 'viem' |
| 2 | + import { formatUnits, parseUnits, type Address } from 'viem' |
3 | 3 | import type { Token } from '../../libs/token/types'
|
4 | 4 | import TokenSelector from './TokenSelector.svelte'
|
5 | 5 | import { debounce } from 'debounce'
|
|
11 | 11 | import { account } from '../../stores/account'
|
12 | 12 | import { network } from '../../stores/network'
|
13 | 13 | import { NoAccountAddressError, NotConnectedError } from '../../libs/error'
|
| 14 | + import type { FetchBalanceResult } from '@wagmi/core' |
14 | 15 |
|
15 | 16 | export let token: Token
|
16 | 17 | export let amount: bigint
|
|
20 | 21 |
|
21 | 22 | let inputElem: HTMLInputElement
|
22 | 23 | let insufficientBalance = false
|
| 24 | + let tokenBalance: FetchBalanceResult |
23 | 25 |
|
24 | 26 | $: value = amount ? formatUnits(amount, token.decimals) : ''
|
25 | 27 | $: isTokenSelected = Boolean(token)
|
| 28 | + $: balance = tokenBalance ? `${tokenBalance.formatted} ${tokenBalance.symbol}` : '' |
26 | 29 |
|
27 | 30 | function onTokenSelect(_token: Token) {
|
28 | 31 | token = _token
|
|
39 | 42 | insufficientBalance = false
|
40 | 43 |
|
41 | 44 | // Do we have enough balance to cover the amount?
|
| 45 | + if (amount > tokenBalance.value) { |
| 46 | + insufficientBalance = true |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + const debouncedOnInput = debounce(onInput, inputConfig.debounceWait) |
| 51 | +
|
| 52 | + async function fetchBalance(token?: Token, chainId?: number, address?: Address) { |
| 53 | + if (!token || !chainId || !address) return |
| 54 | +
|
42 | 55 | try {
|
43 |
| - const balanceResult = await getBalance({ |
44 |
| - token: token.address, |
45 |
| - address: $account.address, |
46 |
| - chainId: $network?.id, |
| 56 | + tokenBalance = await getBalance({ |
| 57 | + address, |
| 58 | + chainId, |
| 59 | + token: token?.address, |
47 | 60 | })
|
48 |
| -
|
49 |
| - if (amount > balanceResult.value) { |
50 |
| - insufficientBalance = true |
51 |
| - } |
52 | 61 | } catch (err) {
|
53 | 62 | console.error(err)
|
54 | 63 |
|
|
65 | 74 | }
|
66 | 75 | }
|
67 | 76 |
|
68 |
| - const debouncedOnInput = debounce(onInput, inputConfig.debounceWait) |
| 77 | + $: fetchBalance(token, $network?.id, $account?.address) |
69 | 78 | </script>
|
70 | 79 |
|
71 | 80 | <div class="TokenAmount" class:error={insufficientBalance}>
|
72 | 81 | <TokenSelector value={token} onSelect={onTokenSelect} disableValue={disableToken} />
|
73 | 82 | <div class="flex flex-col relative">
|
| 83 | + {#if balance} |
| 84 | + <div class="balance">Balance: {balance}</div> |
| 85 | + {/if} |
| 86 | + |
74 | 87 | <input
|
75 | 88 | type="number"
|
76 | 89 | placeholder="0.0"
|
|
96 | 109 | </div>
|
97 | 110 |
|
98 | 111 | <style lang="postcss">
|
| 112 | + .balance { |
| 113 | + @apply absolute |
| 114 | + text-sm |
| 115 | + top-[-1rem] |
| 116 | + right-0; |
| 117 | + } |
| 118 | +
|
99 | 119 | .TokenAmount {
|
100 | 120 | @apply relative
|
101 | 121 | flex
|
|
132 | 152 | [role='alert'] {
|
133 | 153 | @apply absolute
|
134 | 154 | flex
|
| 155 | + text-sm |
135 | 156 | items-center
|
136 | 157 | space-x-2
|
137 | 158 | text-red-400
|
|
0 commit comments