From 64f42ce21334365e3d291686f8205466e8b81d42 Mon Sep 17 00:00:00 2001 From: Abishek Bashyal Date: Tue, 22 Aug 2023 23:17:22 +0545 Subject: [PATCH] time fix --- src/sections/yield/YieldStatsSection.tsx | 31 ++++++++++++++++++----- src/singletons/YieldFarmingDataStoreV2.js | 21 +++++++++++---- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/sections/yield/YieldStatsSection.tsx b/src/sections/yield/YieldStatsSection.tsx index 7b94dc932d..878e40c918 100644 --- a/src/sections/yield/YieldStatsSection.tsx +++ b/src/sections/yield/YieldStatsSection.tsx @@ -21,22 +21,38 @@ const YieldStatsSection = ({ const [formattedDuration, setFormattedDuration] = React.useState(''); + const [epochEndTime,setEpochEndTime] = React.useState(); + + const calculateEpochEndDuration = () =>{ + const epochEndSeconds = poolStats.epochEndTime; + const epochEndTimeStamp = new Date().getTime() + epochEndSeconds * 1000; + setEpochEndTime(epochEndTimeStamp); + + } + + React.useEffect(()=>{ + if(poolStats){ + calculateEpochEndDuration() + } + + },[poolStats]) + const getFormattedDuration = () => { if (poolStats?.epochEndTimestamp) { const epochEndTimestamp = poolStats.epochEndTimestamp.toNumber(); - const duration = epochEndTimestamp - Math.floor(new Date() / 1000); + const now = new Date().getTime(); + const timeRemaining = epochEndTime - now; - if (duration < 0) { + if (timeRemaining < 0) { setPoolStats(null); getLpPoolStats(); } - const day = parseInt(duration / 86400); - const hour = parseInt((duration - day * 86400) / 3600); - - const minutes = parseInt((duration - (day * 86400 + hour * 3600)) / 60); - const seconds = duration - (day * 86400 + hour * 3600 + minutes * 60); + const day = Math.floor(timeRemaining / (1000 * 60 * 60 * 24)); + const hour = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000); setFormattedDuration(`${day}D ${hour}H ${minutes}M ${seconds}S`); } @@ -44,6 +60,7 @@ const YieldStatsSection = ({ React.useEffect(() => { setTimeout(() => { + console.log("Running") getFormattedDuration(); }, 1000); }); diff --git a/src/singletons/YieldFarmingDataStoreV2.js b/src/singletons/YieldFarmingDataStoreV2.js index 98fe01742c..743f90b9e0 100644 --- a/src/singletons/YieldFarmingDataStoreV2.js +++ b/src/singletons/YieldFarmingDataStoreV2.js @@ -62,7 +62,7 @@ export default class YieldFarmingDataStoreV2 { // init init = (account, staking, pushToken, pushCoreV2, yieldFarmingLP, uniswapV2Router02) => { // set account - this.state.account = account; + this.state.account = account; this.state.staking = staking; this.state.pushToken = pushToken; this.state.pushCoreV2 = pushCoreV2; @@ -106,11 +106,21 @@ export default class YieldFarmingDataStoreV2 { const pushStakedAmount = tokenBNtoNumber(await pushCoreV2.totalStakedAmount()); const totalValueLocked = pushStakedAmount * pushPrice + lpNextPoolSize * uniLpPrice; + //calculating epoch Duration - const epochDuration = await yieldFarmingLP.epochDuration(); + const epochDurations = await yieldFarmingLP.epochDuration(); const epochStart = await yieldFarmingLP.epochStart(); - const start = epochStart.add(currentEpochLP.sub(1).mul(epochDuration)); - const epochEndTimestamp = start.add(epochDuration); + const start = epochStart.add(currentEpochLP.sub(1).mul(epochDurations)); + const epochEndTimestamp = start.add(epochDurations); + + + let currentBlockNumber = await provider.getBlock('latest'); + currentBlockNumber = currentBlockNumber.number; + const genesisEpoch = await pushCoreV2.genesisEpoch(); + const epochDuration = await pushCoreV2.epochDuration(); + const remainingBlocks = epochDuration.toNumber() - ( (currentBlockNumber - genesisEpoch.toNumber() ) % epochDuration.toNumber() ) ; + let epochEndTime = (remainingBlocks * 12.6)+3600; + epochEndTime = Math.round(epochEndTime); //calculation total distributed amount const pushTotalDistributedAmount = tokenToBn(ethers.BigNumber.from(this.state.annualPushReward)); @@ -124,6 +134,7 @@ export default class YieldFarmingDataStoreV2 { pushPrice, lpToPushRatio, epochEndTimestamp, + epochEndTime, totalValueLocked, totalDistributedAmount, pushRewardsDistributed, @@ -510,4 +521,4 @@ export default class YieldFarmingDataStoreV2 { // }; -} +} \ No newline at end of file