Skip to content

Commit

Permalink
test: Test cumulative stake/fees funcs from LIP36
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Oct 13, 2023
1 parent 06097c1 commit 131a3e0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
5 changes: 0 additions & 5 deletions contracts/bonding/libraries/EarningsPoolLIP36.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
63 changes: 63 additions & 0 deletions contracts/test/TestEarningsPoolLIP36.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
21 changes: 21 additions & 0 deletions contracts/test/mocks/EarningsPoolFixture.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
1 change: 0 additions & 1 deletion test/integration/BondingVotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 131a3e0

Please sign in to comment.