From a683321cbffcf906b159670d1af8edac9bbf5014 Mon Sep 17 00:00:00 2001 From: Bran <52735957+brancoder@users.noreply.github.com> Date: Tue, 12 Sep 2023 17:50:41 +0200 Subject: [PATCH] fix: simplify validateSendConfirmation (#7410) --- .../popups/send/SendConfirmationPopup.svelte | 36 ++++++++++--------- .../utils/send/validateSendConfirmation.ts | 19 +++------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/packages/desktop/components/popups/send/SendConfirmationPopup.svelte b/packages/desktop/components/popups/send/SendConfirmationPopup.svelte index ccfb78e8023..6636d89bc07 100644 --- a/packages/desktop/components/popups/send/SendConfirmationPopup.svelte +++ b/packages/desktop/components/popups/send/SendConfirmationPopup.svelte @@ -122,24 +122,28 @@ } async function prepareTransactionOutput(): Promise { - const transactionDetails = get(newTransactionDetails) - const outputParams = await getOutputParameters(transactionDetails) - preparedOutput = await prepareOutput($selectedAccount.index, outputParams, DEFAULT_TRANSACTION_OPTIONS) + try { + const transactionDetails = get(newTransactionDetails) + const outputParams = await getOutputParameters(transactionDetails) + preparedOutput = await prepareOutput($selectedAccount.index, outputParams, DEFAULT_TRANSACTION_OPTIONS) - await updateStorageDeposit() + await updateStorageDeposit() - // Note: we need to adjust the surplus - // so we make sure that the surplus is always added on top of the minimum storage deposit - if (Number(surplus) > 0) { - if (minimumStorageDeposit >= Number(surplus)) { - visibleSurplus = surplus = undefined - } else { - visibleSurplus = Number(surplus) - minimumStorageDeposit - // Note: we have to hide it because currently, in the sdk, - // the storage deposit return strategy is only looked at - // if the provided amount is < the minimum required storage deposit - hideGiftToggle = true + // Note: we need to adjust the surplus + // so we make sure that the surplus is always added on top of the minimum storage deposit + if (Number(surplus) > 0) { + if (minimumStorageDeposit >= Number(surplus)) { + visibleSurplus = surplus = undefined + } else { + visibleSurplus = Number(surplus) - minimumStorageDeposit + // Note: we have to hide it because currently, in the sdk, + // the storage deposit return strategy is only looked at + // if the provided amount is < the minimum required storage deposit + hideGiftToggle = true + } } + } catch (err) { + handleError(err) } if (transactionDetails.expirationDate === undefined) { @@ -170,7 +174,7 @@ async function onConfirmClick(): Promise { try { - await validateSendConfirmation($selectedAccount, preparedOutput as CommonOutput) + validateSendConfirmation(preparedOutput as CommonOutput) if ($isActiveLedgerProfile) { ledgerPreparedOutput.set(preparedOutput) diff --git a/packages/shared/lib/core/wallet/utils/send/validateSendConfirmation.ts b/packages/shared/lib/core/wallet/utils/send/validateSendConfirmation.ts index 9e57208f2e8..0f61c532a56 100644 --- a/packages/shared/lib/core/wallet/utils/send/validateSendConfirmation.ts +++ b/packages/shared/lib/core/wallet/utils/send/validateSendConfirmation.ts @@ -1,26 +1,15 @@ -import { getSelectedAccount } from '@core/account' -import { InsufficientFundsForStorageDepositError, InvalidExpirationDateTimeError } from '@contexts/wallet' +import { InvalidExpirationDateTimeError } from '@contexts/wallet' import { convertUnixTimestampToDate, isValidExpirationDateTime } from '@core/utils' -import { getStorageDepositFromOutput } from '../generateActivity/helper' -import { IAccountState } from '@core/account/interfaces' -import { CommonOutput, ExpirationUnlockCondition, OutputType, UnlockConditionType } from '@iota/sdk/out/types' - -export async function validateSendConfirmation(account: IAccountState, output: CommonOutput): Promise { - const parseNumber = (value: string) => parseInt(value, 10) ?? 0 - const amount = parseNumber(output?.amount) - const balance = parseNumber(getSelectedAccount()?.balances?.baseCoin.available.toString() ?? '0') - const { storageDeposit, giftedStorageDeposit } = await getStorageDepositFromOutput(account, output) +import { CommonOutput, ExpirationUnlockCondition, UnlockConditionType } from '@iota/sdk/out/types' +export function validateSendConfirmation(output: CommonOutput): void { const expirationUnlockCondition = output.unlockConditions.find( (c) => c.type === UnlockConditionType.Expiration ) as ExpirationUnlockCondition const expirationUnixTime = expirationUnlockCondition?.unixTime const expirationDateTime = expirationUnixTime ? convertUnixTimestampToDate(expirationUnixTime) : undefined - const isNft = output.type === OutputType.Nft - if (!isNft && (balance < amount + storageDeposit || balance < amount + giftedStorageDeposit)) { - throw new InsufficientFundsForStorageDepositError() - } else if (expirationDateTime && !isValidExpirationDateTime(expirationDateTime)) { + if (expirationDateTime && !isValidExpirationDateTime(expirationDateTime)) { throw new InvalidExpirationDateTimeError() } }