@drift-labs/sdk 0.2.0-master.31 → 0.2.0-master.32
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/lib/dlob/DLOB.d.ts +1 -0
- package/lib/dlob/DLOB.js +29 -0
- package/lib/dlob/NodeList.d.ts +1 -0
- package/lib/dlob/NodeList.js +5 -0
- package/lib/types.d.ts +10 -10
- package/lib/types.js +10 -10
- package/package.json +1 -1
- package/src/dlob/DLOB.ts +33 -0
- package/src/dlob/NodeList.ts +6 -0
- package/src/types.ts +10 -10
- package/tests/bn/test.ts +1 -1
- package/tests/dlob/test.ts +80 -1
- package/src/assert/assert.js +0 -9
- package/src/events/eventList.js +0 -77
- package/src/examples/makeTradeExample.js +0 -157
- package/src/token/index.js +0 -38
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -43,6 +43,7 @@ export declare class DLOB {
|
|
|
43
43
|
* @param silent set to true to prevent logging on inserts and removals
|
|
44
44
|
*/
|
|
45
45
|
constructor(perpMarkets: PerpMarketAccount[], spotMarkets: SpotMarketAccount[], silent?: boolean);
|
|
46
|
+
clear(): void;
|
|
46
47
|
/**
|
|
47
48
|
* initializes a new DLOB instance
|
|
48
49
|
*
|
package/lib/dlob/DLOB.js
CHANGED
|
@@ -68,6 +68,29 @@ class DLOB {
|
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
+
clear() {
|
|
72
|
+
for (const marketType of this.openOrders.keys()) {
|
|
73
|
+
this.openOrders.get(marketType).clear();
|
|
74
|
+
}
|
|
75
|
+
this.openOrders.clear();
|
|
76
|
+
for (const marketType of this.orderLists.keys()) {
|
|
77
|
+
for (const marketIndex of this.orderLists.get(marketType).keys()) {
|
|
78
|
+
const marketNodeLists = this.orderLists
|
|
79
|
+
.get(marketType)
|
|
80
|
+
.get(marketIndex);
|
|
81
|
+
for (const side of Object.keys(marketNodeLists)) {
|
|
82
|
+
for (const orderType of Object.keys(marketNodeLists[side])) {
|
|
83
|
+
marketNodeLists[side][orderType].clear();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
this.orderLists.clear();
|
|
89
|
+
for (const marketType of this.marketIndexToAccount.keys()) {
|
|
90
|
+
this.marketIndexToAccount.get(marketType).clear();
|
|
91
|
+
}
|
|
92
|
+
this.marketIndexToAccount.clear();
|
|
93
|
+
}
|
|
71
94
|
/**
|
|
72
95
|
* initializes a new DLOB instance
|
|
73
96
|
*
|
|
@@ -309,6 +332,9 @@ class DLOB {
|
|
|
309
332
|
if (marketTypeStr === 'perp' && vAsk) {
|
|
310
333
|
generatorList.push((0, NodeList_1.getVammNodeGenerator)(vAsk));
|
|
311
334
|
}
|
|
335
|
+
if (generatorList.length === 0) {
|
|
336
|
+
throw new Error('No ask generators found');
|
|
337
|
+
}
|
|
312
338
|
const askGenerators = generatorList.map((generator) => {
|
|
313
339
|
return {
|
|
314
340
|
next: generator.next(),
|
|
@@ -358,6 +384,9 @@ class DLOB {
|
|
|
358
384
|
if (marketTypeStr === 'perp' && vBid) {
|
|
359
385
|
generatorList.push((0, NodeList_1.getVammNodeGenerator)(vBid));
|
|
360
386
|
}
|
|
387
|
+
if (generatorList.length === 0) {
|
|
388
|
+
throw new Error('No bid generators found');
|
|
389
|
+
}
|
|
361
390
|
const bidGenerators = generatorList.map((generator) => {
|
|
362
391
|
return {
|
|
363
392
|
next: generator.next(),
|
package/lib/dlob/NodeList.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export declare class NodeList<NodeType extends keyof DLOBNodeMap> implements DLO
|
|
|
14
14
|
length: number;
|
|
15
15
|
nodeMap: Map<string, DLOBNodeMap[NodeType]>;
|
|
16
16
|
constructor(nodeType: NodeType, sortDirection: SortDirection);
|
|
17
|
+
clear(): void;
|
|
17
18
|
insert(order: Order, marketType: MarketTypeStr, market: PerpMarketAccount | SpotMarketAccount, userAccount: PublicKey): void;
|
|
18
19
|
prependNode(currentNode: DLOBNodeMap[NodeType], newNode: DLOBNodeMap[NodeType]): boolean;
|
|
19
20
|
update(order: Order, userAccount: PublicKey): void;
|
package/lib/dlob/NodeList.js
CHANGED
|
@@ -14,6 +14,11 @@ class NodeList {
|
|
|
14
14
|
this.length = 0;
|
|
15
15
|
this.nodeMap = new Map();
|
|
16
16
|
}
|
|
17
|
+
clear() {
|
|
18
|
+
this.head = undefined;
|
|
19
|
+
this.length = 0;
|
|
20
|
+
this.nodeMap.clear();
|
|
21
|
+
}
|
|
17
22
|
insert(order, marketType, market, userAccount) {
|
|
18
23
|
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
19
24
|
return;
|
package/lib/types.d.ts
CHANGED
|
@@ -6,19 +6,19 @@ export declare class ExchangeStatus {
|
|
|
6
6
|
active: {};
|
|
7
7
|
};
|
|
8
8
|
static readonly FUNDINGPAUSED: {
|
|
9
|
-
|
|
9
|
+
fundingPaused: {};
|
|
10
10
|
};
|
|
11
11
|
static readonly AMMPAUSED: {
|
|
12
|
-
|
|
12
|
+
ammPaused: {};
|
|
13
13
|
};
|
|
14
14
|
static readonly FILLPAUSED: {
|
|
15
|
-
|
|
15
|
+
fillPaused: {};
|
|
16
16
|
};
|
|
17
17
|
static readonly LIQPAUSED: {
|
|
18
|
-
|
|
18
|
+
liqPaused: {};
|
|
19
19
|
};
|
|
20
20
|
static readonly WITHDRAWPAUSED: {
|
|
21
|
-
|
|
21
|
+
withdrawPaused: {};
|
|
22
22
|
};
|
|
23
23
|
static readonly PAUSED: {
|
|
24
24
|
paused: {};
|
|
@@ -32,19 +32,19 @@ export declare class MarketStatus {
|
|
|
32
32
|
active: {};
|
|
33
33
|
};
|
|
34
34
|
static readonly FUNDINGPAUSED: {
|
|
35
|
-
|
|
35
|
+
fundingPaused: {};
|
|
36
36
|
};
|
|
37
37
|
static readonly AMMPAUSED: {
|
|
38
|
-
|
|
38
|
+
ammPaused: {};
|
|
39
39
|
};
|
|
40
40
|
static readonly FILLPAUSED: {
|
|
41
|
-
|
|
41
|
+
fillPaused: {};
|
|
42
42
|
};
|
|
43
43
|
static readonly WITHDRAWPAUSED: {
|
|
44
|
-
|
|
44
|
+
withdrawPaused: {};
|
|
45
45
|
};
|
|
46
46
|
static readonly REDUCEONLY: {
|
|
47
|
-
|
|
47
|
+
reduceOnly: {};
|
|
48
48
|
};
|
|
49
49
|
static readonly SETTLEMENT: {
|
|
50
50
|
settlement: {};
|
package/lib/types.js
CHANGED
|
@@ -7,22 +7,22 @@ class ExchangeStatus {
|
|
|
7
7
|
}
|
|
8
8
|
exports.ExchangeStatus = ExchangeStatus;
|
|
9
9
|
ExchangeStatus.ACTIVE = { active: {} };
|
|
10
|
-
ExchangeStatus.FUNDINGPAUSED = {
|
|
11
|
-
ExchangeStatus.AMMPAUSED = {
|
|
12
|
-
ExchangeStatus.FILLPAUSED = {
|
|
13
|
-
ExchangeStatus.LIQPAUSED = {
|
|
14
|
-
ExchangeStatus.WITHDRAWPAUSED = {
|
|
10
|
+
ExchangeStatus.FUNDINGPAUSED = { fundingPaused: {} };
|
|
11
|
+
ExchangeStatus.AMMPAUSED = { ammPaused: {} };
|
|
12
|
+
ExchangeStatus.FILLPAUSED = { fillPaused: {} };
|
|
13
|
+
ExchangeStatus.LIQPAUSED = { liqPaused: {} };
|
|
14
|
+
ExchangeStatus.WITHDRAWPAUSED = { withdrawPaused: {} };
|
|
15
15
|
ExchangeStatus.PAUSED = { paused: {} };
|
|
16
16
|
class MarketStatus {
|
|
17
17
|
}
|
|
18
18
|
exports.MarketStatus = MarketStatus;
|
|
19
19
|
MarketStatus.INITIALIZED = { initialized: {} };
|
|
20
20
|
MarketStatus.ACTIVE = { active: {} };
|
|
21
|
-
MarketStatus.FUNDINGPAUSED = {
|
|
22
|
-
MarketStatus.AMMPAUSED = {
|
|
23
|
-
MarketStatus.FILLPAUSED = {
|
|
24
|
-
MarketStatus.WITHDRAWPAUSED = {
|
|
25
|
-
MarketStatus.REDUCEONLY = {
|
|
21
|
+
MarketStatus.FUNDINGPAUSED = { fundingPaused: {} };
|
|
22
|
+
MarketStatus.AMMPAUSED = { ammPaused: {} };
|
|
23
|
+
MarketStatus.FILLPAUSED = { fillPaused: {} };
|
|
24
|
+
MarketStatus.WITHDRAWPAUSED = { withdrawPaused: {} };
|
|
25
|
+
MarketStatus.REDUCEONLY = { reduceOnly: {} };
|
|
26
26
|
MarketStatus.SETTLEMENT = { settlement: {} };
|
|
27
27
|
MarketStatus.DELISTED = { delisted: {} };
|
|
28
28
|
class ContractType {
|
package/package.json
CHANGED
package/src/dlob/DLOB.ts
CHANGED
|
@@ -135,6 +135,32 @@ export class DLOB {
|
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
public clear() {
|
|
139
|
+
for (const marketType of this.openOrders.keys()) {
|
|
140
|
+
this.openOrders.get(marketType).clear();
|
|
141
|
+
}
|
|
142
|
+
this.openOrders.clear();
|
|
143
|
+
|
|
144
|
+
for (const marketType of this.orderLists.keys()) {
|
|
145
|
+
for (const marketIndex of this.orderLists.get(marketType).keys()) {
|
|
146
|
+
const marketNodeLists = this.orderLists
|
|
147
|
+
.get(marketType)
|
|
148
|
+
.get(marketIndex);
|
|
149
|
+
for (const side of Object.keys(marketNodeLists)) {
|
|
150
|
+
for (const orderType of Object.keys(marketNodeLists[side])) {
|
|
151
|
+
marketNodeLists[side][orderType].clear();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
this.orderLists.clear();
|
|
157
|
+
|
|
158
|
+
for (const marketType of this.marketIndexToAccount.keys()) {
|
|
159
|
+
this.marketIndexToAccount.get(marketType).clear();
|
|
160
|
+
}
|
|
161
|
+
this.marketIndexToAccount.clear();
|
|
162
|
+
}
|
|
163
|
+
|
|
138
164
|
/**
|
|
139
165
|
* initializes a new DLOB instance
|
|
140
166
|
*
|
|
@@ -516,6 +542,9 @@ export class DLOB {
|
|
|
516
542
|
if (marketTypeStr === 'perp' && vAsk) {
|
|
517
543
|
generatorList.push(getVammNodeGenerator(vAsk));
|
|
518
544
|
}
|
|
545
|
+
if (generatorList.length === 0) {
|
|
546
|
+
throw new Error('No ask generators found');
|
|
547
|
+
}
|
|
519
548
|
|
|
520
549
|
const askGenerators = generatorList.map((generator) => {
|
|
521
550
|
return {
|
|
@@ -588,6 +617,10 @@ export class DLOB {
|
|
|
588
617
|
if (marketTypeStr === 'perp' && vBid) {
|
|
589
618
|
generatorList.push(getVammNodeGenerator(vBid));
|
|
590
619
|
}
|
|
620
|
+
if (generatorList.length === 0) {
|
|
621
|
+
throw new Error('No bid generators found');
|
|
622
|
+
}
|
|
623
|
+
|
|
591
624
|
const bidGenerators = generatorList.map((generator) => {
|
|
592
625
|
return {
|
|
593
626
|
next: generator.next(),
|
package/src/dlob/NodeList.ts
CHANGED
|
@@ -34,6 +34,12 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
|
|
|
34
34
|
private sortDirection: SortDirection
|
|
35
35
|
) {}
|
|
36
36
|
|
|
37
|
+
public clear() {
|
|
38
|
+
this.head = undefined;
|
|
39
|
+
this.length = 0;
|
|
40
|
+
this.nodeMap.clear();
|
|
41
|
+
}
|
|
42
|
+
|
|
37
43
|
public insert(
|
|
38
44
|
order: Order,
|
|
39
45
|
marketType: MarketTypeStr,
|
package/src/types.ts
CHANGED
|
@@ -5,22 +5,22 @@ import { BN, ZERO } from '.';
|
|
|
5
5
|
|
|
6
6
|
export class ExchangeStatus {
|
|
7
7
|
static readonly ACTIVE = { active: {} };
|
|
8
|
-
static readonly FUNDINGPAUSED = {
|
|
9
|
-
static readonly AMMPAUSED = {
|
|
10
|
-
static readonly FILLPAUSED = {
|
|
11
|
-
static readonly LIQPAUSED = {
|
|
12
|
-
static readonly WITHDRAWPAUSED = {
|
|
8
|
+
static readonly FUNDINGPAUSED = { fundingPaused: {} };
|
|
9
|
+
static readonly AMMPAUSED = { ammPaused: {} };
|
|
10
|
+
static readonly FILLPAUSED = { fillPaused: {} };
|
|
11
|
+
static readonly LIQPAUSED = { liqPaused: {} };
|
|
12
|
+
static readonly WITHDRAWPAUSED = { withdrawPaused: {} };
|
|
13
13
|
static readonly PAUSED = { paused: {} };
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export class MarketStatus {
|
|
17
17
|
static readonly INITIALIZED = { initialized: {} };
|
|
18
18
|
static readonly ACTIVE = { active: {} };
|
|
19
|
-
static readonly FUNDINGPAUSED = {
|
|
20
|
-
static readonly AMMPAUSED = {
|
|
21
|
-
static readonly FILLPAUSED = {
|
|
22
|
-
static readonly WITHDRAWPAUSED = {
|
|
23
|
-
static readonly REDUCEONLY = {
|
|
19
|
+
static readonly FUNDINGPAUSED = { fundingPaused: {} };
|
|
20
|
+
static readonly AMMPAUSED = { ammPaused: {} };
|
|
21
|
+
static readonly FILLPAUSED = { fillPaused: {} };
|
|
22
|
+
static readonly WITHDRAWPAUSED = { withdrawPaused: {} };
|
|
23
|
+
static readonly REDUCEONLY = { reduceOnly: {} };
|
|
24
24
|
static readonly SETTLEMENT = { settlement: {} };
|
|
25
25
|
static readonly DELISTED = { delisted: {} };
|
|
26
26
|
}
|
package/tests/bn/test.ts
CHANGED
package/tests/dlob/test.ts
CHANGED
|
@@ -284,6 +284,85 @@ describe('DLOB Tests', () => {
|
|
|
284
284
|
expect(foundBids).to.equal(0);
|
|
285
285
|
}
|
|
286
286
|
});
|
|
287
|
+
|
|
288
|
+
it('Can clear DLOB', () => {
|
|
289
|
+
const vAsk = new BN(15);
|
|
290
|
+
const vBid = new BN(10);
|
|
291
|
+
const dlob = new DLOB(mockPerpMarkets, mockSpotMarkets, false);
|
|
292
|
+
const marketIndex = 0;
|
|
293
|
+
|
|
294
|
+
const slot = 12;
|
|
295
|
+
const oracle = {
|
|
296
|
+
price: vBid.add(vAsk).div(new BN(2)),
|
|
297
|
+
slot: new BN(slot),
|
|
298
|
+
confidence: new BN(1),
|
|
299
|
+
hasSufficientNumberOfDataPoints: true,
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
insertOrderToDLOB(
|
|
303
|
+
dlob,
|
|
304
|
+
Keypair.generate().publicKey,
|
|
305
|
+
OrderType.LIMIT,
|
|
306
|
+
MarketType.PERP,
|
|
307
|
+
0, // orderId
|
|
308
|
+
marketIndex,
|
|
309
|
+
new BN(9), // price
|
|
310
|
+
BASE_PRECISION, // quantity
|
|
311
|
+
PositionDirection.LONG,
|
|
312
|
+
vBid,
|
|
313
|
+
vAsk
|
|
314
|
+
);
|
|
315
|
+
insertOrderToDLOB(
|
|
316
|
+
dlob,
|
|
317
|
+
Keypair.generate().publicKey,
|
|
318
|
+
OrderType.LIMIT,
|
|
319
|
+
MarketType.PERP,
|
|
320
|
+
1, // orderId
|
|
321
|
+
marketIndex,
|
|
322
|
+
new BN(8), // price
|
|
323
|
+
BASE_PRECISION, // quantity
|
|
324
|
+
PositionDirection.LONG,
|
|
325
|
+
vBid,
|
|
326
|
+
vAsk
|
|
327
|
+
);
|
|
328
|
+
insertOrderToDLOB(
|
|
329
|
+
dlob,
|
|
330
|
+
Keypair.generate().publicKey,
|
|
331
|
+
OrderType.LIMIT,
|
|
332
|
+
MarketType.PERP,
|
|
333
|
+
2, // orderId
|
|
334
|
+
marketIndex,
|
|
335
|
+
new BN(8), // price
|
|
336
|
+
BASE_PRECISION, // quantity
|
|
337
|
+
PositionDirection.LONG,
|
|
338
|
+
vBid,
|
|
339
|
+
vAsk
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
const bids = dlob.getBids(
|
|
343
|
+
marketIndex,
|
|
344
|
+
undefined,
|
|
345
|
+
0,
|
|
346
|
+
MarketType.PERP,
|
|
347
|
+
oracle
|
|
348
|
+
);
|
|
349
|
+
let b = 0;
|
|
350
|
+
for (const _bid of bids) {
|
|
351
|
+
b++;
|
|
352
|
+
}
|
|
353
|
+
expect(b).to.equal(3);
|
|
354
|
+
|
|
355
|
+
dlob.clear();
|
|
356
|
+
let thrown = false;
|
|
357
|
+
try {
|
|
358
|
+
const bids1 = dlob.getBids(marketIndex, vBid, 0, MarketType.PERP, oracle);
|
|
359
|
+
bids1.next();
|
|
360
|
+
} catch (e) {
|
|
361
|
+
console.error(e);
|
|
362
|
+
thrown = true;
|
|
363
|
+
}
|
|
364
|
+
expect(thrown, 'should throw after clearing').to.equal(true);
|
|
365
|
+
});
|
|
287
366
|
});
|
|
288
367
|
|
|
289
368
|
describe('DLOB Perp Tests', () => {
|
|
@@ -393,7 +472,7 @@ describe('DLOB Perp Tests', () => {
|
|
|
393
472
|
const bids = dlob.getBids(marketIndex, vBid, slot, MarketType.PERP, oracle);
|
|
394
473
|
|
|
395
474
|
console.log('The Book Bids:');
|
|
396
|
-
const gotBids = [];
|
|
475
|
+
const gotBids: Array<DLOBNode> = [];
|
|
397
476
|
let countBids = 0;
|
|
398
477
|
for (const bid of bids) {
|
|
399
478
|
gotBids.push(bid);
|
package/src/assert/assert.js
DELETED
package/src/events/eventList.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EventList = void 0;
|
|
4
|
-
class Node {
|
|
5
|
-
constructor(event, next, prev) {
|
|
6
|
-
this.event = event;
|
|
7
|
-
this.next = next;
|
|
8
|
-
this.prev = prev;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
class EventList {
|
|
12
|
-
constructor(eventType, maxSize, sortFn, orderDirection) {
|
|
13
|
-
this.eventType = eventType;
|
|
14
|
-
this.maxSize = maxSize;
|
|
15
|
-
this.sortFn = sortFn;
|
|
16
|
-
this.orderDirection = orderDirection;
|
|
17
|
-
this.size = 0;
|
|
18
|
-
}
|
|
19
|
-
insert(event) {
|
|
20
|
-
this.size++;
|
|
21
|
-
const newNode = new Node(event);
|
|
22
|
-
if (this.head === undefined) {
|
|
23
|
-
this.head = this.tail = newNode;
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (this.sortFn(this.head.event, newNode.event) ===
|
|
27
|
-
(this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
|
|
28
|
-
this.head.prev = newNode;
|
|
29
|
-
newNode.next = this.head;
|
|
30
|
-
this.head = newNode;
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
let currentNode = this.head;
|
|
34
|
-
while (currentNode.next !== undefined &&
|
|
35
|
-
this.sortFn(currentNode.next.event, newNode.event) !==
|
|
36
|
-
(this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
|
|
37
|
-
currentNode = currentNode.next;
|
|
38
|
-
}
|
|
39
|
-
newNode.next = currentNode.next;
|
|
40
|
-
if (currentNode.next !== undefined) {
|
|
41
|
-
newNode.next.prev = newNode;
|
|
42
|
-
}
|
|
43
|
-
currentNode.next = newNode;
|
|
44
|
-
newNode.prev = currentNode;
|
|
45
|
-
}
|
|
46
|
-
if (this.size > this.maxSize) {
|
|
47
|
-
this.detach();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
detach() {
|
|
51
|
-
const node = this.tail;
|
|
52
|
-
if (node.prev !== undefined) {
|
|
53
|
-
node.prev.next = node.next;
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
this.head = node.next;
|
|
57
|
-
}
|
|
58
|
-
if (node.next !== undefined) {
|
|
59
|
-
node.next.prev = node.prev;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
this.tail = node.prev;
|
|
63
|
-
}
|
|
64
|
-
this.size--;
|
|
65
|
-
}
|
|
66
|
-
toArray() {
|
|
67
|
-
return Array.from(this);
|
|
68
|
-
}
|
|
69
|
-
*[Symbol.iterator]() {
|
|
70
|
-
let node = this.head;
|
|
71
|
-
while (node) {
|
|
72
|
-
yield node.event;
|
|
73
|
-
node = node.next;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.EventList = EventList;
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
var __awaiter =
|
|
3
|
-
(this && this.__awaiter) ||
|
|
4
|
-
function (thisArg, _arguments, P, generator) {
|
|
5
|
-
function adopt(value) {
|
|
6
|
-
return value instanceof P
|
|
7
|
-
? value
|
|
8
|
-
: new P(function (resolve) {
|
|
9
|
-
resolve(value);
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
-
function fulfilled(value) {
|
|
14
|
-
try {
|
|
15
|
-
step(generator.next(value));
|
|
16
|
-
} catch (e) {
|
|
17
|
-
reject(e);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function rejected(value) {
|
|
21
|
-
try {
|
|
22
|
-
step(generator['throw'](value));
|
|
23
|
-
} catch (e) {
|
|
24
|
-
reject(e);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function step(result) {
|
|
28
|
-
result.done
|
|
29
|
-
? resolve(result.value)
|
|
30
|
-
: adopt(result.value).then(fulfilled, rejected);
|
|
31
|
-
}
|
|
32
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
36
|
-
exports.getTokenAddress = void 0;
|
|
37
|
-
const anchor_1 = require('@project-serum/anchor');
|
|
38
|
-
const __1 = require('..');
|
|
39
|
-
const spl_token_1 = require('@solana/spl-token');
|
|
40
|
-
const web3_js_1 = require('@solana/web3.js');
|
|
41
|
-
const __2 = require('..');
|
|
42
|
-
const banks_1 = require('../constants/spotMarkets');
|
|
43
|
-
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
44
|
-
return spl_token_1.Token.getAssociatedTokenAddress(
|
|
45
|
-
new web3_js_1.PublicKey(`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`),
|
|
46
|
-
spl_token_1.TOKEN_PROGRAM_ID,
|
|
47
|
-
new web3_js_1.PublicKey(mintAddress),
|
|
48
|
-
new web3_js_1.PublicKey(userPubKey)
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
exports.getTokenAddress = getTokenAddress;
|
|
52
|
-
const main = () =>
|
|
53
|
-
__awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
-
// Initialize Drift SDK
|
|
55
|
-
const sdkConfig = __2.initialize({ env: 'devnet' });
|
|
56
|
-
// Set up the Wallet and Provider
|
|
57
|
-
const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
|
|
58
|
-
const keypair = web3_js_1.Keypair.fromSecretKey(
|
|
59
|
-
Uint8Array.from(JSON.parse(privateKey))
|
|
60
|
-
);
|
|
61
|
-
const wallet = new __1.Wallet(keypair);
|
|
62
|
-
// Set up the Connection
|
|
63
|
-
const rpcAddress = process.env.RPC_ADDRESS; // can use: https://api.devnet.solana.com for devnet; https://api.mainnet-beta.solana.com for mainnet;
|
|
64
|
-
const connection = new web3_js_1.Connection(rpcAddress);
|
|
65
|
-
// Set up the Provider
|
|
66
|
-
const provider = new anchor_1.AnchorProvider(
|
|
67
|
-
connection,
|
|
68
|
-
wallet,
|
|
69
|
-
anchor_1.AnchorProvider.defaultOptions()
|
|
70
|
-
);
|
|
71
|
-
// Check SOL Balance
|
|
72
|
-
const lamportsBalance = yield connection.getBalance(wallet.publicKey);
|
|
73
|
-
console.log('SOL balance:', lamportsBalance / Math.pow(10, 9));
|
|
74
|
-
// Misc. other things to set up
|
|
75
|
-
const usdcTokenAddress = yield exports.getTokenAddress(
|
|
76
|
-
sdkConfig.USDC_MINT_ADDRESS,
|
|
77
|
-
wallet.publicKey.toString()
|
|
78
|
-
);
|
|
79
|
-
// Set up the Drift Clearing House
|
|
80
|
-
const clearingHousePublicKey = new web3_js_1.PublicKey(
|
|
81
|
-
sdkConfig.CLEARING_HOUSE_PROGRAM_ID
|
|
82
|
-
);
|
|
83
|
-
const clearingHouse = new __2.ClearingHouse({
|
|
84
|
-
connection,
|
|
85
|
-
wallet: provider.wallet,
|
|
86
|
-
programID: clearingHousePublicKey,
|
|
87
|
-
});
|
|
88
|
-
yield clearingHouse.subscribe();
|
|
89
|
-
// Set up Clearing House user client
|
|
90
|
-
const user = new __2.ClearingHouseUser({
|
|
91
|
-
clearingHouse,
|
|
92
|
-
userAccountPublicKey: yield clearingHouse.getUserAccountPublicKey(),
|
|
93
|
-
});
|
|
94
|
-
//// Check if clearing house account exists for the current wallet
|
|
95
|
-
const userAccountExists = yield user.exists();
|
|
96
|
-
if (!userAccountExists) {
|
|
97
|
-
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
98
|
-
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
99
|
-
yield clearingHouse.initializeUserAccountAndDepositCollateral(
|
|
100
|
-
depositAmount,
|
|
101
|
-
yield exports.getTokenAddress(
|
|
102
|
-
usdcTokenAddress.toString(),
|
|
103
|
-
wallet.publicKey.toString()
|
|
104
|
-
),
|
|
105
|
-
banks_1.SpotMarkets['devnet'][0].marketIndex
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
yield user.subscribe();
|
|
109
|
-
// Get current price
|
|
110
|
-
const solMarketInfo = sdkConfig.PERP_MARKETS.find(
|
|
111
|
-
(market) => market.baseAssetSymbol === 'SOL'
|
|
112
|
-
);
|
|
113
|
-
const currentMarketPrice = __2.calculateMarkPrice(
|
|
114
|
-
clearingHouse.getMarketAccount(solMarketInfo.marketIndex),
|
|
115
|
-
undefined
|
|
116
|
-
);
|
|
117
|
-
const formattedPrice = __2.convertToNumber(
|
|
118
|
-
currentMarketPrice,
|
|
119
|
-
__2.PRICE_PRECISION
|
|
120
|
-
);
|
|
121
|
-
console.log(`Current Market Price is $${formattedPrice}`);
|
|
122
|
-
// Estimate the slippage for a $5000 LONG trade
|
|
123
|
-
const solMarketAccount = clearingHouse.getMarketAccount(
|
|
124
|
-
solMarketInfo.marketIndex
|
|
125
|
-
);
|
|
126
|
-
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
127
|
-
const slippage = __2.convertToNumber(
|
|
128
|
-
__2.calculateTradeSlippage(
|
|
129
|
-
__2.PositionDirection.LONG,
|
|
130
|
-
longAmount,
|
|
131
|
-
solMarketAccount,
|
|
132
|
-
'quote',
|
|
133
|
-
undefined
|
|
134
|
-
)[0],
|
|
135
|
-
__2.PRICE_PRECISION
|
|
136
|
-
);
|
|
137
|
-
console.log(
|
|
138
|
-
`Slippage for a $5000 LONG on the SOL market would be $${slippage}`
|
|
139
|
-
);
|
|
140
|
-
// Make a $5000 LONG trade
|
|
141
|
-
yield clearingHouse.openPosition(
|
|
142
|
-
__2.PositionDirection.LONG,
|
|
143
|
-
longAmount,
|
|
144
|
-
solMarketInfo.marketIndex
|
|
145
|
-
);
|
|
146
|
-
console.log(`LONGED $5000 SOL`);
|
|
147
|
-
// Reduce the position by $2000
|
|
148
|
-
const reduceAmount = new anchor_1.BN(2000).mul(__2.QUOTE_PRECISION);
|
|
149
|
-
yield clearingHouse.openPosition(
|
|
150
|
-
__2.PositionDirection.SHORT,
|
|
151
|
-
reduceAmount,
|
|
152
|
-
solMarketInfo.marketIndex
|
|
153
|
-
);
|
|
154
|
-
// Close the rest of the position
|
|
155
|
-
yield clearingHouse.closePosition(solMarketInfo.marketIndex);
|
|
156
|
-
});
|
|
157
|
-
main();
|
package/src/token/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseTokenAccount = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
function parseTokenAccount(data) {
|
|
7
|
-
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
-
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
-
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
-
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
-
if (accountInfo.delegateOption === 0) {
|
|
12
|
-
accountInfo.delegate = null;
|
|
13
|
-
// eslint-disable-next-line new-cap
|
|
14
|
-
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
-
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
-
}
|
|
20
|
-
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
-
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
-
if (accountInfo.isNativeOption === 1) {
|
|
23
|
-
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
-
accountInfo.isNative = true;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
accountInfo.rentExemptReserve = null;
|
|
28
|
-
accountInfo.isNative = false;
|
|
29
|
-
}
|
|
30
|
-
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
-
accountInfo.closeAuthority = null;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
-
}
|
|
36
|
-
return accountInfo;
|
|
37
|
-
}
|
|
38
|
-
exports.parseTokenAccount = parseTokenAccount;
|
package/src/tx/types.js
DELETED
package/src/tx/utils.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wrapInTx = void 0;
|
|
4
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
-
const COMPUTE_UNITS_DEFAULT = 200000;
|
|
6
|
-
function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
|
|
7
|
-
) {
|
|
8
|
-
const tx = new web3_js_1.Transaction();
|
|
9
|
-
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|
|
10
|
-
tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
11
|
-
units: computeUnits,
|
|
12
|
-
additionalFee: 0,
|
|
13
|
-
}));
|
|
14
|
-
}
|
|
15
|
-
return tx.add(instruction);
|
|
16
|
-
}
|
|
17
|
-
exports.wrapInTx = wrapInTx;
|
package/src/util/computeUnits.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.findComputeUnitConsumption = void 0;
|
|
13
|
-
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
-
const computeUnits = [];
|
|
17
|
-
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
-
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
-
const match = logMessage.match(regex);
|
|
20
|
-
if (match && match[1]) {
|
|
21
|
-
computeUnits.push(match[1]);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return computeUnits;
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokenAddress = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
-
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
-
};
|
|
9
|
-
exports.getTokenAddress = getTokenAddress;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.promiseTimeout = void 0;
|
|
4
|
-
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
-
let timeoutId;
|
|
6
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
-
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
-
});
|
|
9
|
-
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
-
clearTimeout(timeoutId);
|
|
11
|
-
return result;
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.estimateTps = void 0;
|
|
13
|
-
function estimateTps(programId, connection, failed) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
-
if (failed) {
|
|
17
|
-
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
-
}
|
|
19
|
-
const numberOfSignatures = signatures.length;
|
|
20
|
-
if (numberOfSignatures === 0) {
|
|
21
|
-
return 0;
|
|
22
|
-
}
|
|
23
|
-
return (numberOfSignatures /
|
|
24
|
-
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.estimateTps = estimateTps;
|