Skip to content

Commit

Permalink
Merge pull request #119 from valory-xyz/service_staking_tests
Browse files Browse the repository at this point in the history
test: Initial testing for staking
  • Loading branch information
kupermind authored Oct 6, 2023
2 parents db5b1fc + 7977c7e commit efe9ab1
Show file tree
Hide file tree
Showing 3 changed files with 586 additions and 5 deletions.
1 change: 1 addition & 0 deletions contracts/staking/ServiceStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ contract ServiceStaking is ServiceStakingBase {
/// @param amount Amount to withdraw.
function _withdraw(address to, uint256 amount) internal override {
// Update the contract balance
// TODO: Fuzz this such that the amount is never bigger than the balance
balance -= amount;

// Transfer the amount
Expand Down
12 changes: 7 additions & 5 deletions contracts/staking/ServiceStakingBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface IService {
/// @param id Service Id.
function transferFrom(address from, address to, uint256 id) external;

/// @dev Gets the service instance from the map of services.
/// @dev Gets service parameters from the map of services.
/// @param serviceId Service Id.
/// @return securityDeposit Registration activation deposit.
/// @return multisig Service multisig address.
Expand Down Expand Up @@ -123,6 +123,7 @@ abstract contract ServiceStakingBase is IErrorsRegistries {
revert ZeroAddress();
}

maxNumServices = _maxNumServices;
rewardsPerSecond = _rewardsPerSecond;
minStakingDeposit = _minStakingDeposit;
livenessRatio = _livenessRatio;
Expand Down Expand Up @@ -276,20 +277,20 @@ abstract contract ServiceStakingBase is IErrorsRegistries {
// If total allocated rewards are not enough, adjust the reward value
if (totalRewards > lastAvailableRewards) {
// Traverse all the eligible services and adjust their rewards proportional to leftovers
totalRewards = 0;
uint256 updatedTotalRewards = 0;
for (uint256 i = 0; i < numServices; ++i) {
// Calculate the updated reward
uint256 updatedReward = (eligibleServiceRewards[i] * lastAvailableRewards) / totalRewards;
// Add to the total updated reward
totalRewards += updatedReward;
updatedTotalRewards += updatedReward;
// Add reward to the service overall reward
uint256 curServiceId = eligibleServiceIds[i];
mapServiceInfo[curServiceId].reward += updatedReward;
}

// If the reward adjustment happened to have small leftovers, add it to the last traversed service
if (lastAvailableRewards > totalRewards) {
mapServiceInfo[numServices - 1].reward += lastAvailableRewards - totalRewards;
if (lastAvailableRewards > updatedTotalRewards) {
mapServiceInfo[numServices - 1].reward += lastAvailableRewards - updatedTotalRewards;
}
// Set available rewards to zero
lastAvailableRewards = 0;
Expand All @@ -302,6 +303,7 @@ abstract contract ServiceStakingBase is IErrorsRegistries {
}

// Adjust available rewards
// TODO: Fuzz this such that totalRewards is never bigger than lastAvailableRewards
lastAvailableRewards -= totalRewards;
}

Expand Down
Loading

0 comments on commit efe9ab1

Please sign in to comment.