@unilogin/sdk
Version:
SDK is a JS library, that communicates with relayer. SDK allows managing contract, by creating basic contract-calling messages.
12 lines (10 loc) • 775 B
text/typescript
import {WalletService} from '../services/WalletService';
import {utils} from 'ethers';
import {safeMultiply, ValueRounder, safeMultiplyDecimalsAndFormatEther} from '@unilogin/commons';
export const getValueInUsd = async (tokenAddress: string, walletService: WalletService, value: string, round = 3) => {
const tokenDetails = await walletService.sdk.tokenDetailsService.getTokenDetails(tokenAddress);
const tokenPrice = await walletService.sdk.tokenPricesService.getTokenPriceInEth(tokenDetails);
const etherPriceInUsd = await walletService.sdk.tokenPricesService.getEtherPriceInCurrency('USD');
const ethValue = safeMultiply(utils.bigNumberify(value), tokenPrice);
return ValueRounder.ceil(safeMultiplyDecimalsAndFormatEther(ethValue, etherPriceInUsd), round);
};