@drift-labs/sdk 0.2.0-master.5 → 0.2.0-master.6
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/accounts/types.d.ts +1 -0
- package/lib/clearingHouse.js +49 -30
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/package.json +1 -1
- package/src/clearingHouse.ts +56 -34
- package/src/slot/SlotSubscriber.ts +11 -1
package/lib/accounts/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
|
+
/// <reference types="node" />
|
|
3
4
|
import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount } from '../types';
|
|
4
5
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
6
|
import { EventEmitter } from 'events';
|
package/lib/clearingHouse.js
CHANGED
|
@@ -371,13 +371,19 @@ class ClearingHouse {
|
|
|
371
371
|
});
|
|
372
372
|
}
|
|
373
373
|
else {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
374
|
+
const bankAccount = this.getBankAccount(bankIndex);
|
|
375
|
+
if (!bankAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
376
|
+
remainingAccounts.push({
|
|
377
|
+
pubkey: bankAccount.oracle,
|
|
377
378
|
isSigner: false,
|
|
378
|
-
isWritable:
|
|
379
|
-
}
|
|
380
|
-
|
|
379
|
+
isWritable: false,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
remainingAccounts.push({
|
|
383
|
+
pubkey: bankAccount.pubkey,
|
|
384
|
+
isSigner: false,
|
|
385
|
+
isWritable: true,
|
|
386
|
+
});
|
|
381
387
|
}
|
|
382
388
|
const bank = this.getBankAccount(bankIndex);
|
|
383
389
|
return await this.program.instruction.deposit(bankIndex, amount, reduceOnly, {
|
|
@@ -590,44 +596,57 @@ class ClearingHouse {
|
|
|
590
596
|
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
591
597
|
const marketIndex = order.marketIndex;
|
|
592
598
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
593
|
-
const
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
599
|
+
const oracleAccountMap = new Map();
|
|
600
|
+
const bankAccountMap = new Map();
|
|
601
|
+
const marketAccountMap = new Map();
|
|
602
|
+
marketAccountMap.set(marketIndex.toNumber(), {
|
|
603
|
+
pubkey: marketAccount.pubkey,
|
|
604
|
+
isWritable: true,
|
|
605
|
+
isSigner: false,
|
|
606
|
+
});
|
|
607
|
+
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
608
|
+
pubkey: marketAccount.amm.oracle,
|
|
609
|
+
isWritable: false,
|
|
610
|
+
isSigner: false,
|
|
611
|
+
});
|
|
612
|
+
for (const bankBalance of userAccount.bankBalances) {
|
|
613
|
+
if (!bankBalance.balance.eq(numericConstants_1.ZERO)) {
|
|
614
|
+
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
615
|
+
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
616
|
+
pubkey: bankAccount.pubkey,
|
|
617
|
+
isSigner: false,
|
|
618
|
+
isWritable: false,
|
|
619
|
+
});
|
|
620
|
+
if (!bankAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
621
|
+
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
622
|
+
pubkey: bankAccount.oracle,
|
|
623
|
+
isSigner: false,
|
|
624
|
+
isWritable: false,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
614
629
|
for (const position of userAccount.positions) {
|
|
615
630
|
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
616
631
|
!position.marketIndex.eq(order.marketIndex)) {
|
|
617
632
|
const market = this.getMarketAccount(position.marketIndex);
|
|
618
|
-
|
|
633
|
+
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
619
634
|
pubkey: market.pubkey,
|
|
620
635
|
isWritable: true,
|
|
621
636
|
isSigner: false,
|
|
622
637
|
});
|
|
623
|
-
|
|
638
|
+
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
624
639
|
pubkey: market.amm.oracle,
|
|
625
640
|
isWritable: false,
|
|
626
641
|
isSigner: false,
|
|
627
642
|
});
|
|
628
643
|
}
|
|
629
644
|
}
|
|
630
|
-
const remainingAccounts =
|
|
645
|
+
const remainingAccounts = [
|
|
646
|
+
...oracleAccountMap.values(),
|
|
647
|
+
...bankAccountMap.values(),
|
|
648
|
+
...marketAccountMap.values(),
|
|
649
|
+
];
|
|
631
650
|
if (makerInfo) {
|
|
632
651
|
remainingAccounts.push({
|
|
633
652
|
pubkey: makerInfo.maker,
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { Connection } from '@solana/web3.js';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
2
5
|
declare type SlotSubscriberConfig = {};
|
|
6
|
+
export interface SlotSubscriberEvents {
|
|
7
|
+
newSlot: (newSlot: number) => void;
|
|
8
|
+
}
|
|
3
9
|
export declare class SlotSubscriber {
|
|
4
10
|
private connection;
|
|
5
11
|
currentSlot: number;
|
|
6
12
|
subscriptionId: number;
|
|
13
|
+
eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
|
|
7
14
|
constructor(connection: Connection, _config?: SlotSubscriberConfig);
|
|
8
15
|
subscribe(): Promise<void>;
|
|
9
16
|
getSlot(): number;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SlotSubscriber = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
4
5
|
class SlotSubscriber {
|
|
5
6
|
constructor(connection, _config) {
|
|
6
7
|
this.connection = connection;
|
|
8
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
7
9
|
}
|
|
8
10
|
async subscribe() {
|
|
9
11
|
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
10
12
|
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
11
13
|
this.currentSlot = slotInfo.slot;
|
|
14
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
12
15
|
});
|
|
13
16
|
}
|
|
14
17
|
getSlot() {
|
package/package.json
CHANGED
package/src/clearingHouse.ts
CHANGED
|
@@ -564,13 +564,19 @@ export class ClearingHouse {
|
|
|
564
564
|
writableBankIndex: bankIndex,
|
|
565
565
|
});
|
|
566
566
|
} else {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
567
|
+
const bankAccount = this.getBankAccount(bankIndex);
|
|
568
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
569
|
+
remainingAccounts.push({
|
|
570
|
+
pubkey: bankAccount.oracle,
|
|
570
571
|
isSigner: false,
|
|
571
|
-
isWritable:
|
|
572
|
-
}
|
|
573
|
-
|
|
572
|
+
isWritable: false,
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
remainingAccounts.push({
|
|
576
|
+
pubkey: bankAccount.pubkey,
|
|
577
|
+
isSigner: false,
|
|
578
|
+
isWritable: true,
|
|
579
|
+
});
|
|
574
580
|
}
|
|
575
581
|
|
|
576
582
|
const bank = this.getBankAccount(bankIndex);
|
|
@@ -972,48 +978,64 @@ export class ClearingHouse {
|
|
|
972
978
|
const marketIndex = order.marketIndex;
|
|
973
979
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
974
980
|
|
|
975
|
-
const
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
981
|
+
const oracleAccountMap = new Map<string, AccountMeta>();
|
|
982
|
+
const bankAccountMap = new Map<number, AccountMeta>();
|
|
983
|
+
const marketAccountMap = new Map<number, AccountMeta>();
|
|
984
|
+
|
|
985
|
+
marketAccountMap.set(marketIndex.toNumber(), {
|
|
986
|
+
pubkey: marketAccount.pubkey,
|
|
987
|
+
isWritable: true,
|
|
988
|
+
isSigner: false,
|
|
989
|
+
});
|
|
990
|
+
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
991
|
+
pubkey: marketAccount.amm.oracle,
|
|
992
|
+
isWritable: false,
|
|
993
|
+
isSigner: false,
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
for (const bankBalance of userAccount.bankBalances) {
|
|
997
|
+
if (!bankBalance.balance.eq(ZERO)) {
|
|
998
|
+
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
999
|
+
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
1000
|
+
pubkey: bankAccount.pubkey,
|
|
1001
|
+
isSigner: false,
|
|
1002
|
+
isWritable: false,
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
1006
|
+
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
1007
|
+
pubkey: bankAccount.oracle,
|
|
1008
|
+
isSigner: false,
|
|
1009
|
+
isWritable: false,
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
996
1015
|
for (const position of userAccount.positions) {
|
|
997
1016
|
if (
|
|
998
1017
|
!positionIsAvailable(position) &&
|
|
999
1018
|
!position.marketIndex.eq(order.marketIndex)
|
|
1000
1019
|
) {
|
|
1001
1020
|
const market = this.getMarketAccount(position.marketIndex);
|
|
1002
|
-
|
|
1021
|
+
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
1003
1022
|
pubkey: market.pubkey,
|
|
1004
1023
|
isWritable: true,
|
|
1005
1024
|
isSigner: false,
|
|
1006
1025
|
});
|
|
1007
|
-
|
|
1026
|
+
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
1008
1027
|
pubkey: market.amm.oracle,
|
|
1009
1028
|
isWritable: false,
|
|
1010
1029
|
isSigner: false,
|
|
1011
1030
|
});
|
|
1012
1031
|
}
|
|
1013
1032
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1033
|
+
|
|
1034
|
+
const remainingAccounts = [
|
|
1035
|
+
...oracleAccountMap.values(),
|
|
1036
|
+
...bankAccountMap.values(),
|
|
1037
|
+
...marketAccountMap.values(),
|
|
1038
|
+
];
|
|
1017
1039
|
|
|
1018
1040
|
if (makerInfo) {
|
|
1019
1041
|
remainingAccounts.push({
|
|
@@ -1166,7 +1188,7 @@ export class ClearingHouse {
|
|
|
1166
1188
|
|
|
1167
1189
|
public async placeAndMake(
|
|
1168
1190
|
orderParams: OptionalOrderParams,
|
|
1169
|
-
takerInfo: TakerInfo
|
|
1191
|
+
takerInfo: TakerInfo
|
|
1170
1192
|
): Promise<TransactionSignature> {
|
|
1171
1193
|
const { txSig, slot } = await this.txSender.send(
|
|
1172
1194
|
wrapInTx(await this.getPlaceAndMakeIx(orderParams, takerInfo)),
|
|
@@ -1181,7 +1203,7 @@ export class ClearingHouse {
|
|
|
1181
1203
|
|
|
1182
1204
|
public async getPlaceAndMakeIx(
|
|
1183
1205
|
orderParams: OptionalOrderParams,
|
|
1184
|
-
takerInfo: TakerInfo
|
|
1206
|
+
takerInfo: TakerInfo
|
|
1185
1207
|
): Promise<TransactionInstruction> {
|
|
1186
1208
|
orderParams = this.getOrderParams(orderParams);
|
|
1187
1209
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
import { Connection } from '@solana/web3.js';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
2
4
|
|
|
3
5
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
4
6
|
type SlotSubscriberConfig = {}; // for future customization
|
|
5
7
|
|
|
8
|
+
export interface SlotSubscriberEvents {
|
|
9
|
+
newSlot: (newSlot: number) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
6
12
|
export class SlotSubscriber {
|
|
7
13
|
currentSlot: number;
|
|
8
14
|
subscriptionId: number;
|
|
15
|
+
eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
|
|
9
16
|
|
|
10
17
|
public constructor(
|
|
11
18
|
private connection: Connection,
|
|
12
19
|
_config?: SlotSubscriberConfig
|
|
13
|
-
) {
|
|
20
|
+
) {
|
|
21
|
+
this.eventEmitter = new EventEmitter();
|
|
22
|
+
}
|
|
14
23
|
|
|
15
24
|
public async subscribe(): Promise<void> {
|
|
16
25
|
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
17
26
|
|
|
18
27
|
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
19
28
|
this.currentSlot = slotInfo.slot;
|
|
29
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
20
30
|
});
|
|
21
31
|
}
|
|
22
32
|
|