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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +8 -17
  26. package/lib/admin.js +366 -558
  27. package/lib/clearingHouse.d.ts +84 -109
  28. package/lib/clearingHouse.js +778 -899
  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 +16 -0
  42. package/lib/constants/numericConstants.js +22 -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 +112 -0
  61. package/lib/factory/bigNum.js +356 -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 +2213 -2951
  65. package/lib/index.d.ts +12 -4
  66. package/lib/index.js +12 -4
  67. package/lib/math/amm.d.ts +19 -29
  68. package/lib/math/amm.js +129 -179
  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 -6
  98. package/lib/orders.js +10 -9
  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 +105 -109
  107. package/lib/types.js +13 -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 +10 -3
  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 +205 -346
  128. package/src/clearingHouse.ts +918 -961
  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 +33 -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 +507 -0
  146. package/src/factory/oracleClient.ts +7 -4
  147. package/src/idl/clearing_house.json +2213 -2951
  148. package/src/index.ts +12 -4
  149. package/src/math/amm.ts +229 -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 +175 -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 +35 -20
  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 +108 -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 {
@@ -53,6 +59,12 @@ export function isVariant(object: unknown, type: string) {
53
59
  return object.hasOwnProperty(type);
54
60
  }
55
61
 
62
+ export function isOneOfVariant(object: unknown, types: string[]) {
63
+ return types.reduce((result, type) => {
64
+ return result || object.hasOwnProperty(type);
65
+ }, false);
66
+ }
67
+
56
68
  export enum TradeSide {
57
69
  None = 0,
58
70
  Buy = 1,
@@ -69,58 +81,21 @@ export type CandleResolution =
69
81
  | 'W'
70
82
  | 'M';
71
83
 
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
84
  export type DepositRecord = {
110
85
  ts: BN;
111
- recordId: BN;
112
86
  userAuthority: PublicKey;
113
87
  user: PublicKey;
114
88
  direction: {
115
89
  deposit?: any;
116
90
  withdraw?: any;
117
91
  };
118
- collateralBefore: BN;
119
- cumulativeDepositsBefore: BN;
92
+ bankIndex: BN;
120
93
  amount: BN;
94
+ from?: PublicKey;
95
+ to?: PublicKey;
121
96
  };
122
97
 
123
- export type ExtendedCurveRecord = {
98
+ export type CurveRecord = {
124
99
  ts: BN;
125
100
  recordId: BN;
126
101
  marketIndex: BN;
@@ -140,28 +115,6 @@ export type ExtendedCurveRecord = {
140
115
  tradeId: BN;
141
116
  };
142
117
 
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
118
  export type FundingRateRecord = {
166
119
  ts: BN;
167
120
  recordId: BN;
@@ -175,7 +128,6 @@ export type FundingRateRecord = {
175
128
 
176
129
  export type FundingPaymentRecord = {
177
130
  ts: BN;
178
- recordId: BN;
179
131
  userAuthority: PublicKey;
180
132
  user: PublicKey;
181
133
  marketIndex: BN;
@@ -207,18 +159,21 @@ export type LiquidationRecord = {
207
159
 
208
160
  export type OrderRecord = {
209
161
  ts: BN;
210
- recordId: BN;
211
- order: Order;
212
- user: PublicKey;
213
- authority: PublicKey;
162
+ taker: PublicKey;
163
+ maker: PublicKey;
164
+ takerOrder: Order;
165
+ makerOrder: Order;
214
166
  action: OrderAction;
215
167
  filler: PublicKey;
168
+ fillRecordId: BN;
169
+ marketIndex: BN;
216
170
  baseAssetAmountFilled: BN;
217
171
  quoteAssetAmountFilled: BN;
218
- fee: BN;
172
+ makerRebate: BN;
173
+ takerFee: BN;
219
174
  fillerReward: BN;
220
- tradeRecordId: BN;
221
175
  quoteAssetAmountSurplus: BN;
176
+ oraclePrice: BN;
222
177
  };
223
178
 
224
179
  export type StateAccount = {
@@ -226,23 +181,12 @@ export type StateAccount = {
226
181
  fundingPaused: boolean;
227
182
  exchangePaused: boolean;
228
183
  adminControlsPrices: boolean;
229
- collateralMint: PublicKey;
230
- collateralVault: PublicKey;
231
- collateralVaultAuthority: PublicKey;
232
- collateralVaultNonce: number;
233
184
  insuranceVault: PublicKey;
234
185
  insuranceVaultAuthority: PublicKey;
235
186
  insuranceVaultNonce: number;
236
187
  marginRatioInitial: BN;
237
188
  marginRatioMaintenance: BN;
238
189
  marginRatioPartial: BN;
239
- markets: PublicKey;
240
- curveHistory: PublicKey;
241
- depositHistory: PublicKey;
242
- fundingRateHistory: PublicKey;
243
- fundingPaymentHistory: PublicKey;
244
- tradeHistory: PublicKey;
245
- liquidationHistory: PublicKey;
246
190
  partialLiquidationClosePercentageNumerator: BN;
247
191
  partialLiquidationClosePercentageDenominator: BN;
248
192
  partialLiquidationPenaltyPercentageNumerator: BN;
@@ -258,37 +202,52 @@ export type StateAccount = {
258
202
  discountMint: PublicKey;
259
203
  oracleGuardRails: OracleGuardRails;
260
204
  maxDeposit: BN;
261
- orderState: PublicKey;
262
- extendedCurveHistory: PublicKey;
263
- };
264
-
265
- export type OrderStateAccount = {
266
- orderHistory: PublicKey;
267
- orderFillerRewardStructure: OrderFillerRewardStructure;
205
+ numberOfMarkets: BN;
206
+ numberOfBanks: BN;
268
207
  minOrderQuoteAssetAmount: BN;
269
208
  };
270
209
 
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 = {
210
+ export type MarketAccount = {
211
+ initialized: boolean;
212
+ marketIndex: BN;
213
+ pubkey: PublicKey;
283
214
  amm: AMM;
284
215
  baseAssetAmount: BN;
285
216
  baseAssetAmountLong: BN;
286
217
  baseAssetAmountShort: BN;
287
- initialized: boolean;
288
218
  openInterest: BN;
289
219
  marginRatioInitial: number;
290
220
  marginRatioMaintenance: number;
291
221
  marginRatioPartial: number;
222
+ nextFillRecordId: BN;
223
+ pnlPool: PoolBalance;
224
+ };
225
+
226
+ export type BankAccount = {
227
+ bankIndex: BN;
228
+ pubkey: PublicKey;
229
+ mint: PublicKey;
230
+ vault: PublicKey;
231
+ vaultAuthority: PublicKey;
232
+ vaultAuthorityNonce: number;
233
+ decimals: number;
234
+ optimalUtilization: BN;
235
+ optimalBorrowRate: BN;
236
+ maxBorrowRate: BN;
237
+ cumulativeDepositInterest: BN;
238
+ cumulativeBorrowInterest: BN;
239
+ depositBalance: BN;
240
+ borrowBalance: BN;
241
+ lastUpdated: BN;
242
+ oracle: PublicKey;
243
+ initialAssetWeight: BN;
244
+ maintenanceAssetWeight: BN;
245
+ initialLiabilityWeight: BN;
246
+ maintenanceLiabilityWeight: BN;
247
+ };
248
+
249
+ export type PoolBalance = {
250
+ balance: BN;
292
251
  };
293
252
 
294
253
  export type AMM = {
@@ -314,9 +273,23 @@ export type AMM = {
314
273
  totalFeeWithdrawn: BN;
315
274
  totalFee: BN;
316
275
  minimumQuoteAssetTradeSize: BN;
317
- minimumBaseAssetTradeSize: BN;
276
+ baseAssetAmountStepSize: BN;
318
277
  lastOraclePrice: BN;
319
278
  baseSpread: number;
279
+ curveUpdateIntensity: number;
280
+ netBaseAssetAmount: BN;
281
+ quoteAssetAmountLong: BN;
282
+ quoteAssetAmountShort: BN;
283
+ terminalQuoteAssetReserve: BN;
284
+ feePool: PoolBalance;
285
+ totalExchangeFee: BN;
286
+ totalMmFee: BN;
287
+ netRevenueSinceLastFunding: BN;
288
+ lastUpdateSlot: BN;
289
+ lastBidPriceTwap: BN;
290
+ lastAskPriceTwap: BN;
291
+ longSpread: BN;
292
+ shortSpread: BN;
320
293
  };
321
294
 
322
295
  // # User Account Types
@@ -325,45 +298,46 @@ export type UserPosition = {
325
298
  lastCumulativeFundingRate: BN;
326
299
  marketIndex: BN;
327
300
  quoteAssetAmount: BN;
301
+ quoteEntryAmount: BN;
328
302
  openOrders: BN;
329
- };
330
-
331
- export type UserPositionsAccount = {
332
- positions: UserPosition[];
333
- user: PublicKey;
303
+ unsettledPnl: BN;
304
+ openBids: BN;
305
+ openAsks: BN;
334
306
  };
335
307
 
336
308
  export type UserAccount = {
337
309
  authority: PublicKey;
310
+ name: number[];
311
+ userId: number;
312
+ bankBalances: UserBankBalance[];
338
313
  collateral: BN;
339
314
  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;
315
+ fees: {
316
+ totalFeePaid: BN;
317
+ totalFeeRebate: BN;
318
+ totalTokenDiscount: BN;
319
+ totalReferralReward: BN;
320
+ totalRefereeDiscount: BN;
321
+ };
322
+ positions: UserPosition[];
323
+ orders: Order[];
351
324
  };
352
325
 
353
- export type UserOrdersAccount = {
354
- orders: Order[];
355
- user: PublicKey;
326
+ export type UserBankBalance = {
327
+ bankIndex: BN;
328
+ balanceType: BankBalanceType;
329
+ balance: BN;
356
330
  };
357
331
 
358
332
  export type Order = {
359
333
  status: OrderStatus;
360
334
  orderType: OrderType;
361
335
  ts: BN;
336
+ slot: BN;
362
337
  orderId: BN;
363
338
  userOrderId: number;
364
339
  marketIndex: BN;
365
340
  price: BN;
366
- userBaseAssetAmount: BN;
367
341
  baseAssetAmount: BN;
368
342
  baseAssetAmountFilled: BN;
369
343
  quoteAssetAmount: BN;
@@ -374,10 +348,14 @@ export type Order = {
374
348
  triggerPrice: BN;
375
349
  triggerCondition: OrderTriggerCondition;
376
350
  discountTier: OrderDiscountTier;
351
+ existingPositionDirection: PositionDirection,
377
352
  referrer: PublicKey;
378
353
  postOnly: boolean;
379
354
  immediateOrCancel: boolean;
380
355
  oraclePriceOffset: BN;
356
+ auctionDuration: number;
357
+ auctionStartPrice: BN;
358
+ auctionEndPrice: BN;
381
359
  };
382
360
 
383
361
  export type OrderParams = {
@@ -403,6 +381,11 @@ export type OrderParams = {
403
381
  };
404
382
  };
405
383
 
384
+ export type MakerInfo = {
385
+ maker: PublicKey;
386
+ order: Order;
387
+ };
388
+
406
389
  // # Misc Types
407
390
  export interface IWallet {
408
391
  signTransaction(tx: Transaction): Promise<Transaction>;
@@ -441,6 +424,9 @@ export type FeeStructure = {
441
424
  refereeDiscountNumerator: BN;
442
425
  refereeDiscountDenominator: BN;
443
426
  };
427
+ makerRebateNumerator: BN;
428
+ makerRebateDenominator: BN;
429
+ fillerRewardStructure: OrderFillerRewardStructure;
444
430
  };
445
431
 
446
432
  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
+ });
package/tsconfig.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "target": "es6",
5
- "esModuleInterop": true,
6
- "declaration": true,
7
- "outDir": "./lib",
8
- "resolveJsonModule": true,
9
- "skipLibCheck": true,
10
- },
11
- "include": ["src"],
12
- "exclude": ["node_modules"]
13
- }
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "ES2019",
5
+ "esModuleInterop": true,
6
+ "declaration": true,
7
+ "outDir": "./lib",
8
+ "resolveJsonModule": true,
9
+ "skipLibCheck": true
10
+ },
11
+ "include": ["src"],
12
+ "exclude": ["node_modules", "tests"]
13
+ }