@drift-labs/sdk 0.1.9 → 0.1.13

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.
Files changed (56) hide show
  1. package/lib/accounts/types.d.ts +0 -21
  2. package/lib/accounts/types.d.ts.map +1 -1
  3. package/lib/clearingHouseUser.d.ts +29 -5
  4. package/lib/clearingHouseUser.d.ts.map +1 -1
  5. package/lib/clearingHouseUser.js +206 -78
  6. package/lib/constants/markets.d.ts.map +1 -1
  7. package/lib/constants/markets.js +21 -0
  8. package/lib/constants/numericConstants.d.ts +1 -0
  9. package/lib/constants/numericConstants.d.ts.map +1 -1
  10. package/lib/constants/numericConstants.js +2 -1
  11. package/lib/examples/makeTradeExample.d.ts.map +1 -1
  12. package/lib/examples/makeTradeExample.js +14 -13
  13. package/lib/idl/clearing_house.json +94 -42
  14. package/lib/index.d.ts +2 -1
  15. package/lib/index.d.ts.map +1 -1
  16. package/lib/index.js +2 -1
  17. package/lib/math/amm.d.ts +26 -1
  18. package/lib/math/amm.d.ts.map +1 -1
  19. package/lib/math/amm.js +84 -10
  20. package/lib/math/funding.d.ts +6 -6
  21. package/lib/math/funding.d.ts.map +1 -1
  22. package/lib/math/funding.js +69 -25
  23. package/lib/math/insuranceFund.d.ts +14 -0
  24. package/lib/math/insuranceFund.d.ts.map +1 -0
  25. package/lib/math/insuranceFund.js +35 -0
  26. package/lib/math/position.d.ts +7 -1
  27. package/lib/math/position.d.ts.map +1 -1
  28. package/lib/math/position.js +32 -24
  29. package/lib/math/trade.d.ts +1 -1
  30. package/lib/math/trade.d.ts.map +1 -1
  31. package/lib/math/trade.js +9 -4
  32. package/lib/types.d.ts +0 -50
  33. package/lib/types.d.ts.map +1 -1
  34. package/lib/wallet.d.ts +10 -0
  35. package/lib/wallet.d.ts.map +1 -0
  36. package/lib/wallet.js +35 -0
  37. package/package.json +2 -2
  38. package/src/accounts/types.ts +0 -27
  39. package/src/clearingHouse.ts +2 -2
  40. package/src/clearingHouseUser.ts +301 -107
  41. package/src/constants/markets.ts +21 -0
  42. package/src/constants/numericConstants.ts +3 -0
  43. package/src/examples/makeTradeExample.ts +2 -1
  44. package/src/idl/clearing_house.json +94 -42
  45. package/src/index.ts +2 -1
  46. package/src/math/amm.ts +119 -12
  47. package/src/math/funding.ts +109 -51
  48. package/src/math/insuranceFund.ts +22 -0
  49. package/src/math/position.ts +32 -26
  50. package/src/math/trade.ts +9 -5
  51. package/src/types.ts +0 -54
  52. package/src/wallet.ts +22 -0
  53. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -29
  54. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts.map +0 -1
  55. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  56. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -179
