Skip to content

Commit

Permalink
fix: simplify validateSendConfirmation (#7410)
Browse files Browse the repository at this point in the history
  • Loading branch information
brancoder authored Sep 12, 2023
1 parent 6950144 commit a683321
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,28 @@
}
async function prepareTransactionOutput(): Promise<void> {
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) {
Expand Down Expand Up @@ -170,7 +174,7 @@
async function onConfirmClick(): Promise<void> {
try {
await validateSendConfirmation($selectedAccount, preparedOutput as CommonOutput)
validateSendConfirmation(preparedOutput as CommonOutput)
if ($isActiveLedgerProfile) {
ledgerPreparedOutput.set(preparedOutput)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
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()
}
}

0 comments on commit a683321

Please sign in to comment.