Skip to content

Commit

Permalink
fix(negative_interest): Test can now run
Browse files Browse the repository at this point in the history
- adding greatworldheight=14 in Container cmds fixed the issue.
- first test is half way completed.
  • Loading branch information
DieHard073055 committed Sep 14, 2022
1 parent dd53864 commit 2337e29
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@

import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
// import { ContainerAdapterClient } from '../../container_adapter_client'

// import { BalanceTransferPayload } from '../../../src/category/account'
// import { RegTestFoundationKeys } from '@defichain/jellyfish-network'
// import { RegTestFoundationKeys } from '@defichain/jellyfish-network'
// import { GenesisKeys, StartFlags } from '@defichain/testcontainers'
import { Testing } from '@defichain/jellyfish-testing'
import BigNumber from 'bignumber.js'
// import { TestingGroup } from '@defichain/jellyfish-testing'
// import { RpcApiError } from '@defichain/jellyfish-api-core'
// import { VaultActive, VaultLiquidation } from '../../../src/category/loan'
import { VaultActive } from '../../../src/category/loan'

// const tGroup = TestingGroup.create(2, i => new MasterNodeRegTestContainer(RegTestFoundationKeys[i]))
const testing = Testing.create(new MasterNodeRegTestContainer())

let aliceAddr: string
let bobVaultId: string
let bobVaultAddr: string
// let bobVault: VaultActive
let bobVault: VaultActive
let oracleId: string
let timestamp: number
// const netInterest = (-3 + 0) / 100 // (scheme.rate + loanToken.interest) / 100
// const blocksPerDay = (60 * 60 * 24) / (10 * 60) // 144 in regtest
const netInterest = (5 + 0) / 100 // (scheme.rate + loanToken.interest) / 100
const blocksPerDay = (60 * 60 * 24) / (10 * 60) // 144 in regtest

