@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
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 +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
2
|
-
import { BN } from '.';
|
|
2
|
+
import { BN, ZERO } from '.';
|
|
3
3
|
|
|
4
4
|
// # Utility Types / Enums / Constants
|
|
5
5
|
export class SwapDirection {
|
|
@@ -7,14 +7,25 @@ export class SwapDirection {
|
|
|
7
7
|
static readonly REMOVE = { remove: {} };
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
export class BankBalanceType {
|
|
11
|
+
static readonly DEPOSIT = { deposit: {} };
|
|
12
|
+
static readonly BORROW = { borrow: {} };
|
|
13
|
+
}
|
|
14
|
+
|
|
10
15
|
export class PositionDirection {
|
|
11
16
|
static readonly LONG = { long: {} };
|
|
12
17
|
static readonly SHORT = { short: {} };
|
|
13
18
|
}
|
|
14
19
|
|
|
20
|
+
export class DepositDirection {
|
|
21
|
+
static readonly DEPOSIT = { deposit: {} };
|
|
22
|
+
static readonly WITHDRAW = { withdraw: {} };
|
|
23
|
+
}
|
|
24
|
+
|
|
15
25
|
export class OracleSource {
|
|
16
26
|
static readonly PYTH = { pyth: {} };
|
|
17
27
|
static readonly SWITCHBOARD = { switchboard: {} };
|
|
28
|
+
static readonly QUOTE_ASSET = { quoteAsset: {} };
|
|
18
29
|
}
|
|
19
30
|
|
|
20
31
|
export class OrderType {
|
|
@@ -42,6 +53,20 @@ export class OrderAction {
|
|
|
42
53
|
static readonly CANCEL = { cancel: {} };
|
|
43
54
|
static readonly EXPIRE = { expire: {} };
|
|
44
55
|
static readonly FILL = { fill: {} };
|
|
56
|
+
static readonly TRIGGER = { trigger: {} };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class OrderActionExplanation {
|
|
60
|
+
static readonly NONE = { none: {} };
|
|
61
|
+
static readonly BREACHED_MARGIN_REQUIREMENT = {
|
|
62
|
+
breachedMarginRequirement: {},
|
|
63
|
+
};
|
|
64
|
+
static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
|
|
65
|
+
oraclePriceBreachedLimitPrice: {},
|
|
66
|
+
};
|
|
67
|
+
static readonly MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
|
|
68
|
+
marketOrderFilledToLimitPrice: {},
|
|
69
|
+
};
|
|
45
70
|
}
|
|
46
71
|
|
|
47
72
|
export class OrderTriggerCondition {
|
|
@@ -53,6 +78,12 @@ export function isVariant(object: unknown, type: string) {
|
|
|
53
78
|
return object.hasOwnProperty(type);
|
|
54
79
|
}
|
|
55
80
|
|
|
81
|
+
export function isOneOfVariant(object: unknown, types: string[]) {
|
|
82
|
+
return types.reduce((result, type) => {
|
|
83
|
+
return result || object.hasOwnProperty(type);
|
|
84
|
+
}, false);
|
|
85
|
+
}
|
|
86
|
+
|
|
56
87
|
export enum TradeSide {
|
|
57
88
|
None = 0,
|
|
58
89
|
Buy = 1,
|
|
@@ -69,58 +100,22 @@ export type CandleResolution =
|
|
|
69
100
|
| 'W'
|
|
70
101
|
| 'M';
|
|
71
102
|
|
|
72
|
-
// # ClearingHouse Account Types
|
|
73
|
-
export type TradeHistoryAccount = {
|
|
74
|
-
head: BN;
|
|
75
|
-
tradeRecords: TradeRecord[];
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export type DepositHistoryAccount = {
|
|
79
|
-
head: BN;
|
|
80
|
-
depositRecords: DepositRecord[];
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export type ExtendedCurveHistoryAccount = {
|
|
84
|
-
head: BN;
|
|
85
|
-
curveRecords: ExtendedCurveRecord[];
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export type FundingRateHistoryAccount = {
|
|
89
|
-
head: BN;
|
|
90
|
-
fundingRateRecords: FundingRateRecord[];
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export type FundingPaymentHistoryAccount = {
|
|
94
|
-
head: BN;
|
|
95
|
-
fundingPaymentRecords: FundingPaymentRecord[];
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export type LiquidationHistoryAccount = {
|
|
99
|
-
head: BN;
|
|
100
|
-
liquidationRecords: LiquidationRecord[];
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export type OrderHistoryAccount = {
|
|
104
|
-
head: BN;
|
|
105
|
-
lastOrderId: BN;
|
|
106
|
-
orderRecords: OrderRecord[];
|
|
107
|
-
};
|
|
108
|
-
|
|
109
103
|
export type DepositRecord = {
|
|
110
104
|
ts: BN;
|
|
111
|
-
recordId: BN;
|
|
112
105
|
userAuthority: PublicKey;
|
|
113
106
|
user: PublicKey;
|
|
114
107
|
direction: {
|
|
115
108
|
deposit?: any;
|
|
116
109
|
withdraw?: any;
|
|
117
110
|
};
|
|
118
|
-
|
|
119
|
-
cumulativeDepositsBefore: BN;
|
|
111
|
+
bankIndex: BN;
|
|
120
112
|
amount: BN;
|
|
113
|
+
oraclePrice: BN;
|
|
114
|
+
from?: PublicKey;
|
|
115
|
+
to?: PublicKey;
|
|
121
116
|
};
|
|
122
117
|
|
|
123
|
-
export type
|
|
118
|
+
export type CurveRecord = {
|
|
124
119
|
ts: BN;
|
|
125
120
|
recordId: BN;
|
|
126
121
|
marketIndex: BN;
|
|
@@ -140,28 +135,6 @@ export type ExtendedCurveRecord = {
|
|
|
140
135
|
tradeId: BN;
|
|
141
136
|
};
|
|
142
137
|
|
|
143
|
-
export type TradeRecord = {
|
|
144
|
-
ts: BN;
|
|
145
|
-
recordId: BN;
|
|
146
|
-
userAuthority: PublicKey;
|
|
147
|
-
user: PublicKey;
|
|
148
|
-
direction: {
|
|
149
|
-
long?: any;
|
|
150
|
-
short?: any;
|
|
151
|
-
};
|
|
152
|
-
baseAssetAmount: BN;
|
|
153
|
-
quoteAssetAmount: BN;
|
|
154
|
-
markPriceBefore: BN;
|
|
155
|
-
markPriceAfter: BN;
|
|
156
|
-
fee: BN;
|
|
157
|
-
quoteAssetAmountSurplus: BN;
|
|
158
|
-
refereeDiscount: BN;
|
|
159
|
-
tokenDiscount: BN;
|
|
160
|
-
marketIndex: BN;
|
|
161
|
-
liquidation: boolean;
|
|
162
|
-
oraclePrice: BN;
|
|
163
|
-
};
|
|
164
|
-
|
|
165
138
|
export type FundingRateRecord = {
|
|
166
139
|
ts: BN;
|
|
167
140
|
recordId: BN;
|
|
@@ -175,7 +148,6 @@ export type FundingRateRecord = {
|
|
|
175
148
|
|
|
176
149
|
export type FundingPaymentRecord = {
|
|
177
150
|
ts: BN;
|
|
178
|
-
recordId: BN;
|
|
179
151
|
userAuthority: PublicKey;
|
|
180
152
|
user: PublicKey;
|
|
181
153
|
marketIndex: BN;
|
|
@@ -207,18 +179,24 @@ export type LiquidationRecord = {
|
|
|
207
179
|
|
|
208
180
|
export type OrderRecord = {
|
|
209
181
|
ts: BN;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
182
|
+
taker: PublicKey;
|
|
183
|
+
maker: PublicKey;
|
|
184
|
+
takerOrder: Order;
|
|
185
|
+
makerOrder: Order;
|
|
186
|
+
takerUnsettledPnl: BN;
|
|
187
|
+
makerUnsettledPnl: BN;
|
|
214
188
|
action: OrderAction;
|
|
189
|
+
actionExplanation: OrderActionExplanation;
|
|
215
190
|
filler: PublicKey;
|
|
191
|
+
fillRecordId: BN;
|
|
192
|
+
marketIndex: BN;
|
|
216
193
|
baseAssetAmountFilled: BN;
|
|
217
194
|
quoteAssetAmountFilled: BN;
|
|
218
|
-
|
|
195
|
+
makerRebate: BN;
|
|
196
|
+
takerFee: BN;
|
|
219
197
|
fillerReward: BN;
|
|
220
|
-
tradeRecordId: BN;
|
|
221
198
|
quoteAssetAmountSurplus: BN;
|
|
199
|
+
oraclePrice: BN;
|
|
222
200
|
};
|
|
223
201
|
|
|
224
202
|
export type StateAccount = {
|
|
@@ -226,23 +204,12 @@ export type StateAccount = {
|
|
|
226
204
|
fundingPaused: boolean;
|
|
227
205
|
exchangePaused: boolean;
|
|
228
206
|
adminControlsPrices: boolean;
|
|
229
|
-
collateralMint: PublicKey;
|
|
230
|
-
collateralVault: PublicKey;
|
|
231
|
-
collateralVaultAuthority: PublicKey;
|
|
232
|
-
collateralVaultNonce: number;
|
|
233
207
|
insuranceVault: PublicKey;
|
|
234
208
|
insuranceVaultAuthority: PublicKey;
|
|
235
209
|
insuranceVaultNonce: number;
|
|
236
210
|
marginRatioInitial: BN;
|
|
237
211
|
marginRatioMaintenance: BN;
|
|
238
212
|
marginRatioPartial: BN;
|
|
239
|
-
markets: PublicKey;
|
|
240
|
-
curveHistory: PublicKey;
|
|
241
|
-
depositHistory: PublicKey;
|
|
242
|
-
fundingRateHistory: PublicKey;
|
|
243
|
-
fundingPaymentHistory: PublicKey;
|
|
244
|
-
tradeHistory: PublicKey;
|
|
245
|
-
liquidationHistory: PublicKey;
|
|
246
213
|
partialLiquidationClosePercentageNumerator: BN;
|
|
247
214
|
partialLiquidationClosePercentageDenominator: BN;
|
|
248
215
|
partialLiquidationPenaltyPercentageNumerator: BN;
|
|
@@ -258,37 +225,52 @@ export type StateAccount = {
|
|
|
258
225
|
discountMint: PublicKey;
|
|
259
226
|
oracleGuardRails: OracleGuardRails;
|
|
260
227
|
maxDeposit: BN;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
export type OrderStateAccount = {
|
|
266
|
-
orderHistory: PublicKey;
|
|
267
|
-
orderFillerRewardStructure: OrderFillerRewardStructure;
|
|
228
|
+
numberOfMarkets: BN;
|
|
229
|
+
numberOfBanks: BN;
|
|
268
230
|
minOrderQuoteAssetAmount: BN;
|
|
269
231
|
};
|
|
270
232
|
|
|
271
|
-
export type
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
enabled: boolean;
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
export type MarketsAccount = {
|
|
279
|
-
markets: Market[];
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
export type Market = {
|
|
233
|
+
export type MarketAccount = {
|
|
234
|
+
initialized: boolean;
|
|
235
|
+
marketIndex: BN;
|
|
236
|
+
pubkey: PublicKey;
|
|
283
237
|
amm: AMM;
|
|
284
238
|
baseAssetAmount: BN;
|
|
285
239
|
baseAssetAmountLong: BN;
|
|
286
240
|
baseAssetAmountShort: BN;
|
|
287
|
-
initialized: boolean;
|
|
288
241
|
openInterest: BN;
|
|
289
242
|
marginRatioInitial: number;
|
|
290
243
|
marginRatioMaintenance: number;
|
|
291
244
|
marginRatioPartial: number;
|
|
245
|
+
nextFillRecordId: BN;
|
|
246
|
+
pnlPool: PoolBalance;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
export type BankAccount = {
|
|
250
|
+
bankIndex: BN;
|
|
251
|
+
pubkey: PublicKey;
|
|
252
|
+
mint: PublicKey;
|
|
253
|
+
vault: PublicKey;
|
|
254
|
+
vaultAuthority: PublicKey;
|
|
255
|
+
vaultAuthorityNonce: number;
|
|
256
|
+
decimals: number;
|
|
257
|
+
optimalUtilization: BN;
|
|
258
|
+
optimalBorrowRate: BN;
|
|
259
|
+
maxBorrowRate: BN;
|
|
260
|
+
cumulativeDepositInterest: BN;
|
|
261
|
+
cumulativeBorrowInterest: BN;
|
|
262
|
+
depositBalance: BN;
|
|
263
|
+
borrowBalance: BN;
|
|
264
|
+
lastUpdated: BN;
|
|
265
|
+
oracle: PublicKey;
|
|
266
|
+
initialAssetWeight: BN;
|
|
267
|
+
maintenanceAssetWeight: BN;
|
|
268
|
+
initialLiabilityWeight: BN;
|
|
269
|
+
maintenanceLiabilityWeight: BN;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
export type PoolBalance = {
|
|
273
|
+
balance: BN;
|
|
292
274
|
};
|
|
293
275
|
|
|
294
276
|
export type AMM = {
|
|
@@ -301,6 +283,8 @@ export type AMM = {
|
|
|
301
283
|
lastMarkPriceTwapTs: BN;
|
|
302
284
|
lastOraclePriceTwap: BN;
|
|
303
285
|
lastOraclePriceTwapTs: BN;
|
|
286
|
+
lastOracleMarkSpreadPct: BN;
|
|
287
|
+
lastOracleConfPct: BN;
|
|
304
288
|
oracle: PublicKey;
|
|
305
289
|
oracleSource: OracleSource;
|
|
306
290
|
fundingPeriod: BN;
|
|
@@ -314,9 +298,26 @@ export type AMM = {
|
|
|
314
298
|
totalFeeWithdrawn: BN;
|
|
315
299
|
totalFee: BN;
|
|
316
300
|
minimumQuoteAssetTradeSize: BN;
|
|
317
|
-
|
|
301
|
+
baseAssetAmountStepSize: BN;
|
|
302
|
+
maxBaseAssetAmountRatio: number;
|
|
303
|
+
maxSlippageRatio: number;
|
|
318
304
|
lastOraclePrice: BN;
|
|
319
305
|
baseSpread: number;
|
|
306
|
+
curveUpdateIntensity: number;
|
|
307
|
+
netBaseAssetAmount: BN;
|
|
308
|
+
quoteAssetAmountLong: BN;
|
|
309
|
+
quoteAssetAmountShort: BN;
|
|
310
|
+
terminalQuoteAssetReserve: BN;
|
|
311
|
+
feePool: PoolBalance;
|
|
312
|
+
totalExchangeFee: BN;
|
|
313
|
+
totalMmFee: BN;
|
|
314
|
+
netRevenueSinceLastFunding: BN;
|
|
315
|
+
lastUpdateSlot: BN;
|
|
316
|
+
lastBidPriceTwap: BN;
|
|
317
|
+
lastAskPriceTwap: BN;
|
|
318
|
+
longSpread: BN;
|
|
319
|
+
shortSpread: BN;
|
|
320
|
+
maxSpread: number;
|
|
320
321
|
};
|
|
321
322
|
|
|
322
323
|
// # User Account Types
|
|
@@ -325,45 +326,46 @@ export type UserPosition = {
|
|
|
325
326
|
lastCumulativeFundingRate: BN;
|
|
326
327
|
marketIndex: BN;
|
|
327
328
|
quoteAssetAmount: BN;
|
|
329
|
+
quoteEntryAmount: BN;
|
|
328
330
|
openOrders: BN;
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
positions: UserPosition[];
|
|
333
|
-
user: PublicKey;
|
|
331
|
+
unsettledPnl: BN;
|
|
332
|
+
openBids: BN;
|
|
333
|
+
openAsks: BN;
|
|
334
334
|
};
|
|
335
335
|
|
|
336
336
|
export type UserAccount = {
|
|
337
337
|
authority: PublicKey;
|
|
338
|
+
name: number[];
|
|
339
|
+
userId: number;
|
|
340
|
+
bankBalances: UserBankBalance[];
|
|
338
341
|
collateral: BN;
|
|
339
342
|
cumulativeDeposits: BN;
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
forgoPositionSettlement: number;
|
|
350
|
-
hasSettledPosition: number;
|
|
343
|
+
fees: {
|
|
344
|
+
totalFeePaid: BN;
|
|
345
|
+
totalFeeRebate: BN;
|
|
346
|
+
totalTokenDiscount: BN;
|
|
347
|
+
totalReferralReward: BN;
|
|
348
|
+
totalRefereeDiscount: BN;
|
|
349
|
+
};
|
|
350
|
+
positions: UserPosition[];
|
|
351
|
+
orders: Order[];
|
|
351
352
|
};
|
|
352
353
|
|
|
353
|
-
export type
|
|
354
|
-
|
|
355
|
-
|
|
354
|
+
export type UserBankBalance = {
|
|
355
|
+
bankIndex: BN;
|
|
356
|
+
balanceType: BankBalanceType;
|
|
357
|
+
balance: BN;
|
|
356
358
|
};
|
|
357
359
|
|
|
358
360
|
export type Order = {
|
|
359
361
|
status: OrderStatus;
|
|
360
362
|
orderType: OrderType;
|
|
361
363
|
ts: BN;
|
|
364
|
+
slot: BN;
|
|
362
365
|
orderId: BN;
|
|
363
366
|
userOrderId: number;
|
|
364
367
|
marketIndex: BN;
|
|
365
368
|
price: BN;
|
|
366
|
-
userBaseAssetAmount: BN;
|
|
367
369
|
baseAssetAmount: BN;
|
|
368
370
|
baseAssetAmountFilled: BN;
|
|
369
371
|
quoteAssetAmount: BN;
|
|
@@ -373,18 +375,22 @@ export type Order = {
|
|
|
373
375
|
reduceOnly: boolean;
|
|
374
376
|
triggerPrice: BN;
|
|
375
377
|
triggerCondition: OrderTriggerCondition;
|
|
378
|
+
triggered: boolean;
|
|
376
379
|
discountTier: OrderDiscountTier;
|
|
380
|
+
existingPositionDirection: PositionDirection;
|
|
377
381
|
referrer: PublicKey;
|
|
378
382
|
postOnly: boolean;
|
|
379
383
|
immediateOrCancel: boolean;
|
|
380
384
|
oraclePriceOffset: BN;
|
|
385
|
+
auctionDuration: number;
|
|
386
|
+
auctionStartPrice: BN;
|
|
387
|
+
auctionEndPrice: BN;
|
|
381
388
|
};
|
|
382
389
|
|
|
383
390
|
export type OrderParams = {
|
|
384
391
|
orderType: OrderType;
|
|
385
392
|
userOrderId: number;
|
|
386
393
|
direction: PositionDirection;
|
|
387
|
-
quoteAssetAmount: BN;
|
|
388
394
|
baseAssetAmount: BN;
|
|
389
395
|
price: BN;
|
|
390
396
|
marketIndex: BN;
|
|
@@ -403,6 +409,49 @@ export type OrderParams = {
|
|
|
403
409
|
};
|
|
404
410
|
};
|
|
405
411
|
|
|
412
|
+
export type NecessaryOrderParams = {
|
|
413
|
+
orderType: OrderType;
|
|
414
|
+
marketIndex: BN;
|
|
415
|
+
baseAssetAmount: BN;
|
|
416
|
+
direction: PositionDirection;
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
export type OptionalOrderParams = {
|
|
420
|
+
[Property in keyof OrderParams]?: OrderParams[Property];
|
|
421
|
+
} & NecessaryOrderParams;
|
|
422
|
+
|
|
423
|
+
export const DefaultOrderParams = {
|
|
424
|
+
orderType: OrderType.MARKET,
|
|
425
|
+
userOrderId: 0,
|
|
426
|
+
direction: PositionDirection.LONG,
|
|
427
|
+
baseAssetAmount: ZERO,
|
|
428
|
+
price: ZERO,
|
|
429
|
+
marketIndex: ZERO,
|
|
430
|
+
reduceOnly: false,
|
|
431
|
+
postOnly: false,
|
|
432
|
+
immediateOrCancel: false,
|
|
433
|
+
triggerPrice: ZERO,
|
|
434
|
+
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
435
|
+
positionLimit: ZERO,
|
|
436
|
+
oraclePriceOffset: ZERO,
|
|
437
|
+
padding0: ZERO,
|
|
438
|
+
padding1: ZERO,
|
|
439
|
+
optionalAccounts: {
|
|
440
|
+
discountToken: false,
|
|
441
|
+
referrer: false,
|
|
442
|
+
},
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
export type MakerInfo = {
|
|
446
|
+
maker: PublicKey;
|
|
447
|
+
order: Order;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
export type TakerInfo = {
|
|
451
|
+
taker: PublicKey;
|
|
452
|
+
order: Order;
|
|
453
|
+
};
|
|
454
|
+
|
|
406
455
|
// # Misc Types
|
|
407
456
|
export interface IWallet {
|
|
408
457
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
|
@@ -441,6 +490,9 @@ export type FeeStructure = {
|
|
|
441
490
|
refereeDiscountNumerator: BN;
|
|
442
491
|
refereeDiscountDenominator: BN;
|
|
443
492
|
};
|
|
493
|
+
makerRebateNumerator: BN;
|
|
494
|
+
makerRebateDenominator: BN;
|
|
495
|
+
fillerRewardStructure: OrderFillerRewardStructure;
|
|
444
496
|
};
|
|
445
497
|
|
|
446
498
|
export type OracleGuardRails = {
|
package/src/userName.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeName = exports.encodeName = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
|
|
4
|
+
exports.MAX_NAME_LENGTH = 32;
|
|
5
|
+
exports.DEFAULT_USER_NAME = 'Main Account';
|
|
6
|
+
function encodeName(name) {
|
|
7
|
+
if (name.length > exports.MAX_NAME_LENGTH) {
|
|
8
|
+
throw Error(`User name (${name}) longer than 32 characters`);
|
|
9
|
+
}
|
|
10
|
+
const buffer = Buffer.alloc(32);
|
|
11
|
+
buffer.fill(name);
|
|
12
|
+
buffer.fill(' ', name.length);
|
|
13
|
+
return Array(...buffer);
|
|
14
|
+
}
|
|
15
|
+
exports.encodeName = encodeName;
|
|
16
|
+
function decodeName(bytes) {
|
|
17
|
+
const buffer = Buffer.from(bytes);
|
|
18
|
+
return buffer.toString('utf8').trim();
|
|
19
|
+
}
|
|
20
|
+
exports.decodeName = decodeName;
|
package/src/userName.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const MAX_NAME_LENGTH = 32;
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_USER_NAME = 'Main Account';
|
|
4
|
+
|
|
5
|
+
export function encodeName(name: string): number[] {
|
|
6
|
+
if (name.length > MAX_NAME_LENGTH) {
|
|
7
|
+
throw Error(`User name (${name}) longer than 32 characters`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const buffer = Buffer.alloc(32);
|
|
11
|
+
buffer.fill(name);
|
|
12
|
+
buffer.fill(' ', name.length);
|
|
13
|
+
|
|
14
|
+
return Array(...buffer);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function decodeName(bytes: number[]): string {
|
|
18
|
+
const buffer = Buffer.from(bytes);
|
|
19
|
+
return buffer.toString('utf8').trim();
|
|
20
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTimeout = void 0;
|
|
4
|
+
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
+
let timeoutId;
|
|
6
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
+
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
+
});
|
|
9
|
+
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
+
clearTimeout(timeoutId);
|
|
11
|
+
return result;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.estimateTps = void 0;
|
|
13
|
+
function estimateTps(programId, connection, failed) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
+
if (failed) {
|
|
17
|
+
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
+
}
|
|
19
|
+
const numberOfSignatures = signatures.length;
|
|
20
|
+
if (numberOfSignatures === 0) {
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
return (numberOfSignatures /
|
|
24
|
+
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.estimateTps = estimateTps;
|
package/src/wallet.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Wallet = void 0;
|
|
13
|
+
class Wallet {
|
|
14
|
+
constructor(payer) {
|
|
15
|
+
this.payer = payer;
|
|
16
|
+
}
|
|
17
|
+
signTransaction(tx) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
tx.partialSign(this.payer);
|
|
20
|
+
return tx;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
signAllTransactions(txs) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return txs.map((t) => {
|
|
26
|
+
t.partialSign(this.payer);
|
|
27
|
+
return t;
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
get publicKey() {
|
|
32
|
+
return this.payer.publicKey;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.Wallet = Wallet;
|