Skip to content

Commit

Permalink
Remove redundant borrow fee getters
Browse files Browse the repository at this point in the history
  • Loading branch information
RickGriff committed Feb 4, 2024
1 parent f6a5f85 commit b62169a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 265 deletions.
8 changes: 0 additions & 8 deletions contracts/src/Interfaces/ITroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ interface ITroveManager is ILiquityBase {

function getRedemptionFeeWithDecay(uint _ETHDrawn) external view returns (uint);

function getBorrowingRate() external view returns (uint);
function getBorrowingRateWithDecay() external view returns (uint);

function getBorrowingFee(uint BoldDebt) external view returns (uint);
function getBorrowingFeeWithDecay(uint _boldDebt) external view returns (uint);

function decayBaseRateFromBorrowing() external;

function getTroveStatus(address _borrower) external view returns (uint);

function getTroveStake(address _borrower) external view returns (uint);
Expand Down
43 changes: 0 additions & 43 deletions contracts/src/TroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1386,49 +1386,6 @@ contract TroveManager is LiquityBase, Ownable, CheckContract, ITroveManager {
// return redemptionFee;
}

// --- Borrowing fee functions ---

function getBorrowingRate() public view override returns (uint) {
return _calcBorrowingRate(baseRate);
}

function getBorrowingRateWithDecay() public view override returns (uint) {
return _calcBorrowingRate(_calcDecayedBaseRate());
}

function _calcBorrowingRate(uint _baseRate) internal pure returns (uint) {
return 0;
// return LiquityMath._min(
// BORROWING_FEE_FLOOR + _baseRate,
// MAX_BORROWING_FEE
// );
}

function getBorrowingFee(uint _boldDebt) external view override returns (uint) {
return _calcBorrowingFee(getBorrowingRate(), _boldDebt);
}

function getBorrowingFeeWithDecay(uint _boldDebt) external view override returns (uint) {
return _calcBorrowingFee(getBorrowingRateWithDecay(), _boldDebt);
}

function _calcBorrowingFee(uint _borrowingRate, uint _boldDebt) internal pure returns (uint) {
return _borrowingRate * _boldDebt / DECIMAL_PRECISION;
}

// Updates the baseRate state variable based on time elapsed since the last redemption or Bold borrowing operation.
function decayBaseRateFromBorrowing() external override {
_requireCallerIsBorrowerOperations();

uint decayedBaseRate = _calcDecayedBaseRate();
assert(decayedBaseRate <= DECIMAL_PRECISION); // The baseRate can decay to 0

baseRate = decayedBaseRate;
emit BaseRateUpdated(decayedBaseRate);

_updateLastFeeOpTime();
}

// --- Internal fee functions ---

// Update the last fee operation time only if time passed >= decay interval. This prevents base rate griefing.
Expand Down
4 changes: 1 addition & 3 deletions contracts/test/BorrowerOperationsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3859,9 +3859,7 @@ contract("BorrowerOperations", async (accounts) => {
});

// Get the expected debt based on the Bold request (adding fee and liq. reserve on top)
const expectedDebt = BoldRequest.add(
await troveManager.getBorrowingFee(BoldRequest)
).add(BOLD_GAS_COMPENSATION);
const expectedDebt = BoldRequest.add(BOLD_GAS_COMPENSATION);

const debt_After = await getTroveEntireDebt(alice);
const coll_After = await getTroveEntireColl(alice);
Expand Down
202 changes: 0 additions & 202 deletions contracts/test/FeeArithmeticTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,208 +365,6 @@ contract('Fee arithmetic tests', async accounts => {
}
})

it("decayBaseRateFromBorrowing(): returns the initial base rate for no time increase", async () => {
await troveManagerTester.setBaseRate(dec(5, 17))
await troveManagerTester.setLastFeeOpTimeToNow()

const baseRateBefore = await troveManagerTester.baseRate()
assert.equal(baseRateBefore, dec(5, 17))

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const baseRateAfter = await troveManagerTester.baseRate()

assert.isTrue(baseRateBefore.eq(baseRateAfter))
})

