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

Fix mech requests criteria for rewards eligibility #179

Merged
merged 11 commits into from
Jun 21, 2024
Merged
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
46 changes: 29 additions & 17 deletions frontend/service/Autonolas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const getAgentStakingRewardsInfo = async ({
serviceStakingTokenMechUsageContract.rewardsPerSecond(),
serviceStakingTokenMechUsageContract.calculateStakingReward(serviceId),
serviceStakingTokenMechUsageContract.minStakingDeposit(),
serviceStakingTokenMechUsageContract.tsCheckpoint(),
];

await gnosisMulticallProvider.init();
Expand All @@ -77,8 +78,9 @@ const getAgentStakingRewardsInfo = async ({
livenessPeriod,
livenessRatio,
rewardsPerSecond,
accruedServiceStakingRewards,
minimumStakingDeposit,
accuredStakingReward,
minStakingDeposit,
tsCheckpoint,
] = multicallResponse;

/**
Expand All @@ -97,24 +99,28 @@ const getAgentStakingRewardsInfo = async ({
uint256 inactivity;}
*/

const livenessRatioFormatted = livenessRatio / 1e18;
const nowInSeconds = Math.floor(Date.now() / 1000);

const multisigNonce = serviceInfo[2][1];

const eligibleRequests = mechRequestCount - multisigNonce;

const eligibilityMargin =
livenessPeriod * livenessRatioFormatted +
const requiredMechRequests =
(Math.ceil(Math.max(livenessPeriod, nowInSeconds - tsCheckpoint)) *
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;
const availableRewardsForEpoch = Math.max(
rewardsPerSecond * livenessPeriod, // expected rewards for the epoch
rewardsPerSecond * (nowInSeconds - tsCheckpoint), // incase of late checkpoint
);

// 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,
Expand All @@ -125,7 +131,7 @@ const getAgentStakingRewardsInfo = async ({
isEligibleForRewards,
availableRewardsForEpoch,
accruedServiceStakingRewards: parseFloat(
ethers.utils.formatEther(`${accruedServiceStakingRewards}`),
ethers.utils.formatEther(`${accuredStakingReward}`),
),
minimumStakedAmount,
} as StakingRewardsInfo;
Expand All @@ -134,16 +140,22 @@ const getAgentStakingRewardsInfo = async ({
const getAvailableRewardsForEpoch = async (): Promise<number | undefined> => {
const contractCalls = [
serviceStakingTokenMechUsageContract.rewardsPerSecond(),
serviceStakingTokenMechUsageContract.livenessPeriod(),
serviceStakingTokenMechUsageContract.livenessPeriod(), // epoch length
serviceStakingTokenMechUsageContract.tsCheckpoint(), // last checkpoint timestamp
];

await gnosisMulticallProvider.init();

const multicallResponse = await gnosisMulticallProvider.all(contractCalls);

const [rewardsPerSecond, livenessPeriod] = multicallResponse;
const [rewardsPerSecond, livenessPeriod, tsCheckpoint] = multicallResponse;

const nowInSeconds = Math.floor(Date.now() / 1000);

return rewardsPerSecond * livenessPeriod;
return Math.max(
rewardsPerSecond * livenessPeriod, // expected rewards
rewardsPerSecond * (nowInSeconds - tsCheckpoint), // incase of late checkpoint
);
};

const getServiceRegistryInfo = async (
Expand Down
Loading