Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚬 Add fee to both vessel and global debt #4

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions contracts/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ contract BorrowerOperations is TrinityBase, ReentrancyGuardUpgradeable, UUPSUpgr
}

function collectVesselFee(address _asset, address _borrower) external {
_collectVesselFee(_asset, _borrower);
uint256 collectedFee = _collectVesselFee(_asset, _borrower);
IActivePool(activePool).increaseDebt(_asset, collectedFee);
}

/**
Expand All @@ -343,16 +344,17 @@ contract BorrowerOperations is TrinityBase, ReentrancyGuardUpgradeable, UUPSUpgr
function _collectVesselFee(
address _asset,
address _borrower
) internal {
) internal returns (uint256) {
if(_vesselAlreadyCollected(_asset, _borrower)) {
return;
return 0;
}

IVesselManager(vesselManager).applyPendingRewards(_asset, _borrower);
uint256 debt = IVesselManager(vesselManager).getVesselDebt(_asset, _borrower);

uint256 debtTokenFee = _triggerBorrowingFee(_asset, _borrower, debt);
IVesselManager(vesselManager).increaseVesselDebt(_asset, _borrower, debtTokenFee);
return debtTokenFee;
}

function _getCurrentEpoch() internal view returns (uint256) {
Expand Down
41 changes: 41 additions & 0 deletions test/trinity/BorrowerOperations_FeesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,46 @@ contract("BorrowerOperations_Fees", async accounts => {
const expectedTimestamp = currentTimestamp.sub(currentTimestamp.mod(toBN(SECONDS_IN_ONE_WEEK)))
assert.isTrue(epoch.eq(expectedTimestamp))
})

it('adds to both vessel debt and global debt', async () => {
const {activePool, borrowerOperations, vesselManager, erc20} = contracts.core
await openVessel(alice)

const initialAliceDebt = await vesselManager.getVesselDebt(erc20.address, alice)
const initialTotalDebt = await activePool.getDebtTokenBalance(erc20.address)

await skipToNextEpoch()
await borrowerOperations.collectVesselFee(erc20.address, alice)

const newAliceDebt = await vesselManager.getVesselDebt(erc20.address, alice)
const newGlobalDebt = await activePool.getDebtTokenBalance(erc20.address)

assert.equal(newAliceDebt.toString(), getDebtWithFee(initialAliceDebt).toString())
assert.equal(newGlobalDebt.toString(), getDebtWithFee(initialTotalDebt).toString())
})

it('can close vessel if vesselDebt > activePoolDebt', async () => {
const {activePool, borrowerOperations, vesselManager, debtToken, erc20} = contracts.core
await openVessel(alice)
await debtToken.unprotectedMint(alice, vesselTotalDebt)

const activePoolInitialDebt = await activePool.getDebtTokenBalance(erc20.address)

await skipToNextEpoch()

await borrowerOperations.collectVesselFee(erc20.address, alice)

const vesselDebt = await vesselManager.getVesselDebt(erc20.address, alice)
const activePoolDebt = await activePool.getDebtTokenBalance(erc20.address)

assert.isTrue(vesselDebt.gt(activePoolInitialDebt))
assert.equal(vesselDebt.toString(), activePoolDebt.toString())
assert.isTrue(await vesselManager.isVesselActive(erc20.address, alice))

await openVessel(bob)
await closeVessel(alice)

assert.isFalse(await vesselManager.isVesselActive(erc20.address, alice))
})
})
})
Loading