-
Notifications
You must be signed in to change notification settings - Fork 13
/
surplus.ts
246 lines (223 loc) · 9.19 KB
/
surplus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import memoizee from 'memoizee';
import type {
CompensationAuctionBase,
CompensationAuctionTransactionFees,
Notifier,
SurplusAuction,
SurplusAuctionActive,
SurplusAuctionTransaction,
} from './types';
import { getEarliestDate } from './helpers/getEarliestDate';
import BigNumber from './bignumber';
import getContract from './contracts';
import { Contract } from 'ethers';
import getNetworkDate from './date';
import {
ETH_NUMBER_OF_DIGITS,
MKR_NUMBER_OF_DIGITS,
RAD,
RAD_NUMBER_OF_DIGITS,
WAD,
WAD_NUMBER_OF_DIGITS,
} from './constants/UNITS';
import executeTransaction from './execute';
import { getGasPriceForUI } from './gas';
import { convertMkrToDai, convertSymbolToDai } from './calleeFunctions/helpers/uniswapV3';
const getSurplusAuctionLastIndex = async (contract: Contract): Promise<number> => {
const auctionsQuantityBinary = await contract.kicks();
return new BigNumber(auctionsQuantityBinary._hex).toNumber();
};
const _getSurplusAuctionBidIncreaseCoefficient = async (network: string): Promise<BigNumber> => {
const contract = await getContract(network, 'MCD_FLAP');
const auctionsQuantityBinary = await contract.beg();
return new BigNumber(auctionsQuantityBinary._hex).shiftedBy(-MKR_NUMBER_OF_DIGITS);
};
const getSurplusAuctionBidIncreaseCoefficient = memoizee(_getSurplusAuctionBidIncreaseCoefficient, {
promise: true,
length: 3,
});
export const getNextMinimumBid = async (network: string, surplusAuction: SurplusAuctionActive): Promise<BigNumber> => {
const increaseCoefficient = await getSurplusAuctionBidIncreaseCoefficient(network);
return surplusAuction.bidAmountMKR.multipliedBy(increaseCoefficient);
};
const getSurplusAuctionState = async (network: string, earliestEndDate: Date, greatestBid: number) => {
const isBidExpired = (await getNetworkDate(network)) > earliestEndDate;
const haveBids = greatestBid !== 0;
if (haveBids) {
if (isBidExpired) {
return 'ready-for-collection';
}
return 'have-bids';
}
if (isBidExpired) {
return 'requires-restart';
}
return 'just-started';
};
const _getContractAuctionDuration = async (network: string) => {
const contract = await getContract(network, 'MCD_FLAP');
return await contract.tau();
};
const getContractAuctionDuration = memoizee(_getContractAuctionDuration, {
maxAge: 24 * 60 * 60 * 1000,
promise: true,
length: 1,
});
export const fetchSurplusAuctionByIndex = async function (
network: string,
auctionIndex: number
): Promise<SurplusAuction> {
const contract = await getContract(network, 'MCD_FLAP');
const auctionDuration = await getContractAuctionDuration(network);
const auctionData = await contract.bids(auctionIndex);
const isAuctionCollected = new BigNumber(auctionData.end).eq(0);
const fetchedAt = new Date();
const baseAuctionInfo: CompensationAuctionBase = {
network,
id: auctionIndex,
fetchedAt,
};
if (isAuctionCollected) {
const auctionLastIndex = await getSurplusAuctionLastIndex(contract);
if (auctionLastIndex < auctionIndex) {
throw new Error('No active auction exists with this id');
}
return { ...baseAuctionInfo, state: 'collected' };
}
const auctionEndDate = new Date(auctionData.end * 1000);
const auctionStartDate = new Date(auctionEndDate.getTime() - auctionDuration * 1000);
const bidEndDate = auctionData.tic ? new Date(auctionData.tic * 1000) : undefined;
const earliestEndDate = bidEndDate ? getEarliestDate(auctionEndDate, bidEndDate) : auctionEndDate;
const state = await getSurplusAuctionState(network, earliestEndDate, auctionData.tic);
return {
...baseAuctionInfo,
earliestEndDate,
bidAmountMKR: new BigNumber(auctionData.bid._hex).div(WAD),
receiveAmountDAI: new BigNumber(auctionData.lot._hex).div(RAD),
receiverAddress: auctionData.guy,
auctionEndDate,
bidEndDate,
state,
fetchedAt,
auctionStartDate,
};
};
const getActiveSurplusAuctionOrUndefined = async (network: string, auctionIndex: number) => {
const auction = await fetchSurplusAuctionByIndex(network, auctionIndex);
if (auction.state === 'collected') {
return;
}
return auction;
};
export const fetchActiveSurplusAuctions = async function (network: string): Promise<SurplusAuctionActive[]> {
const contract = await getContract(network, 'MCD_FLAP');
const auctionLastIndex = await getSurplusAuctionLastIndex(contract);
const activeSurplusAuctions: SurplusAuctionActive[] = [];
let currentSurplusAuction;
for (let i = auctionLastIndex; i > 0; i--) {
currentSurplusAuction = await getActiveSurplusAuctionOrUndefined(network, i);
if (!currentSurplusAuction) {
break;
}
activeSurplusAuctions.push(currentSurplusAuction);
}
return activeSurplusAuctions;
};
export const restartSurplusAuction = async function (
network: string,
auctionIndex: number,
notifier?: Notifier
): Promise<void> {
await executeTransaction(network, 'MCD_FLAP', 'tick', [auctionIndex], { notifier });
};
export const bidToSurplusAuction = async function (
network: string,
auctionIndex: number,
bid: BigNumber,
notifier?: Notifier
) {
const auction = await getActiveSurplusAuctionOrUndefined(network, auctionIndex);
if (!auction || !auction.receiveAmountDAI) {
throw new Error('Did not find the auction to bid on.');
}
const transactionParameters = [
auctionIndex,
auction.receiveAmountDAI.shiftedBy(RAD_NUMBER_OF_DIGITS).toFixed(0),
bid.shiftedBy(WAD_NUMBER_OF_DIGITS).toFixed(0),
];
await executeTransaction(network, 'MCD_FLAP', 'tend', transactionParameters, { notifier });
};
export const collectSurplusAuction = async function (network: string, auctionIndex: number, notifier?: Notifier) {
const auction = await getActiveSurplusAuctionOrUndefined(network, auctionIndex);
if (!auction || auction.state !== 'ready-for-collection') {
throw new Error('Did not find the auction to collect.');
}
await executeTransaction(network, 'MCD_FLAP', 'deal', [auctionIndex], { notifier });
};
const getSurplusTransactionFees = async function (network: string): Promise<CompensationAuctionTransactionFees> {
const gasPrice = await getGasPriceForUI(network);
const exchangeRate = await convertSymbolToDai(network, 'ETH', new BigNumber(1), ETH_NUMBER_OF_DIGITS);
const restartTransactionFeeEth = gasPrice.multipliedBy(80563);
const allowanceTransactionFeeEth = gasPrice.multipliedBy(48373);
const bidTransactionFeeEth = gasPrice.multipliedBy(85181);
const collectTransactionFeeEth = gasPrice.multipliedBy(94114);
const authTransactionFeeEth = gasPrice.multipliedBy(48356);
const combinedBidFeesEth = bidTransactionFeeEth.plus(collectTransactionFeeEth);
return {
restartTransactionFeeEth,
restartTransactionFeeDai: restartTransactionFeeEth.multipliedBy(exchangeRate),
allowanceTransactionFeeEth,
allowanceTransactionFeeDai: allowanceTransactionFeeEth.multipliedBy(exchangeRate),
bidTransactionFeeEth,
bidTransactionFeeDai: bidTransactionFeeEth.multipliedBy(exchangeRate),
collectTransactionFeeEth,
collectTransactionFeeDai: collectTransactionFeeEth.multipliedBy(exchangeRate),
authTransactionFeeEth,
authTransactionFeeDai: authTransactionFeeEth.multipliedBy(exchangeRate),
combinedBidFeesEth,
combinedBidFeesDai: combinedBidFeesEth.multipliedBy(exchangeRate),
};
};
const getMarketPriceMkr = async function (network: string, bidAmountMKR: BigNumber): Promise<BigNumber> {
try {
return new BigNumber(1).div((await convertMkrToDai(network, bidAmountMKR)).div(bidAmountMKR));
} catch (error) {
return new BigNumber(NaN);
}
};
export const enrichSurplusAuction = async (
network: string,
auction: SurplusAuctionActive
): Promise<SurplusAuctionTransaction> => {
let nextMinimumBid = await getNextMinimumBid(network, auction);
const unitPrice = auction.bidAmountMKR.div(auction.receiveAmountDAI);
let marketUnitPrice = await getMarketPriceMkr(network, auction.bidAmountMKR);
const marketUnitPriceToUnitPriceRatio = unitPrice.minus(marketUnitPrice).dividedBy(marketUnitPrice);
const fees = await getSurplusTransactionFees(network);
if (nextMinimumBid.isZero()) {
nextMinimumBid = new BigNumber(1000).plus(fees.combinedBidFeesDai).div(marketUnitPrice);
}
if (marketUnitPrice.isNaN()) {
marketUnitPrice = await convertMkrToDai(network, new BigNumber(1));
}
return {
...auction,
...fees,
nextMinimumBid,
unitPrice,
marketUnitPrice,
marketUnitPriceToUnitPriceRatio,
};
};
export const enrichSurplusAuctions = async (
network: string,
auctions: SurplusAuction[]
): Promise<SurplusAuction[]> => {
const auctionsWithNextMinimumBidsPromises = auctions.map(auction => {
if (auction.state === 'collected') {
return auction;
}
return enrichSurplusAuction(network, auction);
});
return await Promise.all(auctionsWithNextMinimumBidsPromises);
};