it("decayBaseRateFromBorrowing(): returns the initial base rate for less than one minute passed ", async () => {
await troveManagerTester.setBaseRate(dec(5, 17))
await troveManagerTester.setLastFeeOpTimeToNow()

// 1 second
const baseRateBefore_1 = await troveManagerTester.baseRate()
assert.equal(baseRateBefore_1, dec(5, 17))

await th.fastForwardTime(1, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const baseRateAfter_1 = await troveManagerTester.baseRate()

assert.isTrue(baseRateBefore_1.eq(baseRateAfter_1))

// 17 seconds
await troveManagerTester.setLastFeeOpTimeToNow()

const baseRateBefore_2 = await troveManagerTester.baseRate()
await th.fastForwardTime(17, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const baseRateAfter_2 = await troveManagerTester.baseRate()

assert.isTrue(baseRateBefore_2.eq(baseRateAfter_2))

// 29 seconds
await troveManagerTester.setLastFeeOpTimeToNow()

const baseRateBefore_3 = await troveManagerTester.baseRate()
await th.fastForwardTime(29, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const baseRateAfter_3 = await troveManagerTester.baseRate()

assert.isTrue(baseRateBefore_3.eq(baseRateAfter_3))

// 50 seconds
await troveManagerTester.setLastFeeOpTimeToNow()

const baseRateBefore_4 = await troveManagerTester.baseRate()
await th.fastForwardTime(50, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const baseRateAfter_4 = await troveManagerTester.baseRate()

assert.isTrue(baseRateBefore_4.eq(baseRateAfter_4))

// (cant quite test up to 59 seconds, as execution of the final tx takes >1 second before the block is mined)
})

it("decayBaseRateFromBorrowing(): returns correctly decayed base rate, for various durations. Initial baseRate = 0.01", async () => {
// baseRate = 0.01
for (i = 0; i < decayBaseRateResults.seconds.length; i++) {
// Set base rate to 0.01 in TroveManager
await troveManagerTester.setBaseRate(dec(1, 16))
const contractBaseRate = await troveManagerTester.baseRate()
assert.equal(contractBaseRate, dec(1, 16))

const startBaseRate = '0.01'

const secondsPassed = decayBaseRateResults.seconds[i]
const expectedDecayedBaseRate = decayBaseRateResults[startBaseRate][i]
await troveManagerTester.setLastFeeOpTimeToNow()

// Progress time
await th.fastForwardTime(secondsPassed, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const decayedBaseRate = await troveManagerTester.baseRate()

const minutesPassed = secondsPassed / 60

const error = decayedBaseRate.sub(toBN(expectedDecayedBaseRate))
// console.log(
// `starting baseRate: ${startBaseRate},
// minutesPassed: ${minutesPassed},
// expectedDecayedBaseRate: ${expectedDecayedBaseRate},
// decayedBaseRate: ${decayedBaseRate},
// error: ${error}`
// )
assert.isAtMost(getDifference(expectedDecayedBaseRate.toString(), decayedBaseRate.toString()), 100000) // allow absolute error tolerance of 1e-13
}
})

it("decayBaseRateFromBorrowing(): returns correctly decayed base rate, for various durations. Initial baseRate = 0.1", async () => {
// baseRate = 0.1
for (i = 0; i < decayBaseRateResults.seconds.length; i++) {
// Set base rate to 0.1 in TroveManager
await troveManagerTester.setBaseRate(dec(1, 17))
const contractBaseRate = await troveManagerTester.baseRate()
assert.equal(contractBaseRate, dec(1, 17))

const startBaseRate = '0.1'

const secondsPassed = decayBaseRateResults.seconds[i]
const expectedDecayedBaseRate = decayBaseRateResults['0.1'][i]
await troveManagerTester.setLastFeeOpTimeToNow()

// Progress time
await th.fastForwardTime(secondsPassed, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const decayedBaseRate = await troveManagerTester.baseRate()

const minutesPassed = secondsPassed / 60

const error = decayedBaseRate.sub(toBN(expectedDecayedBaseRate))
// console.log(
// `starting baseRate: ${startBaseRate},
// minutesPassed: ${minutesPassed},
// expectedDecayedBaseRate: ${expectedDecayedBaseRate},
// decayedBaseRate: ${decayedBaseRate},
// error: ${error}`
// )
assert.isAtMost(getDifference(expectedDecayedBaseRate.toString(), decayedBaseRate.toString()), 1000000) // allow absolute error tolerance of 1e-12
}
})

it("decayBaseRateFromBorrowing(): returns correctly decayed base rate, for various durations. Initial baseRate = 0.34539284", async () => {
// baseRate = 0.34539284
for (i = 0; i < decayBaseRateResults.seconds.length; i++) {
// Set base rate to 0.1 in TroveManager
await troveManagerTester.setBaseRate('345392840000000000')
const contractBaseRate = await troveManagerTester.baseRate()
await troveManagerTester.setBaseRate('345392840000000000')

const startBaseRate = '0.34539284'

const secondsPassed = decayBaseRateResults.seconds[i]
const expectedDecayedBaseRate = decayBaseRateResults[startBaseRate][i]
await troveManagerTester.setLastFeeOpTimeToNow()

// Progress time
await th.fastForwardTime(secondsPassed, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const decayedBaseRate = await troveManagerTester.baseRate()

const minutesPassed = secondsPassed / 60

const error = decayedBaseRate.sub(toBN(expectedDecayedBaseRate))
// console.log(
// `starting baseRate: ${startBaseRate},
// minutesPassed: ${minutesPassed},
// expectedDecayedBaseRate: ${expectedDecayedBaseRate},
// decayedBaseRate: ${decayedBaseRate},
// error: ${error}`
// )

assert.isAtMost(getDifference(expectedDecayedBaseRate.toString(), decayedBaseRate.toString()), 1000000) // allow absolute error tolerance of 1e-12
}
})

it("decayBaseRateFromBorrowing(): returns correctly decayed base rate, for various durations. Initial baseRate = 0.9976", async () => {
// baseRate = 0.9976
for (i = 0; i < decayBaseRateResults.seconds.length; i++) {
// Set base rate to 0.9976 in TroveManager
await troveManagerTester.setBaseRate('997600000000000000')
await troveManagerTester.setBaseRate('997600000000000000')

const startBaseRate = '0.9976'

const secondsPassed = decayBaseRateResults.seconds[i]
const expectedDecayedBaseRate = decayBaseRateResults[startBaseRate][i]
await troveManagerTester.setLastFeeOpTimeToNow()

// progress time
await th.fastForwardTime(secondsPassed, web3.currentProvider)

await troveManagerTester.unprotectedDecayBaseRateFromBorrowing()
const decayedBaseRate = await troveManagerTester.baseRate()

const minutesPassed = secondsPassed / 60

const error = decayedBaseRate.sub(toBN(expectedDecayedBaseRate))

// console.log(
// `starting baseRate: ${startBaseRate},
// minutesPassed: ${minutesPassed},
// expectedDecayedBaseRate: ${expectedDecayedBaseRate},
// decayedBaseRate: ${decayedBaseRate},
// error: ${error}`
// )

assert.isAtMost(getDifference(expectedDecayedBaseRate.toString(), decayedBaseRate.toString()), 10000000) // allow absolute error tolerance of 1e-11
}
})

// --- Exponentiation tests ---

describe('Basic exponentiation', async accounts => {
Expand Down
12 changes: 3 additions & 9 deletions contracts/utils/testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class TestHelper {
* So, it adds the gas compensation and the borrowing fee
*/
static async getOpenTroveTotalDebt(contracts, boldAmount) {
const fee = await contracts.troveManager.getBorrowingFee(boldAmount);
const fee = this.toBN(0);
const compositeDebt = await this.getCompositeDebt(contracts, boldAmount);
return compositeDebt.add(fee);
}
Expand All @@ -355,18 +355,12 @@ class TestHelper {

// Subtracts the borrowing fee
static async getNetBorrowingAmount(contracts, debtWithFee) {
const borrowingRate =
await contracts.troveManager.getBorrowingRateWithDecay();

return this.toBN(debtWithFee)
.mul(MoneyValues._1e18BN)
.div(MoneyValues._1e18BN.add(borrowingRate));
return this.toBN(debtWithFee);
}

// Adds the borrowing fee
static async getAmountWithBorrowingFee(contracts, boldAmount) {
const fee = await contracts.troveManager.getBorrowingFee(boldAmount);
return boldAmount.add(fee);
return boldAmount;
}

// Adds the redemption fee
Expand Down

0 comments on commit b62169a

Please sign in to comment.