Skip to content

Commit

Permalink
πŸŒ™ Add closeVessel fees tests (#9)
Browse files Browse the repository at this point in the history
* πŸŒ™ Add closeVessel fees tests

* Use assert.equal

* Check the balance after first collection
  • Loading branch information
MiksuJak authored Apr 29, 2024
1 parent 4ddadd3 commit f69d51b
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion test/trinity/BorrowerOperations_FeesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {_1e18BN} = testHelpers.MoneyValues


contract("BorrowerOperations_Fees", async accounts => {
const [owner, alice, treasury] = accounts
const [owner, alice, bob, treasury] = accounts

let contracts
let snapshotId
Expand All @@ -29,6 +29,17 @@ contract("BorrowerOperations_Fees", async accounts => {
})
}

async function closeVessel(account) {
const {borrowerOperations, erc20} = contracts.core
return borrowerOperations.closeVessel(erc20.address, {
from: account,
})
}

async function mintDebtTokens(account, amount) {
return contracts.core.debtToken.unprotectedMint(account, amount)
}

async function skipToNextEpoch() {
await th.fastForwardTime(SECONDS_IN_ONE_WEEK, web3.currentProvider)
}
Expand All @@ -37,6 +48,10 @@ contract("BorrowerOperations_Fees", async accounts => {
return debt.add(debt.mul(borrowingFee).div(_1e18BN))
}

function getFee(debt) {
return debt.mul(borrowingFee).div(_1e18BN)
}

function getEpochUpdatedEvent(tx) {
for (let i = 0; i < tx.logs.length; i++) {
if (tx.logs[i].event === "VesselEpochUpdated") {
Expand Down Expand Up @@ -196,6 +211,58 @@ contract("BorrowerOperations_Fees", async accounts => {
})
})

describe('closeVessel', () => {
it('does not add fees mid epoch', async () => {
const {erc20, debtToken, borrowerOperations} = contracts.core
await openVessel(alice)
await skipToNextEpoch()

const debtBeforeFeeCollection = await contracts.core.vesselManager.getVesselDebt(erc20.address, alice)
const treasuryBalanceBeforeFeeCollection = await debtToken.balanceOf(treasury)
await borrowerOperations.collectVesselFee(erc20.address, alice)

const treasuryBalancePostFeeCollection = await debtToken.balanceOf(treasury)
const expectedFee = getFee(debtBeforeFeeCollection)
assert.equal(treasuryBalancePostFeeCollection.toString(), treasuryBalanceBeforeFeeCollection.add(expectedFee).toString())

// bob opens vessel so alice can close hers
await openVessel(bob)

const treasuryBalanceBeforeClose = await debtToken.balanceOf(treasury)
await th.fastForwardTime(SECONDS_IN_ONE_WEEK / 2, web3.currentProvider)
await mintDebtTokens(alice, vesselTotalDebt)
await closeVessel(alice)

const treasuryBalancePostClose = await debtToken.balanceOf(treasury)
assert.equal(treasuryBalancePostClose.toString(), treasuryBalanceBeforeClose.toString())
})

it('skips due fee payment when called before collectVesselFee', async () => {
const {erc20, debtToken, borrowerOperations} = contracts.core
await openVessel(alice)
await skipToNextEpoch()

const debtBeforeFeeCollection = await contracts.core.vesselManager.getVesselDebt(erc20.address, alice)
const treasuryBalanceBeforeFeeCollection = await debtToken.balanceOf(treasury)
await borrowerOperations.collectVesselFee(erc20.address, alice)

const treasuryBalancePostFeeCollection = await debtToken.balanceOf(treasury)
const expectedFee = getFee(debtBeforeFeeCollection)
assert.equal(treasuryBalancePostFeeCollection.toString(), treasuryBalanceBeforeFeeCollection.add(expectedFee).toString())

// bob opens vessel so alice can close hers
await openVessel(bob)

const treasuryBalanceBeforeClose = await debtToken.balanceOf(treasury)
await skipToNextEpoch()
await mintDebtTokens(alice, vesselTotalDebt)
await closeVessel(alice)

const treasuryBalancePostClose = await debtToken.balanceOf(treasury)
assert.equal(treasuryBalancePostClose.toString(), treasuryBalanceBeforeClose.toString())
})
})

describe('collectVesselFee', () => {
it('does nothing if called twice in single epoch', async () => {
const {erc20, borrowerOperations, vesselManager} = contracts.core
Expand Down

0 comments on commit f69d51b

Please sign in to comment.