From edad5952b926e021c9e10ce0aea9757225b01dfc Mon Sep 17 00:00:00 2001 From: padraic-X Date: Wed, 8 Jul 2020 17:25:01 +0100 Subject: [PATCH] added collateralDebtAvailable schema --- .../dai-plugin-mcd/src/schemas/_constants.js | 1 + .../dai-plugin-mcd/src/schemas/computed.js | 36 +++++++++++++++---- .../test/schemas/computed.spec.js | 29 ++++++++++++--- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/packages/dai-plugin-mcd/src/schemas/_constants.js b/packages/dai-plugin-mcd/src/schemas/_constants.js index faf06989b..983b5abd3 100644 --- a/packages/dai-plugin-mcd/src/schemas/_constants.js +++ b/packages/dai-plugin-mcd/src/schemas/_constants.js @@ -80,6 +80,7 @@ export const PROXY_OWNER = 'proxyOwner'; export const COLLATERAL_TYPE_DATA = 'collateralTypeData'; export const COLLATERAL_TYPES_DATA = 'collateralTypesData'; export const COLLATERAL_DEBT_CEILINGS = 'collateralDebtCeilings'; +export const COLLATERAL_DEBT_AVAILABLE = 'collateralDebtAvailable'; // token export const TOKEN_BALANCE = 'tokenBalance'; diff --git a/packages/dai-plugin-mcd/src/schemas/computed.js b/packages/dai-plugin-mcd/src/schemas/computed.js index 899b466c2..4eb17c143 100644 --- a/packages/dai-plugin-mcd/src/schemas/computed.js +++ b/packages/dai-plugin-mcd/src/schemas/computed.js @@ -53,7 +53,8 @@ import { ADAPTER_BALANCE, COLLATERAL_DEBT, COLLATERAL_TYPE_COLLATERALIZATION, - COLLATERAL_TYPE_DATA + COLLATERAL_TYPE_DATA, + COLLATERAL_DEBT_AVAILABLE } from './_constants'; import { validateAddress, validateVaultId } from './_validators'; @@ -256,7 +257,8 @@ export const collateralTypeData = { [LIQUIDATION_PENALTY, collateralTypeName], [LIQUIDATION_RATIO, collateralTypeName], [PRICE_WITH_SAFETY_MARGIN, collateralTypeName], - [DEBT_FLOOR, collateralTypeName] + [DEBT_FLOOR, collateralTypeName], + [COLLATERAL_DEBT_AVAILABLE, collateralTypeName] ], computed: ( collateralTypePrice, @@ -264,7 +266,8 @@ export const collateralTypeData = { liquidationPenalty, liquidationRatio, priceWithSafetyMargin, - debtFloor + debtFloor, + collateralDebtAvailable ) => ({ symbol: collateralTypeName, collateralTypePrice, @@ -273,6 +276,7 @@ export const collateralTypeData = { liquidationPenalty, priceWithSafetyMargin, debtFloor, + collateralDebtAvailable, calculateCollateralizationRatio(collateralAmount, debtValue) { return calcCollateralizationRatio( this.collateralTypePrice.times(collateralAmount), @@ -329,7 +333,8 @@ export const vault = { [LIQUIDATION_PENALTY, [VAULT_TYPE, id]], [ANNUAL_STABILITY_FEE, [VAULT_TYPE, id]], [DEBT_FLOOR, [VAULT_TYPE, id]], - [MIN_SAFE_COLLATERAL_AMOUNT, id] + [MIN_SAFE_COLLATERAL_AMOUNT, id], + [COLLATERAL_DEBT_AVAILABLE, [VAULT_TYPE, id]] ], computed: ( vaultType, @@ -352,7 +357,8 @@ export const vault = { liquidationPenalty, annualStabilityFee, debtFloor, - minSafeCollateralAmount + minSafeCollateralAmount, + collateralDebtAvailable ) => ({ id: parseInt(id), vaultType, @@ -376,6 +382,7 @@ export const vault = { annualStabilityFee, debtFloor, minSafeCollateralAmount, + collateralDebtAvailable, calculateLiquidationPrice({ collateralAmount = this.collateralAmount, debtValue = this.debtValue, @@ -549,6 +556,22 @@ export const collateralDebtCeilings = { }) }; +export const collateralDebtAvailable = { + generate: collateralTypeName => ({ + dependencies: [ + [COLLATERAL_DEBT, collateralTypeName], + [DEBT_CEILING, collateralTypeName] + ], + computed: (collateralDebt, debtCeiling) => { + collateralDebt = collateralDebt + .toBigNumber() + .decimalPlaces(18, BigNumber.ROUND_DOWN); + + return debtCeiling.minus(collateralDebt); + } + }) +}; + export const collateralTypeCollateralization = { generate: (collateralTypeName, percentage = true) => ({ dependencies: [ @@ -654,5 +677,6 @@ export default { proxyOwner, collateralTypeData, collateralTypesData, - collateralDebtCeilings + collateralDebtCeilings, + collateralDebtAvailable }; diff --git a/packages/dai-plugin-mcd/test/schemas/computed.spec.js b/packages/dai-plugin-mcd/test/schemas/computed.spec.js index 2968f01d0..0c27204dc 100644 --- a/packages/dai-plugin-mcd/test/schemas/computed.spec.js +++ b/packages/dai-plugin-mcd/test/schemas/computed.spec.js @@ -34,7 +34,8 @@ import { PROXY_OWNER, COLLATERAL_TYPE_DATA, COLLATERAL_TYPES_DATA, - COLLATERAL_DEBT_CEILINGS + COLLATERAL_DEBT_CEILINGS, + COLLATERAL_DEBT_AVAILABLE } from '../../src/schemas'; import { vatIlks, vatUrns, vatGem } from '../../src/schemas/vat'; @@ -320,10 +321,11 @@ test(VAULT, async () => { const expectedLiqPenalty = BigNumber('0.05'); const expectedAnnStabilityFee = 0.04999999999989363; const expectedDebtFloor = BigNumber('0'); + const expectedCollateralDebtAvailable = DAI(99999); const vault = await maker.latest(VAULT, cdpId); - expect(Object.keys(vault).length).toBe(24); + expect(Object.keys(vault).length).toBe(25); expect(vault.id).toEqual(cdpId); expect(vault.vaultType).toEqual(expectedVaultType); @@ -368,6 +370,9 @@ test(VAULT, async () => { expect(vault.liquidationPenalty).toEqual(expectedLiqPenalty); expect(vault.annualStabilityFee.toNumber()).toEqual(expectedAnnStabilityFee); expect(vault.debtFloor).toEqual(expectedDebtFloor); + expect(vault.collateralDebtAvailable.toString()).toEqual( + expectedCollateralDebtAvailable.toString() + ); }); test(`${VAULT} with non-existent id`, async () => { @@ -491,10 +496,11 @@ test(COLLATERAL_TYPE_DATA, async () => { const expectedAnnStabilityFee = 0.04999999999989363; const expectedPriceWithSafetyMargin = BigNumber('120'); const expectedDebtFloor = BigNumber('0'); + const expectedCollateralDebtAvailable = DAI(99999); const colData = await maker.latest(COLLATERAL_TYPE_DATA, collateralType); - expect(Object.keys(colData).length).toBe(10); + expect(Object.keys(colData).length).toBe(11); expect(colData.symbol).toEqual(collateralType); expect(colData.collateralTypePrice.toString()).toEqual( @@ -509,6 +515,10 @@ test(COLLATERAL_TYPE_DATA, async () => { ); expect(colData.priceWithSafetyMargin).toEqual(expectedPriceWithSafetyMargin); expect(colData.debtFloor).toEqual(expectedDebtFloor); + + expect(colData.collateralDebtAvailable.toString()).toEqual( + expectedCollateralDebtAvailable.toString() + ); }); test(COLLATERAL_TYPES_DATA, async () => { @@ -520,8 +530,8 @@ test(COLLATERAL_TYPES_DATA, async () => { const expectedEth = await maker.latest(COLLATERAL_TYPE_DATA, 'ETH-A'); const expectedBat = await maker.latest(COLLATERAL_TYPE_DATA, 'BAT-A'); - expect(Object.entries(ethAData).length).toBe(10); - expect(Object.entries(batAData).length).toBe(10); + expect(Object.entries(ethAData).length).toBe(11); + expect(Object.entries(batAData).length).toBe(11); expect(JSON.stringify(ethAData)).toEqual(JSON.stringify(expectedEth)); expect(JSON.stringify(batAData)).toEqual(JSON.stringify(expectedBat)); @@ -535,3 +545,12 @@ test(COLLATERAL_DEBT_CEILINGS, async () => { expect(debtCeilings['ETH-A'].toNumber()).toEqual(100000); expect(debtCeilings['BAT-A'].toNumber()).toEqual(5000); }); + +test(COLLATERAL_DEBT_AVAILABLE, async () => { + const ethACollateralDebt = await maker.latest( + COLLATERAL_DEBT_AVAILABLE, + 'ETH-A' + ); + + expect(ethACollateralDebt.toNumber()).toEqual(99999); +});