Skip to content

Commit

Permalink
fix: mana in block issued (#8318)
Browse files Browse the repository at this point in the history
* fix: mana in block issued

* fix: minor improvement

* fix: formatTokenAmountDefault test
  • Loading branch information
cpl121 authored Apr 5, 2024
1 parent d98d6c8 commit 1da3bbf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
$: formattedGiftedStorageDeposit = formatTokenAmountPrecise(activity?.giftedStorageDeposit ?? 0, getBaseToken())
$: formattedSurplus = formatTokenAmountPrecise(activity?.surplus ?? 0, getBaseToken())
$: formattedGasFee = formatTokenAmountPrecise(Number(gasFee ?? 0), getBaseToken())
$: formattedManaPrefix = activity.direction === ActivityDirection.Incoming ? '' : '- '
$: formattedManaPrefix =
activity.direction === ActivityDirection.Incoming || Number(activity?.mana) === 0 ? '' : '- '
$: formattedMana = formattedManaPrefix + formatTokenAmountBestMatch(Number(activity?.mana ?? 0), DEFAULT_MANA)
let transactionDetailsList: IKeyValueBoxList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ describe('File: formatTokenAmountDefault.ts', () => {
})
it('should throw an error if amount is not an integer', () => {
const amount = 42.069
expect(() => formatTokenAmountDefault(amount, decimalToken)).toThrow()
const formattedAmount = formatTokenAmountDefault(amount, decimalToken)
expect(formattedAmount).toBe('-')
})
it('should throw an error if amount is negative', () => {
const amount = -42069
expect(() => formatTokenAmountDefault(amount, decimalToken)).toThrow()
const formattedAmount = formatTokenAmountDefault(amount, decimalToken)
expect(formattedAmount).toBe('-')
})
it('should add correct decimal character for german', () => {
appSettings.update((state) => ({ ...state, language: 'de' }))
Expand Down
22 changes: 13 additions & 9 deletions packages/shared/lib/core/wallet/utils/formatTokenAmountDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ export function formatTokenAmountDefault(
unit?: string,
useGrouping: boolean = true
): string {
if (amount < 0) {
throw new Error('Amount is negative')
} else if (isDecimal(amount)) {
throw new Error('Amount is a decimal number')
} else if (unit && tokenMetadata?.standard === TokenStandard.BaseToken && unit === tokenMetadata?.subunit) {
return formatNumber(amount, 0, 0, 0, useGrouping)
} else {
const value = tokenMetadata?.decimals ? amount / 10 ** tokenMetadata?.decimals : amount
return formatNumber(value, 0, tokenMetadata?.decimals ?? 0, 0, useGrouping)
try {
if (amount < 0) {
throw new Error('Amount is negative')
} else if (isDecimal(amount)) {
throw new Error('Amount is a decimal number')
} else if (unit && tokenMetadata?.standard === TokenStandard.BaseToken && unit === tokenMetadata?.subunit) {
return formatNumber(amount, 0, 0, 0, useGrouping)
} else {
const value = tokenMetadata?.decimals ? amount / 10 ** tokenMetadata?.decimals : amount
return formatNumber(value, 0, tokenMetadata?.decimals ?? 0, 0, useGrouping)
}
} catch (err) {
return '-'
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/core/wallet/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './syncWalletsInParallel'
export * from './validateWalletName'
export * from './isVisibleAsset'
export * from './isVisibleActivity'
export * from './isOutputOfSelectedWalletAddress'

// Folders
export * from './generateActivity'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CommonOutput, UnlockConditionType, OutputData, AddressUnlockCondition } from '@iota/sdk/out/types'
import { selectedWallet } from '../stores'
import { get } from 'svelte/store'
import { AddressConverter } from './AddressConverter'

export async function isOutputOfSelectedWalletAddress(outputData: OutputData): Promise<boolean> {
const output = outputData.output as CommonOutput
const walletAddress = await get(selectedWallet)?.address()
const address = (
output.unlockConditions.find(
(unlockCondition) => unlockCondition.type === UnlockConditionType.Address
) as AddressUnlockCondition
).address
const bech32Address = AddressConverter.addressToBech32(address)
return bech32Address === walletAddress
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function preprocessIncomingTransaction(transaction: TransactionWithMetada

const manaCost = outputs
.filter((output) => !output.remainder)
.reduce((acc, output) => acc + Number(output.output.mana), 0)
.reduce((acc, output) => acc + Number(output.output.mana ?? 0), 0)

return {
outputs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { IProcessedTransaction, IWrappedOutput } from '../../interfaces'
import {
AccountOutput,
Output,
OutputType,
OutputWithMetadata,
TransactionWithMetadata,
UTXOInput,
} from '@iota/sdk/out/types'
import { Output, OutputType, OutputWithMetadata, TransactionWithMetadata, UTXOInput } from '@iota/sdk/out/types'
import { computeOutputId } from './computeOutputId'
import { getOutputIdFromTransactionIdAndIndex } from './getOutputIdFromTransactionIdAndIndex'
import { getDirectionFromOutgoingTransaction } from '../transactions'
Expand All @@ -15,6 +8,9 @@ import { getPassiveManaForOutput } from '@core/network'
import { MILLISECONDS_PER_SECOND } from '@core/utils'
import { getUnixTimestampFromNodeInfoAndSlotIndex, nodeInfoProtocolParameters } from '@core/network'
import { get } from 'svelte/store'
import { isImplicitAccountOutput } from '../isImplicitAccountOutput'
import { isOutputOfSelectedWalletAddress } from '../isOutputOfSelectedWalletAddress'
import { isAccountOutput } from '../isAccountOutput'

export async function preprocessOutgoingTransaction(
transaction: TransactionWithMetadata,
Expand All @@ -39,24 +35,23 @@ export async function preprocessOutgoingTransaction(

const inputs = await Promise.all(inputIds.map((inputId) => wallet.getOutput(inputId)))

let manaCost = 0
const prevAccountOutput = inputs.find((input) => (input.output as AccountOutput).accountId)
if (prevAccountOutput) {
const prevMana = getPassiveManaForOutput(prevAccountOutput) ?? 0
const postAccountOutput = outputs.find(
(output) =>
(prevAccountOutput.output as AccountOutput).accountId === (output.output as AccountOutput).accountId
)
manaCost = prevMana - Number(postAccountOutput?.output?.mana ?? 0)
}
const inputsToConsiderWhenCalculatingMana = inputs.filter(
(input) => isAccountOutput(input) || isImplicitAccountOutput(input) || isOutputOfSelectedWalletAddress(input)
)
const prevManaCost = inputsToConsiderWhenCalculatingMana.reduce(
(acc, input) => acc + (getPassiveManaForOutput(input) ?? 0),
0
)

const postManaCost = outputs.reduce((acc, output) => acc + Number(output.output.mana ?? 0), 0)

return {
outputs: outputs,
transactionId,
direction,
time: new Date(slotUnixTimestamp * MILLISECONDS_PER_SECOND),
inclusionState: transaction.inclusionState,
mana: manaCost,
mana: prevManaCost - postManaCost,
wrappedInputs: <IWrappedOutput[]>inputs,
}
}
Expand Down

0 comments on commit 1da3bbf

Please sign in to comment.