diff --git a/package-lock.json b/package-lock.json index e249741..13bea78 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "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", @@ -18,6 +19,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", @@ -9789,6 +9792,29 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", + "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "dependencies": { + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/axobject-query": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.2.1.tgz", @@ -22973,6 +22999,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", diff --git a/package.json b/package.json index 63d0e6e..6f54e6d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/utils/index.tsx b/src/utils/index.tsx index bed097d..62269da 100644 --- a/src/utils/index.tsx +++ b/src/utils/index.tsx @@ -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 { + 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 { + 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;