Skip to content

Commit

Permalink
changes in AppProvider.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanya-ruby committed May 5, 2024
1 parent a905c95 commit 783ddb5
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 110 deletions.
21 changes: 17 additions & 4 deletions src/context/AppProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 }) => {
Expand Down
203 changes: 97 additions & 106 deletions src/utils/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
};

Expand Down

0 comments on commit 783ddb5

Please sign in to comment.