From 5e89a4bd7c66c00e8b2bbf0b6f2a148a64916786 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Fri, 29 Oct 2021 05:17:40 +0800 Subject: [PATCH] Fix changes on HEAD-7ef68b5 (#781) * fix conflicts * refix vault new resp * revert getvault precision * better get/list vault resp type * fix vault type * undefined if err * remove txn loan payback * remain 7ef68b5 * rm txn loanpack * rm export on md * diff get list vault resp * Remove extra break line * fix updateloantoken dftx char Co-authored-by: jingyi2811 --- docs/node/RPC Category/16-loan.md | 73 +++++++++---------- .../category/loan/closeVault.test.ts | 2 +- .../category/loan/createVault.test.ts | 10 +-- .../category/loan/depositToVault.test.ts | 6 +- .../__tests__/category/loan/getVault.test.ts | 28 +++---- .../category/loan/loanPayback.test.ts | 22 +++--- .../__tests__/category/loan/takeLoan.test.ts | 4 +- .../jellyfish-api-core/src/category/loan.ts | 66 +++++++++++------ .../txn/txn_builder_loan_create_vault.test.ts | 15 ++-- .../txn_builder_loan_deposit_to_vault.test.ts | 2 +- .../txn/txn_builder_loan_take_loan.test.ts | 2 +- .../script/dftx/dftx_loans/TakeLoan.test.ts | 10 +-- .../dftx/dftx_loans/UpdateLoanToken.test.ts | 10 +-- .../src/script/dftx/dftx_loans.ts | 4 +- .../RegTestContainer/ContainerGroup.ts | 2 +- .../RegTestContainer/LoanContainer.ts | 2 +- 16 files changed, 135 insertions(+), 123 deletions(-) diff --git a/docs/node/RPC Category/16-loan.md b/docs/node/RPC Category/16-loan.md index 4dd4511ee6..6a5d6af00c 100644 --- a/docs/node/RPC Category/16-loan.md +++ b/docs/node/RPC Category/16-loan.md @@ -358,20 +358,31 @@ interface loan { getVault (vaultId: string): Promise } +enum VaultState { + UNKNOWN = 'unknown', + ACTIVE = 'active', + IN_LIQUIDATION = 'inliquidation', + FROZEN = 'frozen', + MAY_LIQUIDATE = 'mayliquidate', + FROZEN_IN_LIQUIDATION = 'lockedinliquidation' +} + interface VaultDetails { vaultId: string loanSchemeId: string ownerAddress: string - isUnderLiquidation: boolean - invalidPrice: boolean + state: VaultState + liquidationHeight?: number + liquidationPenalty?: number + batchCount?: number batches?: AuctionBatchDetails[] collateralAmounts?: string[] loanAmounts?: string[] interestAmounts?: string[] collateralValue?: BigNumber loanValue?: BigNumber - interestValue?: BigNumber, - currentRatio?: BigNumber + interestValue?: BigNumber | string // empty string if nothing + currentRatio?: number } interface AuctionBatchDetails { @@ -381,29 +392,22 @@ interface AuctionBatchDetails { } ``` -## closeVault +## listVaults -Close vault. +List all available vaults. -```ts title="client.loan.closeVault()" +```ts title="client.loan.listVaults()" interface loan { - closeVault (closeVault: CloseVault, utxos: UTXO[] = []): Promise + listVaults (pagination: VaultPagination = {}, options: ListVaultOptions = {}): Promise } -interface CloseVault { +interface ListVaultDetails { vaultId: string - to: string + loanSchemeId: string + ownerAddress: string + isUnderLiquidation: boolean } -``` - -## listVaults - -List all available vaults. -```ts title="client.loan.listVaults()" -interface loan { - listVaults (pagination: VaultPagination = {}, options: ListVaultOptions = {}): Promise -} interface ListVaultOptions { ownerAddress?: string @@ -416,27 +420,20 @@ interface VaultPagination { including_start?: boolean limit?: number } +``` -interface VaultDetails { - vaultId: string - loanSchemeId: string - ownerAddress: string - isUnderLiquidation: boolean - invalidPrice: boolean - batches?: AuctionBatchDetails[] - collateralAmounts?: string[] - loanAmounts?: string[] - interestAmounts?: string[] - collateralValue?: BigNumber - loanValue?: BigNumber - interestValue?: BigNumber, - currentRatio?: BigNumber +## closeVault + +Close vault. + +```ts title="client.loan.closeVault()" +interface loan { + closeVault (closeVault: CloseVault, utxos: UTXO[] = []): Promise } -interface AuctionBatchDetails { - index: BigNumber - collaterals: string[] - loan: string +interface CloseVault { + vaultId: string + to: string } ``` @@ -491,7 +488,7 @@ interface loan { loanPayback (metadata: LoanPaybackMetadata, utxos: UTXO[] = []): Promise } -export interface LoanPaybackMetadata { +interface LoanPaybackMetadata { vaultId: string amounts: string | string[] // amount@symbol from: string diff --git a/packages/jellyfish-api-core/__tests__/category/loan/closeVault.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/closeVault.test.ts index 6628fe023c..f485714304 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/closeVault.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/closeVault.test.ts @@ -250,7 +250,7 @@ describe('Loan', () => { it('should not closeVault for liquidated vault', async () => { const liqVault = await tGroup.get(0).container.call('getvault', [vaultWithLiquidationId]) - expect(liqVault.isUnderLiquidation).toStrictEqual(true) + expect(liqVault.state).toStrictEqual('inliquidation') const promise = tGroup.get(0).rpc.loan.closeVault({ vaultId: vaultWithLiquidationId, to: await tGroup.get(0).generateAddress() }) await expect(promise).rejects.toThrow('RpcApiError: \'Vault is under liquidation.\', code: -26, method: closevault') diff --git a/packages/jellyfish-api-core/__tests__/category/loan/createVault.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/createVault.test.ts index 39f278dd91..964ec22dff 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/createVault.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/createVault.test.ts @@ -2,6 +2,7 @@ import { LoanMasterNodeRegTestContainer } from './loan_container' import BigNumber from 'bignumber.js' import { Testing } from '@defichain/jellyfish-testing' import { GenesisKeys } from '@defichain/testcontainers' +import { VaultState } from '../../../src/category/loan' describe('Loan createVault', () => { const container = new LoanMasterNodeRegTestContainer() @@ -48,8 +49,7 @@ describe('Loan createVault', () => { vaultId: vaultId, loanSchemeId: 'scheme', ownerAddress: ownerAddress, - isUnderLiquidation: false, - invalidPrice: false, + state: VaultState.ACTIVE, collateralAmounts: [], loanAmounts: [], interestAmounts: [], @@ -76,8 +76,7 @@ describe('Loan createVault', () => { vaultId: vaultId, loanSchemeId: 'default', // Get default loan scheme ownerAddress: ownerAddress, - isUnderLiquidation: false, - invalidPrice: false, + state: VaultState.ACTIVE, collateralAmounts: [], loanAmounts: [], interestAmounts: [], @@ -125,8 +124,7 @@ describe('Loan createVault', () => { vaultId: vaultId, loanSchemeId: 'scheme', ownerAddress: GenesisKeys[0].owner.address, - isUnderLiquidation: false, - invalidPrice: false, + state: VaultState.ACTIVE, collateralAmounts: [], loanAmounts: [], interestAmounts: [], diff --git a/packages/jellyfish-api-core/__tests__/category/loan/depositToVault.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/depositToVault.test.ts index 515ee8f01a..00238e13cd 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/depositToVault.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/depositToVault.test.ts @@ -170,7 +170,7 @@ describe('Loan depositToVault', () => { const vaultBefore = await tGroup.get(0).container.call('getvault', [vaultId]) expect(vaultBefore.loanSchemeId).toStrictEqual('scheme') expect(vaultBefore.ownerAddress).toStrictEqual(vaultAddress) - expect(vaultBefore.isUnderLiquidation).toStrictEqual(false) + expect(vaultBefore.state).toStrictEqual('active') expect(vaultBefore.loanAmounts).toStrictEqual([]) expect(vaultBefore.loanValue).toStrictEqual(0) expect(vaultBefore.currentRatio).toStrictEqual(-1) // empty loan @@ -191,7 +191,7 @@ describe('Loan depositToVault', () => { // check the changes after deposit expect(vaultAfter.loanSchemeId).toStrictEqual(vaultBefore.loanSchemeId) expect(vaultAfter.ownerAddress).toStrictEqual(vaultBefore.ownerAddress) - expect(vaultAfter.isUnderLiquidation).toStrictEqual(vaultBefore.isUnderLiquidation) + expect(vaultAfter.state).toStrictEqual(vaultBefore.state) expect(vaultAfter.loanAmounts).toStrictEqual(vaultBefore.loanAmounts) expect(vaultAfter.loanValue).toStrictEqual(vaultBefore.loanValue) expect(vaultAfter.currentRatio).toStrictEqual(vaultBefore.currentRatio) @@ -319,7 +319,7 @@ describe('Loan depositToVault', () => { await tGroup.get(0).generate(6) const liqVault = await tGroup.get(0).container.call('getvault', [liqVaultId]) - expect(liqVault.isUnderLiquidation).toStrictEqual(true) + expect(liqVault.state).toStrictEqual('inliquidation') const promise = tGroup.get(0).rpc.loan.depositToVault({ vaultId: liqVaultId, from: collateralAddress, amount: '1000@DFI' diff --git a/packages/jellyfish-api-core/__tests__/category/loan/getVault.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/getVault.test.ts index df085c8468..27b46d6c8b 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/getVault.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/getVault.test.ts @@ -1,6 +1,7 @@ import { LoanMasterNodeRegTestContainer } from './loan_container' import { Testing } from '@defichain/jellyfish-testing' import BigNumber from 'bignumber.js' +import { VaultState } from '../../../src/category/loan' describe('Loan getVault', () => { const container = new LoanMasterNodeRegTestContainer() @@ -72,15 +73,14 @@ describe('Loan getVault', () => { vaultId: vaultId, loanSchemeId: 'default', // Get default loan scheme ownerAddress: ownerAddress, - isUnderLiquidation: false, - invalidPrice: false, + state: VaultState.ACTIVE, collateralAmounts: [], loanAmounts: [], interestAmounts: [], collateralValue: expect.any(BigNumber), loanValue: expect.any(BigNumber), interestValue: '', - currentRatio: expect.any(BigNumber) + currentRatio: expect.any(Number) }) }) @@ -99,8 +99,7 @@ describe('Loan getVault', () => { vaultId: vaultId, loanSchemeId: 'default', // Get default loan scheme ownerAddress: ownerAddress, - isUnderLiquidation: false, - invalidPrice: false, + state: VaultState.ACTIVE, collateralAmounts: ['10000.00000000@DFI', '1.00000000@BTC'], loanAmounts: [], interestAmounts: [], @@ -108,7 +107,7 @@ describe('Loan getVault', () => { collateralValue: new BigNumber(10000 * 1 * 1).plus(new BigNumber(1 * 10000 * 0.5)), loanValue: new BigNumber(0), interestValue: '', - currentRatio: new BigNumber(-1) + currentRatio: -1 }) }) @@ -130,14 +129,13 @@ describe('Loan getVault', () => { const interestInfo: any = await testing.rpc.call('getinterest', ['default', 'TSLA'], 'bignumber') const data = await testing.rpc.loan.getVault(vaultId) - const currentRatioValue: string = data.collateralValue?.dividedBy(data.loanValue as BigNumber).multipliedBy(100).toFixed(0, 4) as string + const currentRatioValue: number = data.collateralValue?.dividedBy(data.loanValue as BigNumber).multipliedBy(100).toNumber() as number expect(data).toStrictEqual({ vaultId: vaultId, loanSchemeId: 'default', // Get default loan scheme ownerAddress: ownerAddress, - isUnderLiquidation: false, - invalidPrice: false, + state: VaultState.ACTIVE, collateralAmounts: ['10000.00000000@DFI', '1.00000000@BTC'], // 30 TSLA + total interest loanAmounts: [new BigNumber(30).plus(interestInfo[0].totalInterest).toFixed(8) + '@TSLA'], // 30.00001140@TSLA @@ -148,7 +146,7 @@ describe('Loan getVault', () => { loanValue: new BigNumber(30).plus(interestInfo[0].totalInterest).multipliedBy(2), interestValue: new BigNumber(0.0000114), // lround ((collateral value / loan value) * 100) - currentRatio: `${currentRatioValue}%` + currentRatio: Math.ceil(currentRatioValue) }) }) @@ -168,7 +166,7 @@ describe('Loan getVault', () => { // check vault not under liquidation. const data = await testing.rpc.loan.getVault(vaultId) - expect(data.isUnderLiquidation).toStrictEqual(false) + expect(data.state).toStrictEqual(VaultState.ACTIVE) // make vault enter under liquidation state by a price hike of the loan token const timestamp = Math.floor(new Date().getTime() / 1000) @@ -176,15 +174,17 @@ describe('Loan getVault', () => { await testing.generate(12) // Wait for 12 blocks which are equivalent to 2 hours (1 block = 10 minutes) in order to liquidate the vault // get auction details - const autionDetails: [] = await testing.rpc.call('listauctions', [], 'bignumber') + const autionDetails: [] = await testing.container.call('listauctions') const vaultDataAfterPriceHike = await testing.rpc.loan.getVault(vaultId) expect(vaultDataAfterPriceHike).toStrictEqual({ vaultId: vaultId, + liquidationHeight: 168, + liquidationPenalty: 5, loanSchemeId: 'default', // Get default loan scheme ownerAddress: ownerAddress, - isUnderLiquidation: true, - invalidPrice: false, + state: VaultState.IN_LIQUIDATION, + batchCount: 2, batches: autionDetails.filter((auction: {vaultId: string}) => auction.vaultId === vaultId).map((auction: {batches: []}) => auction.batches)[0] }) diff --git a/packages/jellyfish-api-core/__tests__/category/loan/loanPayback.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/loanPayback.test.ts index e1d3faa369..138e11d931 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/loanPayback.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/loanPayback.test.ts @@ -282,7 +282,7 @@ describe('loanPayback success', () => { expect(vaultBefore.loanValue).toStrictEqual(80.00009132) expect(vaultBefore.interestAmounts).toStrictEqual(['0.00004566@TSLA']) expect(vaultBefore.interestValue).toStrictEqual(0.00009132) - expect(vaultBefore.currentRatio).toStrictEqual('18750%') + expect(vaultBefore.currentRatio).toStrictEqual(18750) const bobLoanAccBefore = await bob.rpc.account.getAccount(bobloanAddr) expect(bobLoanAccBefore).toStrictEqual(['45.00000000@TSLA']) @@ -318,7 +318,7 @@ describe('loanPayback success', () => { expect(vaultBefore.interestAmounts).toStrictEqual(['0.00002283@TSLA']) expect(vaultBefore.loanValue).toStrictEqual(80.00004566) // loanAmount * 2 (::1 TSLA = 2 USD) expect(vaultBefore.interestValue).toStrictEqual(0.00004566) - expect(vaultBefore.currentRatio).toStrictEqual('18750%') // 15000 / 80.00004566 * 100 + expect(vaultBefore.currentRatio).toStrictEqual(18750) // 15000 / 80.00004566 * 100 { const interests = await bob.rpc.loan.getInterest('scheme') @@ -356,7 +356,7 @@ describe('loanPayback success', () => { expect(vaultAfter.interestAmounts).toStrictEqual(['0.00001541@TSLA']) expect(vaultAfter.loanValue).toStrictEqual(54.00007648) // 27.00003824 * 2 (::1 TSLA = 2 USD) expect(vaultAfter.interestValue).toStrictEqual(0.00003082) - expect(vaultAfter.currentRatio).toStrictEqual('27778%') // 15000 / 54.00007648 * 100 + expect(vaultAfter.currentRatio).toStrictEqual(27778) // 15000 / 54.00007648 * 100 const burnInfoAfter = await bob.container.call('getburninfo') expect(burnInfoAfter.paybackburn).toStrictEqual(0.00000457) @@ -372,7 +372,7 @@ describe('loanPayback success', () => { expect(vaultBefore.interestAmounts).toStrictEqual(['0.00002283@TSLA']) expect(vaultBefore.loanValue).toStrictEqual(80.00004566) // loanAmount * 2 (::1 TSLA = 2 USD) expect(vaultBefore.interestValue).toStrictEqual(0.00004566) - expect(vaultBefore.currentRatio).toStrictEqual('18750%') // 15000 / 80.0000456 * 100 + expect(vaultBefore.currentRatio).toStrictEqual(18750) // 15000 / 80.0000456 * 100 const txid = await alice.rpc.loan.loanPayback({ vaultId: bobVaultId, @@ -390,7 +390,7 @@ describe('loanPayback success', () => { expect(vaultAfter.interestAmounts).toStrictEqual(['0.00001827@TSLA']) expect(vaultAfter.loanValue).toStrictEqual(64.0000822) // 32.0000411 * 2 (::1 TSLA = 2 USD) expect(vaultAfter.interestValue).toStrictEqual(0.00003654) - expect(vaultAfter.currentRatio).toStrictEqual('23437%') // 15000 / 64.00016436 * 100 + expect(vaultAfter.currentRatio).toStrictEqual(23437) // 15000 / 64.00016436 * 100 }) it('should loanPayback more than one amount', async () => { @@ -443,7 +443,7 @@ describe('loanPayback success', () => { expect(vaultBefore.loanAmounts).toStrictEqual(['40.00004566@TSLA', '15.00000856@AMZN']) // eg: tslaTakeLoanAmt + tslaTotalInterest expect(vaultBefore.interestAmounts).toStrictEqual(['0.00004566@TSLA', '0.00000856@AMZN']) expect(vaultBefore.loanValue).toStrictEqual(140.00012556) // (40.00004566 * 2) + (15.00009856 * 4) - expect(vaultBefore.currentRatio).toStrictEqual('10714%') // 15000 / 140.00012556 * 100 + expect(vaultBefore.currentRatio).toStrictEqual(10714) // 15000 / 140.00012556 * 100 const txid = await bob.rpc.loan.loanPayback({ vaultId: bobVaultId, @@ -458,7 +458,7 @@ describe('loanPayback success', () => { expect(vaultAfter.interestAmounts).toStrictEqual(['0.00001541@TSLA', '0.00000514@AMZN']) expect(vaultAfter.loanValue).toStrictEqual(90.00017694) expect(vaultAfter.interestValue).toStrictEqual(0.00005138) - expect(vaultAfter.currentRatio).toStrictEqual('16667%') + expect(vaultAfter.currentRatio).toStrictEqual(16667) const loanTokenAccAfter = await bob.container.call('getaccount', [bobloanAddr]) expect(loanTokenAccAfter).toStrictEqual(['27.00000000@TSLA', '9.00000000@AMZN']) @@ -496,7 +496,7 @@ describe('loanPayback success', () => { expect(vaultAfter.interestAmounts).toStrictEqual(['0.00000799@TSLA', '0.00000172@AMZN']) expect(vaultAfter.loanValue).toStrictEqual(40.0001998) expect(vaultAfter.interestValue).toStrictEqual(0.00002286) - expect(vaultAfter.currentRatio).toStrictEqual('37500%') + expect(vaultAfter.currentRatio).toStrictEqual(37500) const loanTokenAccAfter = await bob.container.call('getaccount', [bobloanAddr]) expect(loanTokenAccAfter).toStrictEqual(['14.00000000@TSLA', '3.00000000@AMZN']) // (27 - 13), (9 - 6) @@ -512,7 +512,7 @@ describe('loanPayback success', () => { expect(vaultBefore.interestAmounts).toStrictEqual(['0.00002283@TSLA']) expect(vaultBefore.loanValue).toStrictEqual(80.00004566) // loanAmount * 2 (::1 TSLA = 2 USD) expect(vaultBefore.interestValue).toStrictEqual(0.00004566) - expect(vaultBefore.currentRatio).toStrictEqual('18750%') // 15000 / 80.00004566 * 100 + expect(vaultBefore.currentRatio).toStrictEqual(18750) // 15000 / 80.00004566 * 100 const utxo = await bob.container.fundAddress(bobloanAddr, 250) @@ -529,7 +529,7 @@ describe('loanPayback success', () => { expect(vaultAfter.interestAmounts).toStrictEqual(['0.00001541@TSLA']) expect(vaultAfter.loanValue).toStrictEqual(54.00012214) // 27.00006107 * 2 (::1 TSLA = 2 USD) expect(vaultAfter.interestValue).toStrictEqual(0.00003082) - expect(vaultAfter.currentRatio).toStrictEqual('27778%') // 15000 / 54.00012214 * 100 + expect(vaultAfter.currentRatio).toStrictEqual(27778) // 15000 / 54.00012214 * 100 const rawtx = await bob.container.call('getrawtransaction', [txid, true]) expect(rawtx.vin[0].txid).toStrictEqual(utxo.txid) @@ -646,7 +646,7 @@ describe('loanPayback failed', () => { await tGroup.get(0).generate(6) const liqVault = await bob.container.call('getvault', [bobLiqVaultId]) - expect(liqVault.isUnderLiquidation).toStrictEqual(true) + expect(liqVault.state).toStrictEqual('inliquidation') const promise = bob.rpc.loan.loanPayback({ vaultId: bobLiqVaultId, diff --git a/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts b/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts index a3ec904892..416244e8b6 100644 --- a/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/loan/takeLoan.test.ts @@ -179,7 +179,7 @@ describe('Loan takeLoan', () => { const vaultBefore = await tGroup.get(0).container.call('getvault', [vaultId]) expect(vaultBefore.loanSchemeId).toStrictEqual('scheme') expect(vaultBefore.ownerAddress).toStrictEqual(vaultAddress) - expect(vaultBefore.isUnderLiquidation).toStrictEqual(false) + expect(vaultBefore.state).toStrictEqual('active') expect(vaultBefore.collateralAmounts).toStrictEqual(['10000.00000000@DFI', '1.00000000@BTC']) expect(vaultBefore.collateralValue).toStrictEqual(15000) expect(vaultBefore.loanAmounts).toStrictEqual([]) @@ -210,7 +210,7 @@ describe('Loan takeLoan', () => { const loanAmounts = new BigNumber(40).plus(interestInfo[0].totalInterest) expect(vaultAfter.loanAmounts).toStrictEqual([loanAmounts.toFixed(8) + '@TSLA']) // 40.00002283@TSLA expect(vaultAfter.loanValue).toStrictEqual(loanAmounts.multipliedBy(2).toNumber()) - expect(vaultAfter.currentRatio).toStrictEqual(`${Math.round(vaultAfter.collateralValue / vaultAfter.loanValue * 100)}%`) + expect(vaultAfter.currentRatio).toStrictEqual(Math.round(vaultAfter.collateralValue / vaultAfter.loanValue * 100)) const vaultAfterTSLAAcc = vaultAfter.loanAmounts.find((amt: string) => amt.split('@')[1] === 'TSLA') const vaultAfterTSLAAmt = Number(vaultAfterTSLAAcc.split('@')[0]) diff --git a/packages/jellyfish-api-core/src/category/loan.ts b/packages/jellyfish-api-core/src/category/loan.ts index b59e5fb251..3f5881f662 100644 --- a/packages/jellyfish-api-core/src/category/loan.ts +++ b/packages/jellyfish-api-core/src/category/loan.ts @@ -223,7 +223,28 @@ export class Loan { * @return {Promise} */ async getVault (vaultId: string): Promise { - return await this.client.call('getvault', [vaultId], 'bignumber') + return await this.client.call( + 'getvault', + [vaultId], + { collateralValue: 'bignumber', loanValue: 'bignumber', interestValue: 'bignumber' } + ) + } + + /** + * List all available vaults. + * + * @param {VaultPagination} [pagination] + * @param {string} [pagination.start] + * @param {boolean} [pagination.including_start] + * @param {number} [pagination.limit=100] + * @param {ListVaultOptions} [options] + * @param {string} [options.ownerAddress] Address of the vault owner + * @param {string} [options.loanSchemeId] Vault's loan scheme id + * @param {boolean} [options.isUnderLiquidation = false] vaults under liquidation + * @return {Promise} Array of objects including details of the vaults. + */ + async listVaults (pagination: VaultPagination = {}, options: ListVaultOptions = {}): Promise { + return await this.client.call('listvaults', [options, pagination], 'number') } /** @@ -241,23 +262,6 @@ export class Loan { return await this.client.call('closevault', [closeVault.vaultId, closeVault.to, utxos], 'number') } - /** - * List all available vaults. - * - * @param {VaultPagination} [pagination] - * @param {string} [pagination.start] - * @param {boolean} [pagination.including_start] - * @param {number} [pagination.limit=100] - * @param {ListVaultOptions} [options] - * @param {string} [options.ownerAddress] Address of the vault owner - * @param {string} [options.loanSchemeId] Vault's loan scheme id - * @param {boolean} [options.isUnderLiquidation = false] vaults under liquidation - * @return {Promise} Array of objects including details of the vaults. - */ - async listVaults (pagination: VaultPagination = {}, options: ListVaultOptions = {}): Promise { - return await this.client.call('listvaults', [options, pagination], 'bignumber') - } - /** * Deposit to vault * @@ -391,20 +395,31 @@ export interface CreateVault { loanSchemeId?: string } +export enum VaultState { + UNKNOWN = 'unknown', + ACTIVE = 'active', + IN_LIQUIDATION = 'inliquidation', + FROZEN = 'frozen', + MAY_LIQUIDATE = 'mayliquidate', + FROZEN_IN_LIQUIDATION = 'lockedinliquidation' +} + export interface VaultDetails { vaultId: string loanSchemeId: string ownerAddress: string - isUnderLiquidation: boolean - invalidPrice: boolean + state: VaultState + liquidationHeight?: number + liquidationPenalty?: number + batchCount?: number batches?: AuctionBatchDetails[] collateralAmounts?: string[] loanAmounts?: string[] interestAmounts?: string[] collateralValue?: BigNumber loanValue?: BigNumber - interestValue?: BigNumber - currentRatio?: BigNumber + interestValue?: BigNumber | string // empty string if nothing + currentRatio?: number } export interface AuctionBatchDetails { @@ -413,6 +428,13 @@ export interface AuctionBatchDetails { loan: string } +export interface ListVaultDetails { + vaultId: string + loanSchemeId: string + ownerAddress: string + isUnderLiquidation: boolean +} + export interface UTXO { txid: string vout: number diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_create_vault.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_create_vault.test.ts index b4365f3223..3e368fd9c4 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_create_vault.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_create_vault.test.ts @@ -79,8 +79,7 @@ describe('loans.createVault', () => { vaultId: txid, loanSchemeId: 'scheme', ownerAddress: await providers.getAddress(), - isUnderLiquidation: false, - invalidPrice: false, + state: 'active', collateralAmounts: [], loanAmounts: [], interestAmounts: [], @@ -115,8 +114,7 @@ describe('loans.createVault', () => { vaultId: txid, loanSchemeId: 'default', ownerAddress: await providers.getAddress(), - isUnderLiquidation: false, - invalidPrice: false, + state: 'active', collateralAmounts: [], loanAmounts: [], interestAmounts: [], @@ -151,8 +149,7 @@ describe('loans.createVault', () => { vaultId: txid, loanSchemeId: 'scheme2', ownerAddress: await providers.getAddress(), - isUnderLiquidation: false, - invalidPrice: false, + state: 'active', collateralAmounts: [], loanAmounts: [], interestAmounts: [], @@ -186,8 +183,7 @@ describe('loans.createVault', () => { vaultId: txid2, loanSchemeId: 'scheme2', ownerAddress: await providers.getAddress(), - isUnderLiquidation: false, - invalidPrice: false, + state: 'active', collateralAmounts: [], loanAmounts: [], interestAmounts: [], @@ -230,8 +226,7 @@ describe('loans.createVault', () => { vaultId: txid, loanSchemeId: 'scheme', ownerAddress: 'bcrt1q0uajendn9xpv87jnsqgjmlad3fne9waf9sxckc', - isUnderLiquidation: false, - invalidPrice: false, + state: 'active', collateralAmounts: [], loanAmounts: [], interestAmounts: [], diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_deposit_to_vault.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_deposit_to_vault.test.ts index 3a70fac699..a097d09ae4 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_deposit_to_vault.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_deposit_to_vault.test.ts @@ -343,7 +343,7 @@ describe('loans.depositToVault', () => { await tGroup.get(0).generate(6) const liqVault = await tGroup.get(0).rpc.loan.getVault(liqVaultId) - expect(liqVault.isUnderLiquidation).toStrictEqual(true) + expect(liqVault.state).toStrictEqual('inliquidation') const script = await providers.elliptic.script() const txn = await builder.loans.depositToVault({ diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_take_loan.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_take_loan.test.ts index e83b6f798c..2476330759 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_take_loan.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_loan_take_loan.test.ts @@ -458,7 +458,7 @@ describe('loans.takeLoan', () => { it('should not takeLoan on liquidation vault', async () => { const liqVault = await tGroup.get(0).rpc.loan.getVault(liqVaultId) - expect(liqVault.isUnderLiquidation).toStrictEqual(true) + expect(liqVault.state).toStrictEqual('inliquidation') // fund if UTXO is not available for fees await fundForFeesIfUTXONotAvailable(10) diff --git a/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/TakeLoan.test.ts b/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/TakeLoan.test.ts index 82ec0ec9c2..f8106b05eb 100644 --- a/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/TakeLoan.test.ts +++ b/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/TakeLoan.test.ts @@ -16,14 +16,14 @@ it('should bi-directional buffer-object-buffer', () => { tokenAmounts: [{'token': 2, 'amount': new BigNumber(40)}] } */ - '6a3344665478461690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f00010200000000286bee00000000', + '6a3344665478581690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f00010200000000286bee00000000', /** * TakeLoan : { vaultId: '6f5bf563700e008f56301fed8cc6729ff334f37d2ced46f33557fe4c5aa49016', tokenAmounts: [{'token': 2, 'amount': new BigNumber(40)}, {'token': 3, 'amount': new BigNumber(40)}] } */ - '6a3f44665478461690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f00020200000000286bee000000000300000000286bee00000000', + '6a3f44665478581690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f00020200000000286bee000000000300000000286bee00000000', /** * TakeLoan : { vaultId: '6f5bf563700e008f56301fed8cc6729ff334f37d2ced46f33557fe4c5aa49016', @@ -31,7 +31,7 @@ it('should bi-directional buffer-object-buffer', () => { tokenAmounts: [{'token': 2, 'amount': new BigNumber(40)}] } */ - '6a4944665478461690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f1600149d04d9764bdd97432f13411fe9753f808dc27926010200000000286bee00000000' + '6a4944665478581690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f1600149d04d9764bdd97432f13411fe9753f808dc27926010200000000286bee00000000' ] fixtures.forEach(hex => { @@ -40,11 +40,11 @@ it('should bi-directional buffer-object-buffer', () => { ) const buffer = toBuffer(stack) expect(buffer.toString('hex')).toStrictEqual(hex) - expect((stack[1] as OP_DEFI_TX).tx.type).toStrictEqual(0x46) + expect((stack[1] as OP_DEFI_TX).tx.type).toStrictEqual(0x58) }) }) -const header = '6a494466547846' // OP_RETURN(0x6a) (length 73 = 0x49) CDfTx.SIGNATURE(0x44665478) CTakeLoan.OP_CODE(0x46) +const header = '6a494466547858' // OP_RETURN(0x6a) (length 73 = 0x49) CDfTx.SIGNATURE(0x44665478) CTakeLoan.OP_CODE(0x58) // TakeLoan.vaultId[LE](0x1690a45a4cfe5735f346ed2c7df334f39f72c68ced1f30568f000e7063f55b6f) // TakeLoan.to(0x1600149d04d9764bdd97432f13411fe9753f808dc27926) // TakeLoan.tokenAmounts(0x010200000000286bee00000000) diff --git a/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/UpdateLoanToken.test.ts b/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/UpdateLoanToken.test.ts index 144488ebbb..4c9c620585 100644 --- a/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/UpdateLoanToken.test.ts +++ b/packages/jellyfish-transaction/__tests__/script/dftx/dftx_loans/UpdateLoanToken.test.ts @@ -20,7 +20,7 @@ it('should bi-directional buffer-object-buffer', () => { * tokenTx: '207fb6dca77e46e58506bfa1a186d1b0c8c183e89a9f7296f58b06d1007a3d13', * } */ - '6a47446654786606546f6b656e3206546f6b656e3206546f6b656e3203555344010000000000000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20', + '6a47446654787806546f6b656e3206546f6b656e3206546f6b656e3203555344010000000000000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20', /** * loan : { * symbol: 'Token3', @@ -31,7 +31,7 @@ it('should bi-directional buffer-object-buffer', () => { * tokenTx: '207fb6dca77e46e58506bfa1a186d1b0c8c183e89a9f7296f58b06d1007a3d13', * } */ - '6a47446654786606546f6b656e3306546f6b656e3306546f6b656e3303555344000000000000000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20', + '6a47446654787806546f6b656e3306546f6b656e3306546f6b656e3303555344000000000000000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20', /** * loan : { * symbol: 'Token4', @@ -42,7 +42,7 @@ it('should bi-directional buffer-object-buffer', () => { * tokenTx: '207fb6dca77e46e58506bfa1a186d1b0c8c183e89a9f7296f58b06d1007a3d13', * } */ - '6a47446654786606546f6b656e3406546f6b656e3406546f6b656e3403555344017802964900000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20' + '6a47446654787806546f6b656e3406546f6b656e3406546f6b656e3403555344017802964900000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20' ] fixtures.forEach(hex => { @@ -51,11 +51,11 @@ it('should bi-directional buffer-object-buffer', () => { ) const buffer = toBuffer(stack) expect(buffer.toString('hex')).toStrictEqual(hex) - expect((stack[1] as OP_DEFI_TX).tx.type).toStrictEqual(0x66) + expect((stack[1] as OP_DEFI_TX).tx.type).toStrictEqual(0x78) }) }) -const header = '6a474466547866' // OP_RETURN(0x6a) OP_PUSHDATA1(0x47) (length 68 = 0x44) CDfTx.SIGNATURE(0x44665478) CSetLoanToken.OP_CODE(0x66) +const header = '6a474466547878' // OP_RETURN(0x6a) OP_PUSHDATA1(0x47) (length 68 = 0x44) CDfTx.SIGNATURE(0x44665478) CSetLoanToken.OP_CODE(0x78) const data = '06546f6b656e3406546f6b656e3406546f6b656e3403555344017802964900000000133d7a00d1068bf596729f9ae883c1c8b0d186a1a1bf0685e5467ea7dcb67f20' // UpdateLoanToken.symbol[BE](06546f6b656e34) // UpdateLoanToken.name[BE](06546f6b656e34) diff --git a/packages/jellyfish-transaction/src/script/dftx/dftx_loans.ts b/packages/jellyfish-transaction/src/script/dftx/dftx_loans.ts index fc6e063144..7dc2630376 100644 --- a/packages/jellyfish-transaction/src/script/dftx/dftx_loans.ts +++ b/packages/jellyfish-transaction/src/script/dftx/dftx_loans.ts @@ -172,7 +172,7 @@ export class CSetLoanToken extends ComposableBuffer { * Immutable by design, bi-directional fromBuffer, toBuffer deep composer. */ export class CUpdateLoanToken extends ComposableBuffer { - static OP_CODE = 0x66 // 'f' + static OP_CODE = 0x78 // 'x' static OP_NAME = 'OP_DEFI_TX_UPDATE_LOAN_TOKEN' composers (ult: UpdateLoanToken): BufferComposer[] { @@ -251,7 +251,7 @@ export interface TakeLoan { * Immutable by design, bi-directional fromBuffer, toBuffer deep composer. */ export class CTakeLoan extends ComposableBuffer { - static OP_CODE = 0x46 // 'F' + static OP_CODE = 0x58 // 'X' static OP_NAME = 'OP_DEFI_TX_TAKE_LOAN' composers (tl: TakeLoan): BufferComposer[] { diff --git a/packages/testcontainers/src/containers/RegTestContainer/ContainerGroup.ts b/packages/testcontainers/src/containers/RegTestContainer/ContainerGroup.ts index 003e3c5b1b..58e090b6aa 100644 --- a/packages/testcontainers/src/containers/RegTestContainer/ContainerGroup.ts +++ b/packages/testcontainers/src/containers/RegTestContainer/ContainerGroup.ts @@ -32,7 +32,7 @@ export class ContainerGroup { Config: [] } }, (err, data) => { - if (err instanceof Error) { + if (err instanceof Error || data === undefined) { return reject(err) } return resolve(data) diff --git a/packages/testcontainers/src/containers/RegTestContainer/LoanContainer.ts b/packages/testcontainers/src/containers/RegTestContainer/LoanContainer.ts index 70165c6e92..a655cbc963 100644 --- a/packages/testcontainers/src/containers/RegTestContainer/LoanContainer.ts +++ b/packages/testcontainers/src/containers/RegTestContainer/LoanContainer.ts @@ -4,7 +4,7 @@ import { MasterNodeRegTestContainer } from './Masternode' export class LoanMasterNodeRegTestContainer extends MasterNodeRegTestContainer { constructor (masternodeKey: MasterNodeKey = RegTestFoundationKeys[0]) { - super(masternodeKey, 'defi/defichain:HEAD-afc41ef') + super(masternodeKey, 'defi/defichain:HEAD-7ef68b5') } protected getCmd (opts: StartOptions): string[] {