forked from ZigZagExchange/market-maker
-
Notifications
You must be signed in to change notification settings - Fork 15
/
marketmaker.js
504 lines (454 loc) · 17.3 KB
/
marketmaker.js
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import WebSocket from 'ws';
import * as zksync from "zksync";
import ethers from 'ethers';
import dotenv from 'dotenv';
import fetch from 'node-fetch';
import fs from 'fs';
dotenv.config();
// Globals
const PRICE_FEEDS = {};
const OPEN_ORDERS = {};
const NONCES = {};
let ACCOUNT_STATE = null;
let ORDER_BROADCASTING = false;
const FILL_QUEUE = [];
// Load MM config
let MM_CONFIG;
if (process.env.MM_CONFIG) {
MM_CONFIG = JSON.parse(process.env.MM_CONFIG);
}
else {
const mmConfigFile = fs.readFileSync("config.json", "utf8");
MM_CONFIG = JSON.parse(mmConfigFile);
}
let activePairs = [];
for (let marketId in MM_CONFIG.pairs) {
const pair = MM_CONFIG.pairs[marketId];
if (pair.active) {
activePairs.push(marketId);
}
}
console.log("ACTIVE PAIRS", activePairs);
// Start price feeds
cryptowatchWsSetup();
// Initiate fill loop
setTimeout(processFillQueue, 1000);
// Connect to zksync
const CHAIN_ID = parseInt(MM_CONFIG.zigzagChainId);
const ETH_NETWORK = (CHAIN_ID === 1) ? "mainnet" : "rinkeby";
let syncWallet, ethersProvider, syncProvider, ethWallet,
fillOrdersInterval, indicateLiquidityInterval;
ethersProvider = ethers.getDefaultProvider(ETH_NETWORK);
try {
syncProvider = await zksync.getDefaultProvider(ETH_NETWORK);
ethWallet = new ethers.Wallet(process.env.ETH_PRIVKEY || MM_CONFIG.ethPrivKey);
syncWallet = await zksync.Wallet.fromEthSigner(ethWallet, syncProvider);
if (!(await syncWallet.isSigningKeySet())) {
console.log("setting sign key");
const signKeyResult = await syncWallet.setSigningKey({
feeToken: "ETH",
ethAuthType: "ECDSA",
});
console.log(signKeyResult);
}
} catch (e) {
console.log(e);
throw new Error("Could not connect to zksync API");
}
// Update account state loop
await updateAccountState();
setInterval(updateAccountState, 30000);
// Get markets info
const activePairsText = activePairs.join(',');
const markets_url = `https://zigzag-markets.herokuapp.com/markets?chainid=${CHAIN_ID}&id=${activePairsText}`
const markets = await fetch(markets_url).then(r => r.json());
if (markets.error) {
console.error(markets);
throw new Error(markets.error);
}
const MARKETS = {};
for (let i in markets) {
const market = markets[i];
MARKETS[market.id] = market;
if (market.alias) {
MARKETS[market.alias] = market;
}
}
let zigzagws = new WebSocket(MM_CONFIG.zigzagWsUrl);
zigzagws.on('open', onWsOpen);
zigzagws.on('error', console.error);
function onWsOpen() {
zigzagws.on('message', handleMessage);
zigzagws.on('close', onWsClose);
fillOrdersInterval = setInterval(fillOpenOrders, 5000);
for (let market in MM_CONFIG.pairs) {
if (MM_CONFIG.pairs[market].active) {
indicateLiquidityInterval = setInterval(() => indicateLiquidity(market), 5000);
const msg = {op:"subscribemarket", args:[CHAIN_ID, market]};
zigzagws.send(JSON.stringify(msg));
}
}
}
function onWsClose () {
console.log("Websocket closed. Restarting");
ORDER_BROADCASTING = false;
setTimeout(() => {
clearInterval(fillOrdersInterval)
clearInterval(indicateLiquidityInterval)
zigzagws = new WebSocket(MM_CONFIG.zigzagWsUrl);
zigzagws.on('open', onWsOpen);
zigzagws.on('error', onWsClose);
}, 5000);
}
async function handleMessage(json) {
const msg = JSON.parse(json);
if (!(["lastprice", "liquidity2"]).includes(msg.op)) console.log(json.toString());
switch(msg.op) {
case 'error':
ORDER_BROADCASTING = false;
break;
case 'orders':
const orders = msg.args[0];
orders.forEach(order => {
const orderid = order[1];
const fillable = isOrderFillable(order);
console.log(fillable);
if (fillable.fillable) {
FILL_QUEUE.push(order);
}
else if (fillable.reason === "badprice") {
OPEN_ORDERS[orderid] = order;
}
});
break
case "userordermatch":
const chainid = msg.args[0];
const orderid = msg.args[1];
try {
await broadcastfill(chainid, orderid, msg.args[2], msg.args[3]);
} catch (e) {
console.error(e);
}
ORDER_BROADCASTING = false;
break
default:
break
}
}
function isOrderFillable(order) {
const chainid = order[0];
const market_id = order[2];
const market = MARKETS[market_id];
const mmConfig = MM_CONFIG.pairs[market_id];
const mmSide = mmConfig.side || 'd';
if (chainid != CHAIN_ID) return { fillable: false, reason: "badchain" }
if (!market) return { fillable: false, reason: "badmarket" }
if (!mmConfig.active) return { fillable: false, reason: "inactivemarket" }
const baseQuantity = order[5];
const quoteQuantity = order[6];
const expires = order[7];
const side = order[3];
const price = order[4];
const sellCurrency = (side === 's') ? market.quoteAsset.symbol : market.baseAsset.symbol;
const sellDecimals = (side === 's') ? market.quoteAsset.decimals : market.baseAsset.decimals;
const sellQuantity = (side === 's') ? quoteQuantity : baseQuantity;
const balance = ACCOUNT_STATE.committed.balances[sellCurrency] / 10**sellDecimals;
const now = Date.now() / 1000 | 0;
if (now > expires) {
return { fillable: false, reason: "expired" };
}
if (mmSide !== 'd' && mmSide == side) {
return { fillable: false, reason: "badside" };
}
if (baseQuantity < mmConfig.minSize) {
return { fillable: false, reason: "badsize" };
}
else if (baseQuantity > mmConfig.maxSize) {
return { fillable: false, reason: "badsize" };
}
if (balance < sellQuantity) {
return { fillable: false, reason: "badbalance" };
}
let quote;
try {
quote = genquote(chainid, market_id, side, baseQuantity);
} catch (e) {
return { fillable: false, reason: e.message }
}
if (side == 's' && price > quote.quotePrice) {
return { fillable: false, reason: "badprice" };
}
else if (side == 'b' && price < quote.quotePrice) {
return { fillable: false, reason: "badprice" };
}
return { fillable: true, reason: null };
}
function genquote(chainid, market_id, side, baseQuantity) {
const market = MARKETS[market_id];
if (CHAIN_ID !== chainid) throw new Error("badchain");
if (!market) throw new Error("badmarket");
if (!(['b','s']).includes(side)) throw new Error("badside");
if (baseQuantity <= 0) throw new Error("badquantity");
validatePriceFeed(market_id);
const mmConfig = MM_CONFIG.pairs[market_id];
const mmSide = mmConfig.side || 'd';
if (mmConfig.side !== 'd' && mmConfig.side === side) {
throw new Error("badside");
}
const primaryPrice = getMidPrice(market_id);
if (!primaryPrice) throw new Error("badprice");
const SPREAD = mmConfig.minSpread + (baseQuantity * mmConfig.slippageRate);
let quoteQuantity;
if (side === 'b') {
quoteQuantity = (baseQuantity * primaryPrice * (1 + SPREAD)) + market.quoteFee;
}
else if (side === 's') {
quoteQuantity = (baseQuantity - market.baseFee) * primaryPrice * (1 - SPREAD);
}
const quotePrice = (quoteQuantity / baseQuantity).toPrecision(6);
if (quotePrice < 0) throw new Error("Amount is inadequate to pay fee");
if (isNaN(quotePrice)) throw new Error("Internal Error. No price generated.");
return { quotePrice, quoteQuantity };
}
function validatePriceFeed(market_id) {
const mmConfig = MM_CONFIG.pairs[market_id];
const mode = MM_CONFIG.pairs[market_id].mode || "pricefeed";
const initPrice = MM_CONFIG.pairs[market_id].initPrice;
const primaryPriceFeedId = MM_CONFIG.pairs[market_id].priceFeedPrimary;
const secondaryPriceFeedId = MM_CONFIG.pairs[market_id].priceFeedSecondary;
// Constant mode checks
if (mode === "constant") {
if (initPrice) return true;
else throw new Error("No initPrice available");
}
// Check if primary price exists
const primaryPrice = PRICE_FEEDS[primaryPriceFeedId];
if (!primaryPrice) throw new Error("Primary price feed unavailable");
// If there is no secondary price feed, the price auto-validates
if (!secondaryPriceFeedId) return true;
// Check if secondary price exists
const secondaryPrice = PRICE_FEEDS[secondaryPriceFeedId];
if (!secondaryPrice) throw new Error("Secondary price feed unavailable");
// If the secondary price feed varies from the primary price feed by more than 1%, assume something is broken
const percentDiff = Math.abs(primaryPrice - secondaryPrice) / primaryPrice;
if (percentDiff > 0.03) {
throw new Error("Circuit breaker triggered");
}
return true;
}
async function sendfillrequest(orderreceipt) {
const chainId = orderreceipt[0];
const orderId = orderreceipt[1];
const market_id = orderreceipt[2];
const market = MARKETS[market_id];
const baseCurrency = market.baseAssetId;
const quoteCurrency = market.quoteAssetId;
const side = orderreceipt[3];
const baseQuantity = orderreceipt[5];
const quoteQuantity = orderreceipt[6];
const quote = genquote(chainId, market_id, side, baseQuantity);
let tokenSell, tokenBuy, sellQuantity, buyQuantity;
if (side === "b") {
tokenSell = market.baseAssetId;
tokenBuy = market.quoteAssetId;
// Add 1 bip to to protect against rounding errors
sellQuantity = (baseQuantity * 1.0001).toFixed(market.baseAsset.decimals);
buyQuantity = (quote.quoteQuantity * 0.9999).toFixed(market.quoteAsset.decimals);
} else if (side === "s") {
tokenSell = market.quoteAssetId;
tokenBuy = market.baseAssetId;
// Add 1 bip to to protect against rounding errors
sellQuantity = (quote.quoteQuantity * 1.0001).toFixed(market.quoteAsset.decimals);
buyQuantity = (baseQuantity * 0.9999).toFixed(market.baseAsset.decimals);
}
const sellQuantityParsed = syncProvider.tokenSet.parseToken(
tokenSell,
sellQuantity
);
const sellQuantityPacked = zksync.utils.closestPackableTransactionAmount(sellQuantityParsed);
const tokenRatio = {};
tokenRatio[tokenBuy] = buyQuantity;
tokenRatio[tokenSell] = sellQuantity;
const one_min_expiry = (Date.now() / 1000 | 0) + 60;
const orderDetails = {
tokenSell,
tokenBuy,
amount: sellQuantityPacked,
ratio: zksync.utils.tokenRatio(tokenRatio),
validUntil: one_min_expiry
}
const fillOrder = await syncWallet.getOrder(orderDetails);
// Set global flag
ORDER_BROADCASTING = true;
const resp = { op: "fillrequest", args: [chainId, orderId, fillOrder] };
zigzagws.send(JSON.stringify(resp));
}
async function broadcastfill(chainid, orderid, swapOffer, fillOrder) {
// Nonce check
const nonce = swapOffer.nonce;
const userNonce = NONCES[swapOffer.accountId];
if (nonce <= userNonce) {
throw new Error("badnonce");
}
const randint = (Math.random()*1000).toFixed(0);
console.time('syncswap' + randint);
const swap = await syncWallet.syncSwap({
orders: [swapOffer, fillOrder],
feeToken: "ETH",
nonce: fillOrder.nonce
});
const txhash = swap.txHash.split(":")[1];
const txhashmsg = {op:"orderstatusupdate", args:[[[chainid,orderid,'b',txhash]]]}
zigzagws.send(JSON.stringify(txhashmsg));
console.timeEnd('syncswap' + randint);
console.time('receipt' + randint);
let receipt, success = false;
try {
receipt = await swap.awaitReceipt();
if (receipt.success) {
success = true;
NONCES[swapOffer.accountId] = swapOffer.nonce;
}
} catch (e) {
receipt = null;
success = false;
}
console.timeEnd('receipt' + randint);
console.log("Swap broadcast result", {swap, receipt});
const newstatus = success ? 'f' : 'r';
const error = success ? null : swap.error.toString();
const ordercommitmsg = {op:"orderstatusupdate", args:[[[chainid,orderid,newstatus,txhash,error]]]}
zigzagws.send(JSON.stringify(ordercommitmsg));
}
async function fillOpenOrders() {
for (let orderid in OPEN_ORDERS) {
const order = OPEN_ORDERS[orderid];
const fillable = isOrderFillable(order);
if (fillable.fillable) {
FILL_QUEUE.push(order);
delete OPEN_ORDERS[orderid];
}
else if (fillable.reason !== "badprice") {
delete OPEN_ORDERS[orderid];
}
}
}
async function processFillQueue() {
if (ORDER_BROADCASTING) {
setTimeout(processFillQueue, 100);
return false;
}
if (FILL_QUEUE.length === 0) {
setTimeout(processFillQueue, 100);
return false;
}
const order = FILL_QUEUE.shift();
try {
await sendfillrequest(order);
} catch (e) {
console.error(e);
ORDER_BROADCASTING = false;
}
setTimeout(processFillQueue, 50);
}
async function cryptowatchWsSetup() {
const cryptowatch_market_ids = [];
for (let market in MM_CONFIG.pairs) {
const primaryPriceFeed = MM_CONFIG.pairs[market].priceFeedPrimary;
const secondaryPriceFeed = MM_CONFIG.pairs[market].priceFeedSecondary;
if (primaryPriceFeed) cryptowatch_market_ids.push(primaryPriceFeed);
if (secondaryPriceFeed) cryptowatch_market_ids.push(secondaryPriceFeed);
}
// Set initial prices
const cryptowatchApiKey = process.env.CRYPTOWATCH_API_KEY || MM_CONFIG.cryptowatchApiKey;
const cryptowatch_markets = await fetch("https://api.cryptowat.ch/markets?apikey=" + cryptowatchApiKey).then(r => r.json());
const cryptowatch_market_prices = await fetch("https://api.cryptowat.ch/markets/prices?apikey=" + cryptowatchApiKey).then(r => r.json());
for (let i in cryptowatch_market_ids) {
const cryptowatch_market_id = cryptowatch_market_ids[i].split(":")[1];
const cryptowatch_market = cryptowatch_markets.result.find(row => row.id == cryptowatch_market_id);
const exchange = cryptowatch_market.exchange;
const pair = cryptowatch_market.pair;
const key = `market:${exchange}:${pair}`;
PRICE_FEEDS[cryptowatch_market_ids[i]] = cryptowatch_market_prices.result[key];
}
console.log(PRICE_FEEDS);
const subscriptionMsg = {
"subscribe": {
"subscriptions": []
}
}
for (let i in cryptowatch_market_ids) {
const cryptowatch_market_id = cryptowatch_market_ids[i].split(":")[1];
// first get initial price info
subscriptionMsg.subscribe.subscriptions.push({
"streamSubscription": {
"resource": `markets:${cryptowatch_market_id}:trades`
}
})
}
let cryptowatch_ws = new WebSocket("wss://stream.cryptowat.ch/connect?apikey=" + cryptowatchApiKey);
cryptowatch_ws.on('open', onopen);
cryptowatch_ws.on('message', onmessage);
cryptowatch_ws.on('close', onclose);
function onopen() {
cryptowatch_ws.send(JSON.stringify(subscriptionMsg));
}
function onmessage (data) {
const msg = JSON.parse(data);
if (!msg.marketUpdate) return;
const market_id = "cryptowatch:" + msg.marketUpdate.market.marketId;
let trades = msg.marketUpdate.tradesUpdate.trades;
let price = trades[trades.length - 1].priceStr / 1;
PRICE_FEEDS[market_id] = price;
};
function onclose () {
setTimeout(cryptowatchWsSetup, 5000);
}
}
const CLIENT_ID = (Math.random() * 100000).toString(16);
function indicateLiquidity (market_id) {
try {
validatePriceFeed(market_id);
} catch(e) {
return false;
}
const marketInfo = MARKETS[market_id];
const mmConfig = MM_CONFIG.pairs[market_id];
const midPrice = getMidPrice(market_id);
const expires = (Date.now() / 1000 | 0) + 10; // 10s expiry
const side = mmConfig.side || 'd';
const baseBalance = ACCOUNT_STATE.committed.balances[marketInfo.baseAsset.symbol] / 10**marketInfo.baseAsset.decimals;
const quoteBalance = ACCOUNT_STATE.committed.balances[marketInfo.quoteAsset.symbol] / 10**marketInfo.quoteAsset.decimals;
const maxSellSize = Math.min(baseBalance, mmConfig.maxSize);
const maxBuySize = Math.min(quoteBalance / midPrice, mmConfig.maxSize);
if (!midPrice) return false;
const splits = 10;
const liquidity = [];
for (let i=1; i <= splits; i++) {
const buyPrice = midPrice * (1 - mmConfig.minSpread - (mmConfig.slippageRate * maxBuySize * i/splits));
const sellPrice = midPrice * (1 + mmConfig.minSpread + (mmConfig.slippageRate * maxSellSize * i/splits));
if ((['b','d']).includes(side)) {
liquidity.push(["b", buyPrice, maxBuySize / splits, expires]);
}
if ((['s','d']).includes(side)) {
liquidity.push(["s", sellPrice, maxSellSize / splits, expires]);
}
}
const msg = { op: "indicateliq2", args: [CHAIN_ID, market_id, liquidity, CLIENT_ID] };
zigzagws.send(JSON.stringify(msg));
}
function getMidPrice (market_id) {
const mmConfig = MM_CONFIG.pairs[market_id];
const mode = mmConfig.mode || "pricefeed";
let midPrice;
if (mode == "constant") {
midPrice = mmConfig.initPrice;
}
else if (mode == "pricefeed") {
midPrice = PRICE_FEEDS[mmConfig.priceFeedPrimary];
}
return midPrice;
}
async function updateAccountState() {
ACCOUNT_STATE = await syncWallet.getAccountState();
}