Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common conversation function #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"dependencies": {
"@etherspot/prime-sdk": "1.5.0",
"@ethersproject/bignumber": "5.7.0",
"@headlessui/react": "1.7.18",
"@heroicons/react": "2.1.1",
"@metamask/detect-provider": "2.0.0",
Expand All @@ -14,6 +15,8 @@
"@testing-library/user-event": "13.5.0",
"@types/jest": "27.5.2",
"@types/react-dom": "18.2.18",
"axios": "1.6.7",
"bignumber.js": "9.1.2",
"ethers": "6.10.0",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
26 changes: 26 additions & 0 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@
* SOFTWARE.
*/

import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import axios from 'axios';

// Utility function to convert wei to Ether
export function tokenHexStringToEther(hexTokenAmount: string): string {
const tokenAmount = new BigNumber(hexTokenAmount);
const ether = ethers.formatEther(tokenAmount.toString());
return ether;
}

// Utility function to convert Ether to USD
export async function etherToUSD(etherAmount: string): Promise<number> {
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd');
const usdRate = response.data.ethereum.usd;
const usdValue = new BigNumber(etherAmount).multipliedBy(usdRate).toNumber();
return usdValue;
}

// Utility function to format balance in USD
export async function BalanceDisplayInUSD(rawBalance: string): Promise<string> {
const etherAmount = tokenHexStringToEther(rawBalance);
const USDAmount = await etherToUSD(etherAmount);
return `${USDAmount.toFixed(2)} $`;
}

export const formatBalance = (rawBalance: string) => {
const balance = (parseInt(rawBalance) / 1000000000000000000).toFixed(2);
return balance;
Expand Down