Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tab activity detection to optimize API calls #112

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ module.exports = {
warning.details &&
warning.details.includes("source-map-loader")
);
},
}
];

return webpackConfig;
}
}
};
};
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
6 changes: 5 additions & 1 deletion src/routes/reservecoin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,11 @@ export default function ReserveCoin() {
? buyValidity === TRANSACTION_VALIDITY.OK
: sellValidity === TRANSACTION_VALIDITY.OK;

const buttonDisabled = isNaN(parseInt(value)) || parseInt(value) === 0 || isWrongChain || !transactionValidated;
const buttonDisabled =
isNaN(parseInt(value)) ||
parseInt(value) === 0 ||
isWrongChain ||
!transactionValidated;

const rcFloat = parseFloat(coinsDetails?.scaledNumberRc.replaceAll(",", ""));
const rcConverted = getRcUsdEquivalent(coinsDetails, rcFloat);
Expand Down
6 changes: 5 additions & 1 deletion src/routes/stablecoin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ export default function Stablecoin() {
? buyValidity === TRANSACTION_VALIDITY.OK
: sellValidity === TRANSACTION_VALIDITY.OK;

const buttonDisabled = isNaN(parseInt(value)) || parseInt(value) === 0 || isWrongChain || !transactionValidated;
const buttonDisabled =
isNaN(parseInt(value)) ||
parseInt(value) === 0 ||
isWrongChain ||
!transactionValidated;

const scFloat = parseFloat(coinsDetails?.scaledNumberSc.replaceAll(",", ""));
const scConverted = getScAdaEquivalent(coinsDetails, scFloat);
Expand Down
21 changes: 9 additions & 12 deletions src/utils/ethereum.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ export const FEE_UI_UNSCALED = decimalUnscaling(

export const getWeb3 = () =>
new Promise(async (resolve, reject) => {

try {
const web3 = new Web3(BLOCKCHAIN_URI);
resolve(web3);
} catch (error) {
reject(error);
}

try {
const web3 = new Web3(BLOCKCHAIN_URI);
resolve(web3);
} catch (error) {
reject(error);
}
});

export const getDjedContract = (web3) => {
Expand Down Expand Up @@ -232,9 +230,9 @@ export const verifyTx = (web3, hash) => {
return new Promise((res) => {
const checkStatus = () => {
web3.eth.getTransactionReceipt(hash).then((receipt) => {
receipt != null ?
res(receipt.status) :
setTimeout(checkStatus, CONFIRMATION_WAIT_PERIOD);
receipt != null
? res(receipt.status)
: setTimeout(checkStatus, CONFIRMATION_WAIT_PERIOD);
});
};
checkStatus();
Expand Down Expand Up @@ -351,7 +349,6 @@ const tradeDataPriceCore = (djed, method, decimals, amountScaled) => {
}
);
};

// reservecoin

/**
Expand Down
Loading