Skip to content

Commit

Permalink
add test for reward accounting
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmrodri committed Oct 19, 2023
1 parent cdae4b3 commit a023846
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions test/Revenues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
CollateralStatus,
TradeKind,
MAX_UINT192,
ONE_PERIOD,
} from '../common/constants'
import { expectEvents } from '../common/events'
import { bn, divCeil, fp, near } from '../common/numbers'
Expand Down Expand Up @@ -554,15 +555,28 @@ describe(`Revenues - P${IMPLEMENTATION}`, () => {
)
})

it('Should forward RSR revenue directly to StRSR', async () => {
it('Should forward RSR revenue directly to StRSR and call payoutRewards()', async () => {
const amount = bn('2000e18')
await rsr.connect(owner).mint(backingManager.address, amount)
expect(await rsr.balanceOf(backingManager.address)).to.equal(amount)
expect(await rsr.balanceOf(stRSR.address)).to.equal(0)
expect(await rsr.balanceOf(rsrTrader.address)).to.equal(0)
expect(await rsr.balanceOf(rTokenTrader.address)).to.equal(0)

await expect(backingManager.forwardRevenue([rsr.address])).to.emit(rsr, 'Transfer')
await expectEvents(backingManager.forwardRevenue([rsr.address]), [
{
contract: rsr,
name: 'Transfer',
args: [backingManager.address, stRSR.address, amount],
emitted: true,
},
{
contract: stRSR,
name: 'RewardsPaid',
emitted: true,
},
])

expect(await rsr.balanceOf(backingManager.address)).to.equal(0)
expect(await rsr.balanceOf(stRSR.address)).to.equal(amount)
expect(await rsr.balanceOf(rsrTrader.address)).to.equal(0)
Expand Down Expand Up @@ -688,6 +702,32 @@ describe(`Revenues - P${IMPLEMENTATION}`, () => {
expect(newRTokenTotal).equal(bn(0))
})

it('Should account rewards when distributing tokenToBuy', async () => {
// 1. StRSR.payoutRewards()
const stRSRBal = await rsr.balanceOf(stRSR.address)
await rsr.connect(owner).mint(rsrTrader.address, issueAmount)
await expect(rsrTrader.distributeTokenToBuy()).to.emit(stRSR, 'RewardsPaid')
const expectedAmountStRSR = stRSRBal.add(issueAmount)
expect(await rsr.balanceOf(stRSR.address)).to.be.closeTo(expectedAmountStRSR, 100)

// 2. Furnace.melt()
// Transfer RTokens to Furnace (to trigger melting later)
const hndAmt: BigNumber = bn('10e18')
await rToken.connect(addr1).transfer(furnace.address, hndAmt)
await advanceTime(Number(ONE_PERIOD))
await furnace.melt()

// Transfer and distribute tokens in Trader (will melt)
await advanceTime(Number(ONE_PERIOD))
await rToken.connect(addr1).transfer(rTokenTrader.address, hndAmt)
await expect(rTokenTrader.distributeTokenToBuy()).to.emit(rToken, 'Melted')
const expectedAmountFurnace = hndAmt.mul(2)
expect(await rToken.balanceOf(furnace.address)).to.be.closeTo(
expectedAmountFurnace,
expectedAmountFurnace.div(1000)
) // within 0.1%
})

it('Should update distribution even if distributeTokenToBuy() reverts', async () => {
// Check initial status
const [rTokenTotal, rsrTotal] = await distributor.totals()
Expand Down

0 comments on commit a023846

Please sign in to comment.