describe('takeLoan with negative interest success', () => {
beforeEach(async () => {
Expand All @@ -30,7 +33,6 @@ describe('takeLoan with negative interest success', () => {
afterEach(async () => {
await testing.container.stop()
})

async function setup (): Promise<void> {
// token setup
aliceAddr = await testing.container.getNewAddress()
Expand All @@ -43,18 +45,17 @@ describe('takeLoan with negative interest success', () => {
{ token: 'DFI', currency: 'USD' },
{ token: 'DUSD', currency: 'USD' }
]
oracleId = await testing.rpc.oracle.appointOracle(addr, priceFeeds, { weightage: 1 })
oracleId = await testing.rpc.oracle.appointOracle(addr, priceFeeds, {
weightage: 1
})
await testing.generate(1)
timestamp = Math.floor(new Date().getTime() / 1000)
await testing.rpc.oracle.setOracleData(
oracleId,
timestamp,
{
prices: [
{ tokenAmount: '1@DFI', currency: 'USD' },
{ tokenAmount: '1@DUSD', currency: 'USD' }
]
})
await testing.rpc.oracle.setOracleData(oracleId, timestamp, {
prices: [
{ tokenAmount: '1@DFI', currency: 'USD' },
{ tokenAmount: '1@DUSD', currency: 'USD' }
]
})
await testing.generate(1)

// collateral token
Expand All @@ -64,8 +65,6 @@ describe('takeLoan with negative interest success', () => {
fixedIntervalPriceId: 'DFI/USD'
})
await testing.generate(1)
const tokens = await testing.rpc.token.listTokens()
console.log(tokens)
// loan token
await testing.rpc.loan.setLoanToken({
symbol: 'DUSD',
Expand All @@ -82,68 +81,62 @@ describe('takeLoan with negative interest success', () => {
await testing.generate(1)

bobVaultAddr = await testing.generateAddress()
bobVaultId = await testing.rpc.loan.createVault({
bobVaultId = await testing.rpc.vault.createVault({
ownerAddress: bobVaultAddr,
loanSchemeId: 'scheme'
})
await testing.generate(1)

// deposit on active vault
await testing.rpc.loan.depositToVault({
vaultId: bobVaultId, from: aliceAddr, amount: '10000@DFI'
await testing.rpc.vault.depositToVault({
vaultId: bobVaultId,
from: aliceAddr,
amount: '10000@DFI'
})
await testing.generate(1)
// await alice.rpc.masternode.setGov({
// ATTRIBUTES: {
// 'v0/token/1/fixed_interval_price_id': 'DUSD/USD',
// 'v0/token/1/loan_minting_enabled': 'true',
// }
// })
// await alice.generate(1)
await testing.rpc.masternode.setGov({
ATTRIBUTES: {
'v0/token/1/loan_minting_interest': '-5'
}
})
await testing.generate(1)
await testing.rpc.loan.depositToVault({
vaultId: bobVaultId, from: aliceAddr, amount: '1@DUSD'
})
await testing.generate(1)

// bobVault = await testing.rpc.loan.getVault(bobVaultId) as VaultActive
// expect(bobVault.loanSchemeId).toStrictEqual('scheme')
// expect(bobVault.ownerAddress).toStrictEqual(bobVaultAddr)
// expect(bobVault.state).toStrictEqual('active')
// expect(bobVault.collateralAmounts).toStrictEqual(['10000.00000000@DFI', '1.00000000@BTC'])
// expect(bobVault.collateralValue).toStrictEqual(new BigNumber(15000))
// expect(bobVault.loanAmounts).toStrictEqual([])
// expect(bobVault.loanValue).toStrictEqual(new BigNumber(0))
// expect(bobVault.interestAmounts).toStrictEqual([])
// expect(bobVault.interestValue).toStrictEqual(new BigNumber(0))
bobVault = await testing.rpc.vault.getVault(bobVaultId) as VaultActive
expect(bobVault.loanSchemeId).toStrictEqual('scheme')
expect(bobVault.ownerAddress).toStrictEqual(bobVaultAddr)
expect(bobVault.state).toStrictEqual('active')
expect(bobVault.collateralAmounts).toStrictEqual(['10000.00000000@DFI'])
expect(bobVault.collateralValue).toStrictEqual(new BigNumber(10000))
expect(bobVault.loanAmounts).toStrictEqual([])
expect(bobVault.loanValue).toStrictEqual(new BigNumber(0))
expect(bobVault.interestAmounts).toStrictEqual([])
expect(bobVault.interestValue).toStrictEqual(new BigNumber(0))
// expect(bobVault.collateralRatio).toStrictEqual(-1) // empty loan
// expect(bobVault.informativeRatio).toStrictEqual(new BigNumber(-1)) // empty loan
expect(bobVault.informativeRatio).toStrictEqual(new BigNumber(-1)) // empty loan
}

it('should takeLoan with negative interest and accrue DUSD', async () => {
console.log('test')
// const dusdLoanAmount = 20000
// const txid = await testing.rpc.loan.takeLoan({
// vaultId: bobVaultId,
// amounts: `${dusdLoanAmount}@DUSD`
// })
// expect(typeof txid).toStrictEqual('string')
// await testing.generate(1)

// const dusdLoanHeight = await testing.container.getBlockCount()
// const interests = await testing.rpc.loan.getInterest('scheme')

// // manually calculate interest to compare rpc getInterest above is working correctly
// const height = await testing.container.getBlockCount()
// const dusdInterestPerBlock = new BigNumber((netInterest * 40) / (365 * blocksPerDay)) // netInterest * loanAmt / 365 * blocksPerDay
// expect(dusdInterestPerBlock.toFixed(8, BigNumber.ROUND_CEIL)).toStrictEqual(interests[0].interestPerBlock.toFixed(8))
// const dusdInterestTotal = dusdInterestPerBlock.multipliedBy(new BigNumber(height - dusdLoanHeight + 1))
// expect(dusdInterestTotal.toFixed(8, BigNumber.ROUND_CEIL)).toStrictEqual(interests[0].totalInterest.toFixed(8))
const dusdLoanAmount = 5000
const txid = await testing.rpc.loan.takeLoan({
vaultId: bobVaultId,
amounts: `${dusdLoanAmount}@DUSD`
})
expect(typeof txid).toStrictEqual('string')
await testing.generate(1)

const dusdLoanHeight = await testing.container.getBlockCount()
const interests = await testing.rpc.loan.getInterest('scheme')

console.log(interests[0].totalInterest.toFixed(8))
console.log(interests[0].interestPerBlock.toFixed(8))
console.log(interests[0].realizedInterestPerBlock.toFixed(8))

// manually calculate interest to compare rpc getInterest above is working correctly
const height = await testing.container.getBlockCount()
const dusdInterestPerBlock = new BigNumber((netInterest * 40) / (365 * blocksPerDay)) // netInterest * loanAmt / 365 * blocksPerDay
expect(dusdInterestPerBlock.toFixed(8, BigNumber.ROUND_CEIL)).toStrictEqual(interests[0].interestPerBlock.toFixed(8))
const dusdInterestTotal = dusdInterestPerBlock.multipliedBy(new BigNumber(height - dusdLoanHeight + 1))
expect(dusdInterestTotal.toFixed(8, BigNumber.ROUND_CEIL)).toStrictEqual(interests[0].totalInterest.toFixed(8))

// const dusdLoanAmountAfter = new BigNumber(dusdLoanAmount).plus(dusdInterestTotal).decimalPlaces(8, BigNumber.ROUND_CEIL)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export class RegTestContainer extends DeFiDContainer {
'-fortcanningroadheight=11',
'-fortcanningcrunchheight=12',
'-fortcanningspringheight=13',
'fortcanninggreatworldheight=14',
'-negativeinterest=1'
'-greatworldheight=14'
]

if (opts.startFlags != null && opts.startFlags.length > 0) {
Expand Down

0 comments on commit 2337e29

Please sign in to comment.