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