Skip to content

Commit 2ec5187

Browse files
committed
wip
1 parent 813d275 commit 2ec5187

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
import { onMount } from 'svelte'
3+
4+
export let mask = 'loading'
5+
6+
let maskElem: HTMLSpanElement
7+
let classes = classNames('animate-pulse rounded-md', $$props.class)
8+
9+
onMount(() => {
10+
// The idea is to use same background color as text color
11+
const textColor = globalThis.getComputedStyle(maskElem).getPropertyValue('color')
12+
maskElem.style.backgroundColor = textColor
13+
})
14+
</script>
15+
16+
<span class={classes} bind:this={maskElem}>{mask}</span>

src/components/LoadingText/index.ts

Whitespace-only changes.

src/components/Swap/TokenAmount.svelte

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { formatUnits, parseUnits } from 'viem'
2+
import { formatUnits, parseUnits, type Address } from 'viem'
33
import type { Token } from '../../libs/token/types'
44
import TokenSelector from './TokenSelector.svelte'
55
import { debounce } from 'debounce'
@@ -11,6 +11,7 @@
1111
import { account } from '../../stores/account'
1212
import { network } from '../../stores/network'
1313
import { NoAccountAddressError, NotConnectedError } from '../../libs/error'
14+
import type { FetchBalanceResult } from '@wagmi/core'
1415
1516
export let token: Token
1617
export let amount: bigint
@@ -20,9 +21,11 @@
2021
2122
let inputElem: HTMLInputElement
2223
let insufficientBalance = false
24+
let tokenBalance: FetchBalanceResult
2325
2426
$: value = amount ? formatUnits(amount, token.decimals) : ''
2527
$: isTokenSelected = Boolean(token)
28+
$: balance = tokenBalance ? `${tokenBalance.formatted} ${tokenBalance.symbol}` : ''
2629
2730
function onTokenSelect(_token: Token) {
2831
token = _token
@@ -39,16 +42,22 @@
3942
insufficientBalance = false
4043
4144
// 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+
4255
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,
4760
})
48-
49-
if (amount > balanceResult.value) {
50-
insufficientBalance = true
51-
}
5261
} catch (err) {
5362
console.error(err)
5463
@@ -65,12 +74,16 @@
6574
}
6675
}
6776
68-
const debouncedOnInput = debounce(onInput, inputConfig.debounceWait)
77+
$: fetchBalance(token, $network?.id, $account?.address)
6978
</script>
7079

7180
<div class="TokenAmount" class:error={insufficientBalance}>
7281
<TokenSelector value={token} onSelect={onTokenSelect} disableValue={disableToken} />
7382
<div class="flex flex-col relative">
83+
{#if balance}
84+
<div class="balance">Balance: {balance}</div>
85+
{/if}
86+
7487
<input
7588
type="number"
7689
placeholder="0.0"
@@ -96,6 +109,13 @@
96109
</div>
97110

98111
<style lang="postcss">
112+
.balance {
113+
@apply absolute
114+
text-sm
115+
top-[-1rem]
116+
right-0;
117+
}
118+
99119
.TokenAmount {
100120
@apply relative
101121
flex
@@ -132,6 +152,7 @@
132152
[role='alert'] {
133153
@apply absolute
134154
flex
155+
text-sm
135156
items-center
136157
space-x-2
137158
text-red-400

src/libs/utils/truncateDecimal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function truncateDecimal(num: number, decimalPlaces: number) {
2+
const factor = 10 ** decimalPlaces;
3+
return Math.floor(num * factor) / factor;
4+
}

src/libs/utils/truncateString.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function truncateString(str: string, maxlength: number, strBoundary = '…') {
2+
return str.length > maxlength ? `${str.substring(0, maxlength)}${strBoundary}` : str;
3+
}

0 commit comments

Comments
 (0)