Skip to content

Commit

Permalink
fix(core): interest update regression post ain:2.3.x update (#870)
Browse files Browse the repository at this point in the history
* test defi/defichain:HEAD-d632b1a

* Update DeFiDContainer.ts

* Update DeFiDContainer.ts

* added fcm height

* Update DeFiDContainer.ts

* Refactored loan tests to suite for new FCM interest calculations.

* Refactored loan tests to suite for new FCM interest calculations iteration2.

* Refactored loan tests to suite for new FCM interest calculations iteration 3.

* Update DeFiDContainer.ts

* fixed getLoanInfo changes

Co-authored-by: surangap <[email protected]>
  • Loading branch information
fuxingloh and surangap authored Dec 28, 2021
1 parent 31074c3 commit 936c0b3
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export class GovernanceMasterNodeRegTestContainer extends MasterNodeRegTestConta
const cmd = super.getCmd(opts)
.filter(cmd => cmd !== '-eunospayaheight=7')
.filter(cmd => cmd !== '-fortcanningheight=8')
.filter(cmd => cmd !== '-fortcanningmuseumheight=9')

return [
...cmd,
'-fortcanningheight=9'
'-fortcanningheight=20'
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ describe('Loan getInterest', () => {
// calculate interest per block for TSLA
const netInterest = (3 + 0) / 100 // (scheme.rate + loanToken.interest) / 100
const blocksPerDay = (60 * 60 * 24) / (10 * 60) // 144 in regtest
const interestPerBlock = new BigNumber(netInterest).multipliedBy(1000).dividedBy(365 * blocksPerDay) // netInterest * loan token amount(1000) / 365 * blocksPerDay
expect(interests[0].interestPerBlock.toFixed(8)).toStrictEqual(interestPerBlock.toFixed(8, 1))
const interestPerBlock = new BigNumber(netInterest * 1000 / (365.0 * blocksPerDay)).decimalPlaces(8, BigNumber.ROUND_CEIL) // netInterest * loan token amount(1000) / 365 * blocksPerDay
expect(interests[0].interestPerBlock.toFixed(8)).toStrictEqual(interestPerBlock.toFixed(8)) // NOTE(sp): AIN use std::ceil(InterestPerBlockFloat()) after FCM hardfork, when storing the per block interest rate in DB

// calculate total interest
const blockHeight = await testing.rpc.blockchain.getBlockCount()
const totalInterest = interestPerBlock.multipliedBy(blockHeight + 1 - interestTSLABlockHeight)
const totalInterest = interestPerBlock.multipliedBy(blockHeight + 1 - interestTSLABlockHeight) // interestPerBlock is ceiled before multiplying with the height.
expect(interests[0].totalInterest.toFixed(8)).toStrictEqual(totalInterest.toFixed(8))
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ describe('Loan - getLoanInfo', () => {
}
})

// combine tests for 2 fields, only collateral deposited vault counted, empty vault is not
it('should count total open vaults and total collateral deposited', async () => {
it('should count total open vaults (deposited and empty) and total collateral deposited', async () => {
// extra preps
await testing.rpc.loan.setCollateralToken({
token: 'DFI',
Expand Down Expand Up @@ -319,7 +318,7 @@ describe('Loan - getLoanInfo', () => {
collateralTokens: new BigNumber(1),
collateralValue: new BigNumber(10000),
schemes: new BigNumber(1),
openVaults: new BigNumber(1) // unchanged
openVaults: new BigNumber(2) // unchanged
}
})
}
Expand Down Expand Up @@ -552,7 +551,7 @@ describe('Loan - getLoanInfo', () => {
totals: {
...extendedStartingData.totals,
// vault liquidated
openVaults: new BigNumber(0),
openVaults: new BigNumber(1),
collateralValue: new BigNumber(0),
openAuctions: new BigNumber(1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ describe('Loan getVault', () => {
collateralAmounts: ['10000.00000000@DFI', '1.00000000@BTC'],
// 30 TSLA + total interest
loanAmounts: [new BigNumber(30).plus(interestInfo[0].totalInterest).toFixed(8) + '@TSLA'], // 30.00000570@TSLA
interestAmounts: ['0.00000570@TSLA'],
interestAmounts: ['0.00000571@TSLA'],
// (10000 DFI * DFIUSD Price * DFI collaterization factor 1) + (1BTC * BTCUSD Price * BTC collaterization factor 0.5)
collateralValue: new BigNumber(10000 * 1 * 1).plus(new BigNumber(1 * 10000 * 0.5)),
// (30 TSLA + total interest) * TSLAUSD Price
loanValue: new BigNumber(30).plus(interestInfo[0].totalInterest).multipliedBy(2),
interestValue: new BigNumber(0.0000114),
interestValue: new BigNumber(0.00001142),
// lround ((collateral value / loan value) * 100)
collateralRatio: Math.ceil(informativeRatio.toNumber()), // 25000
informativeRatio: new BigNumber(informativeRatio.toFixed(5)) // 24999.995250000902 -> 24999.99525
informativeRatio: new BigNumber(informativeRatio.toFixed(8, BigNumber.ROUND_DOWN)) // 24999.995241667572335939 -> 24999.99524166
})
})

Expand Down Expand Up @@ -209,7 +209,7 @@ describe('Loan getVault', () => {
'0.66666666@BTC'
],
index: 0,
loan: '20.00004539@TSLA',
loan: '20.00004547@TSLA',
highestBid: {
amount: '40.00000000@TSLA',
owner: collateralAddress
Expand All @@ -221,7 +221,7 @@ describe('Loan getVault', () => {
'0.33333334@BTC'
],
index: 1,
loan: '10.00002301@TSLA'
loan: '10.00002305@TSLA'
}
]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('Loan listAuctions', () => {
'0.33333333@BTC'
],
index: 0,
loan: '5000.01992715@AAPL',
loan: '5000.01992729@AAPL',
highestBid: {
amount: '5252.00000000@AAPL',
owner: collateralAddress
Expand All @@ -316,7 +316,7 @@ describe('Loan listAuctions', () => {
'0.16666667@BTC'
],
index: 1,
loan: '2500.01003858@AAPL'
loan: '2500.01003866@AAPL'
}
],
loanSchemeId: result?.loanSchemeId,
Expand Down
Loading

0 comments on commit 936c0b3

Please sign in to comment.