diff --git a/src/context/AppProvider.jsx b/src/context/AppProvider.jsx index e89edca1..10cfe0c1 100644 --- a/src/context/AppProvider.jsx +++ b/src/context/AppProvider.jsx @@ -55,6 +55,19 @@ export const AppProvider = ({ children }) => { const [systemParams, setSystemParams] = useState(null); const [accountDetails, setAccountDetails] = useState(null); const [coinBudgets, setCoinBudgets] = useState(null); + const [isVisible, setIsVisible] = useState(document.visibilityState === "visible"); + + useEffect(() => { + const handleVisibilityChange = () => { + setIsVisible(document.visibilityState === "visible"); + }; + + document.addEventListener("visibilitychange", handleVisibilityChange); + + return () => { + document.removeEventListener("visibilitychange", handleVisibilityChange); + }; + }, []); useEffect(() => { if (!account) return; const setUp = async () => { @@ -188,7 +201,7 @@ export const AppProvider = ({ children }) => { useInterval( async () => { - if (coinContracts == null) return; + if (coinContracts == null || !isVisible) return; const accountDetails = await getAccountDetails( web3, account, @@ -206,12 +219,12 @@ export const AppProvider = ({ children }) => { ); setCoinBudgets(coinBudgets); }, - isWalletConnected ? ACCOUNT_DETAILS_REQUEST_INTERVAL : null + isWalletConnected && isVisible ? ACCOUNT_DETAILS_REQUEST_INTERVAL : null ); useInterval( async () => { - if (coinContracts == null) return; + if (coinContracts == null || !isVisible) return; const coinsDetails = await getCoinDetails( coinContracts.stableCoin, coinContracts.reserveCoin, @@ -222,7 +235,7 @@ export const AppProvider = ({ children }) => { ); setCoinsDetails(coinsDetails); }, - isWalletConnected ? COIN_DETAILS_REQUEST_INTERVAL : null + isWalletConnected && isVisible ? COIN_DETAILS_REQUEST_INTERVAL : null ); const isRatioBelowMax = ({ scPrice, reserveBc }) => { diff --git a/src/utils/ethereum.js b/src/utils/ethereum.js index e2d8fc13..fb6426ba 100644 --- a/src/utils/ethereum.js +++ b/src/utils/ethereum.js @@ -34,21 +34,6 @@ export const FEE_UI_UNSCALED = decimalUnscaling( SCALING_DECIMALS ); -let isWindowActive = true; // Flag to track window visibility - -window.addEventListener("visibilitychange", () => { - isWindowActive = document.visibilityState === "visible"; -}); - -const withWindowVisibilityCheck = (action) => { - return (...args) => { - if (!isWindowActive) { - return; - } - return action(...args); - }; -}; - export const getWeb3 = () => new Promise(async (resolve, reject) => { try { @@ -93,75 +78,79 @@ export const getDecimals = async (stableCoin, reserveCoin) => { return { scDecimals, rcDecimals }; }; -export const getCoinDetails = withWindowVisibilityCheck( - async (stableCoin, reserveCoin, djed, scDecimals, rcDecimals) => { - let promiseArray; - - if (NETWORK_ID === "200101" || NETWORK_ID === "2001") { - promiseArray = [ - scaledUnscaledPromise(web3Promise(stableCoin, "totalSupply"), scDecimals), - scaledUnscaledPromise(web3Promise(djed, "scPrice", 0), BC_DECIMALS), - scaledUnscaledPromise(web3Promise(reserveCoin, "totalSupply"), rcDecimals), - scaledUnscaledPromise(web3Promise(djed, "R", 0), BC_DECIMALS), - scaledPromise(web3Promise(djed, "rcBuyingPrice", 0), BC_DECIMALS), - scaledPromise(web3Promise(djed, "scPrice", 0), BC_DECIMALS) - ]; - } else if (NETWORK_ID === "11155111") { - promiseArray = [ - scaledUnscaledPromise(web3Promise(stableCoin, "totalSupply"), BC_DECIMALS), - scaledUnscaledPromise(web3Promise(djed, "scPrice", 0), scDecimals), - scaledUnscaledPromise(web3Promise(reserveCoin, "totalSupply"), rcDecimals), - scaledUnscaledPromise(web3Promise(djed, "R", 0), BC_DECIMALS), - scaledPromise(web3Promise(djed, "rcBuyingPrice", 0), BC_DECIMALS), - scaledPromise(web3Promise(djed, "scPrice", 0), scDecimals) - ]; - } +export const getCoinDetails = async ( + stableCoin, + reserveCoin, + djed, + scDecimals, + rcDecimals +) => { + let promiseArray; + + if (NETWORK_ID === "200101" || NETWORK_ID === "2001") { + promiseArray = [ + scaledUnscaledPromise(web3Promise(stableCoin, "totalSupply"), scDecimals), + scaledUnscaledPromise(web3Promise(djed, "scPrice", 0), BC_DECIMALS), + scaledUnscaledPromise(web3Promise(reserveCoin, "totalSupply"), rcDecimals), + scaledUnscaledPromise(web3Promise(djed, "R", 0), BC_DECIMALS), + scaledPromise(web3Promise(djed, "rcBuyingPrice", 0), BC_DECIMALS), + scaledPromise(web3Promise(djed, "scPrice", 0), BC_DECIMALS) + ]; + } else if (NETWORK_ID === "11155111") { + promiseArray = [ + scaledUnscaledPromise(web3Promise(stableCoin, "totalSupply"), BC_DECIMALS), + scaledUnscaledPromise(web3Promise(djed, "scPrice", 0), scDecimals), + scaledUnscaledPromise(web3Promise(reserveCoin, "totalSupply"), rcDecimals), + scaledUnscaledPromise(web3Promise(djed, "R", 0), BC_DECIMALS), + scaledPromise(web3Promise(djed, "rcBuyingPrice", 0), BC_DECIMALS), + scaledPromise(web3Promise(djed, "scPrice", 0), scDecimals) + ]; + } - const [ - [scaledNumberSc, unscaledNumberSc], - [scaledPriceSc, unscaledPriceSc], - [scaledNumberRc, unscaledNumberRc], - [scaledReserveBc, unscaledReserveBc], - scaledBuyPriceRc, - scaledScExchangeRate - ] = await Promise.all(promiseArray); - - const emptyValue = decimalScaling("0".toString(10), BC_DECIMALS); - let scaledSellPriceRc = emptyValue; - let unscaledSellPriceRc = emptyValue; - let percentReserveRatio = emptyValue; - - //Check total stablecoin supply - if (!BigNumber.from(unscaledNumberRc).isZero()) { - [scaledSellPriceRc, unscaledSellPriceRc] = await scaledUnscaledPromise( - web3Promise(djed, "rcTargetPrice", 0), - BC_DECIMALS - ); - } - //Check total reservecoin supply - if (!BigNumber.from(unscaledNumberSc).isZero()) { - percentReserveRatio = await percentScaledPromise( - web3Promise(djed, "ratio"), - SCALING_DECIMALS - ); - } - return { - scaledNumberSc, - unscaledNumberSc, - scaledPriceSc, - unscaledPriceSc, - scaledNumberRc, - unscaledNumberRc, - scaledReserveBc, - unscaledReserveBc, - percentReserveRatio, - scaledBuyPriceRc, - scaledSellPriceRc, - unscaledSellPriceRc, - scaledScExchangeRate - }; + const [ + [scaledNumberSc, unscaledNumberSc], + [scaledPriceSc, unscaledPriceSc], + [scaledNumberRc, unscaledNumberRc], + [scaledReserveBc, unscaledReserveBc], + scaledBuyPriceRc, + scaledScExchangeRate + ] = await Promise.all(promiseArray); + + const emptyValue = decimalScaling("0".toString(10), BC_DECIMALS); + let scaledSellPriceRc = emptyValue; + let unscaledSellPriceRc = emptyValue; + let percentReserveRatio = emptyValue; + + //Check total stablecoin supply + if (!BigNumber.from(unscaledNumberRc).isZero()) { + [scaledSellPriceRc, unscaledSellPriceRc] = await scaledUnscaledPromise( + web3Promise(djed, "rcTargetPrice", 0), + BC_DECIMALS + ); } -); + //Check total reservecoin supply + if (!BigNumber.from(unscaledNumberSc).isZero()) { + percentReserveRatio = await percentScaledPromise( + web3Promise(djed, "ratio"), + SCALING_DECIMALS + ); + } + return { + scaledNumberSc, + unscaledNumberSc, + scaledPriceSc, + unscaledPriceSc, + scaledNumberRc, + unscaledNumberRc, + scaledReserveBc, + unscaledReserveBc, + percentReserveRatio, + scaledBuyPriceRc, + scaledSellPriceRc, + unscaledSellPriceRc, + scaledScExchangeRate + }; +}; export const getSystemParams = async (djed) => { const [ @@ -190,28 +179,33 @@ export const getSystemParams = async (djed) => { }; }; -export const getAccountDetails = withWindowVisibilityCheck( - async (web3, account, stableCoin, reserveCoin, scDecimals, rcDecimals) => { - const [ - [scaledBalanceSc, unscaledBalanceSc], - [scaledBalanceRc, unscaledBalanceRc], - [scaledBalanceBc, unscaledBalanceBc] - ] = await Promise.all([ - scaledUnscaledPromise(web3Promise(stableCoin, "balanceOf", account), scDecimals), - scaledUnscaledPromise(web3Promise(reserveCoin, "balanceOf", account), rcDecimals), - scaledUnscaledPromise(web3.eth.getBalance(account), BC_DECIMALS) - ]); +export const getAccountDetails = async ( + web3, + account, + stableCoin, + reserveCoin, + scDecimals, + rcDecimals +) => { + const [ + [scaledBalanceSc, unscaledBalanceSc], + [scaledBalanceRc, unscaledBalanceRc], + [scaledBalanceBc, unscaledBalanceBc] + ] = await Promise.all([ + scaledUnscaledPromise(web3Promise(stableCoin, "balanceOf", account), scDecimals), + scaledUnscaledPromise(web3Promise(reserveCoin, "balanceOf", account), rcDecimals), + scaledUnscaledPromise(web3.eth.getBalance(account), BC_DECIMALS) + ]); - return { - scaledBalanceSc, - unscaledBalanceSc, - scaledBalanceRc, - unscaledBalanceRc, - scaledBalanceBc, - unscaledBalanceBc - }; - } -); + return { + scaledBalanceSc, + unscaledBalanceSc, + scaledBalanceRc, + unscaledBalanceRc, + scaledBalanceBc, + unscaledBalanceBc + }; +}; export const getCoinBudgets = async (djed, unscaledBalanceBc, scDecimals, rcDecimals) => { return { @@ -436,9 +430,6 @@ export const checkBuyableRc = (djed, unscaledAmountRc, unscaledBudgetRc) => { }; export const checkSellableRc = (djed, unscaledAmountRc, unscaledBalanceRc) => { - if (!isWindowActive) { - throw new Error("Window is not active. Please activate the tab."); - } return new Promise((r) => r(TRANSACTION_VALIDITY.OK)); };