@drift-labs/sdk 0.1.18-master.8 → 0.1.18-orders.3
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/bulkAccountLoader.d.ts +32 -0
- package/lib/accounts/bulkAccountLoader.js +156 -0
- package/lib/accounts/bulkUserSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +49 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +228 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +25 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +32 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +133 -0
- package/lib/accounts/types.d.ts +34 -3
- package/lib/accounts/utils.d.ts +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +8 -3
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +30 -4
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +6 -3
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +16 -4
- package/lib/addresses.d.ts +4 -1
- package/lib/addresses.js +28 -1
- package/lib/admin.d.ts +11 -4
- package/lib/admin.js +71 -17
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +35 -4
- package/lib/clearingHouse.js +294 -23
- package/lib/clearingHouseUser.d.ts +19 -18
- package/lib/clearingHouseUser.js +129 -101
- package/lib/config.d.ts +0 -1
- package/lib/config.js +1 -1
- package/lib/constants/markets.d.ts +0 -1
- package/lib/constants/numericConstants.d.ts +0 -1
- package/lib/examples/makeTradeExample.d.ts +0 -1
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/clearingHouse.d.ts +25 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +19 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/idl/clearing_house.json +1077 -54
- package/lib/index.d.ts +11 -3
- package/lib/index.js +12 -2
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +38 -15
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +0 -1
- package/lib/math/funding.js +1 -1
- package/lib/math/insuranceFund.d.ts +0 -1
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.js +12 -2
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +30 -0
- package/lib/math/position.d.ts +4 -2
- package/lib/math/position.js +19 -5
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +2 -3
- package/lib/math/utils.js +6 -3
- package/lib/mockUSDCFaucet.d.ts +0 -1
- package/lib/orderParams.d.ts +7 -0
- package/lib/orderParams.js +108 -0
- package/lib/orders.d.ts +5 -0
- package/lib/orders.js +136 -0
- package/lib/pythClient.d.ts +0 -1
- package/lib/pythClient.js +1 -1
- package/lib/token/index.d.ts +3 -0
- package/lib/token/index.js +38 -0
- package/lib/tx/defaultTxSender.d.ts +0 -1
- package/lib/tx/types.d.ts +0 -1
- package/lib/tx/utils.d.ts +0 -1
- package/lib/types.d.ts +144 -2
- package/lib/types.js +36 -1
- package/lib/util/computeUnits.d.ts +0 -1
- package/lib/util/tps.d.ts +0 -1
- package/lib/wallet.d.ts +0 -1
- package/package.json +3 -3
- package/src/accounts/bulkAccountLoader.ts +195 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +326 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +186 -0
- package/src/accounts/types.ts +43 -1
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +51 -1
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +33 -3
- package/src/addresses.ts +37 -0
- package/src/admin.ts +111 -24
- package/src/clearingHouse.ts +399 -22
- package/src/clearingHouseUser.ts +179 -107
- package/src/config.ts +1 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/idl/clearing_house.json +1077 -54
- package/src/index.ts +11 -2
- package/src/math/amm.ts +47 -14
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +39 -0
- package/src/math/position.ts +23 -3
- package/src/math/utils.ts +1 -1
- package/src/orderParams.ts +151 -0
- package/src/orders.ts +230 -0
- package/src/token/index.ts +37 -0
- package/src/types.ts +128 -1
- package/tsconfig.json +0 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/types.d.ts.map +0 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
- package/lib/addresses.d.ts.map +0 -1
- package/lib/admin.d.ts.map +0 -1
- package/lib/assert/assert.d.ts.map +0 -1
- package/lib/clearingHouse.d.ts.map +0 -1
- package/lib/clearingHouseUser.d.ts.map +0 -1
- package/lib/config.d.ts.map +0 -1
- package/lib/constants/markets.d.ts.map +0 -1
- package/lib/constants/numericConstants.d.ts.map +0 -1
- package/lib/examples/makeTradeExample.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/math/amm.d.ts.map +0 -1
- package/lib/math/conversion.d.ts.map +0 -1
- package/lib/math/funding.d.ts.map +0 -1
- package/lib/math/insuranceFund.d.ts.map +0 -1
- package/lib/math/market.d.ts.map +0 -1
- package/lib/math/position.d.ts.map +0 -1
- package/lib/math/trade.d.ts.map +0 -1
- package/lib/math/utils.d.ts.map +0 -1
- package/lib/mockUSDCFaucet.d.ts.map +0 -1
- package/lib/pythClient.d.ts.map +0 -1
- package/lib/tx/defaultTxSender.d.ts.map +0 -1
- package/lib/tx/types.d.ts.map +0 -1
- package/lib/tx/utils.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/util/computeUnits.d.ts.map +0 -1
- package/lib/util/tps.d.ts.map +0 -1
- package/lib/wallet.d.ts.map +0 -1
package/src/accounts/types.ts
CHANGED
|
@@ -5,13 +5,19 @@ import {
|
|
|
5
5
|
FundingRateHistoryAccount,
|
|
6
6
|
LiquidationHistoryAccount,
|
|
7
7
|
MarketsAccount,
|
|
8
|
+
OrderHistoryAccount,
|
|
9
|
+
OrderStateAccount,
|
|
8
10
|
StateAccount,
|
|
9
11
|
TradeHistoryAccount,
|
|
10
12
|
UserAccount,
|
|
13
|
+
UserOrdersAccount,
|
|
11
14
|
UserPositionsAccount,
|
|
12
15
|
} from '../types';
|
|
13
16
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
14
17
|
import { EventEmitter } from 'events';
|
|
18
|
+
import { PublicKey } from '@solana/web3.js';
|
|
19
|
+
import { AccountInfo } from '@solana/spl-token';
|
|
20
|
+
import { ClearingHouseConfigType, ClearingHouseUserConfigType } from '..';
|
|
15
21
|
|
|
16
22
|
export interface AccountSubscriber<T> {
|
|
17
23
|
data?: T;
|
|
@@ -35,7 +41,10 @@ export interface ClearingHouseAccountEvents {
|
|
|
35
41
|
liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
|
|
36
42
|
depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
|
|
37
43
|
curveHistoryAccountUpdate: (payload: ExtendedCurveHistoryAccount) => void;
|
|
44
|
+
orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
|
|
45
|
+
orderStateAccountUpdate: (payload: OrderStateAccount) => void;
|
|
38
46
|
update: void;
|
|
47
|
+
error: (e: Error) => void;
|
|
39
48
|
}
|
|
40
49
|
|
|
41
50
|
export type ClearingHouseAccountTypes =
|
|
@@ -44,7 +53,8 @@ export type ClearingHouseAccountTypes =
|
|
|
44
53
|
| 'fundingPaymentHistoryAccount'
|
|
45
54
|
| 'fundingRateHistoryAccount'
|
|
46
55
|
| 'curveHistoryAccount'
|
|
47
|
-
| 'liquidationHistoryAccount'
|
|
56
|
+
| 'liquidationHistoryAccount'
|
|
57
|
+
| 'orderHistoryAccount';
|
|
48
58
|
|
|
49
59
|
export interface ClearingHouseAccountSubscriber {
|
|
50
60
|
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
@@ -66,12 +76,18 @@ export interface ClearingHouseAccountSubscriber {
|
|
|
66
76
|
getFundingRateHistoryAccount(): FundingRateHistoryAccount;
|
|
67
77
|
getCurveHistoryAccount(): ExtendedCurveHistoryAccount;
|
|
68
78
|
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
79
|
+
getOrderStateAccount(): OrderStateAccount;
|
|
80
|
+
getOrderHistoryAccount(): OrderHistoryAccount;
|
|
81
|
+
|
|
82
|
+
type: ClearingHouseConfigType;
|
|
69
83
|
}
|
|
70
84
|
|
|
71
85
|
export interface UserAccountEvents {
|
|
72
86
|
userAccountData: (payload: UserAccount) => void;
|
|
73
87
|
userPositionsData: (payload: UserPositionsAccount) => void;
|
|
88
|
+
userOrdersData: (payload: UserOrdersAccount) => void;
|
|
74
89
|
update: void;
|
|
90
|
+
error: (e: Error) => void;
|
|
75
91
|
}
|
|
76
92
|
|
|
77
93
|
export interface UserAccountSubscriber {
|
|
@@ -84,4 +100,30 @@ export interface UserAccountSubscriber {
|
|
|
84
100
|
|
|
85
101
|
getUserAccount(): UserAccount;
|
|
86
102
|
getUserPositionsAccount(): UserPositionsAccount;
|
|
103
|
+
getUserOrdersAccount(): UserOrdersAccount;
|
|
104
|
+
type: ClearingHouseUserConfigType;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface TokenAccountEvents {
|
|
108
|
+
tokenAccountUpdate: (payload: AccountInfo) => void;
|
|
109
|
+
update: void;
|
|
110
|
+
error: (e: Error) => void;
|
|
87
111
|
}
|
|
112
|
+
|
|
113
|
+
export interface TokenAccountSubscriber {
|
|
114
|
+
eventEmitter: StrictEventEmitter<EventEmitter, TokenAccountEvents>;
|
|
115
|
+
isSubscribed: boolean;
|
|
116
|
+
|
|
117
|
+
subscribe(): Promise<boolean>;
|
|
118
|
+
fetch(): Promise<void>;
|
|
119
|
+
unsubscribe(): Promise<void>;
|
|
120
|
+
|
|
121
|
+
getTokenAccount(): AccountInfo;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type AccountToPoll = {
|
|
125
|
+
key: string;
|
|
126
|
+
publicKey: PublicKey;
|
|
127
|
+
eventType: string;
|
|
128
|
+
callbackId?: string;
|
|
129
|
+
};
|
|
@@ -11,6 +11,8 @@ import {
|
|
|
11
11
|
FundingRateHistoryAccount,
|
|
12
12
|
LiquidationHistoryAccount,
|
|
13
13
|
MarketsAccount,
|
|
14
|
+
OrderHistoryAccount,
|
|
15
|
+
OrderStateAccount,
|
|
14
16
|
StateAccount,
|
|
15
17
|
TradeHistoryAccount,
|
|
16
18
|
} from '../types';
|
|
@@ -19,8 +21,9 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
19
21
|
import { EventEmitter } from 'events';
|
|
20
22
|
import { getClearingHouseStateAccountPublicKey } from '../addresses';
|
|
21
23
|
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
24
|
+
import { ClearingHouseConfigType } from '../factory/clearingHouse';
|
|
22
25
|
|
|
23
|
-
export class
|
|
26
|
+
export class WebSocketClearingHouseAccountSubscriber
|
|
24
27
|
implements ClearingHouseAccountSubscriber
|
|
25
28
|
{
|
|
26
29
|
isSubscribed: boolean;
|
|
@@ -34,9 +37,13 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
34
37
|
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
35
38
|
curveHistoryAccountSubscriber?: AccountSubscriber<ExtendedCurveHistoryAccount>;
|
|
36
39
|
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
40
|
+
orderStateAccountSubscriber?: AccountSubscriber<OrderStateAccount>;
|
|
41
|
+
orderHistoryAccountSubscriber?: AccountSubscriber<OrderHistoryAccount>;
|
|
37
42
|
|
|
38
43
|
optionalExtraSubscriptions: ClearingHouseAccountTypes[] = [];
|
|
39
44
|
|
|
45
|
+
type: ClearingHouseConfigType = 'websocket';
|
|
46
|
+
|
|
40
47
|
private isSubscribing = false;
|
|
41
48
|
private subscriptionPromise: Promise<boolean>;
|
|
42
49
|
private subscriptionPromiseResolver: (val: boolean) => void;
|
|
@@ -92,6 +99,21 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
92
99
|
this.eventEmitter.emit('update');
|
|
93
100
|
});
|
|
94
101
|
|
|
102
|
+
this.orderStateAccountSubscriber = new WebSocketAccountSubscriber(
|
|
103
|
+
'orderState',
|
|
104
|
+
this.program,
|
|
105
|
+
state.orderState
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
await this.orderStateAccountSubscriber.subscribe(
|
|
109
|
+
(data: OrderStateAccount) => {
|
|
110
|
+
this.eventEmitter.emit('orderStateAccountUpdate', data);
|
|
111
|
+
this.eventEmitter.emit('update');
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const orderState = this.orderStateAccountSubscriber.data;
|
|
116
|
+
|
|
95
117
|
// create subscribers for other state accounts
|
|
96
118
|
|
|
97
119
|
this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
@@ -131,6 +153,12 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
131
153
|
state.extendedCurveHistory
|
|
132
154
|
);
|
|
133
155
|
|
|
156
|
+
this.orderHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
157
|
+
'orderHistory',
|
|
158
|
+
this.program,
|
|
159
|
+
orderState.orderHistory
|
|
160
|
+
);
|
|
161
|
+
|
|
134
162
|
const extraSusbcribersToUse: {
|
|
135
163
|
subscriber: AccountSubscriber<any>;
|
|
136
164
|
eventType: keyof ClearingHouseAccountEvents;
|
|
@@ -172,6 +200,12 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
172
200
|
eventType: 'curveHistoryAccountUpdate',
|
|
173
201
|
});
|
|
174
202
|
|
|
203
|
+
if (optionalSubscriptions?.includes('orderHistoryAccount'))
|
|
204
|
+
extraSusbcribersToUse.push({
|
|
205
|
+
subscriber: this.orderHistoryAccountSubscriber,
|
|
206
|
+
eventType: 'orderHistoryAccountUpdate',
|
|
207
|
+
});
|
|
208
|
+
|
|
175
209
|
this.optionalExtraSubscriptions = optionalSubscriptions ?? [];
|
|
176
210
|
|
|
177
211
|
// await all subcriptions in parallel to boost performance
|
|
@@ -219,6 +253,7 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
219
253
|
|
|
220
254
|
await this.stateAccountSubscriber.unsubscribe();
|
|
221
255
|
await this.marketsAccountSubscriber.unsubscribe();
|
|
256
|
+
await this.orderStateAccountSubscriber.unsubscribe();
|
|
222
257
|
|
|
223
258
|
if (this.optionalExtraSubscriptions.includes('tradeHistoryAccount')) {
|
|
224
259
|
await this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
@@ -246,6 +281,10 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
246
281
|
await this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
247
282
|
}
|
|
248
283
|
|
|
284
|
+
if (this.optionalExtraSubscriptions.includes('orderHistoryAccount')) {
|
|
285
|
+
await this.orderHistoryAccountSubscriber.unsubscribe();
|
|
286
|
+
}
|
|
287
|
+
|
|
249
288
|
this.isSubscribed = false;
|
|
250
289
|
}
|
|
251
290
|
|
|
@@ -318,4 +357,15 @@ export class DefaultClearingHouseAccountSubscriber
|
|
|
318
357
|
this.assertOptionalIsSubscribed('liquidationHistoryAccount');
|
|
319
358
|
return this.liquidationHistoryAccountSubscriber.data;
|
|
320
359
|
}
|
|
360
|
+
|
|
361
|
+
public getOrderHistoryAccount(): OrderHistoryAccount {
|
|
362
|
+
this.assertIsSubscribed();
|
|
363
|
+
this.assertOptionalIsSubscribed('orderHistoryAccount');
|
|
364
|
+
return this.orderHistoryAccountSubscriber.data;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
public getOrderStateAccount(): OrderStateAccount {
|
|
368
|
+
this.assertIsSubscribed();
|
|
369
|
+
return this.orderStateAccountSubscriber.data;
|
|
370
|
+
}
|
|
321
371
|
}
|
|
@@ -8,11 +8,15 @@ import { Program } from '@project-serum/anchor';
|
|
|
8
8
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
9
9
|
import { EventEmitter } from 'events';
|
|
10
10
|
import { PublicKey } from '@solana/web3.js';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
getUserAccountPublicKey,
|
|
13
|
+
getUserOrdersAccountPublicKey,
|
|
14
|
+
} from '../addresses';
|
|
12
15
|
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
13
|
-
import { UserAccount, UserPositionsAccount } from '../types';
|
|
16
|
+
import { UserAccount, UserOrdersAccount, UserPositionsAccount } from '../types';
|
|
17
|
+
import { ClearingHouseConfigType } from '../factory/clearingHouse';
|
|
14
18
|
|
|
15
|
-
export class
|
|
19
|
+
export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
|
|
16
20
|
isSubscribed: boolean;
|
|
17
21
|
program: Program;
|
|
18
22
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
@@ -20,6 +24,9 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
20
24
|
|
|
21
25
|
userDataAccountSubscriber: AccountSubscriber<UserAccount>;
|
|
22
26
|
userPositionsAccountSubscriber: AccountSubscriber<UserPositionsAccount>;
|
|
27
|
+
userOrdersAccountSubscriber: AccountSubscriber<UserOrdersAccount>;
|
|
28
|
+
|
|
29
|
+
type: ClearingHouseConfigType = 'websocket';
|
|
23
30
|
|
|
24
31
|
public constructor(program: Program, authority: PublicKey) {
|
|
25
32
|
this.isSubscribed = false;
|
|
@@ -61,6 +68,23 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
61
68
|
}
|
|
62
69
|
);
|
|
63
70
|
|
|
71
|
+
const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
|
|
72
|
+
this.program.programId,
|
|
73
|
+
userPublicKey
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
this.userOrdersAccountSubscriber = new WebSocketAccountSubscriber(
|
|
77
|
+
'userOrders',
|
|
78
|
+
this.program,
|
|
79
|
+
userOrdersPublicKey
|
|
80
|
+
);
|
|
81
|
+
await this.userOrdersAccountSubscriber.subscribe(
|
|
82
|
+
(data: UserOrdersAccount) => {
|
|
83
|
+
this.eventEmitter.emit('userOrdersData', data);
|
|
84
|
+
this.eventEmitter.emit('update');
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
|
|
64
88
|
this.eventEmitter.emit('update');
|
|
65
89
|
this.isSubscribed = true;
|
|
66
90
|
return true;
|
|
@@ -70,6 +94,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
70
94
|
await Promise.all([
|
|
71
95
|
this.userDataAccountSubscriber.fetch(),
|
|
72
96
|
this.userPositionsAccountSubscriber.fetch(),
|
|
97
|
+
this.userOrdersAccountSubscriber.fetch(),
|
|
73
98
|
]);
|
|
74
99
|
}
|
|
75
100
|
|
|
@@ -81,6 +106,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
81
106
|
await Promise.all([
|
|
82
107
|
this.userDataAccountSubscriber.unsubscribe(),
|
|
83
108
|
this.userPositionsAccountSubscriber.unsubscribe(),
|
|
109
|
+
await this.userOrdersAccountSubscriber.unsubscribe(),
|
|
84
110
|
]);
|
|
85
111
|
|
|
86
112
|
this.isSubscribed = false;
|
|
@@ -103,4 +129,8 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
103
129
|
this.assertIsSubscribed();
|
|
104
130
|
return this.userPositionsAccountSubscriber.data;
|
|
105
131
|
}
|
|
132
|
+
|
|
133
|
+
public getUserOrdersAccount(): UserOrdersAccount {
|
|
134
|
+
return this.userOrdersAccountSubscriber.data;
|
|
135
|
+
}
|
|
106
136
|
}
|
package/src/addresses.ts
CHANGED
|
@@ -10,6 +10,21 @@ export async function getClearingHouseStateAccountPublicKeyAndNonce(
|
|
|
10
10
|
);
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
export async function getOrderStateAccountPublicKey(
|
|
14
|
+
programId: PublicKey
|
|
15
|
+
): Promise<PublicKey> {
|
|
16
|
+
return (await getOrderStateAccountPublicKeyAndNonce(programId))[0];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function getOrderStateAccountPublicKeyAndNonce(
|
|
20
|
+
programId: PublicKey
|
|
21
|
+
): Promise<[PublicKey, number]> {
|
|
22
|
+
return anchor.web3.PublicKey.findProgramAddress(
|
|
23
|
+
[Buffer.from(anchor.utils.bytes.utf8.encode('order_state'))],
|
|
24
|
+
programId
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
13
28
|
export async function getClearingHouseStateAccountPublicKey(
|
|
14
29
|
programId: PublicKey
|
|
15
30
|
): Promise<PublicKey> {
|
|
@@ -32,3 +47,25 @@ export async function getUserAccountPublicKey(
|
|
|
32
47
|
): Promise<PublicKey> {
|
|
33
48
|
return (await getUserAccountPublicKeyAndNonce(programId, authority))[0];
|
|
34
49
|
}
|
|
50
|
+
|
|
51
|
+
export async function getUserOrdersAccountPublicKeyAndNonce(
|
|
52
|
+
programId: PublicKey,
|
|
53
|
+
userAccount: PublicKey
|
|
54
|
+
): Promise<[PublicKey, number]> {
|
|
55
|
+
return anchor.web3.PublicKey.findProgramAddress(
|
|
56
|
+
[
|
|
57
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
|
|
58
|
+
userAccount.toBuffer(),
|
|
59
|
+
],
|
|
60
|
+
programId
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function getUserOrdersAccountPublicKey(
|
|
65
|
+
programId: PublicKey,
|
|
66
|
+
userAccount: PublicKey
|
|
67
|
+
): Promise<PublicKey> {
|
|
68
|
+
return (
|
|
69
|
+
await getUserOrdersAccountPublicKeyAndNonce(programId, userAccount)
|
|
70
|
+
)[0];
|
|
71
|
+
}
|
package/src/admin.ts
CHANGED
|
@@ -5,18 +5,29 @@ import {
|
|
|
5
5
|
SYSVAR_RENT_PUBKEY,
|
|
6
6
|
TransactionSignature,
|
|
7
7
|
} from '@solana/web3.js';
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
FeeStructure,
|
|
10
|
+
IWallet,
|
|
11
|
+
OracleGuardRails,
|
|
12
|
+
OracleSource,
|
|
13
|
+
OrderFillerRewardStructure,
|
|
14
|
+
} from './types';
|
|
15
|
+
import { BN, Provider } from '@project-serum/anchor';
|
|
10
16
|
import * as anchor from '@project-serum/anchor';
|
|
11
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
getClearingHouseStateAccountPublicKey,
|
|
19
|
+
getClearingHouseStateAccountPublicKeyAndNonce,
|
|
20
|
+
getOrderStateAccountPublicKeyAndNonce,
|
|
21
|
+
} from './addresses';
|
|
12
22
|
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
|
13
23
|
import { ClearingHouse } from './clearingHouse';
|
|
14
24
|
import { PEG_PRECISION } from './constants/numericConstants';
|
|
15
|
-
import clearingHouseIDL from './idl/clearing_house.json';
|
|
16
|
-
import { DefaultClearingHouseAccountSubscriber } from './accounts/defaultClearingHouseAccountSubscriber';
|
|
17
|
-
import { DefaultTxSender } from './tx/defaultTxSender';
|
|
18
25
|
import { calculateTargetPriceTrade } from './math/trade';
|
|
19
26
|
import { calculateAmmReservesAfterSwap, getSwapDirection } from './math/amm';
|
|
27
|
+
import {
|
|
28
|
+
getAdmin,
|
|
29
|
+
getWebSocketClearingHouseConfig,
|
|
30
|
+
} from './factory/clearingHouse';
|
|
20
31
|
|
|
21
32
|
export class Admin extends ClearingHouse {
|
|
22
33
|
public static from(
|
|
@@ -25,30 +36,21 @@ export class Admin extends ClearingHouse {
|
|
|
25
36
|
clearingHouseProgramId: PublicKey,
|
|
26
37
|
opts: ConfirmOptions = Provider.defaultOptions()
|
|
27
38
|
): Admin {
|
|
28
|
-
const
|
|
29
|
-
const program = new Program(
|
|
30
|
-
clearingHouseIDL as Idl,
|
|
31
|
-
clearingHouseProgramId,
|
|
32
|
-
provider
|
|
33
|
-
);
|
|
34
|
-
const accountSubscriber = new DefaultClearingHouseAccountSubscriber(
|
|
35
|
-
program
|
|
36
|
-
);
|
|
37
|
-
const txSender = new DefaultTxSender(provider);
|
|
38
|
-
return new Admin(
|
|
39
|
+
const config = getWebSocketClearingHouseConfig(
|
|
39
40
|
connection,
|
|
40
41
|
wallet,
|
|
41
|
-
|
|
42
|
-
accountSubscriber,
|
|
43
|
-
txSender,
|
|
42
|
+
clearingHouseProgramId,
|
|
44
43
|
opts
|
|
45
44
|
);
|
|
45
|
+
return getAdmin(config);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
public async initialize(
|
|
49
49
|
usdcMint: PublicKey,
|
|
50
50
|
adminControlsPrices: boolean
|
|
51
|
-
): Promise<
|
|
51
|
+
): Promise<
|
|
52
|
+
[TransactionSignature, TransactionSignature, TransactionSignature]
|
|
53
|
+
> {
|
|
52
54
|
const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
|
|
53
55
|
await this.getStatePublicKey()
|
|
54
56
|
);
|
|
@@ -172,7 +174,40 @@ export class Admin extends ClearingHouse {
|
|
|
172
174
|
this.opts
|
|
173
175
|
);
|
|
174
176
|
|
|
175
|
-
|
|
177
|
+
const initializeOrderStateTxSig = await this.initializeOrderState();
|
|
178
|
+
|
|
179
|
+
return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public async initializeOrderState(): Promise<TransactionSignature> {
|
|
183
|
+
const orderHistory = anchor.web3.Keypair.generate();
|
|
184
|
+
const [orderStatePublicKey, orderStateNonce] =
|
|
185
|
+
await getOrderStateAccountPublicKeyAndNonce(this.program.programId);
|
|
186
|
+
const clearingHouseStatePublicKey =
|
|
187
|
+
await getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
188
|
+
|
|
189
|
+
const initializeOrderStateTx =
|
|
190
|
+
await this.program.transaction.initializeOrderState(orderStateNonce, {
|
|
191
|
+
accounts: {
|
|
192
|
+
admin: this.wallet.publicKey,
|
|
193
|
+
state: clearingHouseStatePublicKey,
|
|
194
|
+
orderHistory: orderHistory.publicKey,
|
|
195
|
+
orderState: orderStatePublicKey,
|
|
196
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
197
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
198
|
+
},
|
|
199
|
+
instructions: [
|
|
200
|
+
await this.program.account.orderHistory.createInstruction(
|
|
201
|
+
orderHistory
|
|
202
|
+
),
|
|
203
|
+
],
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
return await this.txSender.send(
|
|
207
|
+
initializeOrderStateTx,
|
|
208
|
+
[orderHistory],
|
|
209
|
+
this.opts
|
|
210
|
+
);
|
|
176
211
|
}
|
|
177
212
|
|
|
178
213
|
public async initializeMarket(
|
|
@@ -338,6 +373,25 @@ export class Admin extends ClearingHouse {
|
|
|
338
373
|
});
|
|
339
374
|
}
|
|
340
375
|
|
|
376
|
+
public async resetAmmOracleTwap(
|
|
377
|
+
marketIndex: BN
|
|
378
|
+
): Promise<TransactionSignature> {
|
|
379
|
+
const state = this.getStateAccount();
|
|
380
|
+
const markets = this.getMarketsAccount();
|
|
381
|
+
const marketData = markets.markets[marketIndex.toNumber()];
|
|
382
|
+
const ammData = marketData.amm;
|
|
383
|
+
|
|
384
|
+
return await this.program.rpc.resetAmmOracleTwap(marketIndex, {
|
|
385
|
+
accounts: {
|
|
386
|
+
state: await this.getStatePublicKey(),
|
|
387
|
+
admin: this.wallet.publicKey,
|
|
388
|
+
oracle: ammData.oracle,
|
|
389
|
+
markets: state.markets,
|
|
390
|
+
curveHistory: state.extendedCurveHistory,
|
|
391
|
+
},
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
341
395
|
public async withdrawFromInsuranceVault(
|
|
342
396
|
amount: BN,
|
|
343
397
|
recipient: PublicKey
|
|
@@ -499,6 +553,21 @@ export class Admin extends ClearingHouse {
|
|
|
499
553
|
);
|
|
500
554
|
}
|
|
501
555
|
|
|
556
|
+
public async updateOrderFillerRewardStructure(
|
|
557
|
+
orderFillerRewardStructure: OrderFillerRewardStructure
|
|
558
|
+
): Promise<TransactionSignature> {
|
|
559
|
+
return await this.program.rpc.updateOrderFillerRewardStructure(
|
|
560
|
+
orderFillerRewardStructure,
|
|
561
|
+
{
|
|
562
|
+
accounts: {
|
|
563
|
+
admin: this.wallet.publicKey,
|
|
564
|
+
state: await this.getStatePublicKey(),
|
|
565
|
+
orderState: await this.getOrderStatePublicKey(),
|
|
566
|
+
},
|
|
567
|
+
}
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
502
571
|
public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
|
|
503
572
|
return await this.program.rpc.updateFee(fees, {
|
|
504
573
|
accounts: {
|
|
@@ -539,12 +608,30 @@ export class Admin extends ClearingHouse {
|
|
|
539
608
|
);
|
|
540
609
|
}
|
|
541
610
|
|
|
542
|
-
public async
|
|
611
|
+
public async updateMarketMinimumQuoteAssetTradeSize(
|
|
612
|
+
marketIndex: BN,
|
|
613
|
+
minimumTradeSize: BN
|
|
614
|
+
): Promise<TransactionSignature> {
|
|
615
|
+
const state = this.getStateAccount();
|
|
616
|
+
return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
|
|
617
|
+
marketIndex,
|
|
618
|
+
minimumTradeSize,
|
|
619
|
+
{
|
|
620
|
+
accounts: {
|
|
621
|
+
admin: this.wallet.publicKey,
|
|
622
|
+
state: await this.getStatePublicKey(),
|
|
623
|
+
markets: state.markets,
|
|
624
|
+
},
|
|
625
|
+
}
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
public async updateMarketMinimumBaseAssetTradeSize(
|
|
543
630
|
marketIndex: BN,
|
|
544
631
|
minimumTradeSize: BN
|
|
545
632
|
): Promise<TransactionSignature> {
|
|
546
633
|
const state = this.getStateAccount();
|
|
547
|
-
return await this.program.rpc.
|
|
634
|
+
return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
|
|
548
635
|
marketIndex,
|
|
549
636
|
minimumTradeSize,
|
|
550
637
|
{
|