|
1 | 1 | <script lang="ts">
|
2 | 2 | import { formatCurrency, localize, parseCurrency } from '@core/i18n'
|
3 | 3 | import { getMarketAmountFromAssetValue } from '@core/market/utils'
|
| 4 | + import { activeProfile } from '@core/profile' |
4 | 5 | import { getMaxDecimalsFromTokenMetadata } from '@core/token/utils'
|
5 | 6 | import {
|
6 | 7 | IAsset,
|
|
15 | 16 | import Big from 'big.js'
|
16 | 17 | import { AmountInput, AssetDropdown, InputContainer, SliderInput, Text, TooltipIcon } from 'shared/components'
|
17 | 18 | import UnitInput from './UnitInput.svelte'
|
18 |
| - import { activeProfile } from '@core/profile' |
19 | 19 |
|
20 | 20 | export let inputElement: HTMLInputElement = undefined
|
21 | 21 | export let disabled = false
|
|
53 | 53 | }
|
54 | 54 |
|
55 | 55 | export async function validate(allowZeroOrNull = false): Promise<void> {
|
56 |
| - const amountAsFloat = parseCurrency(amount) |
57 |
| - const isAmountZeroOrNull = !Number(amountAsFloat) |
58 |
| - const standard = asset?.metadata?.standard |
59 |
| - const remainderBalance = Number(Big(availableBalance)?.minus(bigAmount)) |
60 |
| - // Calculate the minimum required storage deposit for a minimal basic output |
61 |
| - // This is used to check if the user is leaving dust behind that cant cover the storage deposit |
62 |
| - const minRequiredStorageDeposit = await getRequiredStorageDepositForMinimalBasicOutput() |
63 |
| - // Zero value transactions can still contain metadata/tags |
64 |
| - error = '' |
65 |
| - if (allowZeroOrNull && isAmountZeroOrNull) { |
66 |
| - rawAmount = Big(0).toString() |
67 |
| - return |
68 |
| - } else if (isAmountZeroOrNull) { |
69 |
| - error = localize('error.send.amountInvalidFormat') |
70 |
| - } else if ( |
71 |
| - ((standard === TokenStandard.BaseToken && unit === asset?.metadata?.subunit) || |
72 |
| - (unit === getUnitFromTokenMetadata(asset?.metadata) && asset?.metadata?.decimals === 0)) && |
73 |
| - Number.parseInt(amount, 10).toString() !== amount |
74 |
| - ) { |
75 |
| - error = localize('error.send.amountNoFloat') |
76 |
| - } else if (bigAmount.gt(Big(availableBalance))) { |
77 |
| - error = localize('error.send.amountTooHigh') |
78 |
| - } else if (bigAmount.lte(Big(0))) { |
79 |
| - error = localize('error.send.amountZero') |
80 |
| - } else if (!bigAmount.mod(1).eq(Big(0))) { |
81 |
| - error = localize('error.send.amountSmallerThanSubunit') |
82 |
| - } else if ( |
83 |
| - standard === TokenStandard.BaseToken && |
84 |
| - remainderBalance !== 0 && |
85 |
| - remainderBalance < minRequiredStorageDeposit |
86 |
| - ) { |
87 |
| - // don't allow leaving dust(amount less than minimum required storage deposit) for base token |
88 |
| - error = localize('error.send.leavingDust', { |
89 |
| - values: { |
90 |
| - minRequiredStorageDeposit: formatTokenAmountBestMatch(minRequiredStorageDeposit, asset?.metadata), |
91 |
| - }, |
92 |
| - }) |
93 |
| - } |
94 |
| -
|
95 |
| - if (error) { |
| 56 | + try { |
| 57 | + const amountAsFloat = parseCurrency(amount) |
| 58 | + const isAmountZeroOrNull = !Number(amountAsFloat) |
| 59 | + const standard = asset?.metadata?.standard |
| 60 | + const remainderBalance = bigAmount && Number(Big(availableBalance)?.minus(bigAmount)) |
| 61 | + // Calculate the minimum required storage deposit for a minimal basic output |
| 62 | + // This is used to check if the user is leaving dust behind that cant cover the storage deposit |
| 63 | + const minRequiredStorageDeposit = await getRequiredStorageDepositForMinimalBasicOutput() |
| 64 | + // Zero value transactions can still contain metadata/tags |
| 65 | + error = '' |
| 66 | + if (allowZeroOrNull && isAmountZeroOrNull) { |
| 67 | + rawAmount = Big(0).toString() |
| 68 | + return |
| 69 | + } else if (isAmountZeroOrNull) { |
| 70 | + throw new Error(localize('error.send.amountInvalidFormat')) |
| 71 | + } else if ( |
| 72 | + ((standard === TokenStandard.BaseToken && unit === asset?.metadata?.subunit) || |
| 73 | + (unit === getUnitFromTokenMetadata(asset?.metadata) && asset?.metadata?.decimals === 0)) && |
| 74 | + Number.parseInt(amount, 10).toString() !== amount |
| 75 | + ) { |
| 76 | + throw new Error(localize('error.send.amountNoFloat')) |
| 77 | + } else if (bigAmount?.gt(Big(availableBalance))) { |
| 78 | + throw new Error(localize('error.send.amountTooHigh')) |
| 79 | + } else if (bigAmount?.lte(Big(0))) { |
| 80 | + throw new Error(localize('error.send.amountZero')) |
| 81 | + } else if (!bigAmount?.mod(1).eq(Big(0))) { |
| 82 | + throw new Error(localize('error.send.amountSmallerThanSubunit')) |
| 83 | + } else if ( |
| 84 | + standard === TokenStandard.BaseToken && |
| 85 | + remainderBalance && |
| 86 | + remainderBalance !== 0 && |
| 87 | + remainderBalance < minRequiredStorageDeposit |
| 88 | + ) { |
| 89 | + // don't allow leaving dust(amount less than minimum required storage deposit) for base token |
| 90 | + throw new Error( |
| 91 | + localize('error.send.leavingDust', { |
| 92 | + values: { |
| 93 | + minRequiredStorageDeposit: formatTokenAmountBestMatch( |
| 94 | + minRequiredStorageDeposit, |
| 95 | + asset?.metadata |
| 96 | + ), |
| 97 | + }, |
| 98 | + }) |
| 99 | + ) |
| 100 | + } |
| 101 | + rawAmount = bigAmount.toString() |
| 102 | + } catch (err) { |
| 103 | + error = err?.message ?? err |
96 | 104 | return Promise.reject(error)
|
97 | 105 | }
|
98 |
| - rawAmount = bigAmount.toString() |
99 | 106 | }
|
100 | 107 | </script>
|
101 | 108 |
|
|
0 commit comments