-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.js
More file actions
261 lines (196 loc) · 7.62 KB
/
stats.js
File metadata and controls
261 lines (196 loc) · 7.62 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
import { getPositions, getTrades, getPrice } from './lib/api.js'
import { ADDRESS_ZERO } from './lib/constants.js'
import { initContract, formatUnits, formatToDisplay } from './lib/utils.js'
import { Table } from 'console-table-printer'
export default async function main() {
const { trading } = initContract();
if (!trading) return console.log('TRADING_CONTRACT null.');
// get positions
let positions = {
recent: await getPositions('recent'),
size: await getPositions('size')
}
// console.log('positions', positions);
let product_prices = {}; // product => product price
let product_info = {}; // product id => product info
let total_upl = {
ETH: 0,
USDC: 0
};
let total_margin = {
ETH: 0,
USDC: 0
};
let longs = {
ETH: 0,
USDC: 0
};
let shorts = {
ETH: 0,
USDC: 0
};
let volume = {
ETH: 0,
USDC: 0
};
let unique_owners = {};
const augmentPositions = async (type) => {
let i = 0;
for (let p of positions[type]) {
if (!p.productId) continue;
if (!product_prices[p.product]) {
let productInfo = await trading.getProduct(p.productId);
let price = await getPrice(p.product);
product_info[p.productId] = productInfo;
product_prices[p.product] = price;
}
const _p = JSON.parse(JSON.stringify(p));
let currency = _p.currency == ADDRESS_ZERO ? 'ETH' : 'USDC';
// Calculate unrealized p/l
let latestPrice = product_prices[p.product] * 1;
let upl = 0;
let interest = 0;
if (latestPrice) {
if (p.isLong) {
upl = p.size * (latestPrice * 1 - p.price * 1) / p.price;
} else {
upl = p.size * (p.price * 1 - latestPrice * 1) / p.price;
}
// Add interest
let now = parseInt(Date.now() / 1000);
if (now < p.createdAtTimestamp * 1 + 1800) {
//console.log('i1');
interest = 0;
} else {
//console.log('i2');
interest = p.size * ((product_info[p.productId].interest * 1 || 0) / 10000) * (now - p.createdAtTimestamp * 1) / (360 * 24 * 3600);
}
if (interest < 0) interest = 0;
upl -= interest;
}
p.canBeLiquidated = p.isLong && p.liquidationPrice*1 > latestPrice*1 || !p.isLong && p.liquidationPrice*1 < latestPrice*1 ? 'Y' : '';
p.upl = formatToDisplay(upl) * 1;
p.productPrice = formatToDisplay(latestPrice) * 1;
if (interest) p.interest = formatToDisplay(-1 * interest) * 1 || 0;
if (type == 'size') {
if (p.updatedAtTimestamp * 1000 >= Date.now() - 24 * 3600 * 1000) {
// volume since 24 hours
volume[currency] += p.size * 1;
}
total_margin[currency] += p.margin * 1;
total_upl[currency] += upl;
if (p.isLong) {
longs[currency] += p.size * 1;
} else {
shorts[currency] += p.size * 1;
}
}
p.createdAtTimestamp = new Date(p.createdAtTimestamp * 1000).toLocaleString();
p.updatedAtTimestamp = new Date(p.updatedAtTimestamp * 1000).toLocaleString();
p.margin = `${p.margin} ${currency}`;
p.size = `${p.size} ${currency}`;
p.upl = `${p.upl} ${currency}`;
let direction = _p.isLong == true ? '↑' : '↓';
p.product = `${direction} ${p.product}`;
delete p.productId;
delete p.currency;
delete p.isLong;
positions[type][i] = p;
unique_owners[p.user] = 1;
i++;
}
}
await augmentPositions('recent');
await augmentPositions('size');
// get trades
const _trades = await getTrades();
let trades = [];
for (let trade of _trades) {
let currency = trade.currency == ADDRESS_ZERO ? 'ETH' : 'USDC';
if (trade.timestamp * 1000 >= Date.now() - 24 * 3600 * 1000) {
volume[currency] += trade.size * 1;
}
trade.margin = `${trade.margin} ${currency}`;
trade.size = `${trade.size} ${currency}`;
trade.pnl = `${trade.pnl} ${currency}`;
trade.timestamp = new Date(trade.timestamp * 1000).toLocaleString();
let direction = trade.isLong == true ? '↑' : '↓';
trade.product = `${direction} ${trade.product}`;
delete trade.isLong;
delete trade.currency;
let duration;
if (trade.duration < 60) {
duration = trade.duration + 's';
} else if (trade.duration < 3600) {
duration = parseInt(trade.duration / 60) + 'm' + parseInt(trade.duration % 60) + 's';
} else {
duration = parseInt(trade.duration / 3600) + 'h' + parseInt((trade.duration % 3600)/60) + 'm';
}
trade.duration = duration;
trade.wasLiquidated = trade.wasLiquidated ? 'Y' : '';
trades.push(trade);
}
// Display in terminal
const p = new Table({
columns: [
{ name: 'product', title: 'Product', color: 'green' },
{ name: 'price', title: 'Entry Price', color: 'yellow' },
{ name: 'productPrice', title: 'Current Price', color: 'yellow' },
{ name: 'margin', title: 'Margin', color: 'yellow' },
{ name: 'size', title: 'Size', color: 'yellow' },
{ name: 'leverage', title: 'Lev', color: 'yellow' },
{ name: 'upl', title: 'UP/L', color: 'yellow' },
{ name: 'interest', title: 'Interest', color: 'yellow' },
{ name: 'liquidationPrice', title: 'Liq. Price', color: 'yellow' },
{ name: 'createdAtTimestamp', title: 'Created', color: 'green' },
{ name: 'updatedAtTimestamp', title: 'Updated', color: 'green' },
{ name: 'user', title: 'User', color: 'green' },
{ name: 'canBeLiquidated', title: 'Can Liq.', color: 'green' }
],
});
p.addRows(positions.size);
console.log("Positions sorted by Size");
console.log("Total UPL: " + formatToDisplay(total_upl.ETH) + " ETH, " + formatToDisplay(total_upl.USDC) + " USDC | Total Margin: ", formatToDisplay(total_margin.ETH) + " ETH, " + formatToDisplay(total_margin.USDC) + " USDC | Positions: " + positions.size.length + " | Unique Wallets: " + Object.keys(unique_owners).length);
console.log("Longs: " + formatToDisplay(longs.ETH) + " ETH, " + formatToDisplay(longs.USDC) + " USDC | Shorts: ", formatToDisplay(shorts.ETH) + " ETH, " + formatToDisplay(shorts.USDC) + " USDC");
console.log("Daily Volume: " + formatToDisplay(volume.ETH) + " ETH, " + formatToDisplay(volume.USDC) + " USDC");
p.printTable();
const p2 = new Table({
columns: [
{ name: 'product', title: 'Product', color: 'green' },
{ name: 'price', title: 'Entry Price', color: 'yellow' },
{ name: 'productPrice', title: 'Current Price', color: 'yellow' },
{ name: 'margin', title: 'Margin', color: 'yellow' },
{ name: 'size', title: 'Size', color: 'yellow' },
{ name: 'leverage', title: 'Lev', color: 'yellow' },
{ name: 'upl', title: 'UP/L', color: 'yellow' },
{ name: 'interest', title: 'Interest', color: 'yellow' },
{ name: 'liquidationPrice', title: 'Liq. Price', color: 'yellow' },
{ name: 'createdAtTimestamp', title: 'Created', color: 'green' },
{ name: 'updatedAtTimestamp', title: 'Updated', color: 'green' },
{ name: 'user', title: 'User', color: 'green' },
{ name: 'canBeLiquidated', title: 'Can Liq.', color: 'green' }
],
});
p2.addRows(positions.recent);
console.log("Positions sorted by Last Updated");
p2.printTable();
const p3 = new Table({
columns: [
{ name: 'product', title: 'Product', color: 'green' },
{ name: 'entryPrice', title: 'Entry Price', color: 'yellow' },
{ name: 'closePrice', title: 'Close Price', color: 'yellow' },
{ name: 'margin', title: 'Margin', color: 'yellow' },
{ name: 'size', title: 'Size', color: 'yellow' },
{ name: 'leverage', title: 'Lev', color: 'yellow' },
{ name: 'pnl', title: 'P/L', color: 'yellow' },
{ name: 'duration', title: 'Duration', color: 'yellow' },
{ name: 'timestamp', title: 'Closed At', color: 'green' },
{ name: 'user', title: 'User', color: 'green' },
{ name: 'wasLiquidated', title: 'Was Liq.', color: 'green' }
],
});
p3.addRows(trades);
console.log("Closed Trades");
p3.printTable();
}
main();