-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsREKT.ts
More file actions
304 lines (264 loc) · 10.4 KB
/
sREKT.ts
File metadata and controls
304 lines (264 loc) · 10.4 KB
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
import fs from 'fs';
import path from 'path';
const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));
const path_ranges = path.resolve(__dirname, `./data/ranges.txt`);
const path_memes = path.resolve(__dirname, `./data/memes.txt`);
import dotenv from 'dotenv';
dotenv.config();
import { ethers, Contract } from 'ethers';
import contracts from './node_modules/synthetix/publish/deployed/mainnet-ovm/deployment.json';
// https://github.com/Synthetixio/synthetix/blob/bf9d09d9d4d6d4222aaf4501592d602edf9e302d/contracts/PerpsV2MarketLiquidate.sol#L215
// keccak256("PositionLiquidated(uint256,address,address,int256,uint256,uint256,uint256,uint256)")
const liquidationEventHash = '0x8e83cfbf9c95216dce50909e376c0dcc3e23129a3aa1edd5013fa8b41648f883';
// https://github.com/Synthetixio/synthetix/blob/bf9d09d9d4d6d4222aaf4501592d602edf9e302d/contracts/PerpsV2MarketProxyable.sol#LL292C20-L292C106
// keccak256("PositionModified(uint256,address,uint256,int256,int256,uint256,uint256,uint256,int256)")
const positionModifiedHash = '0xc0d933baa356386a245ade48f9a9c59db4612af2b5b9c17de5b451c628760f43';
const eventABI = [
'event PositionLiquidated(uint256 id, address account, address liquidator, int256 size, uint256 price, uint256 flaggerFee, uint256 liquidatorFee, uint256 stakersFee)',
'event PositionModified(uint256 indexed id, address indexed account, uint256 margin, int256 size, int256 tradeSize, uint256 lastPrice, uint256 fundingIndex, uint256 fee, int256 skew)',
];
const providerOE = new ethers.providers.WebSocketProvider(process.env.API_KEY_OE_MAINNET || '');
const signer = new ethers.Wallet(process.env.PRIVATE_KEY ?? '', providerOE);
import { TwitterApi } from 'twitter-api-v2';
const appKey = process.env.TWITTER_API_KEY ?? '';
const appSecret = process.env.TWITTER_API_SECRET ?? '';
const accessToken = process.env.TWITTER_ACCESS_TOKEN ?? '';
const accessSecret = process.env.TWITTER_ACCESS_TOKEN_SECRET ?? '';
const twitter = new TwitterApi({
appKey: appKey,
appSecret: appSecret,
accessToken: accessToken,
accessSecret: accessSecret,
});
async function getMarkets() {
const managerABI = contracts.sources.FuturesMarketManager.abi;
const managerAddr = contracts.targets.FuturesMarketManager.address;
const manager = new ethers.Contract(managerAddr, managerABI, providerOE);
// overloaded function names need signature in ethers
const markets = await manager['allMarkets(bool)'](true);
// const marketABI = contracts.sources.PerpsV2Market.abi;
// const marketABI = contracts.sources.PerpsV2MarketLiquidate.abi;
const marketABI = eventABI;
return markets.map((address: string) => new ethers.Contract(address, marketABI, signer));
}
async function getMarketSymbols() {
const marketSymbols = new Map();
const perpsV2MarketABI = contracts.sources.PerpsV2Market.abi;
let targets = Object.entries(contracts.targets);
for (let i = 0; i < targets.length; i++) {
if (
targets[i][1]['source'].includes('PerpsV2Market') &&
targets[i][1]['source'] != 'PerpsV2MarketData' &&
targets[i][1]['source'] != 'PerpsV2MarketSettings' &&
targets[i][1]['source'] != 'PerpsV2MarketState' &&
targets[i][1]['source'] != 'PerpsV2MarketViews' &&
targets[i][1]['source'] != 'PerpsV2MarketDelayedOrders' &&
targets[i][1]['source'] != 'PerpsV2MarketDelayedOrdersOffchain' &&
targets[i][1]['source'] != 'PerpsV2MarketDelayedIntent' &&
targets[i][1]['source'] != 'PerpsV2MarketDelayedExecution' &&
targets[i][1]['source'] != 'PerpsV2MarketLiquidate'
) {
// console.log(targets[i][1]['source']);
const perpsV2Market = new ethers.Contract(
targets[i][1]['address'],
perpsV2MarketABI,
signer
);
const parentAddress = await perpsV2Market.proxy();
marketSymbols.set(
parentAddress,
'$'.concat(
'',
targets[i][1]['name'].replace('PerpsV2Market', '').replace('PERP', '')
)
);
}
}
return marketSymbols;
}
type Liquidations = {
marketSymbol: string;
posSize: string;
type: string;
price: string;
};
function makeFloat(input: string) {
return parseFloat(ethers.utils.formatEther(ethers.BigNumber.from(input)));
}
function loadRanges() {
const rangeFile = fs.readFileSync(path_ranges, { flag: 'r+' });
const rangeFileSplit = rangeFile.toString().replace(/\r\n/g, '\n').split('\n');
const ranges: number[] = [];
for (let i = 0; i < rangeFileSplit.length; i++) {
ranges.push(parseInt(rangeFileSplit[i]));
}
return ranges;
}
function loadMemes() {
const memeFile = fs.readFileSync(path_memes, { flag: 'r+' });
return memeFile.toString().replace(/\r\n/g, '\n').split('\n');
}
function getSkulls(liquidation: Liquidations) {
let ranges = loadRanges();
let value = makeFloat(liquidation.posSize) * makeFloat(liquidation.price);
let i = ranges.length - 1;
for (; i > 0; i--) {
if (value > ranges[i]) {
break;
}
}
let skulls = '💀'.repeat(i + 1);
return skulls;
}
function getFlavorText(liquidation: Liquidations) {
let ranges = loadRanges();
let memes = loadMemes();
let sizeOfMemeRange = Math.round(memes.length / ranges.length);
let value = makeFloat(liquidation.posSize) * makeFloat(liquidation.price);
let i = ranges.length - 1;
for (; i > 0; i--) {
if (value > ranges[i]) {
break;
}
}
// let rangeTopIndex = i * sizeOfMemeRange + (sizeOfMemeRange - 1);
let rangeBottomIndex = i * sizeOfMemeRange;
let memeIndex = Math.floor(Math.random() * sizeOfMemeRange) + rangeBottomIndex;
return memes[memeIndex];
}
function getTweet(liquidation: Liquidations) {
let dollarUSLocale = Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
useGrouping: false,
});
let skulls = getSkulls(liquidation);
let flavorText = getFlavorText(liquidation);
let tweet =
skulls +
' Liquidated ' +
ethers.utils.formatEther(ethers.BigNumber.from(liquidation.posSize)).substring(0, 7) +
' ' +
liquidation.marketSymbol +
' ' +
liquidation.type +
' @ ' +
dollarUSLocale.format(makeFloat(liquidation.price)) +
'\n\n' +
flavorText;
return tweet;
}
enum mutex {
Locked = 1,
Unlocked,
}
const tweetBuffer: string[] = [];
let tweetBufferMutex: mutex = mutex.Unlocked;
const addToTweetBuffer = async (tweet: string) => {
while (tweetBufferMutex == mutex.Locked) {
await delay(1000);
}
tweetBufferMutex = mutex.Locked;
tweetBuffer.push(tweet);
console.log('added tweet:', tweet);
tweetBufferMutex = mutex.Unlocked;
};
const publishFromTweetBuffer = async () => {
tweetBufferMutex = mutex.Locked;
while (tweetBuffer.length > 0) {
try {
let tweet = tweetBuffer.shift();
if (tweet) {
console.log('posted tweet:', tweet);
twitter.v2.tweet(tweet);
}
} catch (e) {
console.log(
new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') +
' publishFromTweetBuffer:' +
e
);
}
await delay(1000);
}
tweetBufferMutex = mutex.Unlocked;
};
async function main() {
console.log('Refreshing Markets');
const markets: Contract[] = await getMarkets();
const marketSymbols = await getMarketSymbols();
// const testLiq = {
// marketSymbol: marketSymbols.get('0x2B3bb4c683BFc5239B029131EEf3B1d214478d93'),
// posSize: '210194164287220310'.replace('-', ''),
// type: makeFloat('210194164287220310') > 0 ? 'LONG' : 'SHORT',
// price: '1239480485360000000000',
// };
// let testTweet = getTweet(testLiq);
// addToTweetBuffer('test1:' + testTweet);
// addToTweetBuffer('test2:' + testTweet);
// addToTweetBuffer('test3:' + testTweet);
// publishFromTweetBuffer();
// const eventABI = [
// 'event PositionLiquidated(uint256 id, address account, address liquidator, int256 size, uint256 price, uint256 flaggerFee, uint256 liquidatorFee, uint256 stakersFee)',
// 'event PositionModified(uint256 indexed id, address indexed account, uint256 margin, int256 size, int256 tradeSize, uint256 lastPrice, uint256 fundingIndex, uint256 fee, int256 skew)',
// ];
for (const market of markets) {
const filterLiquidation = {
address: market.address,
//topics: [liquidationEventHash],
topics: [
ethers.utils.id(
'PositionLiquidated(uint256,address,address,int256,uint256,uint256,uint256,uint256)'
),
],
};
market.on(
filterLiquidation,
//async (id, account, liquidator, size, price, flagFee, liqFee, margin, event) => {
async (
id,
account,
liquidator,
size,
price,
flaggerFee,
liquidatorFee,
stakersFee,
event
) => {
// console.log(
// 'liquidation:',
// id,
// account,
// liquidator,
// size,
// price,
// flaggerFee,
// liquidatorFee,
// stakersFee,
// event
// );
const liquidation = {
marketSymbol: marketSymbols.get(market.address),
posSize: size.toString().replace('-', ''),
type: makeFloat(size) > 0 ? 'LONG' : 'SHORT',
price: price.toString(),
};
if(liquidation.posSize >= 100000)
{
addToTweetBuffer(getTweet(liquidation));
publishFromTweetBuffer();
}
}
);
console.log(
`Listening for ${marketSymbols.get(market.address)} liquidations on contract ${
market.address
}`
);
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});