diff --git a/src/pages/FarmingV2/ShowYieldFarmDetails.tsx b/src/pages/FarmingV2/ShowYieldFarmDetails.tsx index ddf02bc7..d7869d15 100644 --- a/src/pages/FarmingV2/ShowYieldFarmDetails.tsx +++ b/src/pages/FarmingV2/ShowYieldFarmDetails.tsx @@ -147,13 +147,13 @@ const ShowYieldFarmDetails = ({ const signer = library?.getSigner(); const [reload, setReload] = useState(false); const [contentid, setContentId] = useState(undefined); - const [loading, setLoading] = useState(false); + const [loading, setLoading] = useState(true); // const data = useGetFarmData(reload, setReload); const { loadingState } = useUpdateFarm({ reload, setReload, content }); - useFetchYieldFarmDetails({ content, section, setLoading, loading }); + useFetchYieldFarmDetails({ content, section, setLoading, loading, reload }); //const {loadingFarm} = useUpdateNewFarm({reload, setReload, content}); @@ -236,10 +236,10 @@ const ShowYieldFarmDetails = ({ const checkForApproval = async () => { const rgp = await rigelToken(RGP[chainId as number], library); const rgpApproval = await poolAllowance(rgp); - if (content.deposit === "RGP" && Number(content.id) === 1) { + if (content.deposit === "RGP" && content.version === "v1") { const specialPoolV1Approval = await specialPoolV1Allowance(rgp); changeApprovalButton(true, specialPoolV1Approval); - } else if (content.deposit === "RGP" && Number(content.id) === 13) { + } else if (content.deposit === "RGP" && content.version === "v2") { const specialPoolV2Approval = await specialPoolV2Allowance(rgp); changeApprovalButton(true, specialPoolV2Approval); @@ -305,6 +305,7 @@ const ShowYieldFarmDetails = ({ message: `Successful RGP Approval`, }) ); + setReload(true); } getAllowances(); } catch (error: any) { @@ -360,6 +361,7 @@ const ShowYieldFarmDetails = ({ }) ); } + setReload(true); getAllowances(); } catch (error: any) { console.error(error); @@ -393,11 +395,11 @@ const ShowYieldFarmDetails = ({ }; const checkUser = async (val: string) => { - if (content.deposit === "RGP" && Number(content.id) === 1) { + if (content.deposit === "RGP" && content.version === "v1") { await RGPSpecialPoolV1Approval(); setApproveValueForOtherToken(true); setApproveValueForRGP(true); - } else if (content.deposit === "RGP" && Number(content.id) === 13) { + } else if (content.deposit === "RGP" && content.version === "v2") { await RGPSpecialPoolV2Approval(); setApproveValueForOtherToken(true); setApproveValueForRGP(true); @@ -622,10 +624,11 @@ const ShowYieldFarmDetails = ({ ); if (account) { - if (val === "RGP" && Number(content.id) === 1) { - await RGPUnstake(val); - } else if (val === "RGP" && Number(content.id) === 13) { - await RGPUnstakeV2(val); + console.log("id", content.id); + if (val === "RGP" && content?.version === "v1") { + await RGPUnstake(RGPSPECIALPOOLADDRESSES[chainId as number]); + } else if (val === "RGP" && content?.version === "v2") { + await RGPUnstake(RGPSPECIALPOOLADDRESSES2[chainId as number]); } else { tokensWithdrawal(content.id, val); } @@ -959,9 +962,9 @@ const ShowYieldFarmDetails = ({ : ethers.utils.parseUnits(format3, 9).toString(), } ); - const { confirmations, status, logs } = await fetchTransactionData( - data - ); + const receipt = await data.wait(3); + setDeposited(true); + setReload(!reload); GFarmingSuccessTransaction("farming", "stake", val, "v2"); dispatch( setOpenModal({ @@ -972,7 +975,6 @@ const ShowYieldFarmDetails = ({ //callRefreshFarm(confirmations, status); //temporal - setDeposited(true); } } else { const lpTokens = await MasterChefV2Contract( @@ -1017,9 +1019,10 @@ const ShowYieldFarmDetails = ({ message: `Successfully deposited`, }) ); + setReload(!reload); GFarmingSuccessTransaction("farming", "stake", val, "v2"); setDeposited(true); - setReload(true); + setContentId(content.deposit === "RGP" ? undefined : content.id); // callRefreshFarm(confirmations, status); } @@ -1045,11 +1048,12 @@ const ShowYieldFarmDetails = ({ ); try { if (account) { - if (val === "RGP" && Number(content.id) === 1) { - await RGPuseStake(); - } else if (val === "RGP" && Number(content.id) === 13) { - await RGPuseStakeV2(); + if (val === "RGP" && content?.version === "v1") { + await RGPuseStakeV2(RGPSPECIALPOOLADDRESSES[chainId as number]); + } else if (val === "RGP" && content?.version === "v2") { + await RGPuseStakeV2(RGPSPECIALPOOLADDRESSES2[chainId as number]); } else { + console.log("deposit lp"); LPDeposit(content.id, val); } } @@ -1132,13 +1136,10 @@ const ShowYieldFarmDetails = ({ } } }; - const RGPuseStakeV2 = async () => { + const RGPuseStakeV2 = async (poolAddress: string) => { if (account) { try { - const specialPool = await RGPSpecialPool2( - RGPSPECIALPOOLADDRESSES2[chainId as number], - library - ); + const specialPool = await RGPSpecialPool2(poolAddress, library); GFarmingSpecialPoolReferral( referrerAddress === ZERO_ADDRESS ? false : true ); @@ -1184,13 +1185,10 @@ const ShowYieldFarmDetails = ({ }; // withdrawal of funds - const RGPUnstake = async () => { + const RGPUnstake = async (poolAddress: string) => { if (account) { try { - const specialPool = await RGPSpecialPool( - RGPSPECIALPOOLADDRESSES[chainId as number], - library - ); + const specialPool = await RGPSpecialPool(poolAddress, library); const { format1, format2, format3 } = await calculateGas( userGasPricePercentage, library, @@ -1219,14 +1217,23 @@ const ShowYieldFarmDetails = ({ } ); const { confirmations, status } = await fetchTransactionData(data); - GFarmingSuccessTransaction("special pool", "unstake", "RGP", "v1"); - dispatch( - setOpenModal({ - trxState: TrxState.TransactionSuccessful, - message: `Successfully unstaked ${unstakeToken} RGP `, - }) - ); - refreshSpecialData(); + if (confirmations > 1) { + GFarmingSuccessTransaction( + "special pool", + "unstake", + "RGP", + content?.version + ); + dispatch( + setOpenModal({ + trxState: TrxState.TransactionSuccessful, + message: `Successfully unstaked ${unstakeToken} RGP `, + }) + ); + + refreshSpecialData(); + setReload(true); + } // dispatch the getTokenStaked action from here when data changes // callRefreshFarm(confirmations, status); } catch (e: any) { @@ -2382,7 +2389,10 @@ const ShowYieldFarmDetails = ({ ? { background: "rgba(64, 186, 213, 0.15)" } : { background: "#444159" } } - onClick={() => confirmDeposit(content.deposit)} + onClick={() => { + console.log("confirm"); + confirmDeposit(content.deposit); + }} > {depositValue} diff --git a/src/pages/FarmingV2/index.tsx b/src/pages/FarmingV2/index.tsx index daf5c2b0..f397539c 100644 --- a/src/pages/FarmingV2/index.tsx +++ b/src/pages/FarmingV2/index.tsx @@ -85,6 +85,7 @@ import { useFetchFarm, useFetchQuickSwap, useFetchStable, + useSpecialPool, } from "../../utils/hooks/useFarm"; export const BIG_TEN = new bigNumber(10); @@ -166,9 +167,10 @@ export function Index() { const farms = useSelector((state: any) => state.farms); - useFetchFarm(); - useFetchQuickSwap(); - useFetchStable(); + const { loading: LpfarmLoading } = useFetchFarm(); + const { loading: quickswapLoading } = useFetchQuickSwap(); + const { loading: stableFarmLoading } = useFetchStable(); + useSpecialPool(); useEffect(() => { if (location && location.includes("RGPv2")) { @@ -618,7 +620,7 @@ export function Index() { - {loadingState ? ( + {LpfarmLoading ? ( {new Array(4).fill("1").map((item, index) => { return ( @@ -668,7 +670,7 @@ export function Index() { refreshSpecialData={refreshSpecialData} section={"normal"} wallet={wallet} - LoadingState={loadingState} + LoadingState={LpfarmLoading} /> )) )} @@ -808,8 +810,8 @@ export function Index() { - Special pool - {/* {specialPool?.map((content: any, index: number) => ( + + {farms?.SpecialPool?.map((content: any, index: number) => ( - ))} */} + ))} @@ -1052,7 +1054,7 @@ export function Index() { - {loadingLP ? ( + {quickswapLoading ? ( {new Array(4).fill("1").map((item, index) => { return ( @@ -1101,18 +1103,20 @@ export function Index() { ); })} - ) : null} - - {/* */} + ) : ( + farms?.QuickswapFarm.map((content: any) => ( + + )) + )} @@ -1193,7 +1197,7 @@ export function Index() { - {loadingLP ? ( + {stableFarmLoading ? ( {new Array(4).fill("1").map((item, index) => { return ( @@ -1242,18 +1246,20 @@ export function Index() { ); })} - ) : null} - - {/* */} + ) : ( + farms?.StableFarm.map((content: any) => ( + + )) + )} diff --git a/src/state/LPFarm/hooks.ts b/src/state/LPFarm/hooks.ts index 48254e48..322ed10d 100644 --- a/src/state/LPFarm/hooks.ts +++ b/src/state/LPFarm/hooks.ts @@ -5,765 +5,779 @@ import { useMemo, useState, useCallback } from "react"; import { useDispatch } from "react-redux"; import { useWeb3React } from "@web3-react/core"; import { - MasterChefNEWLPContract, - LiquidityPairInstance, - smartFactory, - productStakingContract, smartSwapLPTokenPoolThree, + MasterChefNEWLPContract, + LiquidityPairInstance, + smartFactory, + productStakingContract, + smartSwapLPTokenPoolThree, } from "../../utils/Contracts"; import { - MASTERCHEFNEWLPADDRESSES, - SMARTSWAPFACTORYADDRESSES, - RGPADDRESSES, - BUSD, - USDT, - USDC, - PRODUCTSTAKINGADDRESSES, SMARTSWAPLP_TOKEN3ADDRESSES, SYMBOLS, + MASTERCHEFNEWLPADDRESSES, + SMARTSWAPFACTORYADDRESSES, + RGPADDRESSES, + BUSD, + USDT, + USDC, + PRODUCTSTAKINGADDRESSES, + SMARTSWAPLP_TOKEN3ADDRESSES, + SYMBOLS, } from "../../utils/addresses"; import { getERC20Token } from "../../utils/utilsFunctions"; import { ethers } from "ethers"; import { - updateYieldFarmDetails, - updateNewFilterResult, - updateNewSearchResult, - updateProductFarmDetails, + updateYieldFarmDetails, + updateNewFilterResult, + updateNewSearchResult, + updateProductFarmDetails, } from "../newFarming/action"; import Web3 from "web3"; -import {RootState} from "../index"; -import {farmSection} from "../../pages/FarmingV2"; - -export const useNewLPData = (): farmDataInterface => { - const farms = useSelector((state: State) => state.lpfarm); - return farms; +import { RootState } from "../index"; +import { farmSection } from "../../pages/FarmingV2"; +import { updateQuickSwapFarm, updateStableFarm } from "../farm/actions"; + +export const useNewLPData = (content: any): farmDataInterface => { + const farms = useSelector((state: State) => state.farms); + const farm = + content?.farm === "quick" + ? farms?.QuickswapFarm + : content?.farm === "stable" + ? farms?.StableFarm + : null; + return farm; }; interface updateFarmInterface { - reload: boolean; - setReload: React.Dispatch>; - content: { - id: number | undefined; - img: string; - deposit: string; - symbol0: string; - symbol1: string; - earn: string; - type: string; - totalLiquidity: number; - APY: number; - address: string; - }; - contractID: number; - section: string; - + reload: boolean; + setReload: React.Dispatch>; + content: { + id: number | undefined; + img: string; + deposit: string; + symbol0: string; + symbol1: string; + earn: string; + type: string; + totalLiquidity: number; + APY: number; + address: string; + }; + contractID: number; + section: string; } const getBNBPrice = async (address: string, library: any) => { - try { - let BNBPrice; - const pair = await smartSwapLPTokenPoolThree( - address, - library - ); - const reserves = await pair.getReserves(); - const testNetPair = SMARTSWAPLP_TOKEN3ADDRESSES[97]; - - // BUSD is token0 on testnet but token1 on mainnet, thus the reason to check - // before calculating the price based on BUSD - - if (pair.address === testNetPair) { - BNBPrice = ethers.utils.formatUnits( - reserves[0].mul(1000).div(reserves[1]), - 3 - ); - } else { - BNBPrice = ethers.utils.formatUnits( - reserves[1].mul(1000).div(reserves[0]), - 3 - ); - } - return Number(BNBPrice); - - } catch (e) { - console.log('Could not fetch BNB Price.') + try { + let BNBPrice; + const pair = await smartSwapLPTokenPoolThree(address, library); + const reserves = await pair.getReserves(); + const testNetPair = SMARTSWAPLP_TOKEN3ADDRESSES[97]; + + // BUSD is token0 on testnet but token1 on mainnet, thus the reason to check + // before calculating the price based on BUSD + + if (pair.address === testNetPair) { + BNBPrice = ethers.utils.formatUnits( + reserves[0].mul(1000).div(reserves[1]), + 3 + ); + } else { + BNBPrice = ethers.utils.formatUnits( + reserves[1].mul(1000).div(reserves[0]), + 3 + ); } + return Number(BNBPrice); + } catch (e) { + console.log("Could not fetch BNB Price."); + } }; export const useUpdateNewFarm = ({ - reload, - setReload, - content, contractID, section - }: updateFarmInterface) => { - const data = useNewLPData(); - const searchSection = useSelector((state) => state.newFarming); - - const selectedField = useSelector((state: State) => state.farming.selectedField); - const selected = selectedField === farmSection.SECOND_NEW_LP; - const mainLP = selectedField === farmSection.NEW_LP; - - const trxState = useSelector((state) => state.application.modal?.trxState); - const stateChanged : boolean = trxState === 2; - - const { account, chainId, library } = useWeb3React(); - const [loadingFarm, setLoadingState] = useState(false); - - const dispatch = useDispatch(); - - const handleUpdateFarms = useCallback( - (value) => { - dispatch( - section === "filter" - ? updateNewFilterResult({ farmData: value }) - : section === "search" - ? updateNewSearchResult({ farmData: value }) - : updateYieldFarmDetails({ value }) - ); - }, - [dispatch] - ); + reload, + setReload, + content, + contractID, + section, +}: updateFarmInterface) => { + const data = useNewLPData(content); + const searchSection = useSelector((state) => state.newFarming); + + const selectedField = useSelector( + (state: State) => state.farming.selectedField + ); + const selected = selectedField === farmSection.SECOND_NEW_LP; + const mainLP = selectedField === farmSection.NEW_LP; + + const trxState = useSelector( + (state) => state.application.modal?.trxState + ); + const stateChanged: boolean = trxState === 2; + + const { account, chainId, library } = useWeb3React(); + const [loadingFarm, setLoadingState] = useState(false); + + const dispatch = useDispatch(); + + const handleUpdateFarms = useCallback( + (value) => { + dispatch( + content?.farm === "quick" + ? updateQuickSwapFarm(value) + : content?.farm === "stable" + ? updateStableFarm(value) + : null + ); + }, + [dispatch] + ); + + const calculateRigelPrice = async () => { + try { + let rgpPrice; + const pair = await smartFactory( + SMARTSWAPFACTORYADDRESSES[chainId as number], + library + ); + + if (chainId === 56 || chainId === 97) { + const pairAddress = await pair.getPair( + RGPADDRESSES[chainId as number], + BUSD[chainId as number] + ); + const LpTokenContract = await LiquidityPairInstance( + pairAddress, + library + ); + const token0Address = await LpTokenContract.token0(); + const token1Address = await LpTokenContract.token1(); + + const [token0Contract, token1Contract] = await Promise.all([ + getERC20Token(token0Address, library), + getERC20Token(token1Address, library), + ]); + + const [decimal0, decimal1] = await Promise.all([ + token0Contract.decimals(), + token1Contract.decimals(), + ]); + + const reserves = await LpTokenContract.getReserves(); + const totalBUSD: number | any = ethers.utils.formatUnits( + token0Address === BUSD[chainId as number] ? reserves[0] : reserves[1], + token0Address === BUSD[chainId as number] ? decimal0 : decimal1 + ); - const calculateRigelPrice = async () => { - try { - let rgpPrice; - const pair = await smartFactory( - SMARTSWAPFACTORYADDRESSES[chainId as number], - library - ); - - if (chainId === 56 || chainId === 97) { - const pairAddress = await pair.getPair( - RGPADDRESSES[chainId as number], - BUSD[chainId as number] - ); - const LpTokenContract = await LiquidityPairInstance( - pairAddress, - library - ); - const token0Address = await LpTokenContract.token0(); - const token1Address = await LpTokenContract.token1(); - - const [token0Contract, token1Contract] = await Promise.all([ - getERC20Token(token0Address, library), - getERC20Token(token1Address, library), - ]); - - const [decimal0, decimal1] = await Promise.all([ - token0Contract.decimals(), - token1Contract.decimals(), - ]); - - const reserves = await LpTokenContract.getReserves(); - const totalBUSD: number | any = ethers.utils.formatUnits( - token0Address === BUSD[chainId as number] ? reserves[0] : reserves[1], - token0Address === BUSD[chainId as number] ? decimal0 : decimal1 - ); - - const totalRGP: number | any = ethers.utils.formatUnits( - token0Address === BUSD[chainId as number] ? reserves[1] : reserves[0], - token0Address === BUSD[chainId as number] ? decimal1 : decimal0 - ); - - rgpPrice = totalBUSD / totalRGP; - } else { - const pairAddress = await pair.getPair( - RGPADDRESSES[chainId as number], - USDT[chainId as number] - ); - const LpTokenContract = await LiquidityPairInstance( - pairAddress, - library - ); - const token0Address = await LpTokenContract.token0(); - const token1Address = await LpTokenContract.token1(); - const [token0Contract, token1Contract] = await Promise.all([ - getERC20Token(token0Address, library), - getERC20Token(token1Address, library), - ]); - const [symbol0, symbol1] = await Promise.all([ - token0Contract.symbol(), - token1Contract.symbol(), - ]); - - const [decimal0, decimal1] = await Promise.all([ - token0Contract.decimals(), - token1Contract.decimals(), - ]); - const reserves = await LpTokenContract.getReserves(); - const totalUSDT: number | any = ethers.utils.formatUnits( - token0Address === USDT[chainId as number] ? reserves[0] : reserves[1], - token0Address === USDT[chainId as number] ? decimal0 : decimal1 - ); - const totalRGP: number | any = ethers.utils.formatUnits( - token0Address === USDT[chainId as number] ? reserves[1] : reserves[0], - token0Address === USDT[chainId as number] ? decimal1 : decimal0 - ); - rgpPrice = totalUSDT / totalRGP; - } - - return rgpPrice; - } catch (err) { - console.log(err); - } - }; + const totalRGP: number | any = ethers.utils.formatUnits( + token0Address === BUSD[chainId as number] ? reserves[1] : reserves[0], + token0Address === BUSD[chainId as number] ? decimal1 : decimal0 + ); - const getLpTokenReserve = async (address: string) => { - //this function gets the reserves of an LP token, it takes the LP token address as an argument - try { - const LpTokenContract = await LiquidityPairInstance(address, library); - const [totalReserves, token0, token1, LPLockedPair, LPTotalSupply] = await Promise.all([ - LpTokenContract.getReserves(), - LpTokenContract.token0(), - LpTokenContract.token1(), - LpTokenContract.balanceOf(MASTERCHEFNEWLPADDRESSES[chainId as number][contractID]), - LpTokenContract.totalSupply() - ]); - - const [token0Contract, token1Contract] = await Promise.all([ - getERC20Token(token0, library), - getERC20Token(token1, library), - ]); - const [symbol0, decimals0, symbol1, decimals1] = await Promise.all([ - token0Contract.symbol(), - token0Contract.decimals(), - token1Contract.symbol(), - token1Contract.decimals(), - ]); - return { - reserves0: totalReserves[0], - reserves1: totalReserves[1], - tokenAddress0: token0, - tokenAddress1: token1, - symbol0, - symbol1, - decimals0, - decimals1, - LPSupply: LPLockedPair, - LPTotalSupply - }; - } catch (err) { - console.log(err); - } - }; + rgpPrice = totalBUSD / totalRGP; + } else { + const pairAddress = await pair.getPair( + RGPADDRESSES[chainId as number], + USDT[chainId as number] + ); + const LpTokenContract = await LiquidityPairInstance( + pairAddress, + library + ); + const token0Address = await LpTokenContract.token0(); + const token1Address = await LpTokenContract.token1(); + const [token0Contract, token1Contract] = await Promise.all([ + getERC20Token(token0Address, library), + getERC20Token(token1Address, library), + ]); + const [symbol0, symbol1] = await Promise.all([ + token0Contract.symbol(), + token1Contract.symbol(), + ]); + + const [decimal0, decimal1] = await Promise.all([ + token0Contract.decimals(), + token1Contract.decimals(), + ]); + const reserves = await LpTokenContract.getReserves(); + const totalUSDT: number | any = ethers.utils.formatUnits( + token0Address === USDT[chainId as number] ? reserves[0] : reserves[1], + token0Address === USDT[chainId as number] ? decimal0 : decimal1 + ); + const totalRGP: number | any = ethers.utils.formatUnits( + token0Address === USDT[chainId as number] ? reserves[1] : reserves[0], + token0Address === USDT[chainId as number] ? decimal1 : decimal0 + ); + rgpPrice = totalUSDT / totalRGP; + } - const calculateLiquidityAndApy = async (reward: number | undefined) => { - try { - const masterchef = await MasterChefNEWLPContract( - MASTERCHEFNEWLPADDRESSES[chainId as number][contractID], - library - ); - const LPInstance = await LiquidityPairInstance(content.address, library); - const reserves = await getLpTokenReserve(content.address); - const totalStable = reserves - ? BUSD[chainId as number] === reserves.tokenAddress0 || - USDT[chainId as number] === reserves.tokenAddress0 || - USDC[chainId as number] === reserves.tokenAddress0 - ? ethers.utils.formatUnits( - reserves.reserves0.toString(), - reserves.decimals0 - ) - : ethers.utils.formatUnits( - reserves.reserves1.toString(), - reserves.decimals1 - ) - : "1"; - - const totalBNB = reserves - ? SYMBOLS['BNB'][chainId as number] === reserves.tokenAddress0 - ? reserves.reserves0.toString() - : reserves.reserves1.toString() - : "1"; - - const rgpPrice = await calculateRigelPrice(); - const BNBPrice = await getBNBPrice(SMARTSWAPLP_TOKEN3ADDRESSES[chainId as number], library); - - const [token0, token1] = await Promise.all([ - getERC20Token(reserves && reserves.tokenAddress0, library), - getERC20Token(reserves && reserves.tokenAddress1, library), - ]); - - const [symbol0, symbol1] = await Promise.all([ - token0.symbol(), - token1.symbol(), - ]); - - const totalRGP = reserves - ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 - ? reserves.reserves0.toString() - : reserves.reserves1.toString() - : "1"; - - const totalLiquidity = - symbol0 === "BUSD" || - symbol1 === "BUSD" || - symbol0 === "USDT" || - symbol1 === "USDT" || - symbol0 === "USDC" || - symbol1 === "USDC" - ? parseFloat(totalStable) * 2 : - symbol0 === "WBNB" || symbol1 === "WBNB" - ? parseFloat(ethers.utils.formatEther(totalBNB)) * BNBPrice * 2 - : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; - - const LiquidityLocked = parseFloat(ethers.utils.formatEther(reserves.LPSupply)) / parseFloat(ethers.utils.formatEther(reserves.LPTotalSupply)); - - const newLiquidity = (LiquidityLocked * totalLiquidity); - - const [poolInfo, totalAllocPoint] = await Promise.all([ - masterchef.poolInfo(content.id), - masterchef.totalAllocPoint(), - ]); - const allocPoint = await poolInfo.allocPoint; - const poolReward = - (parseFloat(allocPoint.toString()) / - parseFloat(totalAllocPoint.toString())) * - reward; - const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; - const newAPY = (rgpPrice * poolReward * 365 * 100) / newLiquidity; - - const [tokenEarned, userInfo, FarmTokenBalance, allowance] = - await Promise.all([ - masterchef.pendingRigel(content.id, account), - masterchef.userInfo(content.id, account), - LPInstance.balanceOf(account), - LPInstance.allowance( - account, - MASTERCHEFNEWLPADDRESSES[chainId as number][contractID] - ), - ]); - const tokenStaked = await userInfo.amount; - - return { - id: content.id, - img: "rgp.svg", - deposit: content.deposit, - earn: "RGP", - type: "LP", - totalLiquidity, - APY: newAPY, - tokenStaked: [ - `${content.symbol0}-${content.symbol1}`, - ethers.utils.formatEther(tokenStaked.toString()), - ], - RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), - availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), - poolAllowance: ethers.utils.formatEther(allowance.toString()), - address: content.address, - LPLocked: newLiquidity.toFixed(2), - }; - } catch (err) { - console.log(err); - } - }; + return rgpPrice; + } catch (err) { + console.log(err); + } + }; + + const getLpTokenReserve = async (address: string) => { + //this function gets the reserves of an LP token, it takes the LP token address as an argument + try { + const LpTokenContract = await LiquidityPairInstance(address, library); + const [totalReserves, token0, token1, LPLockedPair, LPTotalSupply] = + await Promise.all([ + LpTokenContract.getReserves(), + LpTokenContract.token0(), + LpTokenContract.token1(), + LpTokenContract.balanceOf( + MASTERCHEFNEWLPADDRESSES[chainId as number][contractID] + ), + LpTokenContract.totalSupply(), + ]); + + const [token0Contract, token1Contract] = await Promise.all([ + getERC20Token(token0, library), + getERC20Token(token1, library), + ]); + const [symbol0, decimals0, symbol1, decimals1] = await Promise.all([ + token0Contract.symbol(), + token0Contract.decimals(), + token1Contract.symbol(), + token1Contract.decimals(), + ]); + return { + reserves0: totalReserves[0], + reserves1: totalReserves[1], + tokenAddress0: token0, + tokenAddress1: token1, + symbol0, + symbol1, + decimals0, + decimals1, + LPSupply: LPLockedPair, + LPTotalSupply, + }; + } catch (err) { + console.log(err); + } + }; - useMemo(async () => { - if (data !== undefined && account) { - if (reload) { - setLoadingState(true); - let newArray = - section === "search" - ? searchSection.newSearchResult !== undefined - ? [...searchSection.newSearchResult] - : [...searchSection.searchResult] - : section === "filter" - ? searchSection.newFilterResult !== undefined - ? [...searchSection.newFilterResult] - : [...searchSection.filterResult] - : searchSection.content !== undefined - ? [...searchSection.content] - : [...data.contents]; - - const updatedFarm = await calculateLiquidityAndApy( - chainId === 137 || chainId === 80001 - ? 2014.83125 - : chainId === 56 || chainId === 97 - ? 4300 - : chainId === 42262 || chainId === 42261 - ? 1343.220833 - : undefined - ); - const index = newArray.findIndex((item) => item.id === content.id); - newArray[index] = updatedFarm; - handleUpdateFarms(newArray); - - setLoadingState(false); - setReload(false); - } - } - }, [reload, stateChanged, chainId, selected, mainLP]); - return { loadingFarm }; + const calculateLiquidityAndApy = async (reward: number | undefined) => { + try { + const masterchef = await MasterChefNEWLPContract( + MASTERCHEFNEWLPADDRESSES[chainId as number][contractID], + library + ); + const LPInstance = await LiquidityPairInstance(content.address, library); + const reserves = await getLpTokenReserve(content.address); + const totalStable = reserves + ? BUSD[chainId as number] === reserves.tokenAddress0 || + USDT[chainId as number] === reserves.tokenAddress0 || + USDC[chainId as number] === reserves.tokenAddress0 + ? ethers.utils.formatUnits( + reserves.reserves0.toString(), + reserves.decimals0 + ) + : ethers.utils.formatUnits( + reserves.reserves1.toString(), + reserves.decimals1 + ) + : "1"; + + const totalBNB = reserves + ? SYMBOLS["BNB"][chainId as number] === reserves.tokenAddress0 + ? reserves.reserves0.toString() + : reserves.reserves1.toString() + : "1"; + + const rgpPrice = await calculateRigelPrice(); + const BNBPrice = await getBNBPrice( + SMARTSWAPLP_TOKEN3ADDRESSES[chainId as number], + library + ); + + const [token0, token1] = await Promise.all([ + getERC20Token(reserves && reserves.tokenAddress0, library), + getERC20Token(reserves && reserves.tokenAddress1, library), + ]); + + const [symbol0, symbol1] = await Promise.all([ + token0.symbol(), + token1.symbol(), + ]); + + const totalRGP = reserves + ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 + ? reserves.reserves0.toString() + : reserves.reserves1.toString() + : "1"; + + const totalLiquidity = + symbol0 === "BUSD" || + symbol1 === "BUSD" || + symbol0 === "USDT" || + symbol1 === "USDT" || + symbol0 === "USDC" || + symbol1 === "USDC" + ? parseFloat(totalStable) * 2 + : symbol0 === "WBNB" || symbol1 === "WBNB" + ? parseFloat(ethers.utils.formatEther(totalBNB)) * BNBPrice * 2 + : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; + + const LiquidityLocked = + parseFloat(ethers.utils.formatEther(reserves.LPSupply)) / + parseFloat(ethers.utils.formatEther(reserves.LPTotalSupply)); + + const newLiquidity = LiquidityLocked * totalLiquidity; + + const [poolInfo, totalAllocPoint] = await Promise.all([ + masterchef.poolInfo(content.id), + masterchef.totalAllocPoint(), + ]); + const allocPoint = await poolInfo.allocPoint; + const poolReward = + (parseFloat(allocPoint.toString()) / + parseFloat(totalAllocPoint.toString())) * + reward; + const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; + const newAPY = (rgpPrice * poolReward * 365 * 100) / newLiquidity; + + const [tokenEarned, userInfo, FarmTokenBalance, allowance] = + await Promise.all([ + masterchef.pendingRigel(content.id, account), + masterchef.userInfo(content.id, account), + LPInstance.balanceOf(account), + LPInstance.allowance( + account, + MASTERCHEFNEWLPADDRESSES[chainId as number][contractID] + ), + ]); + const tokenStaked = await userInfo.amount; + + return { + id: content.id, + img: "rgp.svg", + deposit: content.deposit, + earn: "RGP", + type: "LP", + totalLiquidity, + APY: newAPY, + tokenStaked: [ + `${content.symbol0}-${content.symbol1}`, + ethers.utils.formatEther(tokenStaked.toString()), + ], + RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), + availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), + poolAllowance: ethers.utils.formatEther(allowance.toString()), + address: content.address, + LPLocked: newLiquidity.toFixed(2), + farm: content?.farm, + }; + } catch (err) { + console.log(err); + } + }; + + useMemo(async () => { + if (data !== undefined && account) { + if (reload) { + setLoadingState(true); + let newArray = [...data]; + + const updatedFarm = await calculateLiquidityAndApy( + chainId === 137 || chainId === 80001 + ? 2014.83125 + : chainId === 56 || chainId === 97 + ? 4300 + : chainId === 42262 || chainId === 42261 + ? 1343.220833 + : undefined + ); + const index = newArray.findIndex((item) => item.id === content.id); + newArray[index] = updatedFarm; + handleUpdateFarms(newArray); + + setLoadingState(false); + setReload(false); + } + } + }, [reload, stateChanged, chainId, selected, mainLP]); + return { loadingFarm }; }; interface FetchYieldFarmDetails { - content: { - id: number | undefined; - img: string; - deposit: string; - symbol0: string; - symbol1: string; - earn: string; - type: string; - totalLiquidity: number; - APY: number; - address: string; - }; - section: string; - loading: boolean; - setLoading: React.Dispatch>; - contractID: number + content: { + id: number | undefined; + img: string; + deposit: string; + symbol0: string; + symbol1: string; + earn: string; + type: string; + totalLiquidity: number; + APY: number; + address: string; + }; + section: string; + loading: boolean; + setLoading: React.Dispatch>; + contractID: number; } -export const useNewYieldFarmDetails = ({content, section, loading, setLoading, contractID}: FetchYieldFarmDetails) => { - const data = useNewLPData(); - const searchSection = useSelector((state) => state.newFarming); - const { account, chainId, library } = useWeb3React(); - // const [loading, setLoading] = useState(true); - - const trxState = useSelector((state) => state.application.modal?.trxState); - const stateChanged : boolean = trxState === 2; - - const selectedField = useSelector((state: State) => state.farming.selectedField); - const selected = selectedField === farmSection.SECOND_NEW_LP; - const mainLP = selectedField === farmSection.NEW_LP; - - const dispatch = useDispatch(); - - const handleUpdateFarms = useCallback( - (value) => { - dispatch( - section === "filter" - ? updateNewFilterResult({ farmData: value }) - : section === "search" - ? updateNewSearchResult({ farmData: value }) - : updateYieldFarmDetails({ value }) - ); - }, - [dispatch] - ); +export const useNewYieldFarmDetails = ({ + content, + section, + loading, + setLoading, + contractID, +}: FetchYieldFarmDetails) => { + const data = useNewLPData(content); + const searchSection = useSelector((state) => state.newFarming); + const { account, chainId, library } = useWeb3React(); + // const [loading, setLoading] = useState(true); + + const trxState = useSelector( + (state) => state.application.modal?.trxState + ); + const stateChanged: boolean = trxState === 2; + + const selectedField = useSelector( + (state: State) => state.farming.selectedField + ); + const selected = selectedField === farmSection.SECOND_NEW_LP; + const mainLP = selectedField === farmSection.NEW_LP; + + const dispatch = useDispatch(); + + const handleUpdateFarms = useCallback( + (value) => { + dispatch( + content?.farm === "quick" + ? updateQuickSwapFarm(value) + : content?.farm === "stable" + ? updateStableFarm(value) + : null + ); + }, + [dispatch] + ); + + const calculateRigelPrice = async () => { + try { + let rgpPrice; + const pair = await smartFactory( + SMARTSWAPFACTORYADDRESSES[chainId as number], + library + ); + + if (chainId === 56 || chainId === 97) { + const pairAddress = await pair.getPair( + RGPADDRESSES[chainId as number], + BUSD[chainId as number] + ); + const LpTokenContract = await LiquidityPairInstance( + pairAddress, + library + ); + const token0Address = await LpTokenContract.token0(); + const token1Address = await LpTokenContract.token1(); + + const [token0Contract, token1Contract] = await Promise.all([ + getERC20Token(token0Address, library), + getERC20Token(token1Address, library), + ]); + + const [decimal0, decimal1] = await Promise.all([ + token0Contract.decimals(), + token1Contract.decimals(), + ]); + + const reserves = await LpTokenContract.getReserves(); + const totalBUSD: number | any = ethers.utils.formatUnits( + token0Address === BUSD[chainId as number] ? reserves[0] : reserves[1], + token0Address === BUSD[chainId as number] ? decimal0 : decimal1 + ); - const calculateRigelPrice = async () => { - try { - let rgpPrice; - const pair = await smartFactory( - SMARTSWAPFACTORYADDRESSES[chainId as number], - library - ); - - if (chainId === 56 || chainId === 97) { - const pairAddress = await pair.getPair( - RGPADDRESSES[chainId as number], - BUSD[chainId as number] - ); - const LpTokenContract = await LiquidityPairInstance( - pairAddress, - library - ); - const token0Address = await LpTokenContract.token0(); - const token1Address = await LpTokenContract.token1(); - - const [token0Contract, token1Contract] = await Promise.all([ - getERC20Token(token0Address, library), - getERC20Token(token1Address, library), - ]); - - const [decimal0, decimal1] = await Promise.all([ - token0Contract.decimals(), - token1Contract.decimals(), - ]); - - const reserves = await LpTokenContract.getReserves(); - const totalBUSD: number | any = ethers.utils.formatUnits( - token0Address === BUSD[chainId as number] ? reserves[0] : reserves[1], - token0Address === BUSD[chainId as number] ? decimal0 : decimal1 - ); - - const totalRGP: number | any = ethers.utils.formatUnits( - token0Address === BUSD[chainId as number] ? reserves[1] : reserves[0], - token0Address === BUSD[chainId as number] ? decimal1 : decimal0 - ); - - rgpPrice = totalBUSD / totalRGP; - } else { - const pairAddress = await pair.getPair( - RGPADDRESSES[chainId as number], - USDT[chainId as number] - ); - const LpTokenContract = await LiquidityPairInstance( - pairAddress, - library - ); - const token0Address = await LpTokenContract.token0(); - const token1Address = await LpTokenContract.token1(); - const [token0Contract, token1Contract] = await Promise.all([ - getERC20Token(token0Address, library), - getERC20Token(token1Address, library), - ]); - const [symbol0, symbol1] = await Promise.all([ - token0Contract.symbol(), - token1Contract.symbol(), - ]); - - const [decimal0, decimal1] = await Promise.all([ - token0Contract.decimals(), - token1Contract.decimals(), - ]); - const reserves = await LpTokenContract.getReserves(); - const totalUSDT: number | any = ethers.utils.formatUnits( - token0Address === USDT[chainId as number] ? reserves[0] : reserves[1], - token0Address === USDT[chainId as number] ? decimal0 : decimal1 - ); - const totalRGP: number | any = ethers.utils.formatUnits( - token0Address === USDT[chainId as number] ? reserves[1] : reserves[0], - token0Address === USDT[chainId as number] ? decimal1 : decimal0 - ); - rgpPrice = totalUSDT / totalRGP; - } - - return rgpPrice; - } catch (err) { - console.log(err); - } - }; + const totalRGP: number | any = ethers.utils.formatUnits( + token0Address === BUSD[chainId as number] ? reserves[1] : reserves[0], + token0Address === BUSD[chainId as number] ? decimal1 : decimal0 + ); - const getLpTokenReserve = async (address: string) => { - //this function gets the reserves of an LP token, it takes the LP token address as an argument - try { - const LpTokenContract = await LiquidityPairInstance(address, library); - const [totalReserves, token0, token1, LPLockedPair, LPTotalSupply] = await Promise.all([ - LpTokenContract.getReserves(), - LpTokenContract.token0(), - LpTokenContract.token1(), - LpTokenContract.balanceOf(MASTERCHEFNEWLPADDRESSES[chainId as number][contractID]), - LpTokenContract.totalSupply() - ]); - - const [token0Contract, token1Contract] = await Promise.all([ - getERC20Token(token0, library), - getERC20Token(token1, library), - ]); - const [symbol0, decimals0, symbol1, decimals1] = await Promise.all([ - token0Contract.symbol(), - token0Contract.decimals(), - token1Contract.symbol(), - token1Contract.decimals(), - ]); - return { - reserves0: totalReserves[0], - reserves1: totalReserves[1], - tokenAddress0: token0, - tokenAddress1: token1, - symbol0, - symbol1, - decimals0, - decimals1, - LPSupply: LPLockedPair, - LPTotalSupply - }; - } catch (err) { - console.log(err); - } - }; + rgpPrice = totalBUSD / totalRGP; + } else { + const pairAddress = await pair.getPair( + RGPADDRESSES[chainId as number], + USDT[chainId as number] + ); + const LpTokenContract = await LiquidityPairInstance( + pairAddress, + library + ); + const token0Address = await LpTokenContract.token0(); + const token1Address = await LpTokenContract.token1(); + const [token0Contract, token1Contract] = await Promise.all([ + getERC20Token(token0Address, library), + getERC20Token(token1Address, library), + ]); + const [symbol0, symbol1] = await Promise.all([ + token0Contract.symbol(), + token1Contract.symbol(), + ]); + + const [decimal0, decimal1] = await Promise.all([ + token0Contract.decimals(), + token1Contract.decimals(), + ]); + const reserves = await LpTokenContract.getReserves(); + const totalUSDT: number | any = ethers.utils.formatUnits( + token0Address === USDT[chainId as number] ? reserves[0] : reserves[1], + token0Address === USDT[chainId as number] ? decimal0 : decimal1 + ); + const totalRGP: number | any = ethers.utils.formatUnits( + token0Address === USDT[chainId as number] ? reserves[1] : reserves[0], + token0Address === USDT[chainId as number] ? decimal1 : decimal0 + ); + rgpPrice = totalUSDT / totalRGP; + } - const calculateLiquidityAndApy = async (reward: number | undefined) => { - try { - const masterchef = await MasterChefNEWLPContract( - MASTERCHEFNEWLPADDRESSES[chainId as number][contractID], - library - ); - const LPInstance = await LiquidityPairInstance(content.address, library); - const reserves = await getLpTokenReserve(content.address); - const totalStable = reserves - ? BUSD[chainId as number] === reserves.tokenAddress0 || - USDT[chainId as number] === reserves.tokenAddress0 || - USDC[chainId as number] === reserves.tokenAddress0 - ? ethers.utils.formatUnits( - reserves.reserves0.toString(), - reserves.decimals0 - ) - : ethers.utils.formatUnits( - reserves.reserves1.toString(), - reserves.decimals1 - ) - : "1"; - - const rgpPrice = await calculateRigelPrice(); - const BNBPrice = await getBNBPrice(SMARTSWAPLP_TOKEN3ADDRESSES[chainId as number], library); - - const [token0, token1] = await Promise.all([ - getERC20Token(reserves && reserves.tokenAddress0, library), - getERC20Token(reserves && reserves.tokenAddress1, library), - ]); - - const [symbol0, symbol1] = await Promise.all([ - token0.symbol(), - token1.symbol(), - ]); - - const totalRGP = reserves - ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 - ? reserves.reserves0.toString() - : reserves.reserves1.toString() - : "1"; - - const totalBNB = reserves - ? SYMBOLS['BNB'][chainId as number] === reserves.tokenAddress0 - ? reserves.reserves0.toString() - : reserves.reserves1.toString() - : "1"; - - const totalLiquidity = - symbol0 === "BUSD" || - symbol1 === "BUSD" || - symbol0 === "USDT" || - symbol1 === "USDT" || - symbol0 === "USDC" || - symbol1 === "USDC" - ? parseFloat(totalStable) * 2 : - symbol0 === "WBNB" || symbol1 === "WBNB" - ? parseFloat(ethers.utils.formatEther(totalBNB)) * BNBPrice * 2 - : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; - - const LiquidityLocked = parseFloat(ethers.utils.formatEther(reserves.LPSupply)) / parseFloat(ethers.utils.formatEther(reserves.LPTotalSupply)); - const newLiquidity = (LiquidityLocked * totalLiquidity); - - const [poolInfo, totalAllocPoint] = await Promise.all([ - masterchef.poolInfo(content.id), - masterchef.totalAllocPoint(), - ]); - const allocPoint = await poolInfo.allocPoint; - const poolReward = - (parseFloat(allocPoint.toString()) / - parseFloat(totalAllocPoint.toString())) * - reward; - const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; - const newAPY = (rgpPrice * poolReward * 365 * 100) / newLiquidity; - - - const [tokenEarned, userInfo, FarmTokenBalance, allowance] = - await Promise.all([ - masterchef.pendingRigel(content.id, account), - masterchef.userInfo(content.id, account), - LPInstance.balanceOf(account), - LPInstance.allowance( - account, - MASTERCHEFNEWLPADDRESSES[chainId as number][contractID] - ), - ]); - const tokenStaked = await userInfo.amount; - - return { - id: content.id, - img: "rgp.svg", - deposit: content.deposit, - earn: "RGP", - type: "LP", - totalLiquidity, - APY: newAPY, - tokenStaked: [ - `${content.symbol0}-${content.symbol1}`, - ethers.utils.formatEther(tokenStaked.toString()), - ], - RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), - availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), - poolAllowance: ethers.utils.formatEther(allowance.toString()), - address: content.address, - LPLocked: newLiquidity.toFixed(2) - }; - } catch (err) { - console.log(err); - } - }; + return rgpPrice; + } catch (err) { + console.log(err); + } + }; + + const getLpTokenReserve = async (address: string) => { + //this function gets the reserves of an LP token, it takes the LP token address as an argument + try { + const LpTokenContract = await LiquidityPairInstance(address, library); + const [totalReserves, token0, token1, LPLockedPair, LPTotalSupply] = + await Promise.all([ + LpTokenContract.getReserves(), + LpTokenContract.token0(), + LpTokenContract.token1(), + LpTokenContract.balanceOf( + MASTERCHEFNEWLPADDRESSES[chainId as number][contractID] + ), + LpTokenContract.totalSupply(), + ]); + + const [token0Contract, token1Contract] = await Promise.all([ + getERC20Token(token0, library), + getERC20Token(token1, library), + ]); + const [symbol0, decimals0, symbol1, decimals1] = await Promise.all([ + token0Contract.symbol(), + token0Contract.decimals(), + token1Contract.symbol(), + token1Contract.decimals(), + ]); + return { + reserves0: totalReserves[0], + reserves1: totalReserves[1], + tokenAddress0: token0, + tokenAddress1: token1, + symbol0, + symbol1, + decimals0, + decimals1, + LPSupply: LPLockedPair, + LPTotalSupply, + }; + } catch (err) { + console.log(err); + } + }; - useMemo(async () => { - if (data !== undefined && account) { - setLoading(true); - let newArray = - section === "search" - ? searchSection.newSearchResult !== undefined - ? [...searchSection.newSearchResult] - : [...searchSection.searchResult] - : section === "filter" - ? searchSection.newFilterResult !== undefined - ? [...searchSection.newFilterResult] - : [...searchSection.filterResult] - : searchSection.content !== undefined - ? [...searchSection.content] - : [...data.contents]; - - const updatedFarm = await calculateLiquidityAndApy( - chainId === 137 || chainId === 80001 - ? 2014.83125 - : chainId === 56 || chainId === 97 - ? 4300 - : chainId === 42262 || chainId === 42261 - ? 1343.220833 - : undefined - ); - const index = newArray.findIndex((item) => item.id === content.id); - - newArray[index] = updatedFarm; - - console.log("updated", newArray); - - handleUpdateFarms(newArray); - - setLoading(false); - } - }, [account, stateChanged, chainId, selected, mainLP]); - return { loading }; + const calculateLiquidityAndApy = async (reward: number | undefined) => { + try { + const masterchef = await MasterChefNEWLPContract( + MASTERCHEFNEWLPADDRESSES[chainId as number][contractID], + library + ); + const LPInstance = await LiquidityPairInstance(content.address, library); + const reserves = await getLpTokenReserve(content.address); + const totalStable = reserves + ? BUSD[chainId as number] === reserves.tokenAddress0 || + USDT[chainId as number] === reserves.tokenAddress0 || + USDC[chainId as number] === reserves.tokenAddress0 + ? ethers.utils.formatUnits( + reserves.reserves0.toString(), + reserves.decimals0 + ) + : ethers.utils.formatUnits( + reserves.reserves1.toString(), + reserves.decimals1 + ) + : "1"; + + const rgpPrice = await calculateRigelPrice(); + const BNBPrice = await getBNBPrice( + SMARTSWAPLP_TOKEN3ADDRESSES[chainId as number], + library + ); + + const [token0, token1] = await Promise.all([ + getERC20Token(reserves && reserves.tokenAddress0, library), + getERC20Token(reserves && reserves.tokenAddress1, library), + ]); + + const [symbol0, symbol1] = await Promise.all([ + token0.symbol(), + token1.symbol(), + ]); + + const totalRGP = reserves + ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 + ? reserves.reserves0.toString() + : reserves.reserves1.toString() + : "1"; + + const totalBNB = reserves + ? SYMBOLS["BNB"][chainId as number] === reserves.tokenAddress0 + ? reserves.reserves0.toString() + : reserves.reserves1.toString() + : "1"; + + const totalLiquidity = + symbol0 === "BUSD" || + symbol1 === "BUSD" || + symbol0 === "USDT" || + symbol1 === "USDT" || + symbol0 === "USDC" || + symbol1 === "USDC" + ? parseFloat(totalStable) * 2 + : symbol0 === "WBNB" || symbol1 === "WBNB" + ? parseFloat(ethers.utils.formatEther(totalBNB)) * BNBPrice * 2 + : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; + + const LiquidityLocked = + parseFloat(ethers.utils.formatEther(reserves.LPSupply)) / + parseFloat(ethers.utils.formatEther(reserves.LPTotalSupply)); + const newLiquidity = LiquidityLocked * totalLiquidity; + + const [poolInfo, totalAllocPoint] = await Promise.all([ + masterchef.poolInfo(content.id), + masterchef.totalAllocPoint(), + ]); + const allocPoint = await poolInfo.allocPoint; + const poolReward = + (parseFloat(allocPoint.toString()) / + parseFloat(totalAllocPoint.toString())) * + reward; + const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; + const newAPY = (rgpPrice * poolReward * 365 * 100) / newLiquidity; + + const [tokenEarned, userInfo, FarmTokenBalance, allowance] = + await Promise.all([ + masterchef.pendingRigel(content.id, account), + masterchef.userInfo(content.id, account), + LPInstance.balanceOf(account), + LPInstance.allowance( + account, + MASTERCHEFNEWLPADDRESSES[chainId as number][contractID] + ), + ]); + const tokenStaked = await userInfo.amount; + + return { + id: content.id, + img: "rgp.svg", + deposit: content.deposit, + earn: "RGP", + type: "LP", + totalLiquidity, + APY: newAPY, + tokenStaked: [ + `${content.symbol0}-${content.symbol1}`, + ethers.utils.formatEther(tokenStaked.toString()), + ], + RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), + availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), + poolAllowance: ethers.utils.formatEther(allowance.toString()), + address: content.address, + LPLocked: newLiquidity.toFixed(2), + farm: content?.farm, + }; + } catch (err) { + console.log(err); + } + }; + + useMemo(async () => { + if (data !== undefined && account) { + setLoading(true); + let newArray = [...data]; + + const updatedFarm = await calculateLiquidityAndApy( + chainId === 137 || chainId === 80001 + ? 2014.83125 + : chainId === 56 || chainId === 97 + ? 4300 + : chainId === 42262 || chainId === 42261 + ? 1343.220833 + : undefined + ); + const index = newArray.findIndex((item) => item.id === content.id); + + newArray[index] = updatedFarm; + + console.log("updated", newArray); + + handleUpdateFarms(newArray); + + setLoading(false); + } + }, [account, stateChanged, chainId, selected, mainLP]); + return { loading }; }; interface productFarmState { - content: { - feature:string, - percentageProfitShare:string, - profitTimeLine:string, - totalLiquidity:string, - estimatedTotalProfit:string, - pid:number, - deposit:string, - poolAllowance: "", - type:string - }; - loading: boolean; - setLoading: React.Dispatch>; + content: { + feature: string; + percentageProfitShare: string; + profitTimeLine: string; + totalLiquidity: string; + estimatedTotalProfit: string; + pid: number; + deposit: string; + poolAllowance: ""; + type: string; + }; + loading: boolean; + setLoading: React.Dispatch>; } export const useProductFarmDetails = ({ - content, - loading, - setLoading - }:productFarmState) =>{ - const { account, chainId, library } = useWeb3React(); - const getUserValue = async ()=> { - const productFarm = await productStakingContract( - PRODUCTSTAKINGADDRESSES[chainId as number], - library - ); - const [ userInfo, FarmTokenBalance] = - await Promise.all([ - productFarm.userInfo(account), - productFarm.userData(account), - ]); - const tokenStaked = await FarmTokenBalance.tokenQuantity; - - return { - feature:"AutoTrade", - percentageProfitShare:"25%", - profitTimeLine:"6 months", - totalLiquidity:"188839", - estimatedTotalProfit:"929020003", - deposit: "RGP", - pid:93903, - poolAllowance: "", - type:"AT", - tokenStaked: Web3.utils.fromWei(tokenStaked.toString()) - }; - } - - useMemo(async ()=>{ - let response =await getUserValue() + content, + loading, + setLoading, +}: productFarmState) => { + const { account, chainId, library } = useWeb3React(); + const getUserValue = async () => { + const productFarm = await productStakingContract( + PRODUCTSTAKINGADDRESSES[chainId as number], + library + ); + const [userInfo, FarmTokenBalance] = await Promise.all([ + productFarm.userInfo(account), + productFarm.userData(account), + ]); + const tokenStaked = await FarmTokenBalance.tokenQuantity; + + return { + feature: "AutoTrade", + percentageProfitShare: "25%", + profitTimeLine: "6 months", + totalLiquidity: "188839", + estimatedTotalProfit: "929020003", + deposit: "RGP", + pid: 93903, + poolAllowance: "", + type: "AT", + tokenStaked: Web3.utils.fromWei(tokenStaked.toString()), + }; + }; - updateProductFarmDetails({value:[response]}) + useMemo(async () => { + let response = await getUserValue(); - setLoading(false) - },[]) - return {loading} + updateProductFarmDetails({ value: [response] }); -} + setLoading(false); + }, []); + return { loading }; +}; diff --git a/src/state/farm/actions.ts b/src/state/farm/actions.ts index 3bdb1b35..5bc16b6b 100644 --- a/src/state/farm/actions.ts +++ b/src/state/farm/actions.ts @@ -38,3 +38,4 @@ export const updateQuickSwapFarm = createAction( "farm/updateQuickSwapFarm" ); export const updateStableFarm = createAction("farm/updateStableFarm"); +export const updateSpecialPool = createAction("farm/updateSpecialPool"); diff --git a/src/state/farm/reducer.ts b/src/state/farm/reducer.ts index 640036e5..b7399f87 100644 --- a/src/state/farm/reducer.ts +++ b/src/state/farm/reducer.ts @@ -13,6 +13,7 @@ import { updateLpFarm, updateQuickSwapFarm, updateStableFarm, + updateSpecialPool, } from "./actions"; export interface farmStateInterface { @@ -41,6 +42,7 @@ export interface farmStateInterface { LpFarm: any; QuickswapFarm: any; StableFarm: any; + SpecialPool: any; } export const initialState: farmStateInterface = { @@ -85,6 +87,7 @@ export const initialState: farmStateInterface = { LpFarm: [], QuickswapFarm: [], StableFarm: [], + SpecialPool: [], }; export default createReducer(initialState, (builder) => @@ -193,4 +196,8 @@ export default createReducer(initialState, (builder) => .addCase(updateStableFarm, (state, action) => { state.StableFarm = action.payload; }) + + .addCase(updateSpecialPool, (state, action) => { + state.SpecialPool = action.payload; + }) ); diff --git a/src/state/newfarm/hooks.ts b/src/state/newfarm/hooks.ts index b83227fa..4bb3b165 100644 --- a/src/state/newfarm/hooks.ts +++ b/src/state/newfarm/hooks.ts @@ -12,6 +12,7 @@ import { LiquidityPairInstance, smartFactory, productStakingContract, + RGPSpecialPool2, } from "../../utils/Contracts"; import { MASTERCHEFV2ADDRESSES, @@ -21,6 +22,7 @@ import { USDT, USDC, PRODUCTSTAKINGADDRESSES, + RGPSPECIALPOOLADDRESSES2, } from "../../utils/addresses"; import { getERC20Token } from "../../utils/utilsFunctions"; import { ethers } from "ethers"; @@ -34,10 +36,28 @@ import { updateProductFarmDetails, } from "../farming/action"; import Web3 from "web3"; - -export const useFarmData = (): farmDataInterface => { - const farms = useSelector((state: State) => state.newfarm); - return farms; +import { + updateQuickSwapFarm, + updateLpFarm, + updateStableFarm, + updateSpecialPool, +} from "../farm/actions"; +import farm from "../farm"; + +export const useFarmData = (content: any): farmDataInterface => { + const farms = useSelector((state: State) => state.farms); + const farm = + content?.farm === "lp" + ? farms.LpFarm + : content?.farm === "quick" + ? farms.QuickswapFarm + : content?.farm === "stable" + ? farms.StableFarm + : content?.farm === "special" + ? farms.SpecialPool + : null; + + return farm; }; interface updateFarmInterface { @@ -63,9 +83,10 @@ export const useUpdateFarm = ({ reload, setReload, content, - section, address + section, + address, }: updateFarmInterface) => { - const data = useFarmData(); + const data = useFarmData(content); const searchSection = useSelector((state) => state.farming); const { account, chainId, library } = useWeb3React(); @@ -76,11 +97,15 @@ export const useUpdateFarm = ({ const handleUpdateFarms = useCallback( (value) => { dispatch( - section === "filter" - ? updateNewFilterResult({ farmData: value }) - : section === "search" - ? updateNewSearchResult({ farmData: value }) - : updateYieldFarmDetails({ value }) + content?.farm === "lp" + ? updateLpFarm(value) + : content?.farm === "quick" + ? updateQuickSwapFarm(value) + : content?.farm === "stable" + ? updateStableFarm(value) + : content?.farm === "special" + ? updateSpecialPool(value) + : null ); }, [dispatch] @@ -207,114 +232,156 @@ export const useUpdateFarm = ({ const calculateLiquidityAndApy = async (reward: number | undefined) => { try { - const masterchef = await MasterChefV2Contract( - MASTERCHEFV2ADDRESSES[chainId as number], - library - ); - const LPInstance = await LiquidityPairInstance(content.address, library); - const reserves = await getLpTokenReserve(content.address); - const totalStable = reserves - ? BUSD[chainId as number] === reserves.tokenAddress0 || - USDT[chainId as number] === reserves.tokenAddress0 || - USDC[chainId as number] === reserves.tokenAddress0 - ? ethers.utils.formatUnits( - reserves.reserves0.toString(), - reserves.decimals0 - ) - : ethers.utils.formatUnits( - reserves.reserves1.toString(), - reserves.decimals1 - ) - : "1"; - - const rgpPrice = await calculateRigelPrice(); - - const [token0, token1] = await Promise.all([ - getERC20Token(reserves && reserves.tokenAddress0, library), - getERC20Token(reserves && reserves.tokenAddress1, library), - ]); + if (content?.farm === "special") { + const specialPool = await RGPSpecialPool2(content?.address, library); - const [symbol0, symbol1] = await Promise.all([ - token0.symbol(), - token1.symbol(), - ]); + const token = await getERC20Token( + RGPADDRESSES[chainId as number], + library + ); - const totalRGP = reserves - ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 - ? reserves.reserves0.toString() - : reserves.reserves1.toString() - : "1"; - - const totalLiquidity = - symbol0 === "BUSD" || - symbol1 === "BUSD" || - symbol0 === "USDT" || - symbol1 === "USDT" || - symbol0 === "USDC" || - symbol1 === "USDC" - ? parseFloat(totalStable) * 2 - : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; - - const [poolInfo, totalAllocPoint] = await Promise.all([ - masterchef.poolInfo(content.id), - masterchef.totalAllocPoint(), - ]); - const allocPoint = await poolInfo.allocPoint; - const poolReward = - (parseFloat(allocPoint.toString()) / - parseFloat(totalAllocPoint.toString())) * - reward; - const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; - const [tokenEarned, userInfo, FarmTokenBalance, allowance] = - await Promise.all([ - masterchef.pendingRigel(content.id, account), - masterchef.userInfo(content.id, account), - LPInstance.balanceOf(account), - LPInstance.allowance( - account, - MASTERCHEFV2ADDRESSES[chainId as number] + const [amountStaked, allowance, balance] = await Promise.all([ + specialPool.userInfo(account), + token.allowance(account, RGPSPECIALPOOLADDRESSES2[chainId as number]), + token.balanceOf(account), + ]); + + return { + id: content?.id, + img: "rgp.svg", + // deposit: 'RGP', + deposit: "RGP", + earn: "RGP", + type: "RGP", + ARYValue: content?.ARYValue, + totalLiquidity: content?.totalLiquidity, + tokensStaked: [ + "RGP", + parseFloat( + ethers.utils.formatEther(amountStaked?._amountStaked.toString()) + ), + ], + RGPEarned: parseFloat( + ethers.utils.formatEther(amountStaked?._userReward.toString()) + ), + availableToken: parseFloat( + ethers.utils.formatEther(balance.toString()) + ), + inflationPerDay: 0, + tokenPrice: content?.rgpPrice, + totalVolumePerPool: 0, + farmingFee: 0, + pId: content?.pId, + poolAllowance: parseFloat( + ethers.utils.formatEther(allowance.toString()) ), + poolVersion: content?.poolVersion, + version: content?.version, + farm: "special", + address: content?.address, + }; + } else { + const masterchef = await MasterChefV2Contract( + MASTERCHEFV2ADDRESSES[chainId as number], + library + ); + const LPInstance = await LiquidityPairInstance( + content.address, + library + ); + const reserves = await getLpTokenReserve(content.address); + const totalStable = reserves + ? BUSD[chainId as number] === reserves.tokenAddress0 || + USDT[chainId as number] === reserves.tokenAddress0 || + USDC[chainId as number] === reserves.tokenAddress0 + ? ethers.utils.formatUnits( + reserves.reserves0.toString(), + reserves.decimals0 + ) + : ethers.utils.formatUnits( + reserves.reserves1.toString(), + reserves.decimals1 + ) + : "1"; + + const rgpPrice = await calculateRigelPrice(); + + const [token0, token1] = await Promise.all([ + getERC20Token(reserves && reserves.tokenAddress0, library), + getERC20Token(reserves && reserves.tokenAddress1, library), ]); - const tokenStaked = await userInfo.amount; - return { - id: content.id, - img: "rgp.svg", - deposit: content.deposit, - earn: "RGP", - type: "LP", - totalLiquidity, - APY, - tokenStaked: [ - `${content.symbol0}-${content.symbol1}`, - ethers.utils.formatEther(tokenStaked.toString()), - ], - RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), - availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), - poolAllowance: ethers.utils.formatEther(allowance.toString()), - address: content.address, - }; + const [symbol0, symbol1] = await Promise.all([ + token0.symbol(), + token1.symbol(), + ]); + + const totalRGP = reserves + ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 + ? reserves.reserves0.toString() + : reserves.reserves1.toString() + : "1"; + + const totalLiquidity = + symbol0 === "BUSD" || + symbol1 === "BUSD" || + symbol0 === "USDT" || + symbol1 === "USDT" || + symbol0 === "USDC" || + symbol1 === "USDC" + ? parseFloat(totalStable) * 2 + : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; + + const [poolInfo, totalAllocPoint] = await Promise.all([ + masterchef.poolInfo(content.id), + masterchef.totalAllocPoint(), + ]); + const allocPoint = await poolInfo.allocPoint; + const poolReward = + (parseFloat(allocPoint.toString()) / + parseFloat(totalAllocPoint.toString())) * + reward; + const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; + const [tokenEarned, userInfo, FarmTokenBalance, allowance] = + await Promise.all([ + masterchef.pendingRigel(content.id, account), + masterchef.userInfo(content.id, account), + LPInstance.balanceOf(account), + LPInstance.allowance( + account, + MASTERCHEFV2ADDRESSES[chainId as number] + ), + ]); + const tokenStaked = await userInfo.amount; + + return { + id: content.id, + img: "rgp.svg", + deposit: content.deposit, + earn: "RGP", + type: "LP", + totalLiquidity, + APY, + tokenStaked: [ + `${content.symbol0}-${content.symbol1}`, + ethers.utils.formatEther(tokenStaked.toString()), + ], + RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), + availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), + poolAllowance: ethers.utils.formatEther(allowance.toString()), + address: content.address, + farm: content?.farm, + }; + } } catch (err) { console.log(err); } }; - useMemo(async () => { if (data !== undefined && account) { if (reload) { setLoadingState(true); - let newArray = - section === "search" - ? searchSection.newSearchResult !== undefined - ? [...searchSection.newSearchResult] - : [...searchSection.searchResult] - : section === "filter" - ? searchSection.newFilterResult !== undefined - ? [...searchSection.newFilterResult] - : [...searchSection.filterResult] - : searchSection.content !== undefined - ? [...searchSection.content] - : [...data.contents]; + let newArray = [...data]; const updatedFarm = await calculateLiquidityAndApy( chainId === 137 || chainId === 80001 @@ -353,15 +420,17 @@ interface FetchYieldFarmDetails { section: string; loading: boolean; setLoading: React.Dispatch>; + reload: boolean; } export const useFetchYieldFarmDetails = ({ content, section, loading, - setLoading + setLoading, + reload, }: FetchYieldFarmDetails) => { - const data = useFarmData(); + const data = useFarmData(content); const searchSection = useSelector((state) => state.farming); const { account, chainId, library } = useWeb3React(); // const [loading, setLoading] = useState(true); @@ -371,11 +440,15 @@ export const useFetchYieldFarmDetails = ({ const handleUpdateFarms = useCallback( (value) => { dispatch( - section === "filter" - ? updateNewFilterResult({ farmData: value }) - : section === "search" - ? updateNewSearchResult({ farmData: value }) - : updateYieldFarmDetails({ value }) + content?.farm === "lp" + ? updateLpFarm(value) + : content?.farm === "quick" + ? updateQuickSwapFarm(value) + : content?.farm === "stable" + ? updateStableFarm(value) + : content?.farm === "special" + ? updateSpecialPool(value) + : null ); }, [dispatch] @@ -505,93 +578,147 @@ export const useFetchYieldFarmDetails = ({ const calculateLiquidityAndApy = async (reward: number | undefined) => { try { - const masterchef = await MasterChefV2Contract( - MASTERCHEFV2ADDRESSES[chainId as number], - library - ); - const LPInstance = await LiquidityPairInstance(content.address, library); - const reserves = await getLpTokenReserve(content.address); - const totalStable = reserves - ? BUSD[chainId as number] === reserves.tokenAddress0 || - USDT[chainId as number] === reserves.tokenAddress0 || - USDC[chainId as number] === reserves.tokenAddress0 - ? ethers.utils.formatUnits( - reserves.reserves0.toString(), - reserves.decimals0 - ) - : ethers.utils.formatUnits( - reserves.reserves1.toString(), - reserves.decimals1 - ) - : "1"; - - const rgpPrice = await calculateRigelPrice(); - - const [token0, token1] = await Promise.all([ - getERC20Token(reserves && reserves.tokenAddress0, library), - getERC20Token(reserves && reserves.tokenAddress1, library), - ]); + if (content?.farm === "special") { + const specialPool = await RGPSpecialPool2(content?.address, library); - const [symbol0, symbol1] = await Promise.all([ - token0.symbol(), - token1.symbol(), - ]); + const token = await getERC20Token( + RGPADDRESSES[chainId as number], + library + ); - const totalRGP = reserves - ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 - ? reserves.reserves0.toString() - : reserves.reserves1.toString() - : "1"; - - const totalLiquidity = - symbol0 === "BUSD" || - symbol1 === "BUSD" || - symbol0 === "USDT" || - symbol1 === "USDT" || - symbol0 === "USDC" || - symbol1 === "USDC" - ? parseFloat(totalStable) * 2 - : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; - - const [poolInfo, totalAllocPoint] = await Promise.all([ - masterchef.poolInfo(content.id), - masterchef.totalAllocPoint(), - ]); - const allocPoint = await poolInfo.allocPoint; - const poolReward = - (parseFloat(allocPoint.toString()) / - parseFloat(totalAllocPoint.toString())) * - reward; - const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; - const [tokenEarned, userInfo, FarmTokenBalance, allowance] = - await Promise.all([ - masterchef.pendingRigel(content.id, account), - masterchef.userInfo(content.id, account), - LPInstance.balanceOf(account), - LPInstance.allowance( - account, - MASTERCHEFV2ADDRESSES[chainId as number] + const [amountStaked, allowance, balance] = await Promise.all([ + specialPool.userInfo(account), + token.allowance(account, RGPSPECIALPOOLADDRESSES2[chainId as number]), + token.balanceOf(account), + ]); + + return { + id: content?.id, + img: "rgp.svg", + // deposit: 'RGP', + deposit: "RGP", + earn: "RGP", + type: "RGP", + ARYValue: content?.ARYValue, + totalLiquidity: content?.totalLiquidity, + tokensStaked: [ + "RGP", + parseFloat( + ethers.utils.formatEther(amountStaked?._amountStaked.toString()) + ), + ], + RGPEarned: parseFloat( + ethers.utils.formatEther(amountStaked?._userReward.toString()) ), + availableToken: parseFloat( + ethers.utils.formatEther(balance.toString()) + ), + inflationPerDay: 0, + tokenPrice: content?.rgpPrice, + totalVolumePerPool: 0, + farmingFee: 0, + pId: content?.pId, + poolAllowance: parseFloat( + ethers.utils.formatEther(allowance.toString()) + ), + poolVersion: content?.poolVersion, + version: content?.version, + farm: "special", + address: content?.address, + }; + } else { + const masterchef = await MasterChefV2Contract( + MASTERCHEFV2ADDRESSES[chainId as number], + library + ); + const LPInstance = await LiquidityPairInstance( + content.address, + library + ); + const reserves = await getLpTokenReserve(content.address); + const totalStable = reserves + ? BUSD[chainId as number] === reserves.tokenAddress0 || + USDT[chainId as number] === reserves.tokenAddress0 || + USDC[chainId as number] === reserves.tokenAddress0 + ? ethers.utils.formatUnits( + reserves.reserves0.toString(), + reserves.decimals0 + ) + : ethers.utils.formatUnits( + reserves.reserves1.toString(), + reserves.decimals1 + ) + : "1"; + + const rgpPrice = await calculateRigelPrice(); + + const [token0, token1] = await Promise.all([ + getERC20Token(reserves && reserves.tokenAddress0, library), + getERC20Token(reserves && reserves.tokenAddress1, library), ]); - const tokenStaked = await userInfo.amount; - return { - id: content.id, - img: "rgp.svg", - deposit: content.deposit, - earn: "RGP", - type: "LP", - totalLiquidity, - APY, - tokenStaked: [ - `${content.symbol0}-${content.symbol1}`, - ethers.utils.formatEther(tokenStaked.toString()), - ], - RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), - availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), - poolAllowance: ethers.utils.formatEther(allowance.toString()), - address: content.address, - }; + const [symbol0, symbol1] = await Promise.all([ + token0.symbol(), + token1.symbol(), + ]); + + const totalRGP = reserves + ? RGPADDRESSES[chainId as number] === reserves.tokenAddress0 + ? reserves.reserves0.toString() + : reserves.reserves1.toString() + : "1"; + + const totalLiquidity = + symbol0 === "BUSD" || + symbol1 === "BUSD" || + symbol0 === "USDT" || + symbol1 === "USDT" || + symbol0 === "USDC" || + symbol1 === "USDC" + ? parseFloat(totalStable) * 2 + : parseFloat(ethers.utils.formatEther(totalRGP)) * rgpPrice * 2; + + const [poolInfo, totalAllocPoint] = await Promise.all([ + masterchef.poolInfo(content.id), + masterchef.totalAllocPoint(), + ]); + const allocPoint = await poolInfo.allocPoint; + const poolReward = + (parseFloat(allocPoint.toString()) / + parseFloat(totalAllocPoint.toString())) * + reward; + const APY = (rgpPrice * poolReward * 365 * 100) / totalLiquidity; + const [tokenEarned, userInfo, FarmTokenBalance, allowance] = + await Promise.all([ + masterchef.pendingRigel(content.id, account), + masterchef.userInfo(content.id, account), + LPInstance.balanceOf(account), + LPInstance.allowance( + account, + MASTERCHEFV2ADDRESSES[chainId as number] + ), + ]); + const tokenStaked = await userInfo.amount; + + return { + id: content.id, + img: "rgp.svg", + deposit: content.deposit, + earn: "RGP", + type: "LP", + totalLiquidity, + APY, + tokenStaked: [ + `${content.symbol0}-${content.symbol1}`, + ethers.utils.formatEther(tokenStaked.toString()), + ], + RGPEarned: ethers.utils.formatEther(tokenEarned.toString()), + availableToken: ethers.utils.formatEther(FarmTokenBalance.toString()), + poolAllowance: ethers.utils.formatEther(allowance.toString()), + address: content.address, + farm: content?.farm, + }; + } } catch (err) { console.log(err); } @@ -600,18 +727,7 @@ export const useFetchYieldFarmDetails = ({ useMemo(async () => { if (data !== undefined && account) { setLoading(true); - let newArray = - section === "search" - ? searchSection.newSearchResult !== undefined - ? [...searchSection.newSearchResult] - : [...searchSection.searchResult] - : section === "filter" - ? searchSection.newFilterResult !== undefined - ? [...searchSection.newFilterResult] - : [...searchSection.filterResult] - : searchSection.content !== undefined - ? [...searchSection.content] - : [...data.contents]; + let newArray = [...data]; const updatedFarm = await calculateLiquidityAndApy( chainId === 137 || chainId === 80001 @@ -622,6 +738,7 @@ export const useFetchYieldFarmDetails = ({ ? 1343.220833 : undefined ); + const index = newArray.findIndex((item) => item.id === content.id); newArray[index] = updatedFarm; @@ -632,21 +749,21 @@ export const useFetchYieldFarmDetails = ({ setLoading(false); } - }, []); + }, [reload]); return { loading }; }; interface productFarmState { content: { - feature:string, - percentageProfitShare:string, - profitTimeLine:string, - totalLiquidity:string, - estimatedTotalProfit:string, - pid:number, - deposit:string, - poolAllowance: "", - type:string + feature: string; + percentageProfitShare: string; + profitTimeLine: string; + totalLiquidity: string; + estimatedTotalProfit: string; + pid: number; + deposit: string; + poolAllowance: ""; + type: string; }; loading: boolean; setLoading: React.Dispatch>; @@ -654,53 +771,55 @@ interface productFarmState { export const useProductFarmDetails = ({ content, loading, - setLoading -}:productFarmState) =>{ + setLoading, +}: productFarmState) => { const { account, chainId, library } = useWeb3React(); -const getUserValue = async ()=> { - const productFarm = await productStakingContract( - PRODUCTSTAKINGADDRESSES[chainId as number], - library - ); - const [ userInfo, FarmTokenBalance] = - await Promise.all([ - productFarm.userInfo(account), - productFarm.userData(account), - ]); - const tokenStaked = await FarmTokenBalance.tokenQuantity - console.log({tokenStaked},tokenStaked.toString(),Web3.utils.fromWei(tokenStaked.toString())) - - return { - feature:"AutoTrade", - percentageProfitShare:"25%", - profitTimeLine:"6 months", - totalLiquidity:"188839", - estimatedTotalProfit:"929020003", - deposit: "RGP", - pid:93903, - poolAllowance: "", - type:"AT", - tokenStaked: Web3.utils.fromWei(tokenStaked.toString()) - }; -} - -useMemo(async ()=>{ - let response =await getUserValue() + const getUserValue = async () => { + const productFarm = await productStakingContract( + PRODUCTSTAKINGADDRESSES[chainId as number], + library + ); + const [userInfo, FarmTokenBalance] = await Promise.all([ + productFarm.userInfo(account), + productFarm.userData(account), + ]); + const tokenStaked = await FarmTokenBalance.tokenQuantity; + console.log( + { tokenStaked }, + tokenStaked.toString(), + Web3.utils.fromWei(tokenStaked.toString()) + ); + + return { + feature: "AutoTrade", + percentageProfitShare: "25%", + profitTimeLine: "6 months", + totalLiquidity: "188839", + estimatedTotalProfit: "929020003", + deposit: "RGP", + pid: 93903, + poolAllowance: "", + type: "AT", + tokenStaked: Web3.utils.fromWei(tokenStaked.toString()), + }; + }; - updateProductFarmDetails({value:[response]}) + useMemo(async () => { + let response = await getUserValue(); -setLoading(false) -},[]) -return {loading} + updateProductFarmDetails({ value: [response] }); -} + setLoading(false); + }, []); + return { loading }; +}; export const useUpdateProductFarm = ({ reload, setReload, content, section, }: updateFarmInterface) => { - const data = useFarmData(); + const data = useFarmData(content); const searchSection = useSelector((state) => state.farming); const { account, chainId, library } = useWeb3React(); diff --git a/src/utils/hooks/useFarm.ts b/src/utils/hooks/useFarm.ts index 66d8bdb7..01860ec7 100644 --- a/src/utils/hooks/useFarm.ts +++ b/src/utils/hooks/useFarm.ts @@ -12,6 +12,7 @@ import { MASTERCHEFNEWLPADDRESSES, SMARTSWAPLP_TOKEN3ADDRESSES, SYMBOLS, + RGPSPECIALPOOLADDRESSES2, } from "../addresses"; import LiquidityPairAbi from "../../utils/abis/smartSwapLPToken.json"; import ERC20TokenAbi from "../../utils/abis/erc20.json"; @@ -19,14 +20,17 @@ import smartFactoryAbi from "../../utils/abis/SmartSwapFactoryForSwap.json"; import { useSelector } from "react-redux"; import { RootState } from "../../state"; import { ethers } from "ethers"; -import { smartSwapLPTokenPoolThree } from "../Contracts"; +import { RGPSpecialPool2, smartSwapLPTokenPoolThree } from "../Contracts"; import smartswapLpTokenThreeAbi from "../abis/SmartSwapLPTokenThree.json"; import { updateLpFarm, updateQuickSwapFarm, + updateSpecialPool, updateStableFarm, } from "../../state/farm/actions"; import { useDispatch } from "react-redux"; +import { SupportedChainId } from "../../constants/chains"; +import { getERC20Token, useProvider } from "../utilsFunctions"; type FarmData = Array<{ id: number | undefined; @@ -42,7 +46,10 @@ type FarmData = Array<{ }>; export const useFetchFarm = () => { - const { account, library, chainId } = useWeb3React(); + const { account, library: lib, chainId } = useWeb3React(); + const { prov } = useProvider(); + const library = lib ? lib : prov; + const [loading, setLoading] = useState(true); const dispatch = useDispatch(); @@ -143,11 +150,7 @@ export const useFetchFarm = () => { APY, allocPoint: parseFloat(allocPoint.toString()), address: pools[i].poolAddress, - tokenStaked: [ - `${formatSymbol(reserves[i]?.symbol0)}-${formatSymbol( - reserves[i]?.symbol1 - )}`, - ], + farm: "lp", }); } } @@ -362,10 +365,6 @@ export const useFetchFarm = () => { const token0 = await multi.multiCall(token0Inputs); const token1 = await multi.multiCall(token1Inputs); - console.log(reserves[1]); - - console.log("token0", token0); - for (let i = 0; i < token0[1].length; i++) { symbol0Inputs.push({ interface: ERC20TokenAbi, @@ -430,8 +429,6 @@ export const useFetchFarm = () => { }); } - console.log("Lpreserves", LpReserves); - return LpReserves; } catch (err) { console.log(err); @@ -440,71 +437,74 @@ export const useFetchFarm = () => { useEffect(() => { const fetchPools = async () => { - if (account) { - try { - console.log("started"); - const multi = new MultiCall(library); + try { + setLoading(true); - const poolAddresses = []; + const multi = new MultiCall(library); - const rgpPrice = await calculateRigelPrice(); + const poolAddresses = []; + + const rgpPrice = await calculateRigelPrice(); + + const poolLength = await multi.multiCall([ + { + interface: MasterChefAbi, + args: [], + target: MASTERCHEFV2ADDRESSES[chainId as number], + function: "poolLength", + }, + ]); - const poolLength = await multi.multiCall([ + for (let i = 0; i < parseFloat(poolLength[1][0].toString()); i++) { + const poolInfo = await multi.multiCall([ { interface: MasterChefAbi, - args: [], + args: [i], target: MASTERCHEFV2ADDRESSES[chainId as number], - function: "poolLength", + function: "poolInfo", }, ]); - console.log("poolLength", poolLength); - - for (let i = 0; i < parseFloat(poolLength[1][0].toString()); i++) { - const poolInfo = await multi.multiCall([ - { - interface: MasterChefAbi, - args: [i], - target: MASTERCHEFV2ADDRESSES[chainId as number], - function: "poolInfo", - }, - ]); - - poolAddresses.push({ - poolAddress: poolInfo[1][0]?.lpToken, - allocPoint: poolInfo[1][0]?.allocPoint, - }); - } - - const farm = await calculateLiquidityandApy( - poolAddresses, - rgpPrice, - ChainId === 137 || ChainId === 80001 - ? 2014.83125 - : ChainId === 56 || ChainId === 97 - ? 4300 - : ChainId === 42262 || ChainId === 42261 - ? 1343.220833 - : undefined - ); + poolAddresses.push({ + poolAddress: poolInfo[1][0]?.lpToken, + allocPoint: poolInfo[1][0]?.allocPoint, + }); + } - if (farm) { - dispatch(updateLpFarm(farm)); - } + const farm = await calculateLiquidityandApy( + poolAddresses, + rgpPrice, + ChainId === 137 || ChainId === 80001 + ? 2014.83125 + : ChainId === 56 || ChainId === 97 + ? 4300 + : ChainId === 42262 || ChainId === 42261 + ? 1343.220833 + : undefined + ); - console.log("useFarm", farm); - } catch (err) { - console.log(err); + if (farm) { + dispatch(updateLpFarm(farm)); } + + setLoading(false); + } catch (err) { + setLoading(false); + console.log(err); } }; fetchPools(); - }, [account, ChainId]); + }, [account, ChainId, library]); + + return { loading }; }; export const useFetchQuickSwap = () => { - const { account, library, chainId } = useWeb3React(); + const { account, library: lib, chainId } = useWeb3React(); + const { prov } = useProvider(); + const library = lib ? lib : prov; + const [loading, setLoading] = useState(true); const dispatch = useDispatch(); @@ -625,6 +625,7 @@ export const useFetchQuickSwap = () => { allocPoint: parseFloat(allocPoint.toString()), address: pools[i].poolAddress, LPLocked: newLiquidity.toFixed(2), + farm: "quick", }); } } @@ -849,9 +850,6 @@ export const useFetchQuickSwap = () => { const lockedSupplyInputs = []; for (let i = 0; i < pools.length; i++) { - console.log("before-anything", [ - MASTERCHEFNEWLPADDRESSES[ChainId as number][1], - ]); reservesInputs.push({ interface: LiquidityPairAbi, args: [], @@ -895,21 +893,14 @@ export const useFetchQuickSwap = () => { }); } - console.log("got here", pools); - const reserves = await multi.multiCall(reservesInputs); - console.log("reserves", reserves); const poolInfo = await multi.multiCall(poolInfoInputs); const token0 = await multi.multiCall(token0Inputs); const token1 = await multi.multiCall(token1Inputs); const totalSupply = await multi.multiCall(totalSupplyInputs); const lockedSupply = await multi.multiCall(lockedSupplyInputs); - console.log(reserves[1]); - - console.log("token0", token0); - for (let i = 0; i < token0[1].length; i++) { symbol0Inputs.push({ interface: ERC20TokenAbi, @@ -981,8 +972,6 @@ export const useFetchQuickSwap = () => { }); } - console.log("Lpreserves", LpReserves); - return LpReserves; } catch (err) { console.log(err); @@ -991,80 +980,80 @@ export const useFetchQuickSwap = () => { useEffect(() => { const fetchPools = async () => { - if (account) { - try { - console.log("started"); - const multi = new MultiCall(library); + try { + setLoading(true); - const poolAddresses = []; + const multi = new MultiCall(library); - const rgpPrice = await calculateRigelPrice(); + const poolAddresses = []; + + const rgpPrice = await calculateRigelPrice(); - const BNBPrice = await getBNBPrice(); + const BNBPrice = await getBNBPrice(); - const poolLength = await multi.multiCall([ + const poolLength = await multi.multiCall([ + { + interface: MasterChefAbi, + args: [], + target: MASTERCHEFNEWLPADDRESSES[chainId as number][1], + function: "poolLength", + }, + ]); + + for (let i = 0; i < parseFloat(poolLength[1][0].toString()); i++) { + const poolInfo = await multi.multiCall([ { interface: MasterChefAbi, - args: [], + args: [i], target: MASTERCHEFNEWLPADDRESSES[chainId as number][1], - function: "poolLength", + function: "poolInfo", }, ]); - console.log("poolLength", poolLength); - - for (let i = 0; i < parseFloat(poolLength[1][0].toString()); i++) { - const poolInfo = await multi.multiCall([ - { - interface: MasterChefAbi, - args: [i], - target: MASTERCHEFNEWLPADDRESSES[chainId as number][1], - function: "poolInfo", - }, - ]); - - console.log("before fail", poolInfo[1][0]); - - poolInfo[1][0]?.lpToken !== undefined && - poolAddresses.push({ - pid: i, - poolAddress: poolInfo[1][0]?.lpToken, - allocPoint: poolInfo[1][0]?.allocPoint, - }); - } - - console.log("before fail", poolAddresses); - - const farm = await calculateLiquidityandApy( - poolAddresses, - rgpPrice, - BNBPrice, - ChainId === 137 || ChainId === 80001 - ? 2014.83125 - : ChainId === 56 || ChainId === 97 - ? 4300 - : ChainId === 42262 || ChainId === 42261 - ? 1343.220833 - : undefined - ); + poolInfo[1][0]?.lpToken !== undefined && + poolAddresses.push({ + pid: i, + poolAddress: poolInfo[1][0]?.lpToken, + allocPoint: poolInfo[1][0]?.allocPoint, + }); + } - if (farm) { - dispatch(updateQuickSwapFarm(farm)); - } + const farm = await calculateLiquidityandApy( + poolAddresses, + rgpPrice, + BNBPrice, + ChainId === 137 || ChainId === 80001 + ? 2014.83125 + : ChainId === 56 || ChainId === 97 + ? 4300 + : ChainId === 42262 || ChainId === 42261 + ? 1343.220833 + : undefined + ); - console.log("useFarm-quick", farm); - } catch (err) { - console.log(err); + if (farm) { + dispatch(updateQuickSwapFarm(farm)); } + + setLoading(false); + } catch (err) { + console.log(err); + setLoading(false); } }; fetchPools(); - }, [account, ChainId]); + }, [account, ChainId, library]); + + return { loading }; }; export const useFetchStable = () => { - const { account, library, chainId } = useWeb3React(); + const { account, library: lib, chainId } = useWeb3React(); + const { prov } = useProvider(); + const library = lib ? lib : prov; + + const [loading, setLoading] = useState(true); const dispatch = useDispatch(); const ChainId = useSelector((state) => state.chainId.chainId); @@ -1184,11 +1173,7 @@ export const useFetchStable = () => { allocPoint: parseFloat(allocPoint.toString()), address: pools[i].poolAddress, LPLocked: newLiquidity.toFixed(2), - tokenStaked: [ - `${await formatSymbol( - reserves[i]?.symbol0 - )}-${await formatSymbol(reserves[i]?.symbol1)}`, - ], + farm: "stable", }); } } @@ -1456,21 +1441,14 @@ export const useFetchStable = () => { }); } - console.log("got here", pools); - const reserves = await multi.multiCall(reservesInputs); - console.log("reserves", reserves); const poolInfo = await multi.multiCall(poolInfoInputs); const token0 = await multi.multiCall(token0Inputs); const token1 = await multi.multiCall(token1Inputs); const totalSupply = await multi.multiCall(totalSupplyInputs); const lockedSupply = await multi.multiCall(lockedSupplyInputs); - console.log(reserves[1]); - - console.log("token0", token0); - for (let i = 0; i < token0[1].length; i++) { symbol0Inputs.push({ interface: ERC20TokenAbi, @@ -1534,16 +1512,10 @@ export const useFetchStable = () => { symbol1: symbol1[1][i], totalSupply: totalSupply[1][i], lockedSupply: lockedSupply[1][i], - tokenStaked: [ - `${await formatSymbol(symbol0[1][i])}-${await formatSymbol( - symbol1[1][i] - )}`, - ], + farm: "stable", }); } - console.log("Lpreserves", LpReserves); - return LpReserves; } catch (err) { console.log(err); @@ -1552,70 +1524,298 @@ export const useFetchStable = () => { useEffect(() => { const fetchPools = async () => { - if (account) { - try { - console.log("started"); - const multi = new MultiCall(library); + try { + setLoading(true); - const poolAddresses = []; + const multi = new MultiCall(library); - const rgpPrice = await calculateRigelPrice(); + const poolAddresses = []; - const BNBPrice = await getBNBPrice(); + const rgpPrice = await calculateRigelPrice(); - const poolLength = await multi.multiCall([ + const BNBPrice = await getBNBPrice(); + + const poolLength = await multi.multiCall([ + { + interface: MasterChefAbi, + args: [], + target: MASTERCHEFNEWLPADDRESSES[chainId as number][2], + function: "poolLength", + }, + ]); + + for (let i = 0; i < parseFloat(poolLength[1][0].toString()); i++) { + const poolInfo = await multi.multiCall([ { interface: MasterChefAbi, - args: [], + args: [i], target: MASTERCHEFNEWLPADDRESSES[chainId as number][2], - function: "poolLength", + function: "poolInfo", }, ]); - console.log("poolLength", poolLength); + poolInfo[1][0]?.lpToken !== undefined && + poolAddresses.push({ + pid: i, + poolAddress: poolInfo[1][0]?.lpToken, + allocPoint: poolInfo[1][0]?.allocPoint, + }); + } - for (let i = 0; i < parseFloat(poolLength[1][0].toString()); i++) { - const poolInfo = await multi.multiCall([ - { - interface: MasterChefAbi, - args: [i], - target: MASTERCHEFNEWLPADDRESSES[chainId as number][2], - function: "poolInfo", - }, - ]); - - poolInfo[1][0]?.lpToken !== undefined && - poolAddresses.push({ - pid: i, - poolAddress: poolInfo[1][0]?.lpToken, - allocPoint: poolInfo[1][0]?.allocPoint, - }); - } + const farm = await calculateLiquidityandApy( + poolAddresses, + rgpPrice, + BNBPrice, + ChainId === 137 || ChainId === 80001 + ? 2014.83125 + : ChainId === 56 || ChainId === 97 + ? 4300 + : ChainId === 42262 || ChainId === 42261 + ? 1343.220833 + : undefined + ); + + if (farm) { + dispatch(updateStableFarm(farm)); + } + setLoading(false); + } catch (err) { + console.log(err); + setLoading(false); + } + }; + + fetchPools(); + }, [account, ChainId, library]); + + return { loading }; +}; + +export const useSpecialPool = () => { + const { account, library } = useWeb3React(); + const ChainId = useSelector((state) => state.chainId.chainId); + + const { prov } = useProvider(); + const lib = library ? library : prov; + const dispatch = useDispatch(); - const farm = await calculateLiquidityandApy( - poolAddresses, - rgpPrice, - BNBPrice, - ChainId === 137 || ChainId === 80001 - ? 2014.83125 - : ChainId === 56 || ChainId === 97 - ? 4300 - : ChainId === 42262 || ChainId === 42261 - ? 1343.220833 - : undefined + useEffect(() => { + const fetch = async () => { + try { + if ( + ChainId !== SupportedChainId.OASISMAINNET && + ChainId !== SupportedChainId.OASISTEST + ) { + const specialPool = await RGPSpecialPool2( + RGPSPECIALPOOLADDRESSES2[ChainId as number], + lib + ); + const rgp = await getERC20Token(RGPADDRESSES[ChainId as number], lib); + const RGPBallance = await rgp.balanceOf( + RGPSPECIALPOOLADDRESSES2[ChainId as number] ); - if (farm) { - dispatch(updateStableFarm(farm)); - } + const calculateRigelPrice = async () => { + try { + const multi = new MultiCall(library); + let rgpPrice; + + if (ChainId === 56 || ChainId === 97) { + const pairAddress = await multi.multiCall([ + { + interface: smartFactoryAbi, + args: [ + RGPADDRESSES[ChainId as number], + BUSD[ChainId as number], + ], + target: SMARTSWAPFACTORYADDRESSES[ChainId as number], + function: "getPair", + }, + ]); + const token0Address = await multi.multiCall([ + { + interface: LiquidityPairAbi, + args: [], + target: pairAddress[1][0], + function: "token0", + }, + ]); + const token1Address = await multi.multiCall([ + { + interface: LiquidityPairAbi, + args: [], + target: pairAddress[1][0], + function: "token1", + }, + ]); + + const decimal0 = await multi.multiCall([ + { + interface: ERC20TokenAbi, + args: [], + target: token0Address[1][0], + function: "decimals", + }, + ]); + + const decimal1 = await multi.multiCall([ + { + interface: ERC20TokenAbi, + args: [], + target: token1Address[1][0], + function: "decimals", + }, + ]); + + const reserves = await multi.multiCall([ + { + interface: LiquidityPairAbi, + args: [], + target: pairAddress[1][0], + function: "getReserves", + }, + ]); + const totalBUSD: number | any = ethers.utils.formatUnits( + token0Address[1][0] === BUSD[ChainId as number] + ? reserves[1][0][0] + : reserves[1][0][1], + token0Address[1][0] === BUSD[ChainId as number] + ? decimal0[1][0] + : decimal1[1][0] + ); + + const totalRGP: number | any = ethers.utils.formatUnits( + token0Address[1][0] === BUSD[ChainId as number] + ? reserves[1][0][1] + : reserves[1][0][0], + token0Address[1][0] === BUSD[ChainId as number] + ? decimal1[1][0] + : decimal0[1][0] + ); + + rgpPrice = totalBUSD / totalRGP; + } else { + const pairAddress = await multi.multiCall([ + { + interface: smartFactoryAbi, + args: [ + RGPADDRESSES[ChainId as number], + USDT[ChainId as number], + ], + target: SMARTSWAPFACTORYADDRESSES[ChainId as number], + function: "getPair", + }, + ]); + + const token0Address = await multi.multiCall([ + { + interface: LiquidityPairAbi, + args: [], + target: pairAddress[1][0], + function: "token0", + }, + ]); + const token1Address = await multi.multiCall([ + { + interface: LiquidityPairAbi, + args: [], + target: pairAddress[1][0], + function: "token1", + }, + ]); + const decimal0 = await multi.multiCall([ + { + interface: ERC20TokenAbi, + args: [], + target: token0Address[1][0], + function: "decimals", + }, + ]); + + const decimal1 = await multi.multiCall([ + { + interface: ERC20TokenAbi, + args: [], + target: token1Address[1][0], + function: "decimals", + }, + ]); + const reserves = await multi.multiCall([ + { + interface: LiquidityPairAbi, + args: [], + target: pairAddress[1][0], + function: "getReserves", + }, + ]); + const totalUSDT: number | any = ethers.utils.formatUnits( + token0Address[1][0] === USDT[ChainId as number] + ? reserves[1][0][0] + : reserves[1][0][1], + token0Address[1][0] === USDT[ChainId as number] + ? decimal0[1][0] + : decimal1[1][0] + ); + const totalRGP: number | any = ethers.utils.formatUnits( + token0Address[1][0] === USDT[ChainId as number] + ? reserves[1][0][1] + : reserves[1][0][0], + token0Address[1][0] === USDT[ChainId as number] + ? decimal1[1][0] + : decimal0[1][0] + ); + rgpPrice = totalUSDT / totalRGP; + } + + return rgpPrice; + } catch (err) { + console.log(err); + } + }; + + const rgpPrice = await calculateRigelPrice(); + + const RGPLiquidity = + parseFloat(ethers.utils.formatUnits(RGPBallance)) * + (rgpPrice as number); - console.log("useFarm-quick", farm); - } catch (err) { - console.log(err); + const yeaRate = await specialPool.YEAR_RATE(); + + //this is temporal + + dispatch( + updateSpecialPool([ + { + id: "0", + img: "rgp.svg", + // deposit: 'RGP', + deposit: "RGP", + earn: "RGP", + type: "RGP", + ARYValue: ethers.utils.formatUnits(yeaRate), + totalLiquidity: RGPLiquidity, + tokensStaked: ["RGP", "0"], + RGPEarned: "0", + availableToken: "", + inflationPerDay: 0, + tokenPrice: rgpPrice, + totalVolumePerPool: 0, + farmingFee: 0, + pId: 10793, + poolAllowance: "", + poolVersion: "2", + version: "v2", + farm: "special", + address: RGPSPECIALPOOLADDRESSES2[ChainId as number], + }, + ]) + ); } + } catch (err) { + console.log(err); } }; - fetchPools(); - }, [account, ChainId]); + fetch(); + }, [ChainId, account, lib]); };