From 444142d2434acf2b024cb16cac41665f8f76ffa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Sun, 19 Sep 2021 22:47:23 -0300 Subject: [PATCH] add useBusdExpectation --- .../src/hooks/useBusdExpectation.ts | 21 +++++++++++++++++++ app/javascript/src/pages/Home/Pool.tsx | 4 ++-- app/javascript/src/pages/Wallet/Balance.tsx | 10 ++++++++- app/javascript/src/utils/getPrice.ts | 19 ++++++++++------- 4 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 app/javascript/src/hooks/useBusdExpectation.ts diff --git a/app/javascript/src/hooks/useBusdExpectation.ts b/app/javascript/src/hooks/useBusdExpectation.ts new file mode 100644 index 0000000..01c5020 --- /dev/null +++ b/app/javascript/src/hooks/useBusdExpectation.ts @@ -0,0 +1,21 @@ +import { useEffect, useState } from "react"; + +import type { Token } from "../constants/pancake/Tokens"; +import { getPriceInBusd } from "../utils/getPrice"; + +export const useBusdExpectation = (token: Token, amount: number) => { + const [isLoading, setIsLoading] = useState(true); + const [total, setTotal] = useState("0.00"); + + useEffect(() => { + getPriceInBusd(token).then((price) => { + setTotal(price.multipliedBy(amount).toFixed(2)); + setIsLoading(false); + }); + }); + + return { + total, + isLoading, + }; +}; diff --git a/app/javascript/src/pages/Home/Pool.tsx b/app/javascript/src/pages/Home/Pool.tsx index 1aee2fd..92f5d66 100644 --- a/app/javascript/src/pages/Home/Pool.tsx +++ b/app/javascript/src/pages/Home/Pool.tsx @@ -31,8 +31,8 @@ export const Pool: FC = ({ pool, balance }) => { React.useEffect(() => { (async () => { - const stakingPrice = await getPriceInBusd(router, pool.stakingToken); - const earningPrice = await getPriceInBusd(router, pool.earningToken); + const stakingPrice = await getPriceInBusd(pool.stakingToken); + const earningPrice = await getPriceInBusd(pool.earningToken); const totalStaked = await getTotalStaked(provider, pool); diff --git a/app/javascript/src/pages/Wallet/Balance.tsx b/app/javascript/src/pages/Wallet/Balance.tsx index 4dd01b5..0fabe05 100644 --- a/app/javascript/src/pages/Wallet/Balance.tsx +++ b/app/javascript/src/pages/Wallet/Balance.tsx @@ -4,6 +4,8 @@ import React from "react"; import { useFragment } from "react-relay"; import { Table, TableRow } from "../../components"; +import { tokens } from "../../constants/pancake/Tokens"; +import { useBusdExpectation } from "../../hooks/useBusdExpectation"; import { formatCake } from "../../utils/cake"; import { getCurrencyLogo } from "../../utils/getCurrencyLogo"; import type { Balance_wallet$key } from "./__generated__/Balance_wallet.graphql"; @@ -26,10 +28,15 @@ export const Balance: FC = ({ userRef }) => { const cakeBalance = formatCake(wallet.cakeBalance); + const { total } = useBusdExpectation( + tokens.cake, + parseFloat(wallet.cakeBalance) + ); + return (
- +
@@ -45,6 +52,7 @@ export const Balance: FC = ({ userRef }) => { , cakeBalance, + total, ]} />
diff --git a/app/javascript/src/utils/getPrice.ts b/app/javascript/src/utils/getPrice.ts index cc26a9d..541979f 100644 --- a/app/javascript/src/utils/getPrice.ts +++ b/app/javascript/src/utils/getPrice.ts @@ -1,16 +1,19 @@ -import type { Contract } from "ethers"; -import { ethers } from "ethers"; import BigNumber from "bignumber.js"; -import { tokens } from "../constants/pancake/Tokens"; import type { Token } from "../constants/pancake/Tokens"; -import { BIG_TEN } from "./getTotalStaked"; -// 1 Wei = 1*10^18 Ether -const ONE_BUSD_IN_WEI = ethers.utils.parseUnits("1", 18); +type Response = { + updated_at: number; + data: { + name: string; + symbol: string; + price: string; + price_BNB: string; + }; +}; -export const getPriceInBusd = async (router: Contract, token: Token) => { - const response = await fetch( +export const getPriceInBusd = async (token: Token) => { + const response: Response = await fetch( `https://api.pancakeswap.info/api/v2/tokens/${token.address["56"]}` ).then((r) => r.json());