@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/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
- collateralBefore: BN;
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 ExtendedCurveRecord = {
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
- recordId: BN;
211
- order: Order;
212
- user: PublicKey;
213
- authority: PublicKey;
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
- fee: BN;
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
- orderState: PublicKey;
262
- extendedCurveHistory: PublicKey;
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 SettlementStateAccount = {
272
- totalSettlementValue: BN;
273
- collateralAvailableToClaim: BN;
274
- collateralClaimed: BN;
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
- minimumBaseAssetTradeSize: BN;
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
- export type UserPositionsAccount = {
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
- 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;
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 UserOrdersAccount = {
354
- orders: Order[];
355
- user: PublicKey;
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 = {
@@ -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;
@@ -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;
@@ -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;