From 01c76ae6105d9cbb4a65440f9d6c06eb471062ae Mon Sep 17 00:00:00 2001 From: Valia Fetisov Date: Thu, 5 May 2022 14:29:05 +0200 Subject: [PATCH] add collateralToCoverDebt --- core/src/auctions.ts | 41 +++++++++++++++++++++++++-------- core/src/price.ts | 19 ++++++++------- core/src/types.ts | 1 + frontend/components/Auction.vue | 22 ++++++++++-------- 4 files changed, 54 insertions(+), 29 deletions(-) diff --git a/core/src/auctions.ts b/core/src/auctions.ts index 64efdcd9f..ca3575585 100644 --- a/core/src/auctions.ts +++ b/core/src/auctions.ts @@ -25,42 +25,63 @@ const enrichAuctionWithActualNumbers = async function ( network: string, auction: AuctionInitialInfo ): Promise { + const defaultAcution = { + ...auction, + minimumBidDai: new BigNumber(0), + unitPrice: new BigNumber(0), + approximateUnitPrice: new BigNumber(0), + totalPrice: new BigNumber(0), + collateralToCoverDebt: new BigNumber(NaN), + }; if (!auction.isActive || auction.isFinished) { - return { - ...auction, - minimumBidDai: new BigNumber(0), - unitPrice: new BigNumber(0), - approximateUnitPrice: new BigNumber(0), - totalPrice: new BigNumber(0), - }; + return defaultAcution; } const auctionStatus = await fetchAuctionStatus(network, auction.collateralType, auction.index); const minimumBidDai = await fetchMinimumBidDai(network, auction.collateralType); return { - ...auction, + ...defaultAcution, ...auctionStatus, minimumBidDai, approximateUnitPrice: auctionStatus.unitPrice, }; }; +const calculateCollateralToCoverDebt = async function (network: string, auction: Auction): Promise { + const collateralToCoverDebt = calculateTransactionCollateralOutcome( + auction.debtDAI, + auction.approximateUnitPrice, + auction + ); + if (!collateralToCoverDebt.isNaN()) { + return collateralToCoverDebt; + } + const marketPriceForAllCollateral = await getMarketPrice( + network, + auction.collateralSymbol, + auction.collateralAmount + ); + return auction.debtDAI.dividedBy(marketPriceForAllCollateral); +}; + const enrichAuctionWithMarketValues = async function (auction: Auction, network: string): Promise { if (!auction.isActive || !auction.approximateUnitPrice || auction.isFinished) { return auction; } try { - const marketUnitPrice = await getMarketPrice(network, auction.collateralSymbol, auction.collateralAmount); + const collateralToCoverDebt = await calculateCollateralToCoverDebt(network, auction); + const marketUnitPrice = await getMarketPrice(network, auction.collateralSymbol, collateralToCoverDebt); const marketUnitPriceToUnitPriceRatio = auction.approximateUnitPrice .minus(marketUnitPrice) .dividedBy(marketUnitPrice); const auctionWithMarketValues = { ...auction, + collateralToCoverDebt, marketUnitPrice, marketUnitPriceToUnitPriceRatio, }; return { ...auctionWithMarketValues, - transactionGrossProfit: calculateTransactionGrossProfit(auctionWithMarketValues), + transactionGrossProfit: calculateTransactionGrossProfit(auctionWithMarketValues, collateralToCoverDebt), }; } catch (error) { // since it's expected that some collaterals are not tradable on some networks diff --git a/core/src/price.ts b/core/src/price.ts index e1e67c395..e5dd0354f 100644 --- a/core/src/price.ts +++ b/core/src/price.ts @@ -1,4 +1,4 @@ -import type { Auction, AuctionTransaction } from './types'; +import type { Auction } from './types'; import BigNumber from './bignumber'; import { addSeconds } from 'date-fns'; @@ -41,23 +41,22 @@ export const calculateAuctionDropTime = function (auction: Auction, currentDate: return auction.secondsBetweenPriceDrops - (elapsedTime % auction.secondsBetweenPriceDrops); }; -export const calculateTransactionGrossProfit = function (auction: Auction): BigNumber { +export const calculateTransactionGrossProfit = function ( + auction: Auction, + collateralToCoverDebt: BigNumber +): BigNumber { if (!auction.marketUnitPrice) { return new BigNumber(0); } - const totalMarketPrice = auction.collateralAmount.multipliedBy(auction.marketUnitPrice); - if (totalMarketPrice <= auction.debtDAI) { - return totalMarketPrice.minus(auction.totalPrice); - } - const collateralAmountLimitedByDebt = auction.debtDAI.dividedBy(auction.approximateUnitPrice); - const totalMarketPriceLimitedByDebt = collateralAmountLimitedByDebt.multipliedBy(auction.marketUnitPrice); - return totalMarketPriceLimitedByDebt.minus(auction.debtDAI); + const totalDebtMarketPrice = collateralToCoverDebt.multipliedBy(auction.marketUnitPrice); + const totalDebtPrice = collateralToCoverDebt.multipliedBy(auction.approximateUnitPrice); + return totalDebtMarketPrice.minus(totalDebtPrice); }; export const calculateTransactionCollateralOutcome = function ( bidAmountDai: BigNumber, unitPrice: BigNumber, - auction: AuctionTransaction + auction: Auction ): BigNumber { // Based on the clipper contract logic // https://github.com/makerdao/dss/blob/60690042965500992490f695cf259256cc94c140/src/clip.sol#L357-L380 diff --git a/core/src/types.ts b/core/src/types.ts index c000fff61..71125e410 100644 --- a/core/src/types.ts +++ b/core/src/types.ts @@ -37,6 +37,7 @@ export declare interface AuctionStatus { } export declare interface Auction extends AuctionInitialInfo { + collateralToCoverDebt: BigNumber; unitPrice: BigNumber; totalPrice: BigNumber; approximateUnitPrice: BigNumber; diff --git a/frontend/components/Auction.vue b/frontend/components/Auction.vue index 5342f13a9..1468dddbf 100644 --- a/frontend/components/Auction.vue +++ b/frontend/components/Auction.vue @@ -100,23 +100,27 @@ - Auction Price Total + Auction Debt + + + + + + Collateral amount to cover Debt Unknown - - - - - Debt - - +