You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.3 KiB
57 lines
2.3 KiB
4 years ago
|
import { useMemo } from 'react';
|
||
4 years ago
|
import {
|
||
|
getTokenValueParam,
|
||
|
calcTokenAmount,
|
||
4 years ago
|
} from '../helpers/utils/token-util';
|
||
|
import { useTokenData } from './useTokenData';
|
||
5 years ago
|
|
||
|
/**
|
||
|
* Defines the shape for the Token input parameter for useTokenDisplayValue
|
||
|
* @typedef {Object} Token
|
||
|
* @property {string} symbol - The string to use as a suffix for the token (eg. DAI)
|
||
|
* @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
|
||
4 years ago
|
* @param {string} [transactionData] - Raw data string from token transaction
|
||
|
* @param {Token} [token] - The token associated with this transaction
|
||
|
* @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.
|
||
5 years ago
|
* @return {string} - The computed displayValue of the provided transactionData and token
|
||
|
*/
|
||
4 years ago
|
export function useTokenDisplayValue(
|
||
|
transactionData,
|
||
|
token,
|
||
|
isTokenTransaction = true,
|
||
|
) {
|
||
4 years ago
|
const tokenData = useTokenData(transactionData, isTokenTransaction);
|
||
4 years ago
|
const shouldCalculateTokenValue = Boolean(
|
||
|
// If we are currently processing a token transaction
|
||
|
isTokenTransaction &&
|
||
4 years ago
|
// and raw transaction data string is provided
|
||
|
transactionData &&
|
||
|
// and a token object has been provided
|
||
|
token &&
|
||
|
// and we are able to parse the token details from the raw data
|
||
|
tokenData?.args?.length,
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
const displayValue = useMemo(() => {
|
||
4 years ago
|
if (!shouldCalculateTokenValue) {
|
||
4 years ago
|
return null;
|
||
4 years ago
|
}
|
||
4 years ago
|
const tokenValue = getTokenValueParam(tokenData);
|
||
|
return calcTokenAmount(tokenValue, token.decimals).toString(10);
|
||
|
}, [shouldCalculateTokenValue, tokenData, token]);
|
||
5 years ago
|
|
||
4 years ago
|
return displayValue;
|
||
5 years ago
|
}
|