@drift-labs/sdk 0.1.11 → 0.1.15
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/README.md +2 -2
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.js +17 -0
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -0
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/defaultUserAccountSubscriber.js +16 -0
- package/lib/accounts/types.d.ts +3 -21
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +2 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +13 -3
- package/lib/clearingHouse.d.ts +4 -0
- package/lib/clearingHouse.d.ts.map +1 -1
- package/lib/clearingHouse.js +8 -0
- package/lib/clearingHouseUser.d.ts +33 -5
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +224 -78
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +21 -0
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.d.ts.map +1 -1
- package/lib/examples/makeTradeExample.js +14 -13
- package/lib/idl/clearing_house.json +94 -42
- package/lib/index.d.ts +4 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -1
- package/lib/math/amm.d.ts +26 -1
- package/lib/math/amm.d.ts.map +1 -1
- package/lib/math/amm.js +85 -10
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.d.ts.map +1 -1
- package/lib/math/funding.js +69 -25
- package/lib/math/insuranceFund.d.ts +15 -0
- package/lib/math/insuranceFund.d.ts.map +1 -0
- package/lib/math/insuranceFund.js +36 -0
- package/lib/math/position.d.ts +1 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +15 -23
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.d.ts.map +1 -1
- package/lib/math/trade.js +9 -4
- package/lib/types.d.ts +0 -50
- package/lib/types.d.ts.map +1 -1
- package/lib/util/computeUnits.d.ts +3 -0
- package/lib/util/computeUnits.d.ts.map +1 -0
- package/lib/util/computeUnits.js +27 -0
- package/lib/util/tps.d.ts +3 -0
- package/lib/util/tps.d.ts.map +1 -0
- package/lib/util/tps.js +27 -0
- package/lib/wallet.d.ts +10 -0
- package/lib/wallet.d.ts.map +1 -0
- package/lib/wallet.js +35 -0
- package/package.json +3 -13
- package/src/accounts/defaultClearingHouseAccountSubscriber.ts +18 -0
- package/src/accounts/defaultUserAccountSubscriber.ts +18 -0
- package/src/accounts/types.ts +3 -28
- package/src/accounts/webSocketAccountSubscriber.ts +16 -6
- package/src/clearingHouse.ts +9 -3
- package/src/clearingHouseUser.ts +325 -107
- package/src/constants/markets.ts +21 -0
- package/src/constants/numericConstants.ts +2 -0
- package/src/examples/makeTradeExample.ts +2 -1
- package/src/idl/clearing_house.json +94 -42
- package/src/index.ts +4 -1
- package/src/math/amm.ts +120 -13
- package/src/math/funding.ts +109 -51
- package/src/math/insuranceFund.ts +29 -0
- package/src/math/position.ts +16 -28
- package/src/math/trade.ts +9 -5
- package/src/types.ts +0 -54
- package/src/util/computeUnits.ts +21 -0
- package/src/util/tps.ts +27 -0
- package/src/wallet.ts +22 -0
- package/.eslintrc.json +0 -36
- package/.prettierignore +0 -1
- package/.prettierrc.js +0 -9
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -29
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -179
|
@@ -194,6 +194,24 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
194
194
|
return true;
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
+
public async fetch(): Promise<void> {
|
|
198
|
+
if (!this.isSubscribed) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const promises = this.optionalExtraSubscriptions
|
|
203
|
+
.map((optionalSubscription) => {
|
|
204
|
+
const subscriber = `${optionalSubscription}Subscriber`;
|
|
205
|
+
return this[subscriber].fetch();
|
|
206
|
+
})
|
|
207
|
+
.concat([
|
|
208
|
+
this.stateAccountSubscriber.fetch(),
|
|
209
|
+
this.marketsAccountSubscriber.fetch(),
|
|
210
|
+
]);
|
|
211
|
+
|
|
212
|
+
await Promise.all(promises);
|
|
213
|
+
}
|
|
214
|
+
|
|
197
215
|
public async unsubscribe(): Promise<void> {
|
|
198
216
|
if (!this.isSubscribed) {
|
|
199
217
|
return;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AccountSubscriber,
|
|
3
|
+
NotSubscribedError,
|
|
3
4
|
UserAccountEvents,
|
|
4
5
|
UserAccountSubscriber,
|
|
5
6
|
} from './types';
|
|
@@ -65,6 +66,13 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
65
66
|
return true;
|
|
66
67
|
}
|
|
67
68
|
|
|
69
|
+
async fetch(): Promise<void> {
|
|
70
|
+
await Promise.all([
|
|
71
|
+
this.userDataAccountSubscriber.fetch(),
|
|
72
|
+
this.userPositionsAccountSubscriber.fetch(),
|
|
73
|
+
]);
|
|
74
|
+
}
|
|
75
|
+
|
|
68
76
|
async unsubscribe(): Promise<void> {
|
|
69
77
|
if (!this.isSubscribed) {
|
|
70
78
|
return;
|
|
@@ -76,11 +84,21 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
76
84
|
this.isSubscribed = false;
|
|
77
85
|
}
|
|
78
86
|
|
|
87
|
+
assertIsSubscribed(): void {
|
|
88
|
+
if (!this.isSubscribed) {
|
|
89
|
+
throw new NotSubscribedError(
|
|
90
|
+
'You must call `subscribe` before using this function'
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
79
95
|
public getUserAccount(): UserAccount {
|
|
96
|
+
this.assertIsSubscribed();
|
|
80
97
|
return this.userDataAccountSubscriber.data;
|
|
81
98
|
}
|
|
82
99
|
|
|
83
100
|
public getUserPositionsAccount(): UserPositionsAccount {
|
|
101
|
+
this.assertIsSubscribed();
|
|
84
102
|
return this.userPositionsAccountSubscriber.data;
|
|
85
103
|
}
|
|
86
104
|
}
|
package/src/accounts/types.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { EventEmitter } from 'events';
|
|
|
16
16
|
export interface AccountSubscriber<T> {
|
|
17
17
|
data?: T;
|
|
18
18
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
19
|
+
fetch(): Promise<void>;
|
|
19
20
|
unsubscribe(): void;
|
|
20
21
|
}
|
|
21
22
|
|
|
@@ -54,7 +55,7 @@ export interface ClearingHouseAccountSubscriber {
|
|
|
54
55
|
subscribe(
|
|
55
56
|
optionalSubscriptions?: ClearingHouseAccountTypes[]
|
|
56
57
|
): Promise<boolean>;
|
|
57
|
-
|
|
58
|
+
fetch(): Promise<void>;
|
|
58
59
|
unsubscribe(): Promise<void>;
|
|
59
60
|
|
|
60
61
|
getStateAccount(): StateAccount;
|
|
@@ -67,33 +68,6 @@ export interface ClearingHouseAccountSubscriber {
|
|
|
67
68
|
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
export interface HistoryAccountEvents {
|
|
71
|
-
fundingPaymentHistoryAccountUpdate: (
|
|
72
|
-
payload: FundingPaymentHistoryAccount
|
|
73
|
-
) => void;
|
|
74
|
-
fundingRateHistoryAccountUpdate: (payload: FundingRateHistoryAccount) => void;
|
|
75
|
-
tradeHistoryAccountUpdate: (payload: TradeHistoryAccount) => void;
|
|
76
|
-
liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
|
|
77
|
-
depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
|
|
78
|
-
curveHistoryAccountUpdate: (payload: CurveHistoryAccount) => void;
|
|
79
|
-
update: void;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface HistoryAccountSubscriber {
|
|
83
|
-
eventEmitter: StrictEventEmitter<EventEmitter, HistoryAccountEvents>;
|
|
84
|
-
isSubscribed: boolean;
|
|
85
|
-
|
|
86
|
-
subscribe(): Promise<boolean>;
|
|
87
|
-
unsubscribe(): Promise<void>;
|
|
88
|
-
|
|
89
|
-
getTradeHistoryAccount(): TradeHistoryAccount;
|
|
90
|
-
getDepositHistoryAccount(): DepositHistoryAccount;
|
|
91
|
-
getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
|
|
92
|
-
getFundingRateHistoryAccount(): FundingRateHistoryAccount;
|
|
93
|
-
getCurveHistoryAccount(): CurveHistoryAccount;
|
|
94
|
-
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
71
|
export interface UserAccountEvents {
|
|
98
72
|
userAccountData: (payload: UserAccount) => void;
|
|
99
73
|
userPositionsData: (payload: UserPositionsAccount) => void;
|
|
@@ -105,6 +79,7 @@ export interface UserAccountSubscriber {
|
|
|
105
79
|
isSubscribed: boolean;
|
|
106
80
|
|
|
107
81
|
subscribe(): Promise<boolean>;
|
|
82
|
+
fetch(): Promise<void>;
|
|
108
83
|
unsubscribe(): Promise<void>;
|
|
109
84
|
|
|
110
85
|
getUserAccount(): UserAccount;
|
|
@@ -7,6 +7,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
7
7
|
accountName: string;
|
|
8
8
|
program: Program;
|
|
9
9
|
accountPublicKey: PublicKey;
|
|
10
|
+
onChange: (data: T) => void;
|
|
10
11
|
|
|
11
12
|
public constructor(
|
|
12
13
|
accountName: string,
|
|
@@ -19,20 +20,29 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
async subscribe(onChange: (data: T) => void): Promise<void> {
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
)) as T;
|
|
25
|
-
|
|
26
|
-
onChange(this.data);
|
|
23
|
+
this.onChange = onChange;
|
|
24
|
+
await this.fetch();
|
|
27
25
|
|
|
28
26
|
this.program.account[this.accountName]
|
|
29
27
|
.subscribe(this.accountPublicKey, this.program.provider.opts.commitment)
|
|
30
28
|
.on('change', async (data: T) => {
|
|
31
29
|
this.data = data;
|
|
32
|
-
onChange(data);
|
|
30
|
+
this.onChange(data);
|
|
33
31
|
});
|
|
34
32
|
}
|
|
35
33
|
|
|
34
|
+
async fetch(): Promise<void> {
|
|
35
|
+
const newData = (await this.program.account[this.accountName].fetch(
|
|
36
|
+
this.accountPublicKey
|
|
37
|
+
)) as T;
|
|
38
|
+
|
|
39
|
+
// if data has changed trigger update
|
|
40
|
+
if (JSON.stringify(newData) !== JSON.stringify(this.data)) {
|
|
41
|
+
this.data = newData;
|
|
42
|
+
this.onChange(this.data);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
36
46
|
unsubscribe(): Promise<void> {
|
|
37
47
|
return this.program.account[this.accountName].unsubscribe(
|
|
38
48
|
this.accountPublicKey
|
package/src/clearingHouse.ts
CHANGED
|
@@ -16,7 +16,6 @@ import {
|
|
|
16
16
|
PositionDirection,
|
|
17
17
|
TradeHistoryAccount,
|
|
18
18
|
UserAccount,
|
|
19
|
-
UserPositionsAccount,
|
|
20
19
|
Market,
|
|
21
20
|
} from './types';
|
|
22
21
|
import * as anchor from '@project-serum/anchor';
|
|
@@ -140,6 +139,13 @@ export class ClearingHouse {
|
|
|
140
139
|
]);
|
|
141
140
|
}
|
|
142
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Forces the accountSubscriber to fetch account updates from rpc
|
|
144
|
+
*/
|
|
145
|
+
public async fetchAccounts(): Promise<void> {
|
|
146
|
+
await this.accountSubscriber.fetch();
|
|
147
|
+
}
|
|
148
|
+
|
|
143
149
|
/**
|
|
144
150
|
* Unsubscribe from all currently subscribed state accounts
|
|
145
151
|
*/
|
|
@@ -454,7 +460,7 @@ export class ClearingHouse {
|
|
|
454
460
|
collateralAccountPublicKey: PublicKey
|
|
455
461
|
): Promise<TransactionInstruction> {
|
|
456
462
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
457
|
-
const user:
|
|
463
|
+
const user: any = await this.program.account.user.fetch(
|
|
458
464
|
userAccountPublicKey
|
|
459
465
|
);
|
|
460
466
|
|
|
@@ -659,7 +665,7 @@ export class ClearingHouse {
|
|
|
659
665
|
const liquidateeUserAccount: any = await this.program.account.user.fetch(
|
|
660
666
|
liquidateeUserAccountPublicKey
|
|
661
667
|
);
|
|
662
|
-
const liquidateePositions:
|
|
668
|
+
const liquidateePositions: any =
|
|
663
669
|
await this.program.account.userPositions.fetch(
|
|
664
670
|
liquidateeUserAccount.positions
|
|
665
671
|
);
|