2021-02-04 19:15:23 +01:00
|
|
|
import { useMemo } from 'react';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getTokenValueParam,
|
|
|
|
calcTokenAmount,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../helpers/utils/token-util';
|
|
|
|
import { useTokenData } from './useTokenData';
|
2020-05-12 21:07:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the shape for the Token input parameter for useTokenDisplayValue
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} Token
|
2022-01-07 16:57:33 +01:00
|
|
|
* @property {string} symbol - The string to use as a suffix for the token (eg. DAI)
|
2020-05-12 21:07:35 +02:00
|
|
|
* @property {number} decimals - The number of decimals to show when displaying this type of token
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useTokenDisplayValue
|
|
|
|
* Given the data string from txParams and a token object with symbol and decimals, return
|
|
|
|
* a displayValue that represents a string representing that token amount as a string. Also
|
|
|
|
* return a tokenData object for downstream usage and the suffix for the token to use as props
|
|
|
|
* for other hooks and/or components
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param {string} [transactionData] - Raw data string from token transaction
|
|
|
|
* @param {Token} [token] - The token associated with this transaction
|
2020-05-26 22:49:11 +02:00
|
|
|
* @param {boolean} [isTokenTransaction] - Due to the nature of hooks, it isn't possible
|
|
|
|
* to conditionally call this hook. This flag will
|
|
|
|
* force this hook to return null if it set as false
|
|
|
|
* which indicates the transaction is not associated
|
|
|
|
* with a token.
|
2022-01-07 16:57:33 +01:00
|
|
|
* @returns {string} The computed displayValue of the provided transactionData and token
|
2020-05-12 21:07:35 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useTokenDisplayValue(
|
|
|
|
transactionData,
|
|
|
|
token,
|
|
|
|
isTokenTransaction = true,
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokenData = useTokenData(transactionData, isTokenTransaction);
|
2022-08-01 18:42:58 +02:00
|
|
|
const tokenValue = getTokenValueParam(tokenData);
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
const shouldCalculateTokenValue = Boolean(
|
|
|
|
// If we are currently processing a token transaction
|
|
|
|
isTokenTransaction &&
|
2020-11-03 00:41:28 +01:00
|
|
|
// and raw transaction data string is provided
|
|
|
|
transactionData &&
|
|
|
|
// and a token object has been provided
|
|
|
|
token &&
|
2022-08-01 18:42:58 +02:00
|
|
|
// and the provided token object contains a defined decimal value we need to calculate amount
|
|
|
|
token.decimals &&
|
|
|
|
// and we are able to parse the token detail we to calculate amount from the raw data
|
|
|
|
tokenValue,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-12 21:07:35 +02:00
|
|
|
|
|
|
|
const displayValue = useMemo(() => {
|
2020-05-26 22:49:11 +02:00
|
|
|
if (!shouldCalculateTokenValue) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
2022-08-01 18:42:58 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return calcTokenAmount(tokenValue, token.decimals).toString(10);
|
2022-08-15 16:25:57 +02:00
|
|
|
}, [shouldCalculateTokenValue, tokenValue, token]);
|
2020-05-12 21:07:35 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return displayValue;
|
2020-05-12 21:07:35 +02:00
|
|
|
}
|