@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.2

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 (197) 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 +9 -17
  26. package/lib/admin.js +375 -558
  27. package/lib/clearingHouse.d.ts +86 -109
  28. package/lib/clearingHouse.js +828 -895
  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 +34 -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 +2287 -2938
  65. package/lib/index.d.ts +12 -4
  66. package/lib/index.js +12 -4
  67. package/lib/math/amm.d.ts +24 -29
  68. package/lib/math/amm.js +239 -178
  69. package/lib/math/auction.d.ts +5 -0
  70. package/lib/math/auction.js +39 -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 +5 -0
  80. package/lib/math/orders.js +31 -1
  81. package/lib/math/position.d.ts +7 -5
  82. package/lib/math/position.js +27 -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 -21
  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/orders.d.ts +6 -7
  98. package/lib/orders.js +10 -80
  99. package/lib/slot/SlotSubscriber.d.ts +12 -0
  100. package/lib/slot/SlotSubscriber.js +23 -0
  101. package/lib/tx/retryTxSender.d.ts +2 -2
  102. package/lib/tx/retryTxSender.js +108 -123
  103. package/lib/tx/types.d.ts +5 -1
  104. package/lib/tx/utils.d.ts +1 -1
  105. package/lib/tx/utils.js +11 -2
  106. package/lib/types.d.ts +110 -109
  107. package/lib/types.js +14 -1
  108. package/lib/userName.d.ts +4 -0
  109. package/lib/userName.js +20 -0
  110. package/lib/util/computeUnits.js +10 -21
  111. package/lib/util/tps.js +11 -22
  112. package/lib/wallet.js +7 -20
  113. package/package.json +11 -4
  114. package/src/accounts/bulkAccountLoader.ts +21 -15
  115. package/src/accounts/bulkUserSubscription.ts +1 -45
  116. package/src/accounts/fetch.ts +33 -0
  117. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  118. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  119. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  120. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  121. package/src/accounts/types.ts +41 -70
  122. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  123. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  124. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  125. package/src/addresses/marketAddresses.ts +18 -0
  126. package/src/addresses/pda.ts +118 -0
  127. package/src/admin.ts +218 -346
  128. package/src/clearingHouse.ts +983 -952
  129. package/src/clearingHouseConfig.ts +37 -0
  130. package/src/clearingHouseUser.ts +275 -185
  131. package/src/clearingHouseUserConfig.ts +18 -0
  132. package/src/config.ts +54 -1
  133. package/src/constants/banks.ts +43 -0
  134. package/src/constants/markets.ts +16 -207
  135. package/src/constants/numericConstants.ts +34 -5
  136. package/src/events/eventList.ts +94 -0
  137. package/src/events/eventSubscriber.ts +194 -0
  138. package/src/events/fetchLogs.ts +80 -0
  139. package/src/events/pollingLogProvider.ts +79 -0
  140. package/src/events/sort.ts +65 -0
  141. package/src/events/txEventCache.ts +74 -0
  142. package/src/events/types.ts +98 -0
  143. package/src/events/webSocketLogProvider.ts +38 -0
  144. package/src/examples/makeTradeExample.ts +20 -11
  145. package/src/factory/bigNum.ts +524 -0
  146. package/src/factory/oracleClient.ts +7 -4
  147. package/src/idl/clearing_house.json +2287 -2938
  148. package/src/index.ts +12 -4
  149. package/src/math/amm.ts +414 -245
  150. package/src/math/auction.ts +39 -0
  151. package/src/math/bankBalance.ts +112 -0
  152. package/src/math/conversion.ts +1 -11
  153. package/src/math/funding.ts +12 -9
  154. package/src/math/market.ts +37 -30
  155. package/src/math/orders.ts +38 -0
  156. package/src/math/position.ts +48 -35
  157. package/src/math/repeg.ts +176 -0
  158. package/src/math/trade.ts +45 -35
  159. package/src/math/utils.js +27 -0
  160. package/src/math/utils.js.map +1 -0
  161. package/src/oracles/oracleClientCache.ts +20 -0
  162. package/src/oracles/pythClient.ts +4 -11
  163. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  164. package/src/oracles/switchboardClient.ts +11 -24
  165. package/src/oracles/types.ts +7 -1
  166. package/src/orders.ts +24 -126
  167. package/src/slot/SlotSubscriber.ts +32 -0
  168. package/src/tx/retryTxSender.ts +6 -4
  169. package/src/tx/types.ts +6 -1
  170. package/src/tx/utils.ts +22 -3
  171. package/src/types.ts +111 -122
  172. package/src/userName.ts +20 -0
  173. package/src/util/computeUnits.js +17 -0
  174. package/src/util/computeUnits.js.map +1 -0
  175. package/tests/bn/test.ts +255 -0
  176. package/tsconfig.json +12 -12
  177. package/lib/addresses.d.ts +0 -10
  178. package/lib/addresses.js +0 -93
  179. package/lib/constants/accounts.d.ts +0 -15
  180. package/lib/constants/accounts.js +0 -18
  181. package/lib/factory/clearingHouse.d.ts +0 -35
  182. package/lib/factory/clearingHouse.js +0 -81
  183. package/lib/factory/clearingHouseUser.d.ts +0 -19
  184. package/lib/factory/clearingHouseUser.js +0 -34
  185. package/lib/math/insuranceFund.d.ts +0 -15
  186. package/lib/math/insuranceFund.js +0 -33
  187. package/lib/settlement.d.ts +0 -4
  188. package/lib/settlement.js +0 -10
  189. package/lib/tx/defaultTxSender.d.ts +0 -8
  190. package/lib/tx/defaultTxSender.js +0 -12
  191. package/src/addresses.ts +0 -82
  192. package/src/constants/accounts.ts +0 -26
  193. package/src/factory/clearingHouse.ts +0 -173
  194. package/src/factory/clearingHouseUser.ts +0 -73
  195. package/src/math/insuranceFund.ts +0 -29
  196. package/src/settlement.ts +0 -9
  197. package/src/tx/defaultTxSender.ts +0 -24
