Skip to content

Commit

Permalink
[NO CHANGELOG][Add Funds Widget] Add convertTokenBalanceToUsd function (
Browse files Browse the repository at this point in the history
  • Loading branch information
mimi-imtbl committed Sep 26, 2024
1 parent ec6a309 commit 21699c3
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TokenBalance } from '@0xsquid/sdk/dist/types';
import { RouteResponse } from '@0xsquid/squid-types';
import { ethers } from 'ethers';

export function convertTokenBalanceToUsd(
balance: TokenBalance,
routeResponse: RouteResponse,
): number {
const { usdPrice } = routeResponse.route.estimate.fromToken;

if (!usdPrice) {
throw new Error('USD conversion rate not available');
}

const tokenBalance = ethers.utils.formatUnits(balance.balance, balance.decimals);

const usdBalance = parseFloat(tokenBalance) * usdPrice;

return usdBalance;
}

0 comments on commit 21699c3

Please sign in to comment.