@drift-labs/sdk 2.52.0-beta.13 → 2.52.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/VERSION +1 -1
- package/examples/phoenix.ts +45 -26
- package/lib/phoenix/phoenixSubscriber.js +18 -16
- package/package.json +1 -1
- package/src/phoenix/phoenixSubscriber.ts +19 -20
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.52.0-beta.
|
|
1
|
+
2.52.0-beta.14
|
package/examples/phoenix.ts
CHANGED
|
@@ -1,38 +1,57 @@
|
|
|
1
1
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
BASE_PRECISION,
|
|
4
|
+
L2Level,
|
|
5
|
+
PRICE_PRECISION,
|
|
6
|
+
PhoenixSubscriber,
|
|
7
|
+
} from '../src';
|
|
3
8
|
import { PROGRAM_ID } from '@ellipsis-labs/phoenix-sdk';
|
|
4
9
|
|
|
5
10
|
export async function listenToBook(): Promise<void> {
|
|
6
11
|
const connection = new Connection('https://api.mainnet-beta.solana.com');
|
|
7
12
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
13
|
+
for (const market of [
|
|
14
|
+
'4DoNfFBfF7UokCC2FQzriy7yHK6DY6NVdYpuekQ5pRgg', // SOL/USDC
|
|
15
|
+
'Ew3vFDdtdGrknJAVVfraxCA37uNJtimXYPY4QjnfhFHH', // ETH/USDC
|
|
16
|
+
'2sTMN9A1D1qeZLF95XQgJCUPiKe5DiV52jLfZGqMP46m', // PYTH/USDC
|
|
17
|
+
'BRLLmdtPGuuFn3BU6orYw4KHaohAEptBToi3dwRUnHQZ', // JTO/USDC
|
|
18
|
+
]) {
|
|
19
|
+
const phoenixSubscriber = new PhoenixSubscriber({
|
|
20
|
+
connection,
|
|
21
|
+
programId: PROGRAM_ID,
|
|
22
|
+
marketAddress: new PublicKey(market),
|
|
23
|
+
accountSubscription: {
|
|
24
|
+
type: 'websocket',
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
await phoenixSubscriber.subscribe();
|
|
29
|
+
|
|
30
|
+
const bids = phoenixSubscriber.getL2Levels('bids');
|
|
31
|
+
const asks = phoenixSubscriber.getL2Levels('asks');
|
|
32
|
+
let bid: L2Level | null = null;
|
|
33
|
+
for (const b of bids) {
|
|
34
|
+
bid = b;
|
|
35
|
+
break;
|
|
27
36
|
}
|
|
28
|
-
|
|
29
|
-
for (const
|
|
30
|
-
|
|
37
|
+
let ask: L2Level | null = null;
|
|
38
|
+
for (const a of asks) {
|
|
39
|
+
ask = a;
|
|
40
|
+
break;
|
|
31
41
|
}
|
|
32
|
-
await new Promise((r) => setTimeout(r, 2000));
|
|
33
|
-
}
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
console.log('market', market);
|
|
44
|
+
console.log(
|
|
45
|
+
(bid?.size.toNumber() || 0) / BASE_PRECISION.toNumber(),
|
|
46
|
+
(bid?.price.toNumber() || 0) / PRICE_PRECISION.toNumber(),
|
|
47
|
+
'@',
|
|
48
|
+
(ask?.price.toNumber() || (1 << 53) - 1) / PRICE_PRECISION.toNumber(),
|
|
49
|
+
(ask?.size.toNumber() || 0) / BASE_PRECISION.toNumber()
|
|
50
|
+
);
|
|
51
|
+
console.log();
|
|
52
|
+
|
|
53
|
+
await phoenixSubscriber.unsubscribe();
|
|
54
|
+
}
|
|
36
55
|
}
|
|
37
56
|
|
|
38
57
|
(async function () {
|
|
@@ -99,23 +99,25 @@ class PhoenixSubscriber {
|
|
|
99
99
|
return this.getL2Levels('asks');
|
|
100
100
|
}
|
|
101
101
|
*getL2Levels(side) {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
const basePrecision = new anchor_1.BN(Math.pow(10, this.market.data.header.baseParams.decimals) *
|
|
106
|
-
baseLotsToRawBaseUnits);
|
|
107
|
-
const pricePrecision = numericConstants_1.PRICE_PRECISION.div(tickSize);
|
|
108
|
-
const ladder = (0, phoenix_sdk_1.getMarketLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 20);
|
|
102
|
+
const basePrecision = Math.pow(10, this.market.data.header.baseParams.decimals);
|
|
103
|
+
const pricePrecision = numericConstants_1.PRICE_PRECISION.toNumber();
|
|
104
|
+
const ladder = (0, phoenix_sdk_1.getMarketUiLadder)(this.market, this.lastSlot, this.lastUnixTimestamp, 20);
|
|
109
105
|
for (let i = 0; i < ladder[side].length; i++) {
|
|
110
|
-
const {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
106
|
+
const { price, quantity } = ladder[side][i];
|
|
107
|
+
try {
|
|
108
|
+
const size = new anchor_1.BN(quantity * basePrecision);
|
|
109
|
+
const updatedPrice = new anchor_1.BN(price * pricePrecision);
|
|
110
|
+
yield {
|
|
111
|
+
price: updatedPrice,
|
|
112
|
+
size,
|
|
113
|
+
sources: {
|
|
114
|
+
phoenix: size,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
async unsubscribe() {
|
package/package.json
CHANGED
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
toNum,
|
|
7
7
|
getMarketUiLadder,
|
|
8
8
|
Market,
|
|
9
|
-
getMarketLadder,
|
|
10
9
|
} from '@ellipsis-labs/phoenix-sdk';
|
|
11
10
|
import { PRICE_PRECISION } from '../constants/numericConstants';
|
|
12
11
|
import { BN } from '@coral-xyz/anchor';
|
|
@@ -164,18 +163,14 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
|
|
|
164
163
|
}
|
|
165
164
|
|
|
166
165
|
*getL2Levels(side: 'bids' | 'asks'): Generator<L2Level> {
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const basePrecision = new BN(
|
|
172
|
-
Math.pow(10, this.market.data.header.baseParams.decimals) *
|
|
173
|
-
baseLotsToRawBaseUnits
|
|
166
|
+
const basePrecision = Math.pow(
|
|
167
|
+
10,
|
|
168
|
+
this.market.data.header.baseParams.decimals
|
|
174
169
|
);
|
|
175
170
|
|
|
176
|
-
const pricePrecision = PRICE_PRECISION.
|
|
171
|
+
const pricePrecision = PRICE_PRECISION.toNumber();
|
|
177
172
|
|
|
178
|
-
const ladder =
|
|
173
|
+
const ladder = getMarketUiLadder(
|
|
179
174
|
this.market,
|
|
180
175
|
this.lastSlot,
|
|
181
176
|
this.lastUnixTimestamp,
|
|
@@ -183,18 +178,22 @@ export class PhoenixSubscriber implements L2OrderBookGenerator {
|
|
|
183
178
|
);
|
|
184
179
|
|
|
185
180
|
for (let i = 0; i < ladder[side].length; i++) {
|
|
186
|
-
const {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
181
|
+
const { price, quantity } = ladder[side][i];
|
|
182
|
+
try {
|
|
183
|
+
const size = new BN(quantity * basePrecision);
|
|
184
|
+
const updatedPrice = new BN(price * pricePrecision);
|
|
185
|
+
yield {
|
|
186
|
+
price: updatedPrice,
|
|
187
|
+
size,
|
|
188
|
+
sources: {
|
|
189
|
+
phoenix: size,
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
} catch {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
|
-
|
|
198
197
|
public async unsubscribe(): Promise<void> {
|
|
199
198
|
if (!this.subscribed) {
|
|
200
199
|
return;
|