Skip to content

Commit

Permalink
approval uses tokenData
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-decker committed May 23, 2020
1 parent d336297 commit c702933
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions ui/app/hooks/useTransactionDisplayData.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const pendingStatusMap = {
[SUBMITTED_STATUS]: true,
}

const tokenCategoryMap = {
[TOKEN_METHOD_APPROVE]: true,
[TOKEN_METHOD_TRANSFER]: true,
[TOKEN_METHOD_TRANSFER_FROM]: true,
}

/**
* @typedef {Object} TransactionDisplayData
* @property {string} title - primary description of the transaction
Expand Down Expand Up @@ -82,17 +88,19 @@ export function useTransactionDisplayData (transactionGroup) {
let subtitle
let recipientAddress = to

const isTokenTransfer = transactionCategory === TOKEN_METHOD_TRANSFER || transactionCategory === TOKEN_METHOD_TRANSFER_FROM
// This value is used to determine whether we should look inside txParams.data
// to pull out and render token related information
const isTokenCategory = tokenCategoryMap[transactionCategory]

// these values are always instantiated because they are either
// used by or returned from hooks. Hooks must be called at the top level,
// so as an additional safeguard against inappropriately associating token
// transfers, we explicitly pass undefined to the two following hook calls
// if the transactionCategory doesn't indicate a token transfer.
// These hooks will return null if any argument is null or undefined.
const token = isTokenTransfer && knownTokens.find((token) => token.address === recipientAddress)
const tokenData = useTokenData(isTokenTransfer && initialTransaction?.txParams?.data)
const tokenDisplayValue = useTokenDisplayValue(isTokenTransfer && initialTransaction?.txParams?.data, token)
const token = isTokenCategory && knownTokens.find((token) => token.address === recipientAddress)
const tokenData = useTokenData(isTokenCategory && initialTransaction?.txParams?.data)
const tokenDisplayValue = useTokenDisplayValue(isTokenCategory && initialTransaction?.txParams?.data, token)

let category
let title
Expand Down Expand Up @@ -120,7 +128,7 @@ export function useTransactionDisplayData (transactionGroup) {
title = t('receive')
prefix = ''
subtitle = `${t('from')}: ${shortenAddress(senderAddress)}`
} else if (isTokenTransfer) {
} else if (transactionCategory === TOKEN_METHOD_TRANSFER_FROM || transactionCategory === TOKEN_METHOD_TRANSFER) {
category = TRANSACTION_CATEGORY_SEND
title = t('sendSpecifiedTokens', [token?.symbol || t('token')])
recipientAddress = getTokenToAddress(tokenData.params)
Expand All @@ -136,14 +144,14 @@ export function useTransactionDisplayData (transactionGroup) {

const [primaryCurrency] = useCurrencyDisplay(primaryValue, {
prefix,
displayValue: isTokenTransfer && tokenDisplayValue,
displayValue: isTokenCategory && tokenDisplayValue,
suffix: token?.symbol,
...primaryCurrencyPreferences,
})

const [secondaryCurrency] = useCurrencyDisplay(primaryValue, {
prefix,
displayValue: isTokenTransfer && tokenDisplayValue,
displayValue: isTokenCategory && tokenDisplayValue,
...secondaryCurrencyPreferences,
})

Expand All @@ -155,7 +163,7 @@ export function useTransactionDisplayData (transactionGroup) {
primaryCurrency,
senderAddress,
recipientAddress,
secondaryCurrency: isTokenTransfer ? undefined : secondaryCurrency,
secondaryCurrency: isTokenCategory ? undefined : secondaryCurrency,
status,
}
}

0 comments on commit c702933

Please sign in to comment.