Skip to content

Commit

Permalink
bonding: Update reward cut logic to match LIP
Browse files Browse the repository at this point in the history
It is a little less exact (might overmint on the
last reward call), but the simpler logic might be
just worth it.
  • Loading branch information
victorges committed Jul 21, 2023
1 parent f580f29 commit 90f63e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
28 changes: 12 additions & 16 deletions contracts/bonding/BondingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -873,29 +873,25 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
earningsPool.setStake(t.earningsPoolPerRound[lastUpdateRound].totalStake);
}

if (treasuryBalanceCeiling > 0) {
uint256 treasuryBalance = livepeerToken().balanceOf(treasury());
if (treasuryBalance >= treasuryBalanceCeiling) {
// halt treasury contributions until the cut rate param is updated again
treasuryRewardCutRate = 0;
}
}

// Create reward based on active transcoder's stake relative to the total active stake
// rewardTokens = (current mintable tokens for the round * active transcoder stake) / total active stake
IMinter mtr = minter();
uint256 totalRewardTokens = mtr.createReward(earningsPool.totalStake, currentRoundTotalActiveStake);

uint256 treasuryRewards = PreciseMathUtils.percOf(totalRewardTokens, treasuryRewardCutRate);
if (treasuryRewards > 0) {
address trsy = treasury();
uint256 treasuryBalance = livepeerToken().balanceOf(trsy);

uint256 maxTreasuryRewards = treasuryBalanceCeiling > treasuryBalance
? treasuryBalanceCeiling - treasuryBalance
: 0;
if (treasuryRewards > maxTreasuryRewards) {
treasuryRewards = maxTreasuryRewards;
// halt treasury contributions until the cut rate param is updated again
treasuryRewardCutRate = 0;
}
address trsry = treasury();

if (treasuryRewards > 0) {
mtr.trustedTransferTokens(trsy, treasuryRewards);
emit TreasuryReward(msg.sender, trsy, treasuryRewards);
}
mtr.trustedTransferTokens(trsry, treasuryRewards);

emit TreasuryReward(msg.sender, trsry, treasuryRewards);
}

uint256 transcoderRewards = totalRewardTokens.sub(treasuryRewards);
Expand Down
41 changes: 25 additions & 16 deletions test/unit/BondingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4800,26 +4800,35 @@ describe("BondingManager", () => {
})

describe("ceiling behavior", () => {
beforeEach(async () => {
await fixture.token.setMockUint256(
functionSig("balanceOf(address)"),
990
)
})
describe("under the limit", () => {
beforeEach(async () => {
await fixture.token.setMockUint256(
functionSig("balanceOf(address)"),
990
)
})

it("should limit contribution to ceiling", async () => {
const tx = await bondingManager.connect(transcoder).reward()
it("should contribute normally", async () => {
const tx = await bondingManager
.connect(transcoder)
.reward()

await expect(tx)
.to.emit(fixture.minter, "TrustedTransferTokens")
.withArgs(fixture.treasury.address, 10) // 1000 - 990
})
await expect(tx)
.to.emit(fixture.minter, "TrustedTransferTokens")
.withArgs(fixture.treasury.address, 63)
})

it("should clear treasuryRewardCutRate param", async () => {
await bondingManager.connect(transcoder).reward()
it("should not clear treasuryRewardCutRate param", async () => {
await bondingManager.connect(transcoder).reward()

const cutRate = await bondingManager.treasuryRewardCutRate()
assert.equal(cutRate.toNumber(), 0, "cut rate not cleared")
const cutRate =
await bondingManager.treasuryRewardCutRate()
assert.equal(
cutRate.toString(),
TREASURY_CUT.toString(),
"cut rate updated"
)
})
})

describe("when at limit", () => {
Expand Down

0 comments on commit 90f63e5

Please sign in to comment.