|
1 | 1 | <script lang="ts">
|
2 |
| - import { chainId, connected, selectedAccount } from 'svelte-web3'; |
3 | 2 | import { AmountRangeInput, Button, Input, Select } from '$components';
|
4 | 3 | import { getBech32AddressLengthFromChain, truncateText } from '$lib/common';
|
5 | 4 | import { InputType } from '$lib/common/enums';
|
6 | 5 | import {
|
7 |
| - L2_NATIVE_GAS_TOKEN_DECIMALS, |
8 | 6 | L1_BASE_TOKEN_DECIMALS,
|
| 7 | + L2_NATIVE_GAS_TOKEN_DECIMALS, |
9 | 8 | } from '$lib/constants';
|
10 | 9 | import {
|
11 | 10 | appConfiguration,
|
|
19 | 18 | import {
|
20 | 19 | connectToWallet,
|
21 | 20 | pollBalance,
|
| 21 | + randomBech32Address, |
22 | 22 | storageDeposit,
|
23 | 23 | withdrawStateStore,
|
24 | 24 | } from '$lib/withdraw';
|
| 25 | + import { chainId, connected, selectedAccount } from 'svelte-web3'; |
25 | 26 |
|
26 | 27 | let formInput: WithdrawFormInput = {
|
27 | 28 | receiverAddress: '',
|
|
31 | 32 | };
|
32 | 33 |
|
33 | 34 | let isWithdrawing: boolean = false;
|
34 |
| - let canSetAmountToWithdraw = true; |
| 35 | + let isBaseTokenValueValid: boolean = true; |
| 36 | + let isNativeTokenValueValid: boolean = true; |
| 37 | + let gasNeeded: number | undefined; |
35 | 38 |
|
36 | 39 | $: updateCanWithdraw($withdrawStateStore.availableBaseTokens, {}, null);
|
37 | 40 | $: formattedBalance = (
|
|
44 | 47 | $: canWithdraw =
|
45 | 48 | $withdrawStateStore?.availableBaseTokens > 0 &&
|
46 | 49 | formInput.baseTokensToSend > 0 &&
|
47 |
| - isValidAddress; |
| 50 | + isValidAddress && |
| 51 | + isBaseTokenValueValid && |
| 52 | + isNativeTokenValueValid; |
48 | 53 | $: $withdrawStateStore.isMetamaskConnected = window.ethereum
|
49 | 54 | ? window.ethereum.isMetamaskConnected
|
50 | 55 | : false;
|
|
85 | 90 | nativeTokens: { [key: string]: number },
|
86 | 91 | nftID?: string,
|
87 | 92 | ) {
|
88 |
| - if (!$withdrawStateStore.iscMagic || !formInput.receiverAddress) { |
89 |
| - return (canSetAmountToWithdraw = false); |
| 93 | + if (!$withdrawStateStore.iscMagic) { |
| 94 | + return; |
90 | 95 | }
|
91 |
| -
|
92 | 96 | const nativeTokensToSend = mapNativeToken(nativeTokens);
|
| 97 | + // If the receiver address is not set, we generate a random one to estimate the gas needed. |
| 98 | + const receivedAddress = formInput.receiverAddress?.length |
| 99 | + ? formInput.receiverAddress |
| 100 | + : randomBech32Address($appConfiguration.bech32Hrp); |
93 | 101 | try {
|
94 |
| - const gasNeeded = await $withdrawStateStore.iscMagic.estimateGas( |
| 102 | + gasNeeded = await $withdrawStateStore.iscMagic.estimateGas( |
95 | 103 | $nodeClient,
|
96 |
| - formInput.receiverAddress, |
| 104 | + receivedAddress, |
97 | 105 | baseTokens,
|
98 | 106 | nativeTokensToSend,
|
99 | 107 | nftID,
|
100 | 108 | );
|
101 |
| -
|
102 |
| - canSetAmountToWithdraw = |
103 |
| - $withdrawStateStore.availableBaseTokens > Number(gasNeeded) + 1; |
104 | 109 | } catch (ex) {
|
| 110 | + gasNeeded = undefined; |
105 | 111 | console.log(ex);
|
106 |
| - return (canSetAmountToWithdraw = false); |
107 | 112 | }
|
108 | 113 | }
|
109 | 114 |
|
|
270 | 275 | <AmountRangeInput
|
271 | 276 | label="{$appConfiguration?.ticker} Token:"
|
272 | 277 | bind:value={formInput.baseTokensToSend}
|
273 |
| - disabled={!canSetAmountToWithdraw} |
| 278 | + bind:valid={isBaseTokenValueValid} |
| 279 | + disabled={!( |
| 280 | + $withdrawStateStore?.availableBaseTokens > |
| 281 | + Number(gasNeeded) + 1 |
| 282 | + )} |
274 | 283 | min={storageDepositAdjustedDecimals}
|
275 | 284 | max={Math.max(
|
276 | 285 | $withdrawStateStore.availableBaseTokens - storageDeposit,
|
|
285 | 294 | label="{nativeToken?.metadata?.name ?? ''} Token:"
|
286 | 295 | decimals={nativeToken?.metadata?.decimals || 0}
|
287 | 296 | max={Number(nativeToken.amount)}
|
| 297 | + bind:valid={isNativeTokenValueValid} |
288 | 298 | />
|
289 | 299 | {/each}
|
290 | 300 | </info-box>
|
|
0 commit comments