-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: simplify validateSendConfirmation (#7410)
- Loading branch information
Showing
2 changed files
with
24 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 4 additions & 15 deletions
19
packages/shared/lib/core/wallet/utils/send/validateSendConfirmation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |