Skip to content

Commit

Permalink
more generous adding prices to priceMap, use evmAddress and fall back…
Browse files Browse the repository at this point in the history
… to address when fetching evm prices, seems TokenInfo is inconsistent. if price is null or zero show blank
  • Loading branch information
bthaile committed Feb 6, 2025
1 parent 50908df commit 5785b31
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,10 @@ export class WalletController extends BaseController {

private async evmtokenPrice(tokeninfo, data) {
const token = tokeninfo.symbol.toLowerCase();
const price = await openapiService.getPricesByEvmaddress(tokeninfo.address, data);
const price = await openapiService.getPricesByEvmaddress(
tokeninfo.evmAddress || tokeninfo.address,
data
);

if (token === 'flow') {
const flowPrice = price || data['FLOW'];
Expand Down
4 changes: 2 additions & 2 deletions src/background/service/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,15 @@ class OpenApiService {

data.forEach((token) => {
if (token.evmAddress) {
// EVM price
const { rateToUSD, evmAddress, symbol } = token;
const key = evmAddress.toLowerCase();
pricesMap[key] = Number(rateToUSD).toFixed(8);
const symbolKey = symbol.toUpperCase();
if (symbolKey) {
pricesMap[symbolKey] = Number(rateToUSD).toFixed(8);
}
} else if (token.contractName && token.contractAddress) {
}
if (token.contractName && token.contractAddress) {
// Flow chain price
const { rateToUSD, contractName, contractAddress } = token;
const key = `${contractName.toLowerCase()}${contractAddress.toLowerCase()}`;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/utils/formatPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('formatPrice', () => {
const result = formatPrice(0, 4);
expect(result).toEqual({
price: 0,
formattedPrice: { leadingPart: '-', zeroPart: null, endingPart: null },
formattedPrice: { leadingPart: '', zeroPart: null, endingPart: null },
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/shared/utils/formatPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function formatPrice(price: number, zeroCondenseThreshold = 4): Formatted
return {
price,
formattedPrice: {
leadingPart: '-',
leadingPart: '',
zeroPart: null,
endingPart: null,
},
Expand Down
7 changes: 5 additions & 2 deletions src/ui/views/TokenDetail/TokenInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,12 @@ const TokenInfoCard = ({
</Box>
<Typography variant="body1" color="text.secondary" sx={{ fontSize: '16px' }}>
<Box component="span" sx={{ marginRight: '0.25rem' }}>
<TokenPrice price={balance * price} prefix="$" />
<TokenPrice
price={balance * price}
prefix="$"
postFix={chrome.i18n.getMessage('USD')}
/>
</Box>
{chrome.i18n.getMessage('USD')}
</Typography>
<Box sx={{ display: 'flex', gap: '12px', height: '36px', mt: '24px', width: '100%' }}>
{(!childType || childType === 'evm') && (
Expand Down
12 changes: 7 additions & 5 deletions src/ui/views/TokenDetail/TokenPrice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ interface TokenPriceProps {
className?: string;
showPrefix?: boolean;
prefix?: string;
postFix?: string;
}

export const TokenPrice: React.FC<TokenPriceProps> = ({
price,
className = '',
showPrefix = true,
prefix = '$',
postFix = '',
}) => {
if (price === null || price === undefined || typeof price === 'string') {
return <span className={className}>{price}</span>;
if (price === 0 || price === null || price === undefined || typeof price === 'string') {
return <span className={className}>{''}</span>;
}

const { formattedPrice } = formatPrice(price);
const { leadingPart, zeroPart, endingPart } = formattedPrice;

return (
<span className={className}>
{showPrefix && prefix}
<span style={leadingPart === '-' ? { padding: '0 0.25rem' } : undefined}>{leadingPart}</span>
{prefix}
<span style={leadingPart === '' ? { padding: '0 0.25rem' } : undefined}>{leadingPart}</span>
{zeroPart !== null && (
<sub
style={{
Expand All @@ -37,6 +38,7 @@ export const TokenPrice: React.FC<TokenPriceProps> = ({
</sub>
)}
{endingPart !== null && endingPart}
{postFix && <span style={{ marginLeft: '0.25rem' }}>{postFix}</span>}
</span>
);
};
5 changes: 4 additions & 1 deletion src/ui/views/TokenDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ const TokenDetail = () => {
if (price) {
setPrice(price);
}
const evmPrice = await usewallet.openapi.getPricesByEvmaddress(tokenResult!.address, data);
const evmPrice = await usewallet.openapi.getPricesByEvmaddress(
tokenResult!.evmAddress!,
data
);
if (evmPrice) {
setPrice(evmPrice);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ui/views/Wallet/Coinlist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const CoinList = ({ data, ableFt, isActive, childType, coinLoading }) => {
variant="body1"
sx={{ fontSize: 12, fontWeight: '500', textAlign: 'end', color: 'text.secondary' }}
>
{props.change === null ? '-' : '$'}
{props.secondary}
{props.change === null || props.change === 0 ? '' : '$'}
{props.secondary === null || props.secondary === 0 ? '' : props.secondary}
</Typography>
) : (
<Skeleton variant="text" width={35} height={15} />
Expand Down Expand Up @@ -192,7 +192,7 @@ const CoinList = ({ data, ableFt, isActive, childType, coinLoading }) => {
secondaryAction={
<EndListItemText
primary={parseFloat(coin.balance).toFixed(3)}
secondary={parseFloat(coin.total.toFixed(2))}
secondary={<TokenPrice price={coin.balance * coin.price} />}
unit={coin.unit}
change={parseFloat(coin.change24h.toFixed(2))}
/>
Expand Down

0 comments on commit 5785b31

Please sign in to comment.