Skip to content

Commit

Permalink
Clean up non-liquid delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas committed Mar 3, 2025
1 parent 8ba88b8 commit 5eed549
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 61 deletions.
11 changes: 0 additions & 11 deletions e2e_non-liquid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -502,34 +502,23 @@ report() {

join_all # all validators join the pool
stake_all # all users stake, withdraw rewards, unstake and claim part of it
echo -n "🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
leave_all # all validators leave and withdraw rewards
echo -n "🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
unstake_all # all users unstake everything and withdraw rewards
echo -n "🟪🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
report # print the status
echo "1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣ 1️⃣"
echo -n "🟪🟪🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"

sleep 5s

stake_all
echo -n "🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
unstake_all
echo -n "🟪🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
report
echo "2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣ 2️⃣"
echo -n "🟪🟪🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"

sleep 5s

join_all
stake_all
echo -n "🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
leave_all
echo -n "🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
unstake_all
echo -n "🟪🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
report
echo "3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣ 3️⃣"
echo -n "🟪🟪🟪🟪" && cast call $CONTRACT_ADDRESS "totalRoundingErrors()(uint256)"
65 changes: 15 additions & 50 deletions src/NonLiquidDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ contract NonLiquidDelegation is IDelegation, BaseDelegation, INonLiquidDelegatio
mapping(address => uint256) taxedSinceLastStaking;
int256 immutableRewards;
mapping(address => address) newAddress;
mapping(address => uint256) roundingErrors;
uint256 totalRoundingErrors;
}

// keccak256(abi.encode(uint256(keccak256("zilliqa.storage.NonLiquidDelegation")) - 1)) & ~bytes32(uint256(0xff))
Expand Down Expand Up @@ -316,9 +314,7 @@ uint256 totalRoundingErrors;
*/
function rewards(uint64 additionalSteps) public view returns(uint256) {
NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage();
//TODO: replace
// (uint256 resultInTotal, , , ) = _rewards(additionalSteps);
(uint256 resultInTotal, , , , , ) = _rewards(additionalSteps);
(uint256 resultInTotal, , , , ) = _rewards(additionalSteps);
resultInTotal -= $.taxedSinceLastStaking[_msgSender()];
return resultInTotal - resultInTotal * getCommissionNumerator() / DENOMINATOR + $.availableTaxedRewards[_msgSender()];
}
Expand All @@ -328,9 +324,7 @@ uint256 totalRoundingErrors;
*/
function rewards() public view returns(uint256) {
NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage();
//TODO: replace
// (uint256 resultInTotal, , , ) = _rewards();
(uint256 resultInTotal, , , , , ) = _rewards();
(uint256 resultInTotal, , , , ) = _rewards();
resultInTotal -= $.taxedSinceLastStaking[_msgSender()];
return resultInTotal - resultInTotal * getCommissionNumerator() / DENOMINATOR + $.availableTaxedRewards[_msgSender()];
}
Expand Down Expand Up @@ -399,14 +393,6 @@ uint256 totalRoundingErrors;
emit Staked(_msgSender(), amount, "");
}

//TODO: natspec comment
function totalRoundingErrors() public view returns(uint256) {
NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage();
return $.totalRoundingErrors;
}

