-
Notifications
You must be signed in to change notification settings - Fork 81
/
serumSubscriber.ts
169 lines (147 loc) · 4.22 KB
/
serumSubscriber.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
import { Connection, PublicKey } from '@solana/web3.js';
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
import { Market, Orderbook } from '@project-serum/serum';
import { SerumMarketSubscriberConfig } from './types';
import { BN } from '@coral-xyz/anchor';
import { PRICE_PRECISION } from '../constants/numericConstants';
import { L2Level, L2OrderBookGenerator } from '../dlob/orderBookLevels';
export class SerumSubscriber implements L2OrderBookGenerator {
connection: Connection;
programId: PublicKey;
marketAddress: PublicKey;
subscriptionType: 'polling' | 'websocket';
accountLoader: BulkAccountLoader | undefined;
market: Market;
subscribed: boolean;
asksAddress: PublicKey;
asks: Orderbook;
asksCallbackId: string | number;
lastAsksSlot: number;
bidsAddress: PublicKey;
bids: Orderbook;
bidsCallbackId: string | number;
lastBidsSlot: number;
public constructor(config: SerumMarketSubscriberConfig) {
this.connection = config.connection;
this.programId = config.programId;
this.marketAddress = config.marketAddress;
if (config.accountSubscription.type === 'polling') {
this.subscriptionType = 'polling';
this.accountLoader = config.accountSubscription.accountLoader;
} else {
this.subscriptionType = 'websocket';
}
}
public async subscribe(): Promise<void> {
if (this.subscribed) {
return;
}
this.market = await Market.load(
this.connection,
this.marketAddress,
undefined,
this.programId
);
this.asksAddress = this.market.asksAddress;
this.asks = await this.market.loadAsks(this.connection);
if (this.subscriptionType === 'websocket') {
this.asksCallbackId = this.connection.onAccountChange(
this.asksAddress,
(accountInfo, ctx) => {
this.lastAsksSlot = ctx.slot;
this.asks = Orderbook.decode(this.market, accountInfo.data);
}
);
} else {
this.asksCallbackId = await this.accountLoader.addAccount(
this.asksAddress,
(buffer, slot) => {
this.lastAsksSlot = slot;
this.asks = Orderbook.decode(this.market, buffer);
}
);
}
this.bidsAddress = this.market.bidsAddress;
this.bids = await this.market.loadBids(this.connection);
if (this.subscriptionType === 'websocket') {
this.bidsCallbackId = this.connection.onAccountChange(
this.bidsAddress,
(accountInfo, ctx) => {
this.lastBidsSlot = ctx.slot;
this.bids = Orderbook.decode(this.market, accountInfo.data);
}
);
} else {
this.bidsCallbackId = await this.accountLoader.addAccount(
this.bidsAddress,
(buffer, slot) => {
this.lastBidsSlot = slot;
this.bids = Orderbook.decode(this.market, buffer);
}
);
}
this.subscribed = true;
}
public getBestBid(): BN | undefined {
const bestBid = this.bids.getL2(1)[0];
if (!bestBid) {
return undefined;
}
return new BN(bestBid[0] * PRICE_PRECISION.toNumber());
}
public getBestAsk(): BN | undefined {
const bestAsk = this.asks.getL2(1)[0];
if (!bestAsk) {
return undefined;
}
return new BN(bestAsk[0] * PRICE_PRECISION.toNumber());
}
public getL2Bids(): Generator<L2Level> {
return this.getL2Levels('bids');
}
public getL2Asks(): Generator<L2Level> {
return this.getL2Levels('asks');
}
*getL2Levels(side: 'bids' | 'asks'): Generator<L2Level> {
// @ts-ignore
const basePrecision = Math.pow(10, this.market._baseSplTokenDecimals);
const pricePrecision = PRICE_PRECISION.toNumber();
for (const { price: priceNum, size: sizeNum } of this[side].items(
side === 'bids'
)) {
const price = new BN(priceNum * pricePrecision);
const size = new BN(sizeNum * basePrecision);
yield {
price,
size,
sources: {
serum: size,
},
};
}
}
public async unsubscribe(): Promise<void> {
if (!this.subscribed) {
return;
}
// remove listeners
if (this.subscriptionType === 'websocket') {
await this.connection.removeAccountChangeListener(
this.asksCallbackId as number
);
await this.connection.removeAccountChangeListener(
this.bidsCallbackId as number
);
} else {
this.accountLoader.removeAccount(
this.asksAddress,
this.asksCallbackId as string
);
this.accountLoader.removeAccount(
this.bidsAddress,
this.bidsCallbackId as string
);
}
this.subscribed = false;
}
}