diff --git a/packages/jellyfish-api-core/__tests__/category/loan/negativeInterest.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/negativeInterest.test.ts index 266696e09d..287e5b9d82 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/negativeInterest.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/negativeInterest.test.ts @@ -1,12 +1,15 @@ - 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()) @@ -14,11 +17,11 @@ 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 () => { @@ -30,7 +33,6 @@ describe('takeLoan with negative interest success', () => { afterEach(async () => { await testing.container.stop() }) - async function setup (): Promise { // token setup aliceAddr = await testing.container.getNewAddress() @@ -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 @@ -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', @@ -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) diff --git a/packages/testcontainers/src/containers/RegTestContainer/index.ts b/packages/testcontainers/src/containers/RegTestContainer/index.ts index 036c9defef..17c0c05452 100644 --- a/packages/testcontainers/src/containers/RegTestContainer/index.ts +++ b/packages/testcontainers/src/containers/RegTestContainer/index.ts @@ -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) {