Skip to content

Commit e8d0ec9

Browse files
feat: allow always editing base token range input in withdraw & token validations (#45)
* feat: allow always editing base token range input in withdraw & add base token validation * feat: add native toke validatoion
1 parent 3134a4b commit e8d0ec9

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/routes/sections/withdraw-section.svelte

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<script lang="ts">
2-
import { chainId, connected, selectedAccount } from 'svelte-web3';
32
import { AmountRangeInput, Button, Input, Select } from '$components';
43
import { getBech32AddressLengthFromChain, truncateText } from '$lib/common';
54
import { InputType } from '$lib/common/enums';
65
import {
7-
L2_NATIVE_GAS_TOKEN_DECIMALS,
86
L1_BASE_TOKEN_DECIMALS,
7+
L2_NATIVE_GAS_TOKEN_DECIMALS,
98
} from '$lib/constants';
109
import {
1110
appConfiguration,
@@ -19,9 +18,11 @@
1918
import {
2019
connectToWallet,
2120
pollBalance,
21+
randomBech32Address,
2222
storageDeposit,
2323
withdrawStateStore,
2424
} from '$lib/withdraw';
25+
import { chainId, connected, selectedAccount } from 'svelte-web3';
2526
2627
let formInput: WithdrawFormInput = {
2728
receiverAddress: '',
@@ -31,7 +32,9 @@
3132
};
3233
3334
let isWithdrawing: boolean = false;
34-
let canSetAmountToWithdraw = true;
35+
let isBaseTokenValueValid: boolean = true;
36+
let isNativeTokenValueValid: boolean = true;
37+
let gasNeeded: number | undefined;
3538
3639
$: updateCanWithdraw($withdrawStateStore.availableBaseTokens, {}, null);
3740
$: formattedBalance = (
@@ -44,7 +47,9 @@
4447
$: canWithdraw =
4548
$withdrawStateStore?.availableBaseTokens > 0 &&
4649
formInput.baseTokensToSend > 0 &&
47-
isValidAddress;
50+
isValidAddress &&
51+
isBaseTokenValueValid &&
52+
isNativeTokenValueValid;
4853
$: $withdrawStateStore.isMetamaskConnected = window.ethereum
4954
? window.ethereum.isMetamaskConnected
5055
: false;
@@ -85,25 +90,25 @@
8590
nativeTokens: { [key: string]: number },
8691
nftID?: string,
8792
) {
88-
if (!$withdrawStateStore.iscMagic || !formInput.receiverAddress) {
89-
return (canSetAmountToWithdraw = false);
93+
if (!$withdrawStateStore.iscMagic) {
94+
return;
9095
}
91-
9296
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);
93101
try {
94-
const gasNeeded = await $withdrawStateStore.iscMagic.estimateGas(
102+
gasNeeded = await $withdrawStateStore.iscMagic.estimateGas(
95103
$nodeClient,
96-
formInput.receiverAddress,
104+
receivedAddress,
97105
baseTokens,
98106
nativeTokensToSend,
99107
nftID,
100108
);
101-
102-
canSetAmountToWithdraw =
103-
$withdrawStateStore.availableBaseTokens > Number(gasNeeded) + 1;
104109
} catch (ex) {
110+
gasNeeded = undefined;
105111
console.log(ex);
106-
return (canSetAmountToWithdraw = false);
107112
}
108113
}
109114
@@ -270,7 +275,11 @@
270275
<AmountRangeInput
271276
label="{$appConfiguration?.ticker} Token:"
272277
bind:value={formInput.baseTokensToSend}
273-
disabled={!canSetAmountToWithdraw}
278+
bind:valid={isBaseTokenValueValid}
279+
disabled={!(
280+
$withdrawStateStore?.availableBaseTokens >
281+
Number(gasNeeded) + 1
282+
)}
274283
min={storageDepositAdjustedDecimals}
275284
max={Math.max(
276285
$withdrawStateStore.availableBaseTokens - storageDeposit,
@@ -285,6 +294,7 @@
285294
label="{nativeToken?.metadata?.name ?? ''} Token:"
286295
decimals={nativeToken?.metadata?.decimals || 0}
287296
max={Number(nativeToken.amount)}
297+
bind:valid={isNativeTokenValueValid}
288298
/>
289299
{/each}
290300
</info-box>

0 commit comments

Comments
 (0)