Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
added collateralDebtAvailable schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Padraic-O-Mhuiris committed Jul 8, 2020
1 parent 69a8028 commit edad595
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/dai-plugin-mcd/src/schemas/_constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
36 changes: 30 additions & 6 deletions packages/dai-plugin-mcd/src/schemas/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -256,15 +257,17 @@ 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,
annualStabilityFee,
liquidationPenalty,
liquidationRatio,
priceWithSafetyMargin,
debtFloor
debtFloor,
collateralDebtAvailable
) => ({
symbol: collateralTypeName,
collateralTypePrice,
Expand All @@ -273,6 +276,7 @@ export const collateralTypeData = {
liquidationPenalty,
priceWithSafetyMargin,
debtFloor,
collateralDebtAvailable,
calculateCollateralizationRatio(collateralAmount, debtValue) {
return calcCollateralizationRatio(
this.collateralTypePrice.times(collateralAmount),
Expand Down Expand Up @@ -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,
Expand All @@ -352,7 +357,8 @@ export const vault = {
liquidationPenalty,
annualStabilityFee,
debtFloor,
minSafeCollateralAmount
minSafeCollateralAmount,
collateralDebtAvailable
) => ({
id: parseInt(id),
vaultType,
Expand All @@ -376,6 +382,7 @@ export const vault = {
annualStabilityFee,
debtFloor,
minSafeCollateralAmount,
collateralDebtAvailable,
calculateLiquidationPrice({
collateralAmount = this.collateralAmount,
debtValue = this.debtValue,
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -654,5 +677,6 @@ export default {
proxyOwner,
collateralTypeData,
collateralTypesData,
collateralDebtCeilings
collateralDebtCeilings,
collateralDebtAvailable
};
29 changes: 24 additions & 5 deletions packages/dai-plugin-mcd/test/schemas/computed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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(
Expand All @@ -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 () => {
Expand All @@ -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));
Expand All @@ -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);
});

0 comments on commit edad595

Please sign in to comment.