//TODO: remove
event Test(string,uint);
/**
* @dev Make the requested `amount` of taxed rewards available to the caller for staking or
* withdrawing by traversing the {Staking} history in `1 + additionalSteps`.
Expand All @@ -416,22 +402,14 @@ event Test(string,uint);
*/
function _useRewards(uint256 amount, uint64 additionalSteps) internal whenNotPaused returns(uint256, uint256) {
NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage();
// # emit Test("$.totalRoundingErrors", $.totalRoundingErrors);
// # emit Test("$.roundingErrors[_msgSender()]", $.roundingErrors[_msgSender()]);
// # $.totalRoundingErrors -= $.roundingErrors[_msgSender()];
(
uint256 resultInTotal,
uint256 resultAfterLastStaking,
uint64 posInStakingIndices,
uint64 nextStakingIndex,
uint256 roundingError,
uint256 totalRoundingErrors
) = additionalSteps == type(uint64).max ?
_rewards() :
_rewards(additionalSteps);
// # $.roundingErrors[_msgSender()] += roundingError;
// # $.totalRoundingErrors += roundingError;
// # //$.totalRoundingErrors = totalRoundingErrors;
// the caller has not delegated any stake yet
if (nextStakingIndex == 0)
return (0, 0);
Expand Down Expand Up @@ -465,8 +443,7 @@ uint256 totalRoundingErrors
uint256 resultAfterLastStaking,
uint64 posInStakingIndices,
uint64 nextStakingIndex,
uint256 roundingError,
uint256 totalRoundingErrors
uint256 roundingError
) {
return _rewards(type(uint64).max);
}
Expand All @@ -480,16 +457,12 @@ uint256 totalRoundingErrors
uint256 resultAfterLastStaking,
uint64 posInStakingIndices,
uint64 nextStakingIndex,
uint256 roundingError,
uint256 totalRoundingErrors
uint256 roundingError
) {
NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage();
uint64 firstStakingIndex;
uint256 amount;
uint256 total;
// # totalRoundingErrors = $.totalRoundingErrors;
// # //TODO: enable the below line
// # //roundingError = $.roundingErrors[_msgSender()];
for (
posInStakingIndices = $.firstStakingIndex[_msgSender()];
posInStakingIndices < $.stakingIndices[_msgSender()].length;
Expand All @@ -508,25 +481,21 @@ uint256 totalRoundingErrors
nextStakingIndex < $.stakings.length :
nextStakingIndex <= $.stakingIndices[_msgSender()][posInStakingIndices + 1]
) {
if (total > 0)
if (total > 0) {
resultInTotal += $.stakings[nextStakingIndex].rewards * amount / total;
if (total > 0) {
roundingError +=
1 ether * $.stakings[nextStakingIndex].rewards * amount / total -
1 ether * ($.stakings[nextStakingIndex].rewards * amount / total);
// # // totalRoundingErrors = $.totalRoundingErrors + roundingError - $.roundingErrors[_msgSender()];
}
roundingError +=
1 ether * $.stakings[nextStakingIndex].rewards * amount / total -
1 ether * ($.stakings[nextStakingIndex].rewards * amount / total);
}
total = $.stakings[nextStakingIndex].total;
nextStakingIndex++;
if (nextStakingIndex - firstStakingIndex > additionalSteps)
// # //TODO: remove return (resultInTotal, resultAfterLastStaking, posInStakingIndices, nextStakingIndex);
return (
resultInTotal + roundingError / 1 ether,
resultAfterLastStaking,
posInStakingIndices,
nextStakingIndex,
roundingError - 1 ether * (roundingError / 1 ether),
totalRoundingErrors
roundingError - 1 ether * (roundingError / 1 ether)
);
}
}
Expand All @@ -536,12 +505,9 @@ roundingError +=
// the last step is to add the rewards accrued since the last staking
if (total > 0) {
resultAfterLastStaking = (int256(getRewards()) - $.immutableRewards).toUint256() * amount / total;
// # //TODO: replace the above line with
// # // resultAfterLastStaking = (int256(getRewards()) - totalRoundingErrors / 1 ether - $.immutableRewards).toUint256() * amount / total;
roundingError +=
1 ether * (int256(getRewards()) - $.immutableRewards).toUint256() * amount / total -
1 ether * ((int256(getRewards()) - $.immutableRewards).toUint256() * amount / total);
// # // totalRoundingErrors = $.totalRoundingErrors + roundingError - $.roundingErrors[_msgSender()];
roundingError +=
1 ether * (int256(getRewards()) - $.immutableRewards).toUint256() * amount / total -
1 ether * ((int256(getRewards()) - $.immutableRewards).toUint256() * amount / total);
resultInTotal += resultAfterLastStaking;
}
}
Expand All @@ -551,9 +517,8 @@ roundingError +=
// existed during the current call of the function so that we can continue from there
if (posInStakingIndices > 0)
posInStakingIndices--;
resultInTotal += roundingError / 1 ether;
roundingError -= 1 ether * (roundingError / 1 ether);
// # // totalRoundingErrors = $.totalRoundingErrors + roundingError - $.roundingErrors[_msgSender()];
resultInTotal += roundingError / 1 ether;
roundingError -= 1 ether * (roundingError / 1 ether);
}

/**
Expand Down

0 comments on commit 5eed549

Please sign in to comment.