|
20 | 20 | let settingItems: SettingItems
|
21 | 21 |
|
22 | 22 | let displayEstimatedGas: string = ''
|
23 |
| - let canTrade: boolean = false |
| 23 | + let hasPrice: boolean = false |
| 24 | + let hasError: boolean = false |
24 | 25 | let gettingPrice: boolean = false
|
25 | 26 | let trading: boolean = false
|
26 | 27 |
|
|
29 | 30 | $: console.log('Token to:', tokenTo)
|
30 | 31 | $: console.log('Amount to:', amountTo)
|
31 | 32 |
|
| 33 | + $: canTrade = hasPrice && !hasError && !gettingPrice && !trading |
| 34 | +
|
32 | 35 | async function requestPrice(from?: Token, to?: Token, amount?: bigint, chain?: Chain) {
|
33 | 36 | // We only want to query the price if all the required data is present
|
34 | 37 | if (!from || !to || !amount || !chain) return
|
35 | 38 |
|
36 |
| - canTrade = false |
| 39 | + hasPrice = false |
37 | 40 | gettingPrice = true
|
38 | 41 | displayEstimatedGas = ''
|
39 | 42 | amountTo = BigInt(0)
|
|
55 | 58 | estimatedGas = BigInt(priceData.estimatedGas)
|
56 | 59 | displayEstimatedGas = `${formatUnits(estimatedGas, nativeCurrency.decimals)} ${nativeCurrency.symbol}`
|
57 | 60 |
|
58 |
| - canTrade = true |
| 61 | + hasPrice = true |
59 | 62 | } catch (err) {
|
60 | 63 | console.error(err)
|
61 | 64 | notifyError(err, 'There was an error fetching the price')
|
|
101 | 104 | }
|
102 | 105 | }
|
103 | 106 |
|
| 107 | + function onTokenError(event: CustomEvent<boolean>) { |
| 108 | + hasError = event.detail |
| 109 | + } |
| 110 | +
|
104 | 111 | // This function is called everytime one of the dependencies changes
|
105 | 112 | $: requestPrice(tokenFrom, tokenTo, amountFrom, $network)
|
106 | 113 | </script>
|
|
117 | 124 |
|
118 | 125 | <div class="body flex flex-col flex-1 md:flex-none justify-between">
|
119 | 126 | <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} /> |
121 | 128 |
|
122 | 129 | <SwitchToken bind:tokenFrom bind:tokenTo />
|
123 | 130 |
|
124 | 131 | <TokenAmount
|
125 | 132 | bind:token={tokenTo}
|
126 | 133 | bind:amount={amountTo}
|
| 134 | + on:error={onTokenError} |
127 | 135 | disableToken={tokenFrom}
|
128 | 136 | loading={gettingPrice}
|
129 | 137 | readonly />
|
|
133 | 141 | <div class="text-sm md:text-base">
|
134 | 142 | Estimated Gas: <span class="font-bold">{displayEstimatedGas ?? '?'}</span>
|
135 | 143 | </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}> |
140 | 145 | {#if trading}
|
141 | 146 | <Loading text="Trading…" size="sm" layout="row" />
|
142 | 147 | {:else}
|
|
0 commit comments