1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 21:00:13 +01:00
metamask-extension/ui/app/hooks/useSwappedTokenValue.js

68 lines
2.8 KiB
JavaScript
Raw Normal View History

import { TRANSACTION_CATEGORIES } from '../../../shared/constants/transaction'
2020-10-06 20:28:38 +02:00
import { ETH_SWAPS_TOKEN_ADDRESS } from '../helpers/constants/swaps'
import { getSwapsTokensReceivedFromTxMeta } from '../pages/swaps/swaps.util'
import { useTokenFiatAmount } from './useTokenFiatAmount'
/**
* @typedef {Object} SwappedTokenValue
* @property {string} swapTokenValue - a primary currency string formatted for display
* @property {string} swapTokenFiatAmount - a secondary currency string formatted for display
* @property {boolean} isViewingReceivedTokenFromSwap - true if user is on the asset page for the
* destination/received asset in a swap.
*/
/**
* A Swap transaction group's primaryTransaction contains details of the swap,
2020-10-06 20:28:38 +02:00
* including the source (from) and destination (to) token type (ETH, DAI, etc..)
* When viewing a non ETH asset page, we need to determine if that asset is the
* token that was received (destination) from the swap. In that circumstance we
* would want to show the primaryCurrency in the activity list that is most relevant
* for that token (- 1000 DAI, for example, when swapping DAI for ETH).
* @param {import('../selectors').transactionGroup} transactionGroup - Group of transactions by nonce
* @param {import('./useTokenDisplayValue').Token} currentAsset - The current asset the user is looking at
* @returns {SwappedTokenValue}
*/
2020-11-03 00:41:28 +01:00
export function useSwappedTokenValue(transactionGroup, currentAsset) {
2020-10-06 20:28:38 +02:00
const { symbol, decimals, address } = currentAsset
const { primaryTransaction, initialTransaction } = transactionGroup
const { transactionCategory } = initialTransaction
const { from: senderAddress } = initialTransaction.txParams || {}
2020-11-03 00:41:28 +01:00
const isViewingReceivedTokenFromSwap =
currentAsset?.symbol === primaryTransaction.destinationTokenSymbol ||
(currentAsset.address === ETH_SWAPS_TOKEN_ADDRESS &&
primaryTransaction.destinationTokenSymbol === 'ETH')
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const swapTokenValue =
transactionCategory === TRANSACTION_CATEGORIES.SWAP &&
isViewingReceivedTokenFromSwap
2020-11-03 00:41:28 +01:00
? getSwapsTokensReceivedFromTxMeta(
primaryTransaction.destinationTokenSymbol,
initialTransaction,
address,
senderAddress,
decimals,
)
: transactionCategory === TRANSACTION_CATEGORIES.SWAP &&
primaryTransaction.swapTokenValue
2020-11-03 00:41:28 +01:00
const isNegative =
typeof swapTokenValue === 'string'
? Math.sign(swapTokenValue) === -1
: false
2020-10-06 20:28:38 +02:00
const _swapTokenFiatAmount = useTokenFiatAmount(
address,
swapTokenValue || '',
symbol,
)
2020-11-03 00:41:28 +01:00
const swapTokenFiatAmount =
2020-10-06 20:28:38 +02:00
swapTokenValue && isViewingReceivedTokenFromSwap && _swapTokenFiatAmount
2020-11-03 00:41:28 +01:00
return {
swapTokenValue,
swapTokenFiatAmount,
isViewingReceivedTokenFromSwap,
isNegative,
}
2020-10-06 20:28:38 +02:00
}