Skip to content

Commit

Permalink
Fix: Leaving dust when sending transaction (#7412)
Browse files Browse the repository at this point in the history
* fix: remove balance check from validateSendConfirmation

* fix: dont allow leaving dust on the address

* fix: required storage deposit for minimal basic output

* chore: rename typo

* chore: add comments

* fix: conditions for leaving dust

* chore: improve variable naming & error message

---------

Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
  • Loading branch information
brancoder and begonaalvarezd authored Sep 13, 2023
1 parent ab1adc6 commit d168f57
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/shared/components/inputs/AssetAmountInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
convertToRawAmount,
formatTokenAmountBestMatch,
formatTokenAmountDefault,
getRequiredStorageDepositForMinimalBasicOutput,
getUnitFromTokenMetadata,
visibleSelectedAccountAssets,
} from '@core/wallet'
Expand Down Expand Up @@ -51,9 +52,14 @@
unit = undefined
}
export function validate(allowZeroOrNull = false): Promise<void> {
export async function validate(allowZeroOrNull = false): Promise<void> {
const amountAsFloat = parseCurrency(amount)
const isAmountZeroOrNull = !Number(amountAsFloat)
const standard = asset?.metadata?.standard
const remainderBalance = Number(Big(availableBalance)?.minus(bigAmount))
// Calculate the minimum required storage deposit for a minimal basic output
// This is used to check if the user is leaving dust behind that cant cover the storage deposit
const minRequiredStorageDeposit = await getRequiredStorageDepositForMinimalBasicOutput()
// Zero value transactions can still contain metadata/tags
error = ''
if (allowZeroOrNull && isAmountZeroOrNull) {
Expand All @@ -62,7 +68,7 @@
} else if (isAmountZeroOrNull) {
error = localize('error.send.amountInvalidFormat')
} else if (
((asset?.metadata?.standard === TokenStandard.BaseToken && unit === asset?.metadata?.subunit) ||
((standard === TokenStandard.BaseToken && unit === asset?.metadata?.subunit) ||
(unit === getUnitFromTokenMetadata(asset?.metadata) && asset?.metadata?.decimals === 0)) &&
Number.parseInt(amount, 10).toString() !== amount
) {
Expand All @@ -73,6 +79,17 @@
error = localize('error.send.amountZero')
} else if (!bigAmount.mod(1).eq(Big(0))) {
error = localize('error.send.amountSmallerThanSubunit')
} else if (
standard === TokenStandard.BaseToken &&
remainderBalance !== 0 &&
remainderBalance < minRequiredStorageDeposit
) {
// don't allow leaving dust(amount less than minimum required storage deposit) for base token
error = localize('error.send.leavingDust', {
values: {
minRequiredStorageDeposit: formatTokenAmountBestMatch(minRequiredStorageDeposit, asset?.metadata),
},
})
}
if (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getClient } from '@core/profile-manager/api/getClient'
import { EMPTY_HEX_ID } from '@core/wallet'
import {
AddressType,
AddressUnlockCondition,
BasicOutputBuilderParams,
Ed25519Address,
UnlockConditionType,
} from '@iota/sdk/out/types'
import { plainToInstance } from 'class-transformer'

const MOCK_BASIC_OUTPUT_AMOUNT = '10'

/**
* Calculate minimum storage deposit required for the most minimal basic output with a single address unlock condition.
* @returns The minimum storage deposit.
*/
export async function getRequiredStorageDepositForMinimalBasicOutput(): Promise<number> {
const address = {
type: AddressType.Ed25519,
pubKeyHash: EMPTY_HEX_ID,
}
const ed25519Address = plainToInstance(Ed25519Address, address)

const unlockCondition = {
type: UnlockConditionType.Address,
address: ed25519Address,
}
const addressUnlockCondition = plainToInstance(AddressUnlockCondition, unlockCondition)

const params: BasicOutputBuilderParams = {
amount: MOCK_BASIC_OUTPUT_AMOUNT,
unlockConditions: [addressUnlockCondition],
}

const client = await getClient()
const basicOutput = await client.buildBasicOutput(params)
const minimumRequiredStorageDeposit = await client.minimumRequiredStorageDeposit(basicOutput)

return minimumRequiredStorageDeposit
}
1 change: 1 addition & 0 deletions packages/shared/lib/core/wallet/utils/outputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './isOutputAsync'
export * from './preprocessGroupedOutputs'
export * from './preprocessTransaction'
export * from './getOutputIdFromTransactionIdAndIndex'
export * from './getRequiredStorageDepositForMinimalBasicOutput'
2 changes: 1 addition & 1 deletion packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@
"cannotClaimTwice": "Output has been already claimed",
"noToAccount": "You have not selected a wallet to send the funds to.",
"sendingDust": "You cannot send less than 1 Mi.",
"leavingDust": "You cannot leave less than 1 Mi on your address.",
"leavingDust": "You cannot leave less than the minimum required storage deposit ({minRequiredStorageDeposit}) on your address.",
"cancelled": "The transaction was cancelled.",
"transaction": "There was an error sending your transaction. Please try again.",
"invalidExpirationDateTime": "The chosen expiration date/time is invalid.",
Expand Down

0 comments on commit d168f57

Please sign in to comment.