From 96727127e6f5d1c73f60448f4e8bed3c09c04566 Mon Sep 17 00:00:00 2001 From: truemiller Date: Mon, 17 Jun 2024 11:36:36 +0100 Subject: [PATCH 1/9] feat: Add tsCheckpoint to getAgentStakingRewardsInfo function --- frontend/service/Autonolas.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 1be76ed44..119452960 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -65,6 +65,7 @@ const getAgentStakingRewardsInfo = async ({ serviceStakingTokenMechUsageContract.rewardsPerSecond(), serviceStakingTokenMechUsageContract.calculateStakingReward(serviceId), serviceStakingTokenMechUsageContract.minStakingDeposit(), + serviceStakingTokenMechUsageContract.tsCheckpoint(), ]; await gnosisMulticallProvider.init(); @@ -79,6 +80,7 @@ const getAgentStakingRewardsInfo = async ({ rewardsPerSecond, accruedServiceStakingRewards, minimumStakingDeposit, + lastTsCheckpoint, ] = multicallResponse; /** @@ -97,17 +99,16 @@ const getAgentStakingRewardsInfo = async ({ uint256 inactivity;} */ - const livenessRatioFormatted = livenessRatio / 1e18; - - const multisigNonce = serviceInfo[2][1]; - - const eligibleRequests = mechRequestCount - multisigNonce; - - const eligibilityMargin = - livenessPeriod * livenessRatioFormatted + + const requiredMechRequests = + (Math.ceil(Math.max(livenessPeriod, Date.now() * 1000 - lastTsCheckpoint)) * + livenessRatio) / + 1e18 + REQUIRED_MECH_REQUESTS_SAFETY_MARGIN; - const isEligibleForRewards = eligibleRequests >= eligibilityMargin; + const mechRequestCountOnLastCheckpoint = serviceInfo[2][1]; + const eligibleRequests = mechRequestCount - mechRequestCountOnLastCheckpoint; + + const isEligibleForRewards = eligibleRequests >= requiredMechRequests; const availableRewardsForEpoch = rewardsPerSecond * livenessPeriod; From 6b206b2983bdb4d5291ec5508e28285120bf1fd9 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Mon, 17 Jun 2024 13:59:24 +0100 Subject: [PATCH 2/9] chore: Add console.log statement for debugging purposes --- frontend/service/Autonolas.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 119452960..0f8c9593f 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -83,6 +83,8 @@ const getAgentStakingRewardsInfo = async ({ lastTsCheckpoint, ] = multicallResponse; + console.log('multicallResponse', multicallResponse); + /** * struct ServiceInfo { // Service multisig address @@ -100,7 +102,12 @@ const getAgentStakingRewardsInfo = async ({ */ const requiredMechRequests = - (Math.ceil(Math.max(livenessPeriod, Date.now() * 1000 - lastTsCheckpoint)) * + (Math.ceil( + Math.max( + livenessPeriod, + Math.round(Date.now() / 1000) - lastTsCheckpoint, + ), + ) * livenessRatio) / 1e18 + REQUIRED_MECH_REQUESTS_SAFETY_MARGIN; From be11cc74a8fd2f8d50ac34ddf99176eb4e5c3395 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:00:02 +0100 Subject: [PATCH 3/9] refactor: Remove console.log statement from Autonolas.ts --- frontend/service/Autonolas.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 0f8c9593f..8a7cb28ad 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -83,8 +83,6 @@ const getAgentStakingRewardsInfo = async ({ lastTsCheckpoint, ] = multicallResponse; - console.log('multicallResponse', multicallResponse); - /** * struct ServiceInfo { // Service multisig address From 4d1a4c439a3afc850d87ccff951d15c09b2f74c1 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:56:53 +0100 Subject: [PATCH 4/9] chore: Refactor Autonolas.ts to improve code readability and remove console.log statements --- frontend/service/Autonolas.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 8a7cb28ad..cd7d79070 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -99,13 +99,10 @@ const getAgentStakingRewardsInfo = async ({ uint256 inactivity;} */ + const nowInSeconds = Math.round(Date.now() / 1000); + const requiredMechRequests = - (Math.ceil( - Math.max( - livenessPeriod, - Math.round(Date.now() / 1000) - lastTsCheckpoint, - ), - ) * + (Math.ceil(Math.max(livenessPeriod, nowInSeconds - lastTsCheckpoint)) * livenessRatio) / 1e18 + REQUIRED_MECH_REQUESTS_SAFETY_MARGIN; @@ -140,16 +137,22 @@ const getAgentStakingRewardsInfo = async ({ const getAvailableRewardsForEpoch = async (): Promise => { const contractCalls = [ serviceStakingTokenMechUsageContract.rewardsPerSecond(), - serviceStakingTokenMechUsageContract.livenessPeriod(), + serviceStakingTokenMechUsageContract.epochLength(), + serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp ]; await gnosisMulticallProvider.init(); const multicallResponse = await gnosisMulticallProvider.all(contractCalls); - const [rewardsPerSecond, livenessPeriod] = multicallResponse; + const [rewardsPerSecond, epochLength, tsCheckpoint] = multicallResponse; + + const nowInSeconds = Math.round(Date.now() / 1000); - return rewardsPerSecond * livenessPeriod; + return Math.max( + rewardsPerSecond * epochLength, + rewardsPerSecond * (nowInSeconds - tsCheckpoint), + ); }; const getServiceRegistryInfo = async ( From 195e3d8b76526b03bd4e0825c269d53c340eafe5 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:57:09 +0100 Subject: [PATCH 5/9] chore: Refactor Autonolas.ts to use Math.floor instead of Math.round for calculating nowInSeconds --- frontend/service/Autonolas.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index cd7d79070..550ad8978 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -99,7 +99,7 @@ const getAgentStakingRewardsInfo = async ({ uint256 inactivity;} */ - const nowInSeconds = Math.round(Date.now() / 1000); + const nowInSeconds = Math.floor(Date.now() / 1000); const requiredMechRequests = (Math.ceil(Math.max(livenessPeriod, nowInSeconds - lastTsCheckpoint)) * @@ -147,7 +147,7 @@ const getAvailableRewardsForEpoch = async (): Promise => { const [rewardsPerSecond, epochLength, tsCheckpoint] = multicallResponse; - const nowInSeconds = Math.round(Date.now() / 1000); + const nowInSeconds = Math.floor(Date.now() / 1000); return Math.max( rewardsPerSecond * epochLength, From 7968fc5d3d0ab508f770477f9b5106b3fa1066bf Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:10:57 +0100 Subject: [PATCH 6/9] chore: Refactor Autonolas.ts to improve code readability and variable naming --- frontend/service/Autonolas.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 550ad8978..6adfe1eee 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -78,9 +78,9 @@ const getAgentStakingRewardsInfo = async ({ livenessPeriod, livenessRatio, rewardsPerSecond, - accruedServiceStakingRewards, - minimumStakingDeposit, - lastTsCheckpoint, + accuredStakingReward, + minStakingDeposit, + tsCheckpoint, ] = multicallResponse; /** @@ -102,7 +102,7 @@ const getAgentStakingRewardsInfo = async ({ const nowInSeconds = Math.floor(Date.now() / 1000); const requiredMechRequests = - (Math.ceil(Math.max(livenessPeriod, nowInSeconds - lastTsCheckpoint)) * + (Math.ceil(Math.max(livenessPeriod, nowInSeconds - tsCheckpoint)) * livenessRatio) / 1e18 + REQUIRED_MECH_REQUESTS_SAFETY_MARGIN; @@ -112,12 +112,15 @@ const getAgentStakingRewardsInfo = async ({ const isEligibleForRewards = eligibleRequests >= requiredMechRequests; - const availableRewardsForEpoch = rewardsPerSecond * livenessPeriod; + const availableRewardsForEpoch = Math.max( + rewardsPerSecond * livenessPeriod, + rewardsPerSecond * (nowInSeconds - tsCheckpoint), + ); // Minimum staked amount is double the minimum staking deposit - // (basically all the bonds must be the same as deposit) + // (all the bonds must be the same as deposit) const minimumStakedAmount = - parseFloat(ethers.utils.formatEther(`${minimumStakingDeposit}`)) * 2; + parseFloat(ethers.utils.formatEther(`${minStakingDeposit}`)) * 2; return { mechRequestCount, @@ -128,7 +131,7 @@ const getAgentStakingRewardsInfo = async ({ isEligibleForRewards, availableRewardsForEpoch, accruedServiceStakingRewards: parseFloat( - ethers.utils.formatEther(`${accruedServiceStakingRewards}`), + ethers.utils.formatEther(`${accuredStakingReward}`), ), minimumStakedAmount, } as StakingRewardsInfo; @@ -137,20 +140,20 @@ const getAgentStakingRewardsInfo = async ({ const getAvailableRewardsForEpoch = async (): Promise => { const contractCalls = [ serviceStakingTokenMechUsageContract.rewardsPerSecond(), - serviceStakingTokenMechUsageContract.epochLength(), - serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp + serviceStakingTokenMechUsageContract.livenessPeriod(), // epoch length? + serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp? ]; await gnosisMulticallProvider.init(); const multicallResponse = await gnosisMulticallProvider.all(contractCalls); - const [rewardsPerSecond, epochLength, tsCheckpoint] = multicallResponse; + const [rewardsPerSecond, livenessPeriod, tsCheckpoint] = multicallResponse; const nowInSeconds = Math.floor(Date.now() / 1000); return Math.max( - rewardsPerSecond * epochLength, + rewardsPerSecond * livenessPeriod, rewardsPerSecond * (nowInSeconds - tsCheckpoint), ); }; From 63b0745a84143d00842c39953f1d30bbace714a8 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:11:57 +0100 Subject: [PATCH 7/9] Refactor Autonolas.ts to improve code readability and variable naming --- frontend/service/Autonolas.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 6adfe1eee..55273d699 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -113,8 +113,8 @@ const getAgentStakingRewardsInfo = async ({ const isEligibleForRewards = eligibleRequests >= requiredMechRequests; const availableRewardsForEpoch = Math.max( - rewardsPerSecond * livenessPeriod, - rewardsPerSecond * (nowInSeconds - tsCheckpoint), + rewardsPerSecond * livenessPeriod, // expected rewards for the epoch + rewardsPerSecond * (nowInSeconds - tsCheckpoint), // incase of late checkpoint ); // Minimum staked amount is double the minimum staking deposit @@ -153,8 +153,8 @@ const getAvailableRewardsForEpoch = async (): Promise => { const nowInSeconds = Math.floor(Date.now() / 1000); return Math.max( - rewardsPerSecond * livenessPeriod, - rewardsPerSecond * (nowInSeconds - tsCheckpoint), + rewardsPerSecond * livenessPeriod, // expected rewards + rewardsPerSecond * (nowInSeconds - tsCheckpoint), // incase of late checkpoint ); }; From e8a616f4e2d00086cc5881a0a3f8f210de3d49d8 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Fri, 21 Jun 2024 13:57:31 +0100 Subject: [PATCH 8/9] Update frontend/service/Autonolas.ts --- frontend/service/Autonolas.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 55273d699..84fa05242 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -141,7 +141,7 @@ const getAvailableRewardsForEpoch = async (): Promise => { const contractCalls = [ serviceStakingTokenMechUsageContract.rewardsPerSecond(), serviceStakingTokenMechUsageContract.livenessPeriod(), // epoch length? - serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp? + serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp ]; await gnosisMulticallProvider.init(); From 46753db9731010593826e094bf1eac2fcd45cd52 Mon Sep 17 00:00:00 2001 From: truemiller <31908788+truemiller@users.noreply.github.com> Date: Fri, 21 Jun 2024 13:57:38 +0100 Subject: [PATCH 9/9] Update frontend/service/Autonolas.ts --- frontend/service/Autonolas.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/service/Autonolas.ts b/frontend/service/Autonolas.ts index 84fa05242..4959ee7ef 100644 --- a/frontend/service/Autonolas.ts +++ b/frontend/service/Autonolas.ts @@ -140,7 +140,7 @@ const getAgentStakingRewardsInfo = async ({ const getAvailableRewardsForEpoch = async (): Promise => { const contractCalls = [ serviceStakingTokenMechUsageContract.rewardsPerSecond(), - serviceStakingTokenMechUsageContract.livenessPeriod(), // epoch length? + serviceStakingTokenMechUsageContract.livenessPeriod(), // epoch length serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp ];