Skip to content

Commit 270ef11

Browse files
committed
Improve trade button
1 parent 39d37be commit 270ef11

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/components/Swap/Swap.svelte

+13-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
let settingItems: SettingItems
2121
2222
let displayEstimatedGas: string = ''
23-
let canTrade: boolean = false
23+
let hasPrice: boolean = false
24+
let hasError: boolean = false
2425
let gettingPrice: boolean = false
2526
let trading: boolean = false
2627
@@ -29,11 +30,13 @@
2930
$: console.log('Token to:', tokenTo)
3031
$: console.log('Amount to:', amountTo)
3132
33+
$: canTrade = hasPrice && !hasError && !gettingPrice && !trading
34+
3235
async function requestPrice(from?: Token, to?: Token, amount?: bigint, chain?: Chain) {
3336
// We only want to query the price if all the required data is present
3437
if (!from || !to || !amount || !chain) return
3538
36-
canTrade = false
39+
hasPrice = false
3740
gettingPrice = true
3841
displayEstimatedGas = ''
3942
amountTo = BigInt(0)
@@ -55,7 +58,7 @@
5558
estimatedGas = BigInt(priceData.estimatedGas)
5659
displayEstimatedGas = `${formatUnits(estimatedGas, nativeCurrency.decimals)} ${nativeCurrency.symbol}`
5760
58-
canTrade = true
61+
hasPrice = true
5962
} catch (err) {
6063
console.error(err)
6164
notifyError(err, 'There was an error fetching the price')
@@ -101,6 +104,10 @@
101104
}
102105
}
103106
107+
function onTokenError(event: CustomEvent<boolean>) {
108+
hasError = event.detail
109+
}
110+
104111
// This function is called everytime one of the dependencies changes
105112
$: requestPrice(tokenFrom, tokenTo, amountFrom, $network)
106113
</script>
@@ -117,13 +124,14 @@
117124

118125
<div class="body flex flex-col flex-1 md:flex-none justify-between">
119126
<div class="space-y-2 md:space-y-4 flex flex-col items-center my-4">
120-
<TokenAmount bind:token={tokenFrom} bind:amount={amountFrom} disableToken={tokenTo} />
127+
<TokenAmount bind:token={tokenFrom} bind:amount={amountFrom} on:error={onTokenError} disableToken={tokenTo} />
121128

122129
<SwitchToken bind:tokenFrom bind:tokenTo />
123130

124131
<TokenAmount
125132
bind:token={tokenTo}
126133
bind:amount={amountTo}
134+
on:error={onTokenError}
127135
disableToken={tokenFrom}
128136
loading={gettingPrice}
129137
readonly />
@@ -133,10 +141,7 @@
133141
<div class="text-sm md:text-base">
134142
Estimated Gas: <span class="font-bold">{displayEstimatedGas ?? '?'}</span>
135143
</div>
136-
<button
137-
disabled={!canTrade || trading}
138-
class="btn btn-primary btn-md w-full relative md:btn-lg"
139-
on:click={trade}>
144+
<button disabled={!canTrade} class="btn btn-primary btn-md w-full relative md:btn-lg" on:click={trade}>
140145
{#if trading}
141146
<Loading text="Trading…" size="sm" layout="row" />
142147
{:else}

src/components/Swap/TokenAmount.svelte

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import TokenSelector from './TokenSelector.svelte'
55
import { debounce } from 'debounce'
66
import { inputConfig } from '../../app.config'
7-
import { tick } from 'svelte'
7+
import { createEventDispatcher, tick } from 'svelte'
88
import Loading from '../Loading/Loading.svelte'
99
import FaExclamationTriangle from 'svelte-icons/fa/FaExclamationTriangle.svelte'
1010
import getBalance from '../../libs/token/getBalance'
@@ -20,6 +20,8 @@
2020
export let disableToken: Token
2121
export let loading = false
2222
23+
const dispatch = createEventDispatcher<{ error: boolean }>()
24+
2325
let inputElem: HTMLInputElement
2426
let insufficientBalance = false
2527
let tokenBalanceResult: FetchBalanceResult
@@ -47,6 +49,9 @@
4749
// Do we have enough balance to cover the amount?
4850
if (amount > tokenBalanceResult.value) {
4951
insufficientBalance = true
52+
dispatch('error', true)
53+
} else {
54+
dispatch('error', false)
5055
}
5156
}
5257

0 commit comments

Comments
 (0)