@drift-labs/sdk 0.1.18-master.2 → 0.1.18-orders.0
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/defaultClearingHouseAccountSubscriber.d.ts +7 -4
- package/lib/accounts/defaultClearingHouseAccountSubscriber.js +27 -2
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +28 -0
- package/lib/accounts/defaultHistoryAccountSubscriber.js +110 -0
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +3 -2
- package/lib/accounts/defaultUserAccountSubscriber.js +14 -3
- package/lib/accounts/types.d.ts +32 -6
- package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
- package/lib/addresses.d.ts +4 -1
- package/lib/addresses.js +28 -1
- package/lib/admin.d.ts +10 -5
- package/lib/admin.js +53 -30
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +22 -3
- package/lib/clearingHouse.js +246 -15
- package/lib/clearingHouseUser.d.ts +10 -17
- package/lib/clearingHouseUser.js +111 -97
- 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/idl/clearing_house.json +895 -132
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -0
- 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 +2 -2
- 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 +0 -1
- package/lib/mockUSDCFaucet.d.ts +0 -1
- package/lib/orderParams.d.ts +7 -0
- package/lib/orderParams.js +88 -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/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 +140 -7
- 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 +1 -1
- package/src/accounts/defaultClearingHouseAccountSubscriber.ts +52 -5
- package/src/accounts/defaultHistoryAccountSubscriber.ts +176 -0
- package/src/accounts/defaultUserAccountSubscriber.ts +31 -4
- package/src/accounts/types.ts +42 -5
- package/src/addresses.ts +35 -0
- package/src/admin.ts +86 -33
- package/src/clearingHouse.ts +340 -7
- package/src/clearingHouseUser.ts +154 -102
- package/src/config.ts +1 -1
- package/src/idl/clearing_house.json +895 -132
- package/src/index.ts +3 -0
- package/src/math/amm.ts +47 -14
- package/src/math/funding.ts +4 -3
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +39 -0
- package/src/math/position.ts +23 -3
- package/src/orderParams.ts +128 -0
- package/src/orders.ts +230 -0
- package/src/types.ts +124 -6
- 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
|
@@ -0,0 +1,176 @@
|
|
|
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
|
+
TradeHistoryAccount,
|
|
10
|
+
} from '../types';
|
|
11
|
+
import { Program } from '@project-serum/anchor';
|
|
12
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
13
|
+
import { EventEmitter } from 'events';
|
|
14
|
+
import { getClearingHouseStateAccountPublicKey } from '../addresses';
|
|
15
|
+
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
16
|
+
|
|
17
|
+
export class DefaultHistoryAccountSubscriber
|
|
18
|
+
implements HistoryAccountSubscriber
|
|
19
|
+
{
|
|
20
|
+
isSubscribed: boolean;
|
|
21
|
+
program: Program;
|
|
22
|
+
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
23
|
+
tradeHistoryAccountSubscriber?: AccountSubscriber<TradeHistoryAccount>;
|
|
24
|
+
depositHistoryAccountSubscriber?: AccountSubscriber<DepositHistoryAccount>;
|
|
25
|
+
fundingPaymentHistoryAccountSubscriber?: AccountSubscriber<FundingPaymentHistoryAccount>;
|
|
26
|
+
fundingRateHistoryAccountSubscriber?: AccountSubscriber<FundingRateHistoryAccount>;
|
|
27
|
+
curveHistoryAccountSubscriber?: AccountSubscriber<CurveHistoryAccount>;
|
|
28
|
+
liquidationHistoryAccountSubscriber?: AccountSubscriber<LiquidationHistoryAccount>;
|
|
29
|
+
|
|
30
|
+
public constructor(program: Program) {
|
|
31
|
+
this.isSubscribed = false;
|
|
32
|
+
this.program = program;
|
|
33
|
+
this.eventEmitter = new EventEmitter();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async subscribe(): Promise<boolean> {
|
|
37
|
+
if (this.isSubscribed) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const statePublicKey = await getClearingHouseStateAccountPublicKey(
|
|
42
|
+
this.program.programId
|
|
43
|
+
);
|
|
44
|
+
const state: any = await this.program.account.state.fetch(statePublicKey);
|
|
45
|
+
|
|
46
|
+
this.tradeHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
47
|
+
'tradeHistory',
|
|
48
|
+
this.program,
|
|
49
|
+
state.tradeHistory
|
|
50
|
+
);
|
|
51
|
+
await this.tradeHistoryAccountSubscriber.subscribe(
|
|
52
|
+
(data: TradeHistoryAccount) => {
|
|
53
|
+
this.eventEmitter.emit('tradeHistoryAccountUpdate', data);
|
|
54
|
+
this.eventEmitter.emit('update');
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
this.depositHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
59
|
+
'depositHistory',
|
|
60
|
+
this.program,
|
|
61
|
+
state.depositHistory
|
|
62
|
+
);
|
|
63
|
+
await this.depositHistoryAccountSubscriber.subscribe(
|
|
64
|
+
(data: DepositHistoryAccount) => {
|
|
65
|
+
this.eventEmitter.emit('depositHistoryAccountUpdate', data);
|
|
66
|
+
this.eventEmitter.emit('update');
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
this.fundingPaymentHistoryAccountSubscriber =
|
|
71
|
+
new WebSocketAccountSubscriber(
|
|
72
|
+
'fundingPaymentHistory',
|
|
73
|
+
this.program,
|
|
74
|
+
state.fundingPaymentHistory
|
|
75
|
+
);
|
|
76
|
+
await this.fundingPaymentHistoryAccountSubscriber.subscribe(
|
|
77
|
+
(data: FundingPaymentHistoryAccount) => {
|
|
78
|
+
this.eventEmitter.emit('fundingPaymentHistoryAccountUpdate', data);
|
|
79
|
+
this.eventEmitter.emit('update');
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
this.fundingRateHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
84
|
+
'fundingRateHistory',
|
|
85
|
+
this.program,
|
|
86
|
+
state.fundingRateHistory
|
|
87
|
+
);
|
|
88
|
+
await this.fundingRateHistoryAccountSubscriber.subscribe(
|
|
89
|
+
(data: FundingRateHistoryAccount) => {
|
|
90
|
+
this.eventEmitter.emit('fundingRateHistoryAccountUpdate', data);
|
|
91
|
+
this.eventEmitter.emit('update');
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
this.liquidationHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
96
|
+
'liquidationHistory',
|
|
97
|
+
this.program,
|
|
98
|
+
state.liquidationHistory
|
|
99
|
+
);
|
|
100
|
+
await this.liquidationHistoryAccountSubscriber.subscribe(
|
|
101
|
+
(data: LiquidationHistoryAccount) => {
|
|
102
|
+
this.eventEmitter.emit('liquidationHistoryAccountUpdate', data);
|
|
103
|
+
this.eventEmitter.emit('update');
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
this.curveHistoryAccountSubscriber = new WebSocketAccountSubscriber(
|
|
108
|
+
'curveHistory',
|
|
109
|
+
this.program,
|
|
110
|
+
state.curveHistory
|
|
111
|
+
);
|
|
112
|
+
await this.curveHistoryAccountSubscriber.subscribe(
|
|
113
|
+
(data: CurveHistoryAccount) => {
|
|
114
|
+
this.eventEmitter.emit('curveHistoryAccountUpdate', data);
|
|
115
|
+
this.eventEmitter.emit('update');
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
this.eventEmitter.emit('update');
|
|
120
|
+
|
|
121
|
+
this.isSubscribed = true;
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public async unsubscribe(): Promise<void> {
|
|
126
|
+
if (!this.isSubscribed) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
await this.tradeHistoryAccountSubscriber.unsubscribe();
|
|
131
|
+
await this.fundingRateHistoryAccountSubscriber.unsubscribe();
|
|
132
|
+
await this.fundingPaymentHistoryAccountSubscriber.unsubscribe();
|
|
133
|
+
await this.depositHistoryAccountSubscriber.unsubscribe();
|
|
134
|
+
await this.curveHistoryAccountSubscriber.unsubscribe();
|
|
135
|
+
await this.liquidationHistoryAccountSubscriber.unsubscribe();
|
|
136
|
+
this.isSubscribed = false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
assertIsSubscribed(): void {
|
|
140
|
+
if (!this.isSubscribed) {
|
|
141
|
+
throw new NotSubscribedError(
|
|
142
|
+
'You must call `subscribe` before using this function'
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public getTradeHistoryAccount(): TradeHistoryAccount {
|
|
148
|
+
this.assertIsSubscribed();
|
|
149
|
+
return this.tradeHistoryAccountSubscriber.data;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public getDepositHistoryAccount(): DepositHistoryAccount {
|
|
153
|
+
this.assertIsSubscribed();
|
|
154
|
+
return this.depositHistoryAccountSubscriber.data;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount {
|
|
158
|
+
this.assertIsSubscribed();
|
|
159
|
+
return this.fundingPaymentHistoryAccountSubscriber.data;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public getFundingRateHistoryAccount(): FundingRateHistoryAccount {
|
|
163
|
+
this.assertIsSubscribed();
|
|
164
|
+
return this.fundingRateHistoryAccountSubscriber.data;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public getCurveHistoryAccount(): CurveHistoryAccount {
|
|
168
|
+
this.assertIsSubscribed();
|
|
169
|
+
return this.curveHistoryAccountSubscriber.data;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public getLiquidationHistoryAccount(): LiquidationHistoryAccount {
|
|
173
|
+
this.assertIsSubscribed();
|
|
174
|
+
return this.liquidationHistoryAccountSubscriber.data;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -8,9 +8,12 @@ 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';
|
|
14
17
|
|
|
15
18
|
export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
16
19
|
isSubscribed: boolean;
|
|
@@ -20,6 +23,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
20
23
|
|
|
21
24
|
userDataAccountSubscriber: AccountSubscriber<UserAccount>;
|
|
22
25
|
userPositionsAccountSubscriber: AccountSubscriber<UserPositionsAccount>;
|
|
26
|
+
userOrdersAccountSubscriber: AccountSubscriber<UserOrdersAccount>;
|
|
23
27
|
|
|
24
28
|
public constructor(program: Program, authority: PublicKey) {
|
|
25
29
|
this.isSubscribed = false;
|
|
@@ -61,6 +65,23 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
61
65
|
}
|
|
62
66
|
);
|
|
63
67
|
|
|
68
|
+
const userOrdersPublicKey = await getUserOrdersAccountPublicKey(
|
|
69
|
+
this.program.programId,
|
|
70
|
+
this.authority
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
this.userOrdersAccountSubscriber = new WebSocketAccountSubscriber(
|
|
74
|
+
'userOrders',
|
|
75
|
+
this.program,
|
|
76
|
+
userOrdersPublicKey
|
|
77
|
+
);
|
|
78
|
+
await this.userOrdersAccountSubscriber.subscribe(
|
|
79
|
+
(data: UserOrdersAccount) => {
|
|
80
|
+
this.eventEmitter.emit('userOrdersData', data);
|
|
81
|
+
this.eventEmitter.emit('update');
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
|
|
64
85
|
this.eventEmitter.emit('update');
|
|
65
86
|
this.isSubscribed = true;
|
|
66
87
|
return true;
|
|
@@ -70,6 +91,7 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
70
91
|
await Promise.all([
|
|
71
92
|
this.userDataAccountSubscriber.fetch(),
|
|
72
93
|
this.userPositionsAccountSubscriber.fetch(),
|
|
94
|
+
this.userOrdersAccountSubscriber.fetch(),
|
|
73
95
|
]);
|
|
74
96
|
}
|
|
75
97
|
|
|
@@ -78,8 +100,9 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
78
100
|
return;
|
|
79
101
|
}
|
|
80
102
|
|
|
81
|
-
|
|
82
|
-
|
|
103
|
+
this.userDataAccountSubscriber.unsubscribe();
|
|
104
|
+
this.userPositionsAccountSubscriber.unsubscribe();
|
|
105
|
+
this.userOrdersAccountSubscriber.unsubscribe();
|
|
83
106
|
|
|
84
107
|
this.isSubscribed = false;
|
|
85
108
|
}
|
|
@@ -101,4 +124,8 @@ export class DefaultUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
101
124
|
this.assertIsSubscribed();
|
|
102
125
|
return this.userPositionsAccountSubscriber.data;
|
|
103
126
|
}
|
|
127
|
+
|
|
128
|
+
public getUserOrdersAccount(): UserOrdersAccount {
|
|
129
|
+
return this.userOrdersAccountSubscriber.data;
|
|
130
|
+
}
|
|
104
131
|
}
|
package/src/accounts/types.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
CurveHistoryAccount,
|
|
2
3
|
DepositHistoryAccount,
|
|
3
|
-
ExtendedCurveHistoryAccount,
|
|
4
4
|
FundingPaymentHistoryAccount,
|
|
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';
|
|
@@ -17,7 +20,7 @@ export interface AccountSubscriber<T> {
|
|
|
17
20
|
data?: T;
|
|
18
21
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
19
22
|
fetch(): Promise<void>;
|
|
20
|
-
unsubscribe():
|
|
23
|
+
unsubscribe(): void;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
export class NotSubscribedError extends Error {
|
|
@@ -34,7 +37,9 @@ export interface ClearingHouseAccountEvents {
|
|
|
34
37
|
tradeHistoryAccountUpdate: (payload: TradeHistoryAccount) => void;
|
|
35
38
|
liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
|
|
36
39
|
depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
|
|
37
|
-
curveHistoryAccountUpdate: (payload:
|
|
40
|
+
curveHistoryAccountUpdate: (payload: CurveHistoryAccount) => void;
|
|
41
|
+
orderHistoryAccountUpdate: (payload: OrderHistoryAccount) => void;
|
|
42
|
+
orderStateAccountUpdate: (payload: OrderStateAccount) => void;
|
|
38
43
|
update: void;
|
|
39
44
|
}
|
|
40
45
|
|
|
@@ -44,7 +49,8 @@ export type ClearingHouseAccountTypes =
|
|
|
44
49
|
| 'fundingPaymentHistoryAccount'
|
|
45
50
|
| 'fundingRateHistoryAccount'
|
|
46
51
|
| 'curveHistoryAccount'
|
|
47
|
-
| 'liquidationHistoryAccount'
|
|
52
|
+
| 'liquidationHistoryAccount'
|
|
53
|
+
| 'orderHistoryAccount';
|
|
48
54
|
|
|
49
55
|
export interface ClearingHouseAccountSubscriber {
|
|
50
56
|
eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
|
|
@@ -64,13 +70,43 @@ export interface ClearingHouseAccountSubscriber {
|
|
|
64
70
|
getDepositHistoryAccount(): DepositHistoryAccount;
|
|
65
71
|
getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
|
|
66
72
|
getFundingRateHistoryAccount(): FundingRateHistoryAccount;
|
|
67
|
-
getCurveHistoryAccount():
|
|
73
|
+
getCurveHistoryAccount(): CurveHistoryAccount;
|
|
74
|
+
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
75
|
+
getOrderStateAccount(): OrderStateAccount;
|
|
76
|
+
getOrderHistoryAccount(): OrderHistoryAccount;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface HistoryAccountEvents {
|
|
80
|
+
fundingPaymentHistoryAccountUpdate: (
|
|
81
|
+
payload: FundingPaymentHistoryAccount
|
|
82
|
+
) => void;
|
|
83
|
+
fundingRateHistoryAccountUpdate: (payload: FundingRateHistoryAccount) => void;
|
|
84
|
+
tradeHistoryAccountUpdate: (payload: TradeHistoryAccount) => void;
|
|
85
|
+
liquidationHistoryAccountUpdate: (payload: LiquidationHistoryAccount) => void;
|
|
86
|
+
depositHistoryAccountUpdate: (payload: DepositHistoryAccount) => void;
|
|
87
|
+
curveHistoryAccountUpdate: (payload: CurveHistoryAccount) => void;
|
|
88
|
+
update: void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface HistoryAccountSubscriber {
|
|
92
|
+
eventEmitter: StrictEventEmitter<EventEmitter, HistoryAccountEvents>;
|
|
93
|
+
isSubscribed: boolean;
|
|
94
|
+
|
|
95
|
+
subscribe(): Promise<boolean>;
|
|
96
|
+
unsubscribe(): Promise<void>;
|
|
97
|
+
|
|
98
|
+
getTradeHistoryAccount(): TradeHistoryAccount;
|
|
99
|
+
getDepositHistoryAccount(): DepositHistoryAccount;
|
|
100
|
+
getFundingPaymentHistoryAccount(): FundingPaymentHistoryAccount;
|
|
101
|
+
getFundingRateHistoryAccount(): FundingRateHistoryAccount;
|
|
102
|
+
getCurveHistoryAccount(): CurveHistoryAccount;
|
|
68
103
|
getLiquidationHistoryAccount(): LiquidationHistoryAccount;
|
|
69
104
|
}
|
|
70
105
|
|
|
71
106
|
export interface UserAccountEvents {
|
|
72
107
|
userAccountData: (payload: UserAccount) => void;
|
|
73
108
|
userPositionsData: (payload: UserPositionsAccount) => void;
|
|
109
|
+
userOrdersData: (payload: UserOrdersAccount) => void;
|
|
74
110
|
update: void;
|
|
75
111
|
}
|
|
76
112
|
|
|
@@ -84,4 +120,5 @@ export interface UserAccountSubscriber {
|
|
|
84
120
|
|
|
85
121
|
getUserAccount(): UserAccount;
|
|
86
122
|
getUserPositionsAccount(): UserPositionsAccount;
|
|
123
|
+
getUserOrdersAccount(): UserOrdersAccount;
|
|
87
124
|
}
|
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,23 @@ 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
|
+
authority: PublicKey
|
|
54
|
+
): Promise<[PublicKey, number]> {
|
|
55
|
+
return anchor.web3.PublicKey.findProgramAddress(
|
|
56
|
+
[
|
|
57
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
|
|
58
|
+
authority.toBuffer(),
|
|
59
|
+
],
|
|
60
|
+
programId
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function getUserOrdersAccountPublicKey(
|
|
65
|
+
programId: PublicKey,
|
|
66
|
+
authority: PublicKey
|
|
67
|
+
): Promise<PublicKey> {
|
|
68
|
+
return (await getUserOrdersAccountPublicKeyAndNonce(programId, authority))[0];
|
|
69
|
+
}
|
package/src/admin.ts
CHANGED
|
@@ -5,10 +5,20 @@ import {
|
|
|
5
5
|
SYSVAR_RENT_PUBKEY,
|
|
6
6
|
TransactionSignature,
|
|
7
7
|
} from '@solana/web3.js';
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
FeeStructure,
|
|
10
|
+
IWallet,
|
|
11
|
+
OracleGuardRails,
|
|
12
|
+
OracleSource,
|
|
13
|
+
OrderFillerRewardStructure,
|
|
14
|
+
} from './types';
|
|
9
15
|
import { BN, Idl, Program, 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';
|
|
@@ -48,7 +58,9 @@ export class Admin extends ClearingHouse {
|
|
|
48
58
|
public async initialize(
|
|
49
59
|
usdcMint: PublicKey,
|
|
50
60
|
adminControlsPrices: boolean
|
|
51
|
-
): Promise<
|
|
61
|
+
): Promise<
|
|
62
|
+
[TransactionSignature, TransactionSignature, TransactionSignature]
|
|
63
|
+
> {
|
|
52
64
|
const stateAccountRPCResponse = await this.connection.getParsedAccountInfo(
|
|
53
65
|
await this.getStatePublicKey()
|
|
54
66
|
);
|
|
@@ -153,7 +165,7 @@ export class Admin extends ClearingHouse {
|
|
|
153
165
|
await this.program.account.depositHistory.createInstruction(
|
|
154
166
|
depositHistory
|
|
155
167
|
),
|
|
156
|
-
await this.program.account.
|
|
168
|
+
await this.program.account.curveHistory.createInstruction(
|
|
157
169
|
curveHistory
|
|
158
170
|
),
|
|
159
171
|
],
|
|
@@ -172,7 +184,40 @@ export class Admin extends ClearingHouse {
|
|
|
172
184
|
this.opts
|
|
173
185
|
);
|
|
174
186
|
|
|
175
|
-
|
|
187
|
+
const initializeOrderStateTxSig = await this.initializeOrderState();
|
|
188
|
+
|
|
189
|
+
return [initializeTxSig, initializeHistoryTxSig, initializeOrderStateTxSig];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public async initializeOrderState(): Promise<TransactionSignature> {
|
|
193
|
+
const orderHistory = anchor.web3.Keypair.generate();
|
|
194
|
+
const [orderStatePublicKey, orderStateNonce] =
|
|
195
|
+
await getOrderStateAccountPublicKeyAndNonce(this.program.programId);
|
|
196
|
+
const clearingHouseStatePublicKey =
|
|
197
|
+
await getClearingHouseStateAccountPublicKey(this.program.programId);
|
|
198
|
+
|
|
199
|
+
const initializeOrderStateTx =
|
|
200
|
+
await this.program.transaction.initializeOrderState(orderStateNonce, {
|
|
201
|
+
accounts: {
|
|
202
|
+
admin: this.wallet.publicKey,
|
|
203
|
+
state: clearingHouseStatePublicKey,
|
|
204
|
+
orderHistory: orderHistory.publicKey,
|
|
205
|
+
orderState: orderStatePublicKey,
|
|
206
|
+
rent: SYSVAR_RENT_PUBKEY,
|
|
207
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
208
|
+
},
|
|
209
|
+
instructions: [
|
|
210
|
+
await this.program.account.orderHistory.createInstruction(
|
|
211
|
+
orderHistory
|
|
212
|
+
),
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return await this.txSender.send(
|
|
217
|
+
initializeOrderStateTx,
|
|
218
|
+
[orderHistory],
|
|
219
|
+
this.opts
|
|
220
|
+
);
|
|
176
221
|
}
|
|
177
222
|
|
|
178
223
|
public async initializeMarket(
|
|
@@ -230,38 +275,13 @@ export class Admin extends ClearingHouse {
|
|
|
230
275
|
marketIndex: BN
|
|
231
276
|
): Promise<TransactionSignature> {
|
|
232
277
|
const state = this.getStateAccount();
|
|
233
|
-
const markets = this.getMarketsAccount();
|
|
234
|
-
const marketData = markets.markets[marketIndex.toNumber()];
|
|
235
|
-
const ammData = marketData.amm;
|
|
236
|
-
|
|
237
278
|
return await this.program.rpc.updateK(sqrtK, marketIndex, {
|
|
238
279
|
accounts: {
|
|
239
280
|
state: await this.getStatePublicKey(),
|
|
240
281
|
admin: this.wallet.publicKey,
|
|
241
282
|
markets: state.markets,
|
|
242
|
-
curveHistory: state.extendedCurveHistory,
|
|
243
|
-
oracle: ammData.oracle,
|
|
244
|
-
},
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
public async updateCurveHistory(): Promise<TransactionSignature> {
|
|
249
|
-
const extendedCurveHistory = anchor.web3.Keypair.generate();
|
|
250
|
-
|
|
251
|
-
const state = this.getStateAccount();
|
|
252
|
-
return await this.program.rpc.updateCurveHistory({
|
|
253
|
-
accounts: {
|
|
254
|
-
state: await this.getStatePublicKey(),
|
|
255
|
-
admin: this.wallet.publicKey,
|
|
256
283
|
curveHistory: state.curveHistory,
|
|
257
|
-
extendedCurveHistory: extendedCurveHistory.publicKey,
|
|
258
284
|
},
|
|
259
|
-
instructions: [
|
|
260
|
-
await this.program.account.extendedCurveHistory.createInstruction(
|
|
261
|
-
extendedCurveHistory
|
|
262
|
-
),
|
|
263
|
-
],
|
|
264
|
-
signers: [extendedCurveHistory],
|
|
265
285
|
});
|
|
266
286
|
}
|
|
267
287
|
|
|
@@ -314,7 +334,7 @@ export class Admin extends ClearingHouse {
|
|
|
314
334
|
admin: this.wallet.publicKey,
|
|
315
335
|
oracle: ammData.oracle,
|
|
316
336
|
markets: state.markets,
|
|
317
|
-
curveHistory: state.
|
|
337
|
+
curveHistory: state.curveHistory,
|
|
318
338
|
},
|
|
319
339
|
});
|
|
320
340
|
}
|
|
@@ -480,6 +500,21 @@ export class Admin extends ClearingHouse {
|
|
|
480
500
|
);
|
|
481
501
|
}
|
|
482
502
|
|
|
503
|
+
public async updateOrderFillerRewardStructure(
|
|
504
|
+
orderFillerRewardStructure: OrderFillerRewardStructure
|
|
505
|
+
): Promise<TransactionSignature> {
|
|
506
|
+
return await this.program.rpc.updateOrderFillerRewardStructure(
|
|
507
|
+
orderFillerRewardStructure,
|
|
508
|
+
{
|
|
509
|
+
accounts: {
|
|
510
|
+
admin: this.wallet.publicKey,
|
|
511
|
+
state: await this.getStatePublicKey(),
|
|
512
|
+
orderState: await this.getOrderStatePublicKey(),
|
|
513
|
+
},
|
|
514
|
+
}
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
483
518
|
public async updateFee(fees: FeeStructure): Promise<TransactionSignature> {
|
|
484
519
|
return await this.program.rpc.updateFee(fees, {
|
|
485
520
|
accounts: {
|
|
@@ -520,12 +555,30 @@ export class Admin extends ClearingHouse {
|
|
|
520
555
|
);
|
|
521
556
|
}
|
|
522
557
|
|
|
523
|
-
public async
|
|
558
|
+
public async updateMarketMinimumQuoteAssetTradeSize(
|
|
559
|
+
marketIndex: BN,
|
|
560
|
+
minimumTradeSize: BN
|
|
561
|
+
): Promise<TransactionSignature> {
|
|
562
|
+
const state = this.getStateAccount();
|
|
563
|
+
return await this.program.rpc.updateMarketMinimumQuoteAssetTradeSize(
|
|
564
|
+
marketIndex,
|
|
565
|
+
minimumTradeSize,
|
|
566
|
+
{
|
|
567
|
+
accounts: {
|
|
568
|
+
admin: this.wallet.publicKey,
|
|
569
|
+
state: await this.getStatePublicKey(),
|
|
570
|
+
markets: state.markets,
|
|
571
|
+
},
|
|
572
|
+
}
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
public async updateMarketMinimumBaseAssetTradeSize(
|
|
524
577
|
marketIndex: BN,
|
|
525
578
|
minimumTradeSize: BN
|
|
526
579
|
): Promise<TransactionSignature> {
|
|
527
580
|
const state = this.getStateAccount();
|
|
528
|
-
return await this.program.rpc.
|
|
581
|
+
return await this.program.rpc.updateMarketMinimumBaseAssetTradeSize(
|
|
529
582
|
marketIndex,
|
|
530
583
|
minimumTradeSize,
|
|
531
584
|
{
|