From 131a3e0d24b7a73ad6b80f63831221980b92d2a0 Mon Sep 17 00:00:00 2001 From: Victor Elias Date: Fri, 13 Oct 2023 18:11:44 -0300 Subject: [PATCH] test: Test cumulative stake/fees funcs from LIP36 --- .../bonding/libraries/EarningsPoolLIP36.sol | 5 -- contracts/test/TestEarningsPoolLIP36.sol | 63 +++++++++++++++++++ contracts/test/mocks/EarningsPoolFixture.sol | 21 +++++++ test/integration/BondingVotes.js | 1 - 4 files changed, 84 insertions(+), 6 deletions(-) diff --git a/contracts/bonding/libraries/EarningsPoolLIP36.sol b/contracts/bonding/libraries/EarningsPoolLIP36.sol index ad74d540..314419de 100644 --- a/contracts/bonding/libraries/EarningsPoolLIP36.sol +++ b/contracts/bonding/libraries/EarningsPoolLIP36.sol @@ -125,11 +125,6 @@ library EarningsPoolLIP36 { _startPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1); } - // If the end cumulativeRewardFactor is 0 set the default value to PreciseMathUtils.percPoints(1, 1) - if (_endPool.cumulativeRewardFactor == 0) { - _endPool.cumulativeRewardFactor = PreciseMathUtils.percPoints(1, 1); - } - uint256 earnedFees = PreciseMathUtils.percOf( _stake, _endPool.cumulativeFeeFactor.sub(_startPool.cumulativeFeeFactor), diff --git a/contracts/test/TestEarningsPoolLIP36.sol b/contracts/test/TestEarningsPoolLIP36.sol index 2d989b8d..df44bcf9 100644 --- a/contracts/test/TestEarningsPoolLIP36.sol +++ b/contracts/test/TestEarningsPoolLIP36.sol @@ -70,4 +70,67 @@ contract TestEarningsPoolLIP36 { fixture.updateCumulativeRewardFactor(1000); Assert.equal(expRewardFactor, fixture.getCumulativeRewardFactor(), "incorrect cumulative reward factor"); } + + function test_delegatorCumulativeFees() public { + uint256 stake = 1000; + uint256 fees = 10; + + // all zeroed factors, should just return current fees + Assert.equal(10, fixture.delegatorCumulativeFees(stake, fees), "incorrect delegator cumulative fees"); + + fixture.setPrevPoolEarningsFactors(3, 2); + fixture.setEarningsFactors(3, 2); + + // no increased fee factor yet, should still return current fees + Assert.equal(10, fixture.delegatorCumulativeFees(stake, fees), "incorrect delegator cumulative fees"); + + fixture.setEarningsFactors(6, 0); // end pool reward factor should not be used, set as 0 + + // earned fees = 1000 * (6 - 3) / 2 = 1500 + Assert.equal(1510, fixture.delegatorCumulativeFees(stake, fees), "incorrect delegator cumulative fees"); + } + + function test_delegatorCumulativeStake() public { + uint256 stake = 1000; + + // all zeroed factors, should just return current stake + Assert.equal(1000, fixture.delegatorCumulativeStake(stake), "incorrect delegator cumulative stake"); + + fixture.setPrevPoolEarningsFactors(0, 4); + fixture.setEarningsFactors(0, 4); + + // no increased reward factor yet, should still return current stake + Assert.equal(1000, fixture.delegatorCumulativeStake(stake), "incorrect delegator cumulative stake"); + + fixture.setEarningsFactors(0, 10); + + // stake = 1000 * 10 / 4 = 2500 + Assert.equal(2500, fixture.delegatorCumulativeStake(stake), "incorrect delegator cumulative stake"); + } + + function test_delegatorCumulativeStakeAndFees() public { + uint256 stake = 1000; + uint256 fees = 10; + + // all zeroed factors, should just return current stake + (uint256 cStake, uint256 cFees) = fixture.delegatorCumulativeStakeAndFees(stake, fees); + Assert.equal(1000, cStake, "incorrect delegator cumulative stake"); + Assert.equal(10, cFees, "incorrect delegator cumulative fee"); + + fixture.setPrevPoolEarningsFactors(2, 5); + fixture.setEarningsFactors(2, 5); + + // no increased factors yet, should still return current values + (cStake, cFees) = fixture.delegatorCumulativeStakeAndFees(stake, fees); + Assert.equal(1000, cStake, "incorrect delegator cumulative stake"); + Assert.equal(10, cFees, "incorrect delegator cumulative fee"); + + fixture.setEarningsFactors(5, 15); + + (cStake, cFees) = fixture.delegatorCumulativeStakeAndFees(stake, fees); + // stake = 1000 * 15 / 5 = 3000 + Assert.equal(3000, cStake, "incorrect delegator cumulative stake"); + // earned fees = 1000 * (5 - 2) / 5 = 600 + Assert.equal(610, cFees, "incorrect delegator cumulative fee"); + } } diff --git a/contracts/test/mocks/EarningsPoolFixture.sol b/contracts/test/mocks/EarningsPoolFixture.sol index acbd0e10..a8883eea 100644 --- a/contracts/test/mocks/EarningsPoolFixture.sol +++ b/contracts/test/mocks/EarningsPoolFixture.sol @@ -36,6 +36,11 @@ contract EarningsPoolFixture { prevPool.cumulativeRewardFactor = _cumulativeRewardFactor; } + function setEarningsFactors(uint256 _cumulativeFeeFactor, uint256 _cumulativeRewardFactor) public { + pool.cumulativeFeeFactor = _cumulativeFeeFactor; + pool.cumulativeRewardFactor = _cumulativeRewardFactor; + } + function getTranscoderRewardCut() public view returns (uint256) { return pool.transcoderRewardCut; } @@ -55,4 +60,20 @@ contract EarningsPoolFixture { function getCumulativeFeeFactor() public view returns (uint256) { return pool.cumulativeFeeFactor; } + + function delegatorCumulativeStake(uint256 _stake) public view returns (uint256) { + return EarningsPoolLIP36.delegatorCumulativeStake(prevPool, pool, _stake); + } + + function delegatorCumulativeFees(uint256 _stake, uint256 _fees) public view returns (uint256) { + return EarningsPoolLIP36.delegatorCumulativeFees(prevPool, pool, _stake, _fees); + } + + function delegatorCumulativeStakeAndFees(uint256 _stake, uint256 _fees) + public + view + returns (uint256 cStake, uint256 cFees) + { + return EarningsPoolLIP36.delegatorCumulativeStakeAndFees(prevPool, pool, _stake, _fees); + } } diff --git a/test/integration/BondingVotes.js b/test/integration/BondingVotes.js index 566dc783..3eb68f8d 100644 --- a/test/integration/BondingVotes.js +++ b/test/integration/BondingVotes.js @@ -961,7 +961,6 @@ describe("BondingVotes", () => { const creationRound = await roundsManager.currentRound() const creationRoundBlockHash = await roundsManager.blockHashForRound(creationRound) - console.log(creationRoundBlockHash) const auxData = ethers.utils.solidityPack( ["uint256", "bytes32"], [creationRound, creationRoundBlockHash]