Skip to content

Commit

Permalink
fix: calculateTransactionCollateralOutcome calculation and increase…
Browse files Browse the repository at this point in the history
…d precision (#602)
  • Loading branch information
valiafetisov authored Mar 20, 2023
1 parent 090637b commit 42e3abd
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 42 deletions.
8 changes: 8 additions & 0 deletions core/simulations/configs/specificBlockFork.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addDaiToBalance, addMkrToBalance } from '../../helpers/hardhat/balance';
import { warpTime, resetNetworkAndSetupWallet } from '../../helpers/hardhat/network';
import promptToGetBlockNumber from '../helpers/promptToGetBlockNumber';
import { Simulation } from '../types';
Expand All @@ -12,6 +13,13 @@ const simulation: Simulation = {
await resetNetworkAndSetupWallet(selectedBlockNumber);
},
},
{
title: 'Add DAI and MKR to the wallet',
entry: async () => {
await addDaiToBalance();
await addMkrToBalance();
},
},
{
title: 'Skip time',
entry: async () => {
Expand Down
4 changes: 2 additions & 2 deletions core/src/bignumber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BigNumber from 'bignumber.js';
import { RAY_NUMBER_OF_DIGITS } from './constants/UNITS';
import { RAD_NUMBER_OF_DIGITS } from './constants/UNITS';

BigNumber.config({ DECIMAL_PLACES: RAY_NUMBER_OF_DIGITS });
BigNumber.config({ DECIMAL_PLACES: RAD_NUMBER_OF_DIGITS });

export default BigNumber;
12 changes: 12 additions & 0 deletions core/src/helpers/parseMetamaskError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ const getErrorTitle = function (errorMessage = '') {
return errorMessage.substring(0, jsonStartIndex).trim();
};

const getVMError = function (errorMessage = '') {
const prefix = 'Error: VM Exception while processing transaction:';
if (!errorMessage.startsWith(prefix)) {
return '';
}
return errorMessage.replace(prefix, 'VM').trim();
};

const parseMetamaskError = function (errorMessage = ''): unknown {
const vmError = getVMError(errorMessage);
if (vmError) {
return vmError;
}
const errorTitle = getErrorTitle(errorMessage);
if (!errorTitle) {
return truncateText(errorMessage || UNKNOWN_ERROR_TEXT);
Expand Down
12 changes: 6 additions & 6 deletions core/src/price.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Auction } from './types';
import BigNumber from './bignumber';
import { addSeconds } from 'date-fns';
import { RAD_NUMBER_OF_DIGITS } from './constants/UNITS';

const checkAuctionStartDate = function (startDate: Date, currentDate: Date): void {
const auctionStartTimestamp = startDate.getTime();
Expand Down Expand Up @@ -65,16 +66,15 @@ export const calculateTransactionCollateralOutcome = function (
const collateralToBuyForTheBid = bidAmountDai.dividedBy(unitPrice);
const potentialOutcomeCollateralAmount = BigNumber.minimum(collateralToBuyForTheBid, auction.collateralAmount); // slice
const potentialOutcomeTotalPrice = potentialOutcomeCollateralAmount.multipliedBy(unitPrice); // owe
const approximateDebt = new BigNumber(auction.debtDAI.toPrecision(16, BigNumber.ROUND_DOWN));
const approximatePotentialOutcomeTotalPrice = new BigNumber(
potentialOutcomeTotalPrice.toPrecision(16, BigNumber.ROUND_DOWN)
const potentialOutcomeTotalPriceRounded = new BigNumber( // rounded up to match debtDAI precision
potentialOutcomeTotalPrice.toPrecision(RAD_NUMBER_OF_DIGITS, BigNumber.ROUND_UP)
);
if (
// if owe > tab
// soft compensation because of precision problems.
approximateDebt.isLessThan(approximatePotentialOutcomeTotalPrice)
potentialOutcomeTotalPriceRounded.isGreaterThan(auction.debtDAI)
) {
return auction.debtDAI.dividedBy(unitPrice); // return tab / price
// return tab / price + 0.1% compensation for the js/sol math differences
return auction.debtDAI.dividedBy(unitPrice).multipliedBy(1.001);
} else if (
// if owe < tab && slice < lot
potentialOutcomeTotalPrice.isLessThan(auction.debtDAI) &&
Expand Down
6 changes: 4 additions & 2 deletions core/src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const trackTransaction = async function (
});
} catch (error: any) {
notifier('error', {
content: `Transaction error: "${parseMetamaskError(error?.message)}"`,
content: `Transaction error: "${parseMetamaskError(error?.data?.message || error?.message)}"`,
key: messageId,
duration: DEFAULT_NOTIFICATION_DURATION,
});
Expand Down Expand Up @@ -72,7 +72,9 @@ const trackTransaction = async function (
return confirmedTransactionReceipt.transactionHash;
} catch (error: any) {
notifier('error', {
content: `Transaction was rejected with error: "${parseMetamaskError(error?.message)}"`,
content: `Transaction was rejected with error: "${parseMetamaskError(
error?.data?.message || error?.message
)}"`,
key: messageId,
duration: DEFAULT_NOTIFICATION_DURATION,
});
Expand Down
6 changes: 4 additions & 2 deletions core/test/surplus-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ describe('Surplus Auction', () => {
const auctions = await fetchActiveSurplusAuctions(network);
const auction = auctions[0] as SurplusAuctionActive;
const enrichedAucton = await enrichSurplusAuction(network, auction);
expect(enrichedAucton.marketUnitPrice?.toString()).to.equal('0.001395892836151232667549186');
expect(enrichedAucton.marketUnitPrice?.toString()).to.equal('0.001395892836151232667549185678491851066262951');
expect(enrichedAucton.unitPrice?.toString()).to.equal('0.0005430260298973471');
expect(enrichedAucton.marketUnitPriceToUnitPriceRatio?.toString()).to.equal('-0.61098300970253348099040009');
expect(enrichedAucton.marketUnitPriceToUnitPriceRatio?.toString()).to.equal(
'-0.610983009702533480990400000000000000000000026'
);
});
it('enrichesAuctionWithMinimumBids', async () => {
const network = TEST_NETWORK;
Expand Down
50 changes: 25 additions & 25 deletions core/test/vault-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@ const compareVaultTransactionsNotLiquidated = (
expect(expected.network).to.eq(actual.network);
expect(expected.nextPriceChange).to.deep.equalInAnyOrder(actual.nextPriceChange);

expect(expected.liquidationRatio).to.deep.equalInAnyOrder(actual.liquidationRatio);
expect(expected.collateralizationRatio).to.deep.equalInAnyOrder(actual.collateralizationRatio);
expect(expected.proximityToLiquidation).to.deep.equalInAnyOrder(actual.proximityToLiquidation);
expect(expected.liquidationRatio.toFixed()).to.eq(actual.liquidationRatio.toFixed());
expect(expected.collateralizationRatio.toFixed()).to.eq(actual.collateralizationRatio.toFixed());
expect(expected.proximityToLiquidation.toFixed()).to.eq(actual.proximityToLiquidation.toFixed());
expect(expected.id).to.eq(actual.id);
expect(expected.incentiveCombinedDai).to.deep.equalInAnyOrder(actual.incentiveCombinedDai);
expect(expected.incentiveRelativeDai).to.deep.equalInAnyOrder(actual.incentiveRelativeDai);
expect(expected.incentiveConstantDai).to.deep.equalInAnyOrder(actual.incentiveConstantDai);
expect(expected.grossProfitDai).to.deep.equalInAnyOrder(actual.grossProfitDai);
expect(expected.incentiveCombinedDai.toFixed()).to.eq(actual.incentiveCombinedDai.toFixed());
expect(expected.incentiveRelativeDai.toFixed()).to.eq(actual.incentiveRelativeDai.toFixed());
expect(expected.incentiveConstantDai.toFixed()).to.eq(actual.incentiveConstantDai.toFixed());
expect(expected.grossProfitDai.toFixed()).to.eq(actual.grossProfitDai.toFixed());
// TODO: enable the check and fix the bug with inconsistent price received
// expect(expected.netProfitDai).to.deep.equalInAnyOrder(actual.netProfitDai);
expect(expected.stabilityFeeRate).to.deep.equalInAnyOrder(actual.stabilityFeeRate);
expect(expected.minUnitPrice).to.deep.equalInAnyOrder(actual.minUnitPrice);
expect(expected.maximumProtocolDebtDai).to.deep.equalInAnyOrder(actual.maximumProtocolDebtDai);
expect(expected.currentProtocolDebtDai).to.deep.equalInAnyOrder(actual.currentProtocolDebtDai);
expect(expected.currentCollateralDebtDai).to.deep.equalInAnyOrder(actual.currentCollateralDebtDai);
expect(expected.maximumCollateralDebtDai).to.deep.equalInAnyOrder(actual.maximumCollateralDebtDai);
expect(expected.debtDai).to.deep.equalInAnyOrder(actual.debtDai);
expect(expected.collateralAmount).to.deep.equalInAnyOrder(actual.collateralAmount);
expect(expected.currentUnitPrice).to.deep.equalInAnyOrder(actual.currentUnitPrice);
expect(expected.nextUnitPrice).to.deep.equalInAnyOrder(actual.nextUnitPrice);
expect(expected.initialDebtDai).to.deep.equalInAnyOrder(actual.initialDebtDai);
expect(expected.liquidationPenaltyRatio).to.deep.equalInAnyOrder(actual.liquidationPenaltyRatio);
expect(expected.minimalAuctionedDai).to.deep.equalInAnyOrder(actual.minimalAuctionedDai);
// expect(expected.netProfitDai.toFixed()).to.eq(actual.netProfitDai.toFixed());
expect(expected.stabilityFeeRate.toFixed()).to.eq(actual.stabilityFeeRate.toFixed());
expect(expected.minUnitPrice.toFixed()).to.eq(actual.minUnitPrice.toFixed());
expect(expected.maximumProtocolDebtDai.toFixed()).to.eq(actual.maximumProtocolDebtDai.toFixed());
expect(expected.currentProtocolDebtDai.toFixed()).to.eq(actual.currentProtocolDebtDai.toFixed());
expect(expected.currentCollateralDebtDai.toFixed()).to.eq(actual.currentCollateralDebtDai.toFixed());
expect(expected.maximumCollateralDebtDai.toFixed()).to.eq(actual.maximumCollateralDebtDai.toFixed());
expect(expected.debtDai.toFixed()).to.eq(actual.debtDai.toFixed());
expect(expected.collateralAmount.toFixed()).to.eq(actual.collateralAmount.toFixed());
expect(expected.currentUnitPrice.toFixed()).to.eq(actual.currentUnitPrice.toFixed());
expect(expected.nextUnitPrice.toFixed()).to.eq(actual.nextUnitPrice.toFixed());
expect(expected.initialDebtDai.toFixed()).to.eq(actual.initialDebtDai.toFixed());
expect(expected.liquidationPenaltyRatio.toFixed()).to.eq(actual.liquidationPenaltyRatio.toFixed());
expect(expected.minimalAuctionedDai.toFixed()).to.eq(actual.minimalAuctionedDai.toFixed());
};

describe('Vaults', () => {
Expand All @@ -82,8 +82,8 @@ describe('Vaults', () => {
nextPriceChange: new Date('2022-09-09T10:00:00.000Z'),

liquidationRatio: new BigNumber('1.45'),
collateralizationRatio: new BigNumber('3.204790430641710905476031356'),
proximityToLiquidation: new BigNumber('0.547552318511616565008415095'),
collateralizationRatio: new BigNumber('3.204790430641710905476031355953603471763531393'),
proximityToLiquidation: new BigNumber('0.547552318511616565008415094516004867207418444'),
liquidationPenaltyRatio: new BigNumber('1.13'),
minimalAuctionedDai: new BigNumber('15000'),
id: 22025,
Expand Down Expand Up @@ -141,8 +141,8 @@ describe('Vaults', () => {
nextPriceChange: new Date('2022-06-13T11:00:00.000Z'),

liquidationRatio: new BigNumber(1.7),
collateralizationRatio: new BigNumber('1.698313241866926788910221381'),
proximityToLiquidation: new BigNumber('-0.000993196126304112564617121'),
collateralizationRatio: new BigNumber('1.698313241866926788910221380759664664393806833'),
proximityToLiquidation: new BigNumber('-0.000993196126304112564617120755317536277614422'),
liquidationPenaltyRatio: new BigNumber('1.13'),
minimalAuctionedDai: new BigNumber('5000'),
id: 27435,
Expand Down
21 changes: 17 additions & 4 deletions frontend/components/common/formatters/AnimatedNumber.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<template>
<span v-if="isValidNumber"
><span v-if="isValueSmallButNotZero">under </span
><animated-number :value="formattedValue" :format-value="format" :duration="duration"
/></span>
<Popover :content="pureValue" placement="top">
<span v-if="isValidNumber"
><span v-if="isValueSmallButNotZero">under </span
><animated-number :value="formattedValue" :format-value="format" :duration="duration"
/></span>
</Popover>
</template>

<script lang="ts">
import Vue from 'vue';
import { Popover } from 'ant-design-vue';
import AnimatedNumber from 'animated-number-vue';
import BigNumber from 'bignumber.js';
import {
Expand All @@ -18,6 +21,7 @@ import {
export default Vue.extend({
components: {
AnimatedNumber,
Popover,
},
props: {
value: {
Expand All @@ -38,6 +42,15 @@ export default Vue.extend({
},
},
computed: {
pureValue(): string {
if (this.value === undefined) {
return '';
}
if (typeof this.value === 'number') {
return this.value.toString();
}
return this.value.toFixed();
},
formattedValue(): string {
return formatToAutomaticDecimalPoints(this.value, {
decimalPlaces: this.decimalPlaces,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 42e3abd

Please sign in to comment.