@@ -1,179 +0,0 @@
1
- import { ClearingHouseAccountEvents, HistoryAccountSubscriber } from './types';
2
- import { AccountSubscriber, NotSubscribedError } from './types';
3
- import {
4
- CurveHistoryAccount,
5
- DepositHistoryAccount,
6
- FundingPaymentHistoryAccount,
7
- FundingRateHistoryAccount,
8
- LiquidationHistoryAccount,
9
- StateAccount,
10
- TradeHistoryAccount,
11
- } from '../types';
12
- import { Program } from '@project-serum/anchor';
13
- import StrictEventEmitter from 'strict-event-emitter-types';
14
- import { EventEmitter } from 'events';
15
- import { getClearingHouseStateAccountPublicKey } from '../addresses';
16
- import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
17
-
18
- export class DefaultHistoryAccountSubscriber
19
- implements HistoryAccountSubscriber
20
- {
21
- isSubscribed: boolean;
22
- program: Program;
23
- eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
24
- tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
25
- depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
26
- fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
27
- fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
28
- curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
29
- liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
30
-
31
- public constructor(program: Program) {
32
- this.isSubscribed = false;
33
- this.program = program;
34
- this.eventEmitter = new EventEmitter();
35
- }
36
-
37
- public async subscribe(): Promise<boolean> {
38
- if (this.isSubscribed) {
39
- return true;
40
- }
41
-
42
- const statePublicKey = await getClearingHouseStateAccountPublicKey(
43
- this.program.programId
44
- );
45
- const state: StateAccount = await this.program.account.state.fetch(
46
- statePublicKey
47
- );
48
-
49
- this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
50
- 'tradeHistory',
51
- this.program,
52
- state.tradeHistory
53
- );
54
- await this.tradeHistoryAccountSubscriber.subscribe(
55
- (data: TradeHistoryAccount) => {
56
- this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
57
- this.eventEmitter.emit('update');
58
- }
59
- );
60
-
61
- this.depositHistoryAccountSubscriber = new WebSocketAccountSubscriber(
62
- 'depositHistory',
63
- this.program,
64
- state.depositHistory
65
- );
66
- await this.depositHistoryAccountSubscriber.subscribe(
67
- (data: DepositHistoryAccount) => {
68
- this.eventEmitter.emit('depositHistoryAccountUpdate', data);
69
- this.eventEmitter.emit('update');
70
- }
71
- );
72
-
73
- this.fundingPaymentHistoryAccountSubscriber =
74
- new WebSocketAccountSubscriber(
75
- 'fundingPaymentHistory',
76
- this.program,
77
- state.fundingPaymentHistory
78
- );
79
- await this.fundingPaymentHistoryAccountSubscriber.subscribe(
80
- (data: FundingPaymentHistoryAccount) => {
81
- this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
82
- this.eventEmitter.emit('update');
83
- }
84
- );
85
-
86
- this.fundingRateHistoryAccountSubscriber = new WebSocketAccountSubscriber(
87
- 'fundingRateHistory',
88
- this.program,
89
- state.fundingRateHistory
90
- );
91
- await this.fundingRateHistoryAccountSubscriber.subscribe(
92
- (data: FundingRateHistoryAccount) => {
93
- this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
94
- this.eventEmitter.emit('update');
95
- }
96
- );
97
-
98
- this.liquidationHistoryAccountSubscriber = new WebSocketAccountSubscriber(
99
- 'liquidationHistory',
100
- this.program,
101
- state.liquidationHistory
102
- );
103
- await this.liquidationHistoryAccountSubscriber.subscribe(
104
- (data: LiquidationHistoryAccount) => {
105
- this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
106
- this.eventEmitter.emit('update');
107
- }
108
- );
109
-
110
- this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
111
- 'curveHistory',
112
- this.program,
113
- state.curveHistory
114
- );
115
- await this.curveHistoryAccountSubscriber.subscribe(
116
- (data: CurveHistoryAccount) => {
117
- this.eventEmitter.emit('curveHistoryAccountUpdate', data);
118
- this.eventEmitter.emit('update');
119
- }
120
- );
121
-
122
- this.eventEmitter.emit('update');
123
-
124
- this.isSubscribed = true;
125
- return true;
126
- }
127
-
128
- public async unsubscribe(): Promise<void> {
129
- if (!this.isSubscribed) {
130
- return;
131
- }
132
-
133
- await this.tradeHistoryAccountSubscriber.unsubscribe();
134
- await this.fundingRateHistoryAccountSubscriber.unsubscribe();
135
- await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
136
- await this.depositHistoryAccountSubscriber.unsubscribe();
137
- await this.curveHistoryAccountSubscriber.unsubscribe();
138
- await this.liquidationHistoryAccountSubscriber.unsubscribe();
139
- this.isSubscribed = false;
140
- }
141
-
142
- assertIsSubscribed(): void {
143
- if (!this.isSubscribed) {
144
- throw new NotSubscribedError(
145
- 'You must call `subscribe` before using this function'
146
- );
147
- }
148
- }
149
-
150
- public getTradeHistoryAccount(): TradeHistoryAccount {
151
- this.assertIsSubscribed();
152
- return this.tradeHistoryAccountSubscriber.data;
153
- }
154
-
155
- public getDepositHistoryAccount(): DepositHistoryAccount {
156
- this.assertIsSubscribed();
157
- return this.depositHistoryAccountSubscriber.data;
158
- }
159
-
160
- public getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount {
161
- this.assertIsSubscribed();
162
- return this.fundingPaymentHistoryAccountSubscriber.data;
163
- }
164
-
165
- public getFundingRateHistoryAccount(): FundingRateHistoryAccount {
166
- this.assertIsSubscribed();
167
- return this.fundingRateHistoryAccountSubscriber.data;
168
- }
169
-
170
- public getCurveHistoryAccount(): CurveHistoryAccount {
171
- this.assertIsSubscribed();
172
- return this.curveHistoryAccountSubscriber.data;
173
- }
174
-
175
- public getLiquidationHistoryAccount(): LiquidationHistoryAccount {
176
- this.assertIsSubscribed();
177
- return this.liquidationHistoryAccountSubscriber.data;
178
- }
179
- }