Skip to content

Commit

Permalink
add collateralToCoverDebt
Browse files Browse the repository at this point in the history
  • Loading branch information
valiafetisov committed May 5, 2022
1 parent 1f3aa05 commit 01c76ae
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
41 changes: 31 additions & 10 deletions core/src/auctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,63 @@ const enrichAuctionWithActualNumbers = async function (
network: string,
auction: AuctionInitialInfo
): Promise<Auction> {
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<BigNumber> {
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<Auction> {
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
Expand Down
19 changes: 9 additions & 10 deletions core/src/price.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Auction, AuctionTransaction } from './types';
import type { Auction } from './types';
import BigNumber from './bignumber';
import { addSeconds } from 'date-fns';

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export declare interface AuctionStatus {
}

export declare interface Auction extends AuctionInitialInfo {
collateralToCoverDebt: BigNumber;
unitPrice: BigNumber;
totalPrice: BigNumber;
approximateUnitPrice: BigNumber;
Expand Down
22 changes: 13 additions & 9 deletions frontend/components/Auction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,27 @@
</td>
</tr>
<tr class="bg-gray-100 dark:bg-gray-800">
<td>Auction Price Total</td>
<td>Auction Debt</td>
<td>
<format-currency :value="auction.debtDAI" currency="DAI" />
</td>
</tr>
<tr class="bg-gray-100 dark:bg-gray-800">
<td>Collateral amount to cover Debt</td>
<td>
<Popover
v-if="!auction.isActive && !auction.isFinished"
placement="top"
content="Since the auction is not active, there is no total Auction Price for this auction."
content="Can not be computed since the auction is not active"
trigger="hover"
>
<span class="opacity-50">Unknown</span>
</Popover>
<format-currency v-else :value="auction.totalPrice" currency="DAI" />
</td>
</tr>
<tr class="bg-gray-100 dark:bg-gray-800">
<td>Debt</td>
<td>
<format-currency :value="auction.debtDAI" currency="DAI" />
<format-currency
v-else
:value="auction.collateralToCoverDebt"
:currency="auction.collateralSymbol"
/>
</td>
</tr>
<tr class="bg-gray-100 dark:bg-gray-800">
Expand Down

0 comments on commit 01c76ae

Please sign in to comment.