package/src/types.ts CHANGED
@@ -7,6 +7,11 @@ 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: {} };
@@ -15,6 +20,7 @@ export class PositionDirection {
15
20
  export class OracleSource {
16
21
  static readonly PYTH = { pyth: {} };
17
22
  static readonly SWITCHBOARD = { switchboard: {} };
23
+ static readonly QUOTE_ASSET = { quoteAsset: {} };
18
24
  }
19
25
 
20
26
  export class OrderType {
@@ -42,6 +48,7 @@ export class OrderAction {
42
48
  static readonly CANCEL = { cancel: {} };
43
49
  static readonly EXPIRE = { expire: {} };
44
50
  static readonly FILL = { fill: {} };
51
+ static readonly TRIGGER = { trigger: {} };
45
52
  }
46
53
 
47
54
  export class OrderTriggerCondition {
@@ -53,6 +60,12 @@ export function isVariant(object: unknown, type: string) {
53
60
  return object.hasOwnProperty(type);
54
61
  }
55
62
 
63
+ export function isOneOfVariant(object: unknown, types: string[]) {
64
+ return types.reduce((result, type) => {
65
+ return result || object.hasOwnProperty(type);
66
+ }, false);
67
+ }
68
+
56
69
  export enum TradeSide {
57
70
  None = 0,
58
71
  Buy = 1,
@@ -69,58 +82,21 @@ export type CandleResolution =
69
82
  | 'W'
70
83
  | 'M';
71
84
 
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
85
  export type DepositRecord = {
110
86
  ts: BN;
111
- recordId: BN;
112
87
  userAuthority: PublicKey;
113
88
  user: PublicKey;
114
89
  direction: {
115
90
  deposit?: any;
116
91
  withdraw?: any;
117
92
  };
118
- collateralBefore: BN;
119
- cumulativeDepositsBefore: BN;
93
+ bankIndex: BN;
120
94
  amount: BN;
95
+ from?: PublicKey;
96
+ to?: PublicKey;
121
97
  };
122
98
 
123
- export type ExtendedCurveRecord = {
99
+ export type CurveRecord = {
124
100
  ts: BN;
125
101
  recordId: BN;
126
102
  marketIndex: BN;
@@ -140,28 +116,6 @@ export type ExtendedCurveRecord = {
140
116
  tradeId: BN;
141
117
  };
142
118
 
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
119
  export type FundingRateRecord = {
166
120
  ts: BN;
167
121
  recordId: BN;
@@ -175,7 +129,6 @@ export type FundingRateRecord = {
175
129
 
176
130
  export type FundingPaymentRecord = {
177
131
  ts: BN;
178
- recordId: BN;
179
132
  userAuthority: PublicKey;
180
133
  user: PublicKey;
181
134
  marketIndex: BN;
@@ -207,18 +160,21 @@ export type LiquidationRecord = {
207
160
 
208
161
  export type OrderRecord = {
209
162
  ts: BN;
210
- recordId: BN;
211
- order: Order;
212
- user: PublicKey;
213
- authority: PublicKey;
163
+ taker: PublicKey;
164
+ maker: PublicKey;
165
+ takerOrder: Order;
166
+ makerOrder: Order;
214
167
  action: OrderAction;
215
168
  filler: PublicKey;
169
+ fillRecordId: BN;
170
+ marketIndex: BN;
216
171
  baseAssetAmountFilled: BN;
217
172
  quoteAssetAmountFilled: BN;
218
- fee: BN;
173
+ makerRebate: BN;
174
+ takerFee: BN;
219
175
  fillerReward: BN;
220
- tradeRecordId: BN;
221
176
  quoteAssetAmountSurplus: BN;
177
+ oraclePrice: BN;
222
178
  };
223
179
 
224
180
  export type StateAccount = {
@@ -226,23 +182,12 @@ export type StateAccount = {
226
182
  fundingPaused: boolean;
227
183
  exchangePaused: boolean;
228
184
  adminControlsPrices: boolean;
229
- collateralMint: PublicKey;
230
- collateralVault: PublicKey;
231
- collateralVaultAuthority: PublicKey;
232
- collateralVaultNonce: number;
233
185
  insuranceVault: PublicKey;
234
186
  insuranceVaultAuthority: PublicKey;
235
187
  insuranceVaultNonce: number;
236
188
  marginRatioInitial: BN;
237
189
  marginRatioMaintenance: BN;
238
190
  marginRatioPartial: BN;
239
- markets: PublicKey;
240
- curveHistory: PublicKey;
241
- depositHistory: PublicKey;
242
- fundingRateHistory: PublicKey;
243
- fundingPaymentHistory: PublicKey;
244
- tradeHistory: PublicKey;
245
- liquidationHistory: PublicKey;
246
191
  partialLiquidationClosePercentageNumerator: BN;
247
192
  partialLiquidationClosePercentageDenominator: BN;
248
193
  partialLiquidationPenaltyPercentageNumerator: BN;
@@ -258,37 +203,52 @@ export type StateAccount = {
258
203
  discountMint: PublicKey;
259
204
  oracleGuardRails: OracleGuardRails;
260
205
  maxDeposit: BN;
261
- orderState: PublicKey;
262
- extendedCurveHistory: PublicKey;
263
- };
264
-
265
- export type OrderStateAccount = {
266
- orderHistory: PublicKey;
267
- orderFillerRewardStructure: OrderFillerRewardStructure;
206
+ numberOfMarkets: BN;
207
+ numberOfBanks: BN;
268
208
  minOrderQuoteAssetAmount: BN;
269
209
  };
270
210
 
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 = {
211
+ export type MarketAccount = {
212
+ initialized: boolean;
213
+ marketIndex: BN;
214
+ pubkey: PublicKey;
283
215
  amm: AMM;
284
216
  baseAssetAmount: BN;
285
217
  baseAssetAmountLong: BN;
286
218
  baseAssetAmountShort: BN;
287
- initialized: boolean;
288
219
  openInterest: BN;
289
220
  marginRatioInitial: number;
290
221
  marginRatioMaintenance: number;
291
222
  marginRatioPartial: number;
223
+ nextFillRecordId: BN;
224
+ pnlPool: PoolBalance;
225
+ };
226
+
227
+ export type BankAccount = {
228
+ bankIndex: BN;
229
+ pubkey: PublicKey;
230
+ mint: PublicKey;
231
+ vault: PublicKey;
232
+ vaultAuthority: PublicKey;
233
+ vaultAuthorityNonce: number;
234
+ decimals: number;
235
+ optimalUtilization: BN;
236
+ optimalBorrowRate: BN;
237
+ maxBorrowRate: BN;
238
+ cumulativeDepositInterest: BN;
239
+ cumulativeBorrowInterest: BN;
240
+ depositBalance: BN;
241
+ borrowBalance: BN;
242
+ lastUpdated: BN;
243
+ oracle: PublicKey;
244
+ initialAssetWeight: BN;
245
+ maintenanceAssetWeight: BN;
246
+ initialLiabilityWeight: BN;
247
+ maintenanceLiabilityWeight: BN;
248
+ };
249
+
250
+ export type PoolBalance = {
251
+ balance: BN;
292
252
  };
293
253
 
294
254
  export type AMM = {
@@ -314,9 +274,24 @@ export type AMM = {
314
274
  totalFeeWithdrawn: BN;
315
275
  totalFee: BN;
316
276
  minimumQuoteAssetTradeSize: BN;
317
- minimumBaseAssetTradeSize: BN;
277
+ baseAssetAmountStepSize: BN;
318
278
  lastOraclePrice: BN;
319
279
  baseSpread: number;
280
+ curveUpdateIntensity: number;
281
+ netBaseAssetAmount: BN;
282
+ quoteAssetAmountLong: BN;
283
+ quoteAssetAmountShort: BN;
284
+ terminalQuoteAssetReserve: BN;
285
+ feePool: PoolBalance;
286
+ totalExchangeFee: BN;
287
+ totalMmFee: BN;
288
+ netRevenueSinceLastFunding: BN;
289
+ lastUpdateSlot: BN;
290
+ lastBidPriceTwap: BN;
291
+ lastAskPriceTwap: BN;
292
+ longSpread: BN;
293
+ shortSpread: BN;
294
+ maxSpread: number;
320
295
  };
321
296
 
322
297
  // # User Account Types
@@ -325,45 +300,46 @@ export type UserPosition = {
325
300
  lastCumulativeFundingRate: BN;
326
301
  marketIndex: BN;
327
302
  quoteAssetAmount: BN;
303
+ quoteEntryAmount: BN;
328
304
  openOrders: BN;
329
- };
330
-
331
- export type UserPositionsAccount = {
332
- positions: UserPosition[];
333
- user: PublicKey;
305
+ unsettledPnl: BN;
306
+ openBids: BN;
307
+ openAsks: BN;
334
308
  };
335
309
 
336
310
  export type UserAccount = {
337
311
  authority: PublicKey;
312
+ name: number[];
313
+ userId: number;
314
+ bankBalances: UserBankBalance[];
338
315
  collateral: BN;
339
316
  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;
317
+ fees: {
318
+ totalFeePaid: BN;
319
+ totalFeeRebate: BN;
320
+ totalTokenDiscount: BN;
321
+ totalReferralReward: BN;
322
+ totalRefereeDiscount: BN;
323
+ };
324
+ positions: UserPosition[];
325
+ orders: Order[];
351
326
  };
352
327
 
353
- export type UserOrdersAccount = {
354
- orders: Order[];
355
- user: PublicKey;
328
+ export type UserBankBalance = {
329
+ bankIndex: BN;
330
+ balanceType: BankBalanceType;
331
+ balance: BN;
356
332
  };
357
333
 
358
334
  export type Order = {
359
335
  status: OrderStatus;
360
336
  orderType: OrderType;
361
337
  ts: BN;
338
+ slot: BN;
362
339
  orderId: BN;
363
340
  userOrderId: number;
364
341
  marketIndex: BN;
365
342
  price: BN;
366
- userBaseAssetAmount: BN;
367
343
  baseAssetAmount: BN;
368
344
  baseAssetAmountFilled: BN;
369
345
  quoteAssetAmount: BN;
@@ -373,11 +349,16 @@ export type Order = {
373
349
  reduceOnly: boolean;
374
350
  triggerPrice: BN;
375
351
  triggerCondition: OrderTriggerCondition;
352
+ triggered: boolean;
376
353
  discountTier: OrderDiscountTier;
354
+ existingPositionDirection: PositionDirection;
377
355
  referrer: PublicKey;
378
356
  postOnly: boolean;
379
357
  immediateOrCancel: boolean;
380
358
  oraclePriceOffset: BN;
359
+ auctionDuration: number;
360
+ auctionStartPrice: BN;
361
+ auctionEndPrice: BN;
381
362
  };
382
363
 
383
364
  export type OrderParams = {
@@ -403,6 +384,11 @@ export type OrderParams = {
403
384
  };
404
385
  };
405
386
 
387
+ export type MakerInfo = {
388
+ maker: PublicKey;
389
+ order: Order;
390
+ };
391
+
406
392
  // # Misc Types
407
393
  export interface IWallet {
408
394
  signTransaction(tx: Transaction): Promise<Transaction>;
@@ -441,6 +427,9 @@ export type FeeStructure = {
441
427
  refereeDiscountNumerator: BN;
442
428
  refereeDiscountDenominator: BN;
443
429
  };
430
+ makerRebateNumerator: BN;
431
+ makerRebateDenominator: BN;
432
+ fillerRewardStructure: OrderFillerRewardStructure;
444
433
  };
445
434
 
446
435
  export type OracleGuardRails = {
@@ -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,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findComputeUnitConsumption = void 0;
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
+ }
13
+ });
14
+ return computeUnits;
15
+ }
16
+ exports.findComputeUnitConsumption = findComputeUnitConsumption;
17
+ //# sourceMappingURL=computeUnits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"computeUnits.js","sourceRoot":"","sources":["computeUnits.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,0BAA0B,CAC/C,SAAoB,EACpB,UAAsB,EACtB,WAAmB,EACnB,aAAuB,WAAW;IAElC,MAAM,EAAE,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;IACxE,MAAM,YAAY,GAAG,EAAE,CAAC;IACxB,MAAM,KAAK,GAAG,IAAI,MAAM,CACvB,WAAW,SAAS,CAAC,QAAQ,EAAE,gDAAgD,CAC/E,CAAC;IACF,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAC1C,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE;YACtB,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;SAC5B;IACF,CAAC,CAAC,CAAC;IACH,OAAO,YAAY,CAAC;AACrB,CAAC;AAlBD,gEAkBC"}
@@ -0,0 +1,255 @@
1
+ import { BN } from '@project-serum/anchor';
2
+ import { expect } from 'chai';
3
+ import { BigNum } from '../../src/factory/bigNum';
4
+ import {
5
+ AMM_RESERVE_PRECISION_EXP,
6
+ BASE_PRECISION_EXP,
7
+ TEN_THOUSAND,
8
+ } from '../../src/constants/numericConstants';
9
+ // if you used the '@types/mocha' method to install mocha type definitions, uncomment the following line
10
+ // import 'mocha';
11
+
12
+ describe('BigNum Tests', () => {
13
+ it('basic string representations are correct', () => {
14
+ const bn = BigNum.from(TEN_THOUSAND);
15
+ expect(bn.toString()).to.equal('10000');
16
+ expect(bn.print()).to.equal('10000');
17
+
18
+ const bn2 = BigNum.from(TEN_THOUSAND, new BN(4));
19
+ expect(bn2.toString()).to.equal('10000');
20
+ expect(bn2.print()).to.equal('1.0000');
21
+
22
+ const bn3 = BigNum.from(new BN('123456789'), new BN(4));
23
+ expect(bn3.toString()).to.equal('123456789');
24
+ expect(bn3.print()).to.equal('12345.6789');
25
+ });
26
+
27
+ it('can do basic maths correctly', () => {
28
+ const val1 = BigNum.from(10 ** 4, 2).mul(BigNum.from(123456));
29
+
30
+ expect(val1.toString()).to.equal('1234560000');
31
+
32
+ // should trim one point of precision off
33
+ const val2 = val1.div(BigNum.from(10 ** 5));
34
+
35
+ expect(val2.toString()).to.equal('12345');
36
+ expect(val2.print()).to.equal('123.45');
37
+
38
+ // Trying to represent a 33.33333333% figure to precision 4
39
+ const baseNumberPrecision = 10;
40
+ const adjustmentPrecision = 4;
41
+
42
+ const currentNumber = 400 * 10 ** baseNumberPrecision;
43
+ const comparisonNumber = 300 * 10 ** baseNumberPrecision;
44
+
45
+ const val3 = BigNum.from(currentNumber, baseNumberPrecision)
46
+ .sub(BigNum.from(comparisonNumber, baseNumberPrecision))
47
+ .mul(BigNum.from(10 ** adjustmentPrecision, adjustmentPrecision))
48
+ .mul(BigNum.from(100))
49
+ .div(BigNum.from(comparisonNumber, baseNumberPrecision))
50
+ .abs();
51
+
52
+ expect(val3.toString()).to.equal('333333');
53
+ expect(val3.print()).to.equal('33.3333');
54
+ });
55
+
56
+ it('can shift numbers correctly', () => {
57
+ const val1 = BigNum.from(new BN(`319657850313098510000000000`), 23).shift(
58
+ new BN(-10)
59
+ );
60
+
61
+ expect(val1.toString()).to.equal(`31965785031309851`);
62
+ expect(val1.print()).to.equal(`3196.5785031309851`);
63
+ });
64
+
65
+ it('can print numbers correctly', () => {
66
+ // Case 1
67
+ const val = BigNum.from(123456789, 5);
68
+
69
+ expect(val.toString()).to.equal('123456789');
70
+
71
+ expect(val.print()).to.equal('1234.56789');
72
+
73
+ expect(val.toFixed(3)).to.equal('1234.567');
74
+
75
+ expect(val.toPrecision(1)).to.equal('1e3');
76
+ expect(val.toPrecision(3)).to.equal('123e1');
77
+ expect(val.toPrecision(4)).to.equal('1234');
78
+ expect(val.toPrecision(5)).to.equal('1234.5');
79
+ expect(val.toPrecision(11)).to.equal('1234.5678900');
80
+
81
+ // Case 2
82
+ const val2 = BigNum.from(1, 5);
83
+
84
+ expect(val2.toString()).to.equal('1');
85
+
86
+ expect(val2.print()).to.equal('0.00001');
87
+
88
+ // Case 3
89
+ const val3 = BigNum.from(101003, 5);
90
+
91
+ expect(val3.toString()).to.equal('101003');
92
+
93
+ expect(val3.print()).to.equal('1.01003');
94
+ expect(val3.toPrecision(7)).to.equal('1.010030');
95
+
96
+ // Case 4
97
+ const rawQuoteValue = 1;
98
+ const entryPriceNum = 40;
99
+ const val4 = BigNum.from(rawQuoteValue * 10 ** 8)
100
+ .shift(AMM_RESERVE_PRECISION_EXP)
101
+ .div(BigNum.from(entryPriceNum * 10 ** 8));
102
+
103
+ expect(val4.toString()).to.equal('250000000000');
104
+ expect(val4.print()).to.equal('0.0250000000000');
105
+ expect(val4.toFixed(3)).to.equal('0.025');
106
+ expect(val4.toPrecision(4)).to.equal('0.025');
107
+
108
+ // Case 5
109
+ expect(BigNum.fromPrint('1').toMillified()).to.equal('1.00');
110
+ expect(BigNum.fromPrint('12').toMillified()).to.equal('12.0');
111
+ expect(BigNum.fromPrint('123').toMillified()).to.equal('123');
112
+ expect(BigNum.fromPrint('1234').toMillified()).to.equal('1.23K');
113
+ expect(BigNum.fromPrint('12345').toMillified()).to.equal('12.3K');
114
+ expect(BigNum.fromPrint('123456').toMillified()).to.equal('123K');
115
+ expect(BigNum.fromPrint('1234567').toMillified()).to.equal('1.23M');
116
+ expect(BigNum.fromPrint('12345678').toMillified()).to.equal('12.3M');
117
+ expect(BigNum.fromPrint('123456789').toMillified()).to.equal('123M');
118
+
119
+ expect(BigNum.fromPrint('1').toMillified(5)).to.equal('1.0000');
120
+ expect(BigNum.fromPrint('12').toMillified(5)).to.equal('12.000');
121
+ expect(BigNum.fromPrint('123').toMillified(5)).to.equal('123.00');
122
+ expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('1.2340K');
123
+ expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('12.345K');
124
+ expect(BigNum.fromPrint('123456').toMillified(5)).to.equal('123.45K');
125
+ expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
126
+ expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
127
+ expect(BigNum.fromPrint('123456789').toMillified(5)).to.equal('123.45M');
128
+ });
129
+
130
+ it('can initialise from string values correctly', () => {
131
+ // Case 1
132
+
133
+ const baseAmountVal1 = '14.33';
134
+ const val1 = BigNum.fromPrint(baseAmountVal1, BASE_PRECISION_EXP);
135
+
136
+ expect(val1.toString()).to.equal('143300000000000');
137
+ expect(val1.print()).to.equal('14.3300000000000');
138
+
139
+ const baseAmountVal2 = '34.1';
140
+ const val2 = BigNum.fromPrint(baseAmountVal2, BASE_PRECISION_EXP);
141
+
142
+ expect(val2.printShort()).to.equal('34.1');
143
+ });
144
+
145
+ it('is immutable', () => {
146
+ // Case 1
147
+ const initVal = BigNum.from(1);
148
+ const postShift = initVal.shift(new BN(10), true);
149
+ const postScale = postShift.scale(1, 10 ** 10);
150
+
151
+ expect(initVal.toString()).to.equal(postScale.toString());
152
+ expect(initVal === postShift).to.equal(false);
153
+ expect(initVal.val === postShift.val).to.equal(false);
154
+ expect(initVal === postScale).to.equal(false);
155
+ expect(initVal.val === postScale.val).to.equal(false);
156
+ expect(postShift === postScale).to.equal(false);
157
+ expect(postShift.val === postScale.val).to.equal(false);
158
+
159
+ const postMul = postScale.mul(new BN(1000));
160
+ const postDiv = postMul.div(new BN(1000));
161
+
162
+ expect(postMul.toString()).to.equal('1000');
163
+ expect(postDiv.toString()).to.equal('1');
164
+ expect(postMul === postDiv).to.equal(false);
165
+ expect(postMul.val === postDiv.val).to.equal(false);
166
+
167
+ const postAdd = postDiv.add(BigNum.from(new BN(1000)));
168
+ const postSub = postAdd.sub(BigNum.from(new BN(1000)));
169
+
170
+ expect(postAdd.toString()).to.equal('1001');
171
+ expect(postSub.toString()).to.equal('1');
172
+ expect(postAdd === postSub).to.equal(false);
173
+ expect(postAdd.val === postSub.val).to.equal(false);
174
+ });
175
+
176
+ it('serializes properly', () => {
177
+ // JSON
178
+ let val = BigNum.from(new BN('123456'), 3);
179
+ expect(val.toString()).to.equal('123456');
180
+ val = val.shift(new BN(3));
181
+ expect(val.toString()).to.equal('123456000');
182
+ expect(val.print()).to.equal('123.456000');
183
+
184
+ const stringified = JSON.stringify(val);
185
+
186
+ expect(stringified).to.equal('{"val":"123456000","precision":"6"}');
187
+
188
+ let parsed = BigNum.fromJSON(JSON.parse(stringified));
189
+ expect(parsed.toString()).to.equal('123456000');
190
+ expect(parsed.print()).to.equal('123.456000');
191
+
192
+ parsed = parsed.shift(new BN(3));
193
+ expect(parsed.toString()).to.equal('123456000000');
194
+ expect(parsed.print()).to.equal('123.456000000');
195
+ });
196
+
197
+ it('can convert to a percentage', () => {
198
+ // JSON
199
+ const val = BigNum.from(new BN('100000'), 3);
200
+ const val2 = BigNum.from(new BN('200000'), 3);
201
+ const val3 = BigNum.from(new BN('66666'), 3);
202
+ const val4 = BigNum.from(new BN('50000'), 3);
203
+ const val5 = BigNum.from(new BN('700000'), 3);
204
+
205
+ expect(val.toPercentage(val2, 3)).to.equal('50.0');
206
+ expect(val.toPercentage(val2, 4)).to.equal('50.00');
207
+ expect(val3.toPercentage(val2, 4)).to.equal('33.33');
208
+ expect(val4.toPercentage(val2, 4)).to.equal('25.00');
209
+ expect(val.toPercentage(val5, 6)).to.equal('14.2857');
210
+ });
211
+
212
+ it('can print without unnecessary trailing zeroes', () => {
213
+ const rawQuoteValue = 1;
214
+ const entryPriceNum = 40;
215
+ const val = BigNum.from(rawQuoteValue * 10 ** 8)
216
+ .shift(AMM_RESERVE_PRECISION_EXP)
217
+ .div(BigNum.from(entryPriceNum * 10 ** 8));
218
+
219
+ expect(val.toString()).to.equal('250000000000');
220
+ expect(val.printShort()).to.equal('0.025');
221
+
222
+ const val2 = BigNum.from(10000, 4);
223
+ expect(val2.print()).to.equal('1.0000');
224
+ expect(val2.printShort()).to.equal('1');
225
+ });
226
+
227
+ it('can pretty print', () => {
228
+ const val = BigNum.from('123');
229
+ expect(val.prettyPrint()).to.equal('123');
230
+
231
+ const val2 = BigNum.from('1234');
232
+ expect(val2.prettyPrint()).to.equal('1,234');
233
+
234
+ const val3 = BigNum.from('123456');
235
+ expect(val3.prettyPrint()).to.equal('123,456');
236
+
237
+ const val4 = BigNum.from('1234567');
238
+ expect(val4.prettyPrint()).to.equal('1,234,567');
239
+
240
+ const val5 = BigNum.from('12345678');
241
+ expect(val5.prettyPrint()).to.equal('12,345,678');
242
+
243
+ const val6 = BigNum.from('123456', 3);
244
+ expect(val6.prettyPrint()).to.equal('123.456');
245
+
246
+ const val7 = BigNum.from('123456789', 3);
247
+ expect(val7.prettyPrint()).to.equal('123,456.789');
248
+
249
+ const val8 = BigNum.from('1000000000000', 6);
250
+ expect(val8.prettyPrint()).to.equal('1,000,000');
251
+
252
+ const val9 = BigNum.from('1000000000123', 6);
253
+ expect(val9.prettyPrint()).to.equal('1,000,000.000123');
254
+ });
255
+ });