Skip to content

Commit

Permalink
Avoid deprecated EdgeMetadata.amountFiat
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Jan 4, 2024
1 parent 624ad5f commit 51258ac
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
24 changes: 12 additions & 12 deletions src/__tests__/actions/TransactionExportActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const edgeTxs: EdgeTransaction[] = [
metadata: {
name: 'Crazy Person',
category: 'Income:Mo Money',
notes: 'Hell yeah! Thanks for the fish <<&&>>',
amountFiat: 12000.45
exchangeAmount: { 'iso:USD': 12000.45 },
notes: 'Hell yeah! Thanks for the fish <<&&>>'
},
nativeAmount: '123000000',
networkFee: '1000',
Expand All @@ -40,8 +40,8 @@ const edgeTxs: EdgeTransaction[] = [
metadata: {
name: 'Crazy Person 2',
category: 'Expense:Less Money',
notes: 'Hell yeah! Here\'s a fish"',
amountFiat: 36001.45
exchangeAmount: { 'iso:USD': 36001.45 },
notes: 'Hell yeah! Here\'s a fish"'
},
nativeAmount: '-321000000',
networkFee: '2000',
Expand All @@ -61,8 +61,8 @@ const edgeTxs: EdgeTransaction[] = [
metadata: {
name: 'Transfer',
category: 'Transfer:Edge',
notes: '',
amountFiat: 36001.45
exchangeAmount: { 'iso:USD': 36001.45 },
notes: ''
},
nativeAmount: '-321000000',
networkFee: '2000',
Expand All @@ -82,8 +82,8 @@ const edgeTxs: EdgeTransaction[] = [
metadata: {
name: 'Transfer but actually income',
category: 'Transfer:Edge',
notes: '',
amountFiat: 36001.45
exchangeAmount: { 'iso:USD': 36001.45 },
notes: ''
},
nativeAmount: '321000000',
networkFee: '2000',
Expand All @@ -104,8 +104,8 @@ const edgeTxs: EdgeTransaction[] = [
metadata: {
name: 'Transfer but no fee',
category: 'Transfer:Edge',
notes: '',
amountFiat: 36001.45
exchangeAmount: { 'iso:USD': 36001.45 },
notes: ''
},
nativeAmount: '-321000000',
networkFee: '0',
Expand All @@ -125,8 +125,8 @@ const edgeTxs: EdgeTransaction[] = [
metadata: {
name: 'Transfer but no fiat amount',
category: 'Transfer:Edge',
notes: '',
amountFiat: 0
exchangeAmount: { 'iso:USD': 0 },
notes: ''
},
nativeAmount: '-321000000',
networkFee: '2000',
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/scenes/TransactionDetailsScene.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('TransactionDetailsScene', () => {
isSend: true,
memos: [],
metadata: {
amountFiat: -6392.93,
exchangeAmount: { 'iso:USD': -6392.93 },
name: 'timmy'
},
nativeAmount: '-12300000',
Expand Down
69 changes: 37 additions & 32 deletions src/actions/TransactionExportActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ export function updateTxsFiat(wallet: EdgeCurrencyWallet, currencyCode: string,

let promises: Array<Promise<void>> = []
for (const tx of txs) {
if ((tx.metadata?.amountFiat ?? 0) === 0) {
const amountFiat = tx.metadata?.exchangeAmount?.[fiatCurrencyCode] ?? 0

if (amountFiat === 0) {
const date = new Date(tx.date * 1000).toISOString()
promises.push(
getHistoricalRate(`${currencyCode}_${fiatCurrencyCode}`, date)
.then(rate => {
if (tx.metadata == null) {
tx.metadata = {}
tx.metadata = {
...tx.metadata,
exchangeAmount: {
...tx.metadata?.exchangeAmount,
[fiatCurrencyCode]: rate * Number(div(tx.nativeAmount, exchangeDenom.multiplier, DECIMAL_PRECISION))
}
}
tx.metadata.amountFiat = rate * Number(div(tx.nativeAmount, exchangeDenom.multiplier, DECIMAL_PRECISION))
})
.catch(e => console.warn(e.message))
)
Expand Down Expand Up @@ -173,7 +178,7 @@ function makeBitwaveDateTime(date: number): string {
// 2. category set to 'Expense:Network Fee'
// 3. txid set to old txid + '-TRANSFER_TX'

export function getTransferTx(oldEdgeTransaction: EdgeTransaction): EdgeTransaction[] | null {
export function getTransferTx(oldEdgeTransaction: EdgeTransaction, fiatCurrencyCode: string): EdgeTransaction[] | null {
const edgeTransaction = { ...oldEdgeTransaction }
edgeTransaction.metadata = { ...oldEdgeTransaction.metadata }

Expand All @@ -184,7 +189,7 @@ export function getTransferTx(oldEdgeTransaction: EdgeTransaction): EdgeTransact

const nativeAmountNoFee = add(edgeTransaction.nativeAmount, edgeTransaction.networkFee)
let newTxFiatFee = 0
let amountFiat = edgeTransaction.metadata?.amountFiat ?? 0
let amountFiat = edgeTransaction.metadata?.exchangeAmount?.[fiatCurrencyCode] ?? 0

if (amountFiat > 0) {
const exchangeRate: string = div(amountFiat.toString(), edgeTransaction.nativeAmount, 16)
Expand All @@ -195,13 +200,24 @@ export function getTransferTx(oldEdgeTransaction: EdgeTransaction): EdgeTransact

const newEdgeTransaction: EdgeTransaction = { ...edgeTransaction }
newEdgeTransaction.nativeAmount = `-${edgeTransaction.networkFee}`
newEdgeTransaction.metadata = { ...edgeTransaction.metadata }
newEdgeTransaction.metadata.category = `Expense:Network Fee`
newEdgeTransaction.metadata.amountFiat = newTxFiatFee
newEdgeTransaction.metadata = {
...edgeTransaction.metadata,
category: `Expense:Network Fee`,
exchangeAmount: {
...edgeTransaction.metadata?.exchangeAmount,
[fiatCurrencyCode]: newTxFiatFee
}
}
newEdgeTransaction.txid += '-TRANSFER_TX'
edgeTransaction.nativeAmount = nativeAmountNoFee
edgeTransaction.networkFee = '0'
edgeTransaction.metadata.amountFiat = amountFiat
edgeTransaction.metadata = {
...edgeTransaction.metadata,
exchangeAmount: {
...edgeTransaction.metadata?.exchangeAmount,
[fiatCurrencyCode]: amountFiat
}
}
return [edgeTransaction, newEdgeTransaction]
}

Expand All @@ -216,7 +232,7 @@ export function exportTransactionsToQBOInner(
const now = makeOfxDate(dateNow / 1000)

for (const tx of edgeTransactions) {
const newTxs = getTransferTx(tx)
const newTxs = getTransferTx(tx, fiatCurrencyCode)
if (newTxs != null) {
edgeTxToQbo(newTxs[0])
edgeTxToQbo(newTxs[1])
Expand All @@ -229,16 +245,11 @@ export function exportTransactionsToQBOInner(
const TRNAMT: string = denom ? div(edgeTx.nativeAmount, denom, DECIMAL_PRECISION) : edgeTx.nativeAmount
const TRNTYPE = lt(edgeTx.nativeAmount, '0') ? 'DEBIT' : 'CREDIT'
const DTPOSTED = makeOfxDate(edgeTx.date)
let NAME: string = ''
let amountFiat: number = 0
let category: string = ''
let notes: string = ''
if (edgeTx.metadata) {
NAME = edgeTx.metadata.name ? edgeTx.metadata.name : ''
amountFiat = edgeTx.metadata.amountFiat ? edgeTx.metadata.amountFiat : 0
category = edgeTx.metadata.category ? edgeTx.metadata.category : ''
notes = edgeTx.metadata.notes ? edgeTx.metadata.notes : ''
}
const NAME: string = edgeTx.metadata?.name ?? ''
const amountFiat: number = edgeTx.metadata?.exchangeAmount?.[fiatCurrencyCode] ?? 0
const category: string = edgeTx.metadata?.category ?? ''
const notes: string = edgeTx.metadata?.notes ?? ''

const absFiat = abs(amountFiat.toString())
const absAmount = abs(TRNAMT)
const CURRATE = absAmount !== '0' ? div(absFiat, absAmount, 8) : '0'
Expand Down Expand Up @@ -343,7 +354,7 @@ export function exportTransactionsToCSVInner(
const items: any[] = []

for (const tx of edgeTransactions) {
const newTxs = getTransferTx(tx)
const newTxs = getTransferTx(tx, fiatCurrencyCode)
if (newTxs != null) {
edgeTxToCsv(newTxs[0])
edgeTxToCsv(newTxs[1])
Expand All @@ -356,16 +367,10 @@ export function exportTransactionsToCSVInner(
const amount: string = denom ? div(edgeTx.nativeAmount, denom, DECIMAL_PRECISION) : edgeTx.nativeAmount
const networkFeeField: string = denom ? div(edgeTx.networkFee, denom, DECIMAL_PRECISION) : edgeTx.networkFee
const { date, time } = makeCsvDateTime(edgeTx.date)
let name: string = ''
let amountFiat: number = 0
let category: string = ''
let notes: string = ''
if (edgeTx.metadata) {
name = edgeTx.metadata.name ? edgeTx.metadata.name : ''
amountFiat = edgeTx.metadata.amountFiat ? edgeTx.metadata.amountFiat : 0
category = edgeTx.metadata.category ? edgeTx.metadata.category : ''
notes = edgeTx.metadata.notes ? edgeTx.metadata.notes : ''
}
const name: string = edgeTx.metadata?.name ?? ''
const amountFiat: number = edgeTx.metadata?.exchangeAmount?.[fiatCurrencyCode] ?? 0
const category: string = edgeTx.metadata?.category ?? ''
const notes: string = edgeTx.metadata?.notes ?? ''

items.push({
CURRENCY_CODE: currencyCode,
Expand Down
22 changes: 12 additions & 10 deletions src/components/scenes/TransactionDetailsScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const TransactionDetailsComponent = (props: Props) => {
const historicFiat = historicRate * Number(absExchangeAmount)

// Figure out which amount to show:
const originalFiat = metadata.amountFiat == null || metadata.amountFiat === 0 ? historicFiat : Math.abs(metadata.amountFiat)
const metadataFiat = metadata.exchangeAmount?.[wallet.fiatCurrencyCode]
const originalFiat = metadataFiat == null || metadataFiat === 0 ? historicFiat : Math.abs(metadataFiat)

// Percent difference:
const percentChange = originalFiat === 0 ? 0 : (currentFiat - originalFiat) / originalFiat
Expand Down Expand Up @@ -145,7 +146,7 @@ const TransactionDetailsComponent = (props: Props) => {

// Check for NaN, Infinity, and 0:
if (amountFiat === 0 || JSON.stringify(amountFiat) === 'null') return
await onSaveTxDetails({ amountFiat })
await onSaveTxDetails({ exchangeAmount: { [wallet.fiatCurrencyCode]: amountFiat } })
})
.catch(showError)
})
Expand Down Expand Up @@ -225,18 +226,19 @@ const TransactionDetailsComponent = (props: Props) => {
}

const onSaveTxDetails = (newDetails: Partial<EdgeMetadata>) => {
const { name, notes, bizId, category, amountFiat } = { ...localMetadata, ...newDetails }
transaction.metadata = {
name,
category,
notes,
amountFiat,
bizId
const mergedMetadata = {
...localMetadata,
...newDetails,
exchangeAmount: {
...localMetadata.exchangeAmount,
...newDetails.exchangeAmount
}
}
transaction.metadata = mergedMetadata

wallet.saveTxMetadata(transaction.txid, transaction.currencyCode, transaction.metadata).catch(error => showError(error))

setLocalMetadata({ ...localMetadata, ...newDetails })
setLocalMetadata(mergedMetadata)
}

const personLabel = direction === 'receive' ? lstrings.transaction_details_sender : lstrings.transaction_details_recipient
Expand Down
18 changes: 7 additions & 11 deletions src/components/themed/TransactionListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ export function TransactionListRow(props: Props) {

const { navigation, currencyCode, wallet, tokenId, transaction } = props
const { canReplaceByFee = false } = wallet.currencyInfo
const { metadata, action } = transaction
const { name, amountFiat: defaultAmountFiat = 0 } = metadata ?? {}
const { metadata = {}, action } = transaction
const { category, name } = metadata
const defaultAmountFiat = metadata.exchangeAmount?.[wallet.fiatCurrencyCode] ?? 0

const isSentTransaction = transaction.nativeAmount.startsWith('-') || (eq(transaction.nativeAmount, '0') && transaction.isSend)

Expand All @@ -64,11 +65,7 @@ export function TransactionListRow(props: Props) {
const fiatDenomination = getDenomFromIsoCode(nonIsoFiatCurrencyCode)
const denominationSymbol = displayDenomination.symbol

const currencyName =
currencyCode === currencyInfo.currencyCode
? currencyInfo.displayName
: currencyInfo.metaTokens.find(metaToken => metaToken.currencyCode === currencyCode)?.currencyName
const selectedCurrencyName = currencyName || currencyCode
const { displayName = currencyCode } = tokenId == null ? currencyInfo : wallet.currencyConfig.allTokens[tokenId]

// Required Confirmations
const requiredConfirmations = currencyInfo.requiredConfirmations || 1 // set default requiredConfirmations to 1, so once the transaction is in a block consider fully confirmed
Expand Down Expand Up @@ -104,9 +101,9 @@ export function TransactionListRow(props: Props) {
const transactionTitle =
action != null
? TX_ACTION_LABEL_MAP[action.type]
: transaction.metadata && transaction.metadata.name
? transaction.metadata.name
: sprintf(isSentTransaction ? lstrings.transaction_sent_1s : lstrings.transaction_received_1s, selectedCurrencyName)
: name != null && name !== ''
? name
: sprintf(isSentTransaction ? lstrings.transaction_sent_1s : lstrings.transaction_received_1s, displayName)

// Icon & Thumbnail
const thumbnailPath = useContactThumbnail(name)
Expand Down Expand Up @@ -155,7 +152,6 @@ export function TransactionListRow(props: Props) {
// Transaction Category
const defaultCategory = !isSentTransaction ? 'income' : 'expense'
let categoryText: string | undefined
const category = transaction.metadata?.category
if (category != null && category !== '') {
categoryText = formatCategory(splitCategory(category, defaultCategory))
}
Expand Down

0 comments on commit 51258ac

Please sign in to comment.