Skip to content

Commit

Permalink
feat: Added collateral withdrawal block and logic, fixed collateral p…
Browse files Browse the repository at this point in the history
…rofit (#167)
  • Loading branch information
valiafetisov authored Mar 31, 2022
1 parent bc6bf23 commit fee1229
Show file tree
Hide file tree
Showing 21 changed files with 626 additions and 121 deletions.
236 changes: 236 additions & 0 deletions core/src/abis/MCD_JOIN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
[
{
"inputs": [
{
"internalType": "address",
"name": "vat_",
"type": "address"
},
{
"internalType": "bytes32",
"name": "ilk_",
"type": "bytes32"
},
{
"internalType": "address",
"name": "gem_",
"type": "address"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": true,
"inputs": [
{
"indexed": true,
"internalType": "bytes4",
"name": "sig",
"type": "bytes4"
},
{
"indexed": true,
"internalType": "address",
"name": "usr",
"type": "address"
},
{
"indexed": true,
"internalType": "bytes32",
"name": "arg1",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "bytes32",
"name": "arg2",
"type": "bytes32"
},
{
"indexed": false,
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"name": "LogNote",
"type": "event"
},
{
"constant": false,
"inputs": [],
"name": "cage",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "dec",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "usr",
"type": "address"
}
],
"name": "deny",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "usr",
"type": "address"
},
{
"internalType": "uint256",
"name": "wad",
"type": "uint256"
}
],
"name": "exit",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "gem",
"outputs": [
{
"internalType": "contract GemLike",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ilk",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "usr",
"type": "address"
},
{
"internalType": "uint256",
"name": "wad",
"type": "uint256"
}
],
"name": "join",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "live",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"internalType": "address",
"name": "usr",
"type": "address"
}
],
"name": "rely",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "vat",
"outputs": [
{
"internalType": "contract VatLike",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "wards",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
5 changes: 3 additions & 2 deletions core/src/auctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
calculateAuctionPrice,
calculateTransactionGrossProfit,
calculateTransactionGrossProfitDate,
calculateTransactionCollateralOutcome,
} from './price';
import { getSupportedCollateralTypes } from './addresses';
import getContract, { getClipperNameByCollateralType } from './contracts';
Expand Down Expand Up @@ -181,14 +182,14 @@ export const restartAuction = async function (

export const bidWithDai = async function (
network: string,
auction: Auction,
auction: AuctionTransaction,
bidAmountDai: BigNumber,
profitAddress: string,
notifier?: Notifier
): Promise<string> {
const contractName = getClipperNameByCollateralType(auction.collateralType);
const updatedAuction = await enrichAuctionWithActualNumbers(network, auction);
const collateralAmount = bidAmountDai.dividedBy(updatedAuction.unitPrice);
const collateralAmount = calculateTransactionCollateralOutcome(bidAmountDai, updatedAuction.unitPrice, auction);
const contractParameters = [
convertNumberTo32Bytes(auction.index),
collateralAmount.shiftedBy(WAD_NUMBER_OF_DIGITS).toFixed(0),
Expand Down
4 changes: 4 additions & 0 deletions core/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fetchContractAddressByNetwork } from './addresses';
import MCD_DAI from './abis/MCD_DAI.json';
import MCD_VAT from './abis/MCD_VAT.json';
import MCD_JOIN_DAI from './abis/MCD_JOIN_DAI.json';
import MCD_JOIN from './abis/MCD_JOIN.json';
import MCD_CLIP_CALC from './abis/MCD_CLIP_CALC.json';
import MCD_CLIP from './abis/MCD_CLIP.json';
import MCD_DOG from './abis/MCD_DOG.json';
Expand Down Expand Up @@ -41,6 +42,9 @@ const getContractInterfaceByName = async function (contractName: string): Promis
if (contractName === 'MCD_JOIN_DAI') {
return MCD_JOIN_DAI;
}
if (contractName.startsWith('MCD_JOIN_')) {
return MCD_JOIN;
}
if (contractName.startsWith('MCD_CLIP_CALC_')) {
return MCD_CLIP_CALC;
}
Expand Down
12 changes: 5 additions & 7 deletions core/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { AuctionInitialInfo, AuctionStatus } from './types';
import { ethers } from 'ethers';
import memoizee from 'memoizee';
import BigNumber from './bignumber';
import getContract, { getClipperNameByCollateralType } from './contracts';
import { RAD, RAY, WAD } from './constants/UNITS';
import { RAD, RAD_NUMBER_OF_DIGITS, RAY, WAD } from './constants/UNITS';
import { getCollateralConfigByType } from './constants/COLLATERALS';
import convertNumberTo32Bytes from './helpers/convertNumberTo32Bytes';

Expand All @@ -25,11 +24,10 @@ const fetchMaximumAuctionDurationInSeconds = memoizee(_fetchMaximumAuctionDurati
});

const _fetchMinimumBidDai = async function (network: string, collateralType: string): Promise<BigNumber> {
const dogContract = await getContract(network, 'MCD_DOG');
const encodedCollateralType = ethers.utils.formatBytes32String(collateralType);
const collateralParameters = await dogContract.ilks(encodedCollateralType);
const minimumBidDai = new BigNumber(collateralParameters.dirt._hex).div(RAD);
return minimumBidDai;
const contractName = getClipperNameByCollateralType(collateralType);
const contract = await getContract(network, contractName);
const minimumBidRaw = await contract.chost();
return new BigNumber(minimumBidRaw._hex).shiftedBy(-RAD_NUMBER_OF_DIGITS);
};

export const fetchMinimumBidDai = memoizee(_fetchMinimumBidDai, {
Expand Down
40 changes: 39 additions & 1 deletion core/src/price.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Auction } from './types';
import type { Auction, AuctionTransaction } from './types';
import BigNumber from './bignumber';
import { addSeconds } from 'date-fns';

Expand Down Expand Up @@ -54,6 +54,44 @@ export const calculateTransactionGrossProfit = function (auction: Auction): BigN
return totalMarketPriceLimitedByDebt.minus(auction.debtDAI);
};

export const calculateTransactionCollateralOutcome = function (
bidAmountDai: BigNumber,
unitPrice: BigNumber,
auction: AuctionTransaction
): BigNumber {
// Based on the clipper contract logic
// https://github.com/makerdao/dss/blob/60690042965500992490f695cf259256cc94c140/src/clip.sol#L357-L380
const collateralToBuyForTheBid = bidAmountDai.dividedBy(unitPrice);
const potentialOutcomeCollateralAmount = BigNumber.minimum(collateralToBuyForTheBid, auction.collateralAmount); // slice
const potentialOutcomeTotalPrice = potentialOutcomeCollateralAmount.multipliedBy(unitPrice); // owe
if (
// if owe > tab
potentialOutcomeTotalPrice.isGreaterThan(auction.debtDAI)
) {
return auction.debtDAI.dividedBy(unitPrice); // return tab / price
} else if (
// if owe < tab && slice < lot
potentialOutcomeTotalPrice.isLessThan(auction.debtDAI) &&
potentialOutcomeCollateralAmount.isLessThan(auction.collateralAmount)
) {
if (
// if tab - owe < _chost
auction.debtDAI.minus(potentialOutcomeTotalPrice).isLessThan(auction.minimumBidDai)
) {
if (
// if tab > _chost
auction.debtDAI.isLessThanOrEqualTo(auction.minimumBidDai)
) {
// shouldn't be possible to left less than minimumBidDai
return new BigNumber(NaN);
}
// tab - _chost / price
return auction.debtDAI.minus(auction.minimumBidDai).dividedBy(unitPrice);
}
}
return potentialOutcomeCollateralAmount;
};

export const calculateTransactionGrossProfitDate = function (auction: Auction, currentDate: Date): Date | undefined {
if (
auction.secondsBetweenPriceDrops === undefined ||
Expand Down
Loading

0 comments on commit fee1229

Please sign in to comment.