-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
371 lines (334 loc) · 11.9 KB
/
index.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
declare const process: { env: { [key: string]: string; }, exit: { (exitCode: number): void } }
require("dotenv").config();
import fetch from "node-fetch";
import "reflect-metadata";
import { plainToClass } from "class-transformer";
import Web3 from "web3";
import { fromWei, toChecksumAddress, numberToHex, toHex, toWei, hexToAscii } from "web3-utils";
import { CDP, ReservesData, Reserve, OneSplitReturn, User } from "./schemas"
import GasPriceFetcher from "./gasPriceFetcher"
import BigNumber from "bignumber.js";
const CoinGecko = require("coingecko-api");
const CoinGeckoClient = new CoinGecko();
const AAVE_LIQUIDATIONS = "https://protocol-api.aave.com/data/users/liquidations";
const fetcher = new GasPriceFetcher()
const { PRIVATE_KEY, GAS_PRICE, RPC_WSS_URL, LIQUIDATOR_ADDRESS } = process.env
const httpProvider = new Web3.providers.WebsocketProvider(RPC_WSS_URL);
const web3 = new Web3(httpProvider);
const account = web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY)
console.log('account.address', account.address)
web3.eth.accounts.wallet.add('0x' + PRIVATE_KEY)
web3.eth.defaultAccount = account.address
const LENDING_POOL_ABI = require("./abi/lendingPool.json");
const lendingPool = new web3.eth.Contract(
LENDING_POOL_ABI,
"0x398eC7346DcD622eDc5ae82352F02bE94C62d119"
);
const LIQUIDATOR_ABI = require('./abi/liquidator.json')
const flashLiquidator = new web3.eth.Contract(
LIQUIDATOR_ABI,
LIQUIDATOR_ADDRESS
);
const ONESPLIT_ADDRESS = "0xC586BeF4a0992C495Cf22e1aeEE4E446CECDee0E";
const ONESPLIT_ABI = require("./abi/oneSplit.json");
const oneSplit = new web3.eth.Contract(ONESPLIT_ABI, ONESPLIT_ADDRESS);
const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const SAI = "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359";
const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const TOKENS: Record<string, number> = {
[WETH]: 0,
[SAI]: 1,
[USDC]: 2,
[DAI]: 3,
}
function addressToMarketId(address: string): number {
return TOKENS[toChecksumAddress(address)];
}
function marketIdToAddress(id: number) {
for (let [ token, marketId ] of Object.entries(TOKENS)) {
if (marketId === id) {
return token
}
}
return
}
async function getPriceInUSD(tokenAddress: string) {
const arpa = await CoinGeckoClient.simple.fetchTokenPrice({
contract_addresses: tokenAddress,
vs_currencies: "usd",
assetPlatform: "ethereum",
});
return arpa.data[tokenAddress.toLowerCase()].usd;
}
async function getCDPs(): Promise<CDP[]> {
let cdps: CDP[] = []
try {
const result = await fetch(AAVE_LIQUIDATIONS, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
}
});
const jsonData = await result.json();
for(let cdp of jsonData.data) {
cdps.push(plainToClass(CDP, cdp))
}
return cdps
} catch(e) {
console.log(e.message)
return cdps
}
}
async function healthFactorFromContract(user: string): Promise<number> {
const { healthFactor } = await lendingPool.methods
.getUserAccountData(user)
.call();
return Number(fromWei(healthFactor));
}
async function getExpectedReturn(fromToken: string, toToken: string, amount: BigNumber): Promise<OneSplitReturn> {
return new Promise((resolve, reject) => {
const call = async (retryAttempt: number) => {
retryAttempt++;
try {
let result = await oneSplit.methods
.getExpectedReturn(fromToken, toToken, amount, 100, 0)
.call();
result = plainToClass(OneSplitReturn, result)
resolve(result);
} catch (e) {
console.log("RPC failed. RetryAttempt is", retryAttempt, e.message);
if (retryAttempt > 20) {
reject(e);
} else {
call(retryAttempt);
}
}
};
call(0);
});
}
function add5Percent(amount: BigNumber) {
return amount.plus(amount.multipliedBy(5).dividedToIntegerBy(100));
}
async function prepareArgs(reserve: Reserve, borrowing: BigNumber, collateral: ReservesData) {
borrowing = borrowing.multipliedBy(new BigNumber(10).pow(reserve.decimals))
const collateralAmount = collateral.principalATokenBalance.multipliedBy(new BigNumber(10).pow(collateral.reserve.decimals))
let beforeLiquidationSplit: string[] = [];
let afterLiquidationSplit: string[] = [];
let remaningReserveSplit: string[] = [];
let flashTokenId: number;
const reserveMarketId = addressToMarketId(reserve.underlyingAsset);
const collateralMarketId = addressToMarketId(collateral.reserve.underlyingAsset);
let flashTokenAmount;
if (reserveMarketId !== undefined) {
console.log("we can get flash loan in reserve token");
flashTokenId = reserveMarketId;
flashTokenAmount = borrowing; // TODO probably we should take a half
const { distribution } = await getExpectedReturn(
collateral.reserve.underlyingAsset,
reserve.underlyingAsset,
collateralAmount // TODO
);
afterLiquidationSplit = distribution;
} else if (collateralMarketId) {
console.log("we can get flash loan in collateral token");
flashTokenId = collateralMarketId;
flashTokenAmount = collateralAmount;
let { distribution } = await getExpectedReturn(
collateral.reserve.underlyingAsset,
reserve.underlyingAsset,
collateralAmount // TODO we probably should exchange a half or so
);
beforeLiquidationSplit = distribution;
({ distribution } = await getExpectedReturn(
reserve.underlyingAsset,
collateral.reserve.underlyingAsset,
borrowing
));
remaningReserveSplit = distribution;
} else {
console.log("lets take flash loan in WETH then");
flashTokenId = addressToMarketId(WETH);
// a rough calculation of how much WETH we need
let { returnAmount, distribution } = await getExpectedReturn(
reserve.underlyingAsset,
WETH,
borrowing
);
flashTokenAmount = add5Percent(returnAmount);
// since we borrow more than we need, there will be so reserve leftovers, so we need to exchange them back
remaningReserveSplit = distribution;
// the distribution of the WETH to "reserve" swap
({ distribution } = await getExpectedReturn(
WETH,
reserve.underlyingAsset,
flashTokenAmount
));
beforeLiquidationSplit = distribution;
// the distribution of the "collateral" to WETH swap
({ distribution } = await getExpectedReturn(
collateral.reserve.underlyingAsset,
WETH,
collateralAmount // TODO we probably should exchange a half or so
));
afterLiquidationSplit = distribution;
}
return {
flashTokenId,
flashTokenAmount,
beforeLiquidationSplit,
afterLiquidationSplit,
remaningReserveSplit
};
}
async function liquidateTx(cdp: CDP) :Promise<any> {
// console.log('cdp', cdp.user.reservesData)
const reserve = cdp.reserve
// console.log('reserve', reserve)
const user = cdp.user
let largestCollateral = user.reservesData[0]
for (let {
principalATokenBalance,
reserve,
currentUnderlyingBalanceUSD
} of user.reservesData) {
if (currentUnderlyingBalanceUSD.gt(largestCollateral.currentUnderlyingBalanceUSD)) {
// console.log(
// ` ${principalATokenBalance} ${reserve.symbol} ($${currentUnderlyingBalanceUSD})`
// );
largestCollateral = {
principalATokenBalance,
reserve,
currentUnderlyingBalanceUSD
}
}
}
// console.log('largestCollateral', largestCollateral)
const {
flashTokenId,
flashTokenAmount,
beforeLiquidationSplit,
afterLiquidationSplit,
remaningReserveSplit
} = await prepareArgs(reserve, cdp.principalBorrows, largestCollateral)
// console.log('beforeLiquidationSplit', beforeLiquidationSplit)
// console.log('afterLiquidationSplit', afterLiquidationSplit)
// console.log('afterLiquidationSplit', remaningReserveSplit)
const data = await flashLiquidator.methods.liquidate(
flashTokenId,
flashTokenAmount,
user.id,
reserve.underlyingAsset,
largestCollateral.reserve.underlyingAsset,
beforeLiquidationSplit,
afterLiquidationSplit,
remaningReserveSplit
).encodeABI()
let nonce = await web3.eth.getTransactionCount(account.address);
const tx = {
from: web3.eth.defaultAccount,
value: "0x00",
gas: numberToHex(3500000),
gasPrice: toHex(toWei(fetcher.gasPrices.standard.toString(), "gwei")),
to: LIQUIDATOR_ADDRESS,
netId: 1,
data,
nonce,
};
return { tx, flashToken: marketIdToAddress(flashTokenId) }
}
function printCDP(previousUser: string, principalBorrows: BigNumber, reserve: Reserve, user: User, HF: number) {
if (previousUser !== user.id) {
console.log("\n=================NEXT USER CDPs===============\n");
} else {
console.log("");
}
previousUser = user.id;
console.log(
`There is a CDP of ${principalBorrows} ${reserve.symbol} ${reserve.underlyingAsset} ($${user.totalBorrowsUSD})`
);
console.log("Colaterals are:");
for (let {
principalATokenBalance,
reserve,
currentUnderlyingBalanceUSD
} of user.reservesData) {
if (Number(principalATokenBalance) > 0) {
console.log(
` ${principalATokenBalance} ${reserve.symbol} ${reserve.underlyingAsset} ($${currentUnderlyingBalanceUSD})`
);
}
}
console.log(`The healthFactor is ${user.healthFactor} (${HF})`);
console.log(`User address is ${user.id}`);
}
async function main() {
const cdps: CDP[] = await getCDPs();
console.log(`Start processing new ${cdps.length} CDPs`)
let previousUser = null;
for (let { principalBorrows, reserve, user } of cdps) {
if (user.totalBorrowsUSD.gt(5) && user.healthFactor > 0) {
const HF: number = await healthFactorFromContract(user.id);
if (HF >= 1) continue; // outdated data
if (previousUser === null) {
previousUser = user.id;
}
printCDP(previousUser, principalBorrows, reserve, user, HF)
const { tx, flashToken } = await liquidateTx({ principalBorrows, reserve, user })
try {
const gas = new BigNumber(await web3.eth.estimateGas(tx));
console.log("Gas required:", gas.toString());
const realProfitHex = (await web3.eth.call(tx)).toString();
let realProfit = Number(fromWei(
realProfitHex,
flashToken === USDC ? "picoether" : "ether" // usdc has 6 decimal
));
const price = await getPriceInUSD(flashToken);
realProfit = Number(price) * Number(realProfit);
if (realProfit === Infinity) {
throw new Error('eth_call failed');
}
console.log(`Real profit $${realProfit}`)
const ethPrice = await getPriceInUSD(WETH);
const expenceInWei = new BigNumber(toWei(fetcher.gasPrices.standard.toString(), "gwei"))
.multipliedBy(gas)
.toString();
const expense =
Number(fromWei(expenceInWei)) *
ethPrice;
console.log(`Tx cost $${expense} Gas price ${fetcher.gasPrices.standard} Gwei`);
if (realProfit > expense + 1) {
let signedTx = await web3.eth.accounts.signTransaction(
tx,
PRIVATE_KEY
);
let result = web3.eth.sendSignedTransaction(
signedTx.rawTransaction!
);
result
.once("transactionHash", function (txHash) {
console.log(
`Success: A new successfully sent tx https://etherscan.io/tx/${txHash}`
);
})
.on("error", async function (e) {
console.log("error", e.message);
});
}
} catch (e) {
console.error("Error: skipping tx ", e.message);
const message = await web3.eth.call(tx);
console.log("Error: Revert reason is", hexToAscii(toHex(message.toString())));
} finally {
tx.nonce = undefined
tx.netId = undefined
console.log('The tx is\n', JSON.stringify(tx))
}
}
}
console.log("Finished processing the whole aave market. Lets wait 60 sec and start again.")
setTimeout(main, 60000)
}
main();