@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26

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 (184) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
  3. package/lib/accounts/types.d.ts +8 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
  6. package/lib/addresses/pda.d.ts +7 -6
  7. package/lib/addresses/pda.js +31 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +83 -42
  10. package/lib/clearingHouse.d.ts +69 -42
  11. package/lib/clearingHouse.js +753 -277
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +16 -16
  14. package/lib/clearingHouseUser.js +139 -119
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +20 -20
  17. package/lib/constants/numericConstants.d.ts +12 -12
  18. package/lib/constants/numericConstants.js +13 -13
  19. package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
  20. package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
  21. package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
  22. package/lib/constants/{banks.js → spotMarkets.js} +16 -16
  23. package/lib/events/types.d.ts +2 -1
  24. package/lib/events/types.js +1 -0
  25. package/lib/examples/makeTradeExample.js +7 -7
  26. package/lib/idl/clearing_house.json +1008 -279
  27. package/lib/index.d.ts +5 -3
  28. package/lib/index.js +5 -3
  29. package/lib/math/amm.d.ts +2 -2
  30. package/lib/math/amm.js +1 -1
  31. package/lib/math/funding.d.ts +6 -6
  32. package/lib/math/funding.js +2 -1
  33. package/lib/math/margin.d.ts +4 -4
  34. package/lib/math/margin.js +18 -11
  35. package/lib/math/market.d.ts +10 -9
  36. package/lib/math/market.js +29 -6
  37. package/lib/math/oracles.d.ts +2 -1
  38. package/lib/math/oracles.js +11 -1
  39. package/lib/math/orders.d.ts +5 -5
  40. package/lib/math/position.d.ts +13 -13
  41. package/lib/math/position.js +19 -19
  42. package/lib/math/spotBalance.d.ts +19 -0
  43. package/lib/math/spotBalance.js +176 -0
  44. package/lib/math/spotMarket.d.ts +4 -0
  45. package/lib/math/spotMarket.js +8 -0
  46. package/lib/math/spotPosition.d.ts +2 -0
  47. package/lib/math/spotPosition.js +8 -0
  48. package/lib/math/state.js +2 -2
  49. package/lib/math/trade.d.ts +4 -4
  50. package/lib/orderParams.d.ts +4 -4
  51. package/lib/orderParams.js +12 -4
  52. package/lib/serum/serumSubscriber.d.ts +23 -0
  53. package/lib/serum/serumSubscriber.js +41 -0
  54. package/lib/serum/types.d.ts +11 -0
  55. package/lib/serum/types.js +2 -0
  56. package/lib/tx/retryTxSender.d.ts +1 -1
  57. package/lib/tx/retryTxSender.js +4 -2
  58. package/lib/tx/types.d.ts +1 -1
  59. package/lib/types.d.ts +123 -33
  60. package/lib/types.js +31 -9
  61. package/my-script/.env +7 -0
  62. package/my-script/getUserStats.ts +106 -0
  63. package/my-script/multiConnections.ts +119 -0
  64. package/my-script/test-regex.ts +11 -0
  65. package/my-script/utils.ts +52 -0
  66. package/package.json +1 -1
  67. package/src/accounts/bulkAccountLoader.js +249 -0
  68. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  69. package/src/accounts/bulkUserSubscription.js +75 -0
  70. package/src/accounts/fetch.js +92 -0
  71. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  72. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  73. package/src/accounts/pollingOracleSubscriber.js +156 -0
  74. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  75. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  76. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  77. package/src/accounts/types.js +28 -0
  78. package/src/accounts/types.ts +11 -9
  79. package/src/accounts/utils.js +7 -0
  80. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  82. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  83. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  84. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  85. package/src/addresses/pda.js +186 -0
  86. package/src/addresses/pda.ts +49 -44
  87. package/src/admin.js +1284 -0
  88. package/src/admin.ts +140 -47
  89. package/src/assert/assert.js +1 -1
  90. package/src/clearingHouse.js +3433 -0
  91. package/src/clearingHouse.ts +1083 -378
  92. package/src/clearingHouseConfig.js +2 -0
  93. package/src/clearingHouseConfig.ts +2 -2
  94. package/src/clearingHouseUser.js +874 -0
  95. package/src/clearingHouseUser.ts +232 -168
  96. package/src/clearingHouseUserConfig.js +2 -0
  97. package/src/clearingHouseUserStats.js +115 -0
  98. package/src/clearingHouseUserStatsConfig.js +2 -0
  99. package/src/config.js +80 -0
  100. package/src/config.ts +29 -29
  101. package/src/constants/numericConstants.js +18 -11
  102. package/src/constants/numericConstants.ts +17 -15
  103. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  104. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  105. package/src/constants/spotMarkets.js +51 -0
  106. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  107. package/src/events/eventList.js +66 -23
  108. package/src/events/eventSubscriber.js +202 -0
  109. package/src/events/fetchLogs.js +117 -0
  110. package/src/events/pollingLogProvider.js +113 -0
  111. package/src/events/sort.js +41 -0
  112. package/src/events/txEventCache.js +22 -19
  113. package/src/events/types.js +25 -0
  114. package/src/events/types.ts +3 -0
  115. package/src/events/webSocketLogProvider.js +76 -0
  116. package/src/examples/makeTradeExample.ts +10 -8
  117. package/src/factory/bigNum.js +183 -180
  118. package/src/factory/oracleClient.js +9 -9
  119. package/src/idl/clearing_house.json +1008 -279
  120. package/src/index.js +75 -0
  121. package/src/index.ts +5 -3
  122. package/src/math/amm.js +422 -0
  123. package/src/math/amm.ts +6 -3
  124. package/src/math/auction.js +10 -10
  125. package/src/math/conversion.js +4 -3
  126. package/src/math/funding.js +223 -175
  127. package/src/math/funding.ts +7 -7
  128. package/src/math/insurance.js +27 -0
  129. package/src/math/margin.js +77 -0
  130. package/src/math/margin.ts +34 -23
  131. package/src/math/market.js +105 -0
  132. package/src/math/market.ts +71 -19
  133. package/src/math/oracles.js +40 -10
  134. package/src/math/oracles.ts +18 -1
  135. package/src/math/orders.js +153 -0
  136. package/src/math/orders.ts +5 -5
  137. package/src/math/position.js +172 -0
  138. package/src/math/position.ts +31 -31
  139. package/src/math/repeg.js +40 -40
  140. package/src/math/spotBalance.js +176 -0
  141. package/src/math/spotBalance.ts +290 -0
  142. package/src/math/spotMarket.js +8 -0
  143. package/src/math/spotMarket.ts +9 -0
  144. package/src/math/spotPosition.js +8 -0
  145. package/src/math/spotPosition.ts +6 -0
  146. package/src/math/state.ts +2 -2
  147. package/src/math/trade.js +81 -74
  148. package/src/math/trade.ts +4 -4
  149. package/src/math/utils.js +8 -7
  150. package/src/oracles/oracleClientCache.js +10 -9
  151. package/src/oracles/pythClient.js +52 -17
  152. package/src/oracles/quoteAssetOracleClient.js +44 -13
  153. package/src/oracles/switchboardClient.js +69 -37
  154. package/src/oracles/types.js +1 -1
  155. package/src/orderParams.js +14 -6
  156. package/src/orderParams.ts +16 -8
  157. package/src/serum/serumSubscriber.js +102 -0
  158. package/src/serum/serumSubscriber.ts +80 -0
  159. package/src/serum/types.js +2 -0
  160. package/src/serum/types.ts +13 -0
  161. package/src/slot/SlotSubscriber.js +67 -20
  162. package/src/token/index.js +4 -4
  163. package/src/tokenFaucet.js +288 -154
  164. package/src/tx/retryTxSender.js +280 -0
  165. package/src/tx/retryTxSender.ts +5 -2
  166. package/src/tx/types.js +1 -1
  167. package/src/tx/types.ts +2 -1
  168. package/src/tx/utils.js +7 -6
  169. package/src/types.js +216 -0
  170. package/src/types.ts +110 -33
  171. package/src/userName.js +5 -5
  172. package/src/util/computeUnits.js +46 -11
  173. package/src/util/promiseTimeout.js +5 -5
  174. package/src/util/tps.js +46 -12
  175. package/src/wallet.js +55 -18
  176. package/lib/math/bankBalance.d.ts +0 -15
  177. package/lib/math/bankBalance.js +0 -150
  178. package/src/addresses/marketAddresses.js +0 -26
  179. package/src/constants/banks.js +0 -42
  180. package/src/examples/makeTradeExample.js +0 -80
  181. package/src/math/bankBalance.ts +0 -258
  182. package/src/math/state.js +0 -15
  183. package/src/math/utils.js.map +0 -1
  184. package/src/util/getTokenAddress.js +0 -9
package/src/types.ts CHANGED
@@ -2,12 +2,24 @@ import { PublicKey, Transaction } from '@solana/web3.js';
2
2
  import { BN, ZERO } from '.';
3
3
 
4
4
  // # Utility Types / Enums / Constants
5
+
6
+ export class MarketStatus {
7
+ static readonly INITIALIZED = { initialized: {} };
8
+ static readonly REDUCEONLY = { reduceonly: {} };
9
+ static readonly SETTLEMENT = { settlement: {} };
10
+ }
11
+
12
+ export class ContractType {
13
+ static readonly PERPETUAL = { perpetual: {} };
14
+ static readonly FUTURE = { future: {} };
15
+ }
16
+
5
17
  export class SwapDirection {
6
18
  static readonly ADD = { add: {} };
7
19
  static readonly REMOVE = { remove: {} };
8
20
  }
9
21
 
10
- export class BankBalanceType {
22
+ export class SpotBalanceType {
11
23
  static readonly DEPOSIT = { deposit: {} };
12
24
  static readonly BORROW = { borrow: {} };
13
25
  }
@@ -35,6 +47,11 @@ export class OrderType {
35
47
  static readonly MARKET = { market: {} };
36
48
  }
37
49
 
50
+ export class MarketType {
51
+ static readonly SPOT = { spot: {} };
52
+ static readonly PERP = { perp: {} };
53
+ }
54
+
38
55
  export class OrderStatus {
39
56
  static readonly INIT = { init: {} };
40
57
  static readonly OPEN = { open: {} };
@@ -77,6 +94,15 @@ export class OrderTriggerCondition {
77
94
  static readonly BELOW = { below: {} };
78
95
  }
79
96
 
97
+ export class SpotFulfillmentType {
98
+ static readonly SERUM_v3 = { serumV3: {} };
99
+ }
100
+
101
+ export class SpotFulfillmentStatus {
102
+ static readonly ENABLED = { enabled: {} };
103
+ static readonly DISABLED = { disabled: {} };
104
+ }
105
+
80
106
  export function isVariant(object: unknown, type: string) {
81
107
  return object.hasOwnProperty(type);
82
108
  }
@@ -120,7 +146,7 @@ export type DepositRecord = {
120
146
  deposit?: any;
121
147
  withdraw?: any;
122
148
  };
123
- bankIndex: BN;
149
+ marketIndex: BN;
124
150
  amount: BN;
125
151
  oraclePrice: BN;
126
152
  referrer: PublicKey;
@@ -148,6 +174,19 @@ export type CurveRecord = {
148
174
  tradeId: BN;
149
175
  };
150
176
 
177
+ export declare type InsuranceFundRecord = {
178
+ ts: BN;
179
+ bankIndex: BN;
180
+ marketIndex: BN;
181
+ userIfFactor: BN;
182
+ totalIfFactor: BN;
183
+ vaultAmountBefore: BN;
184
+ insuranceVaultAmountBefore: BN;
185
+ amount: BN;
186
+ totalIfSharesBefore: BN;
187
+ totalIfSharesAfter: BN;
188
+ };
189
+
151
190
  export type LPRecord = {
152
191
  ts: BN;
153
192
  user: PublicKey;
@@ -243,28 +282,28 @@ export type LiquidatePerpRecord = {
243
282
  };
244
283
 
245
284
  export type LiquidateBorrowRecord = {
246
- assetBankIndex: BN;
285
+ assetMarketIndex: BN;
247
286
  assetPrice: BN;
248
287
  assetTransfer: BN;
249
- liabilityBankIndex: BN;
288
+ liabilityMarketIndex: BN;
250
289
  liabilityPrice: BN;
251
290
  liabilityTransfer: BN;
252
291
  };
253
292
 
254
293
  export type LiquidateBorrowForPerpPnlRecord = {
255
- marketIndex: BN;
294
+ perpMarketIndex: BN;
256
295
  marketOraclePrice: BN;
257
296
  pnlTransfer: BN;
258
- liabilityBankIndex: BN;
297
+ liabilityMarketIndex: BN;
259
298
  liabilityPrice: BN;
260
299
  liabilityTransfer: BN;
261
300
  };
262
301
 
263
302
  export type LiquidatePerpPnlForDepositRecord = {
264
- marketIndex: BN;
303
+ perpMarketIndex: BN;
265
304
  marketOraclePrice: BN;
266
305
  pnlTransfer: BN;
267
- assetBankIndex: BN;
306
+ assetMarketIndex: BN;
268
307
  assetPrice: BN;
269
308
  assetTransfer: BN;
270
309
  };
@@ -276,7 +315,7 @@ export type PerpBankruptcyRecord = {
276
315
  };
277
316
 
278
317
  export type BorrowBankruptcyRecord = {
279
- bankIndex: BN;
318
+ marketIndex: BN;
280
319
  borrowAmount: BN;
281
320
  cumulativeDepositInterestDelta: BN;
282
321
  };
@@ -288,7 +327,7 @@ export type SettlePnlRecord = {
288
327
  pnl: BN;
289
328
  baseAssetAmount: BN;
290
329
  quoteAssetAmountAfter: BN;
291
- quoteEntryamount: BN;
330
+ quoteEntryAmount: BN;
292
331
  settlePrice: BN;
293
332
  };
294
333
 
@@ -318,12 +357,14 @@ export type OrderActionRecord = {
318
357
  quoteAssetAmountSurplus: BN | null;
319
358
  taker: PublicKey | null;
320
359
  takerOrderId: BN | null;
360
+ takerOrderDirection: PositionDirection | null;
321
361
  takerOrderBaseAssetAmount: BN | null;
322
362
  takerOrderBaseAssetAmountFilled: BN | null;
323
363
  takerOrderQuoteAssetAmountFilled: BN | null;
324
364
  takerOrderFee: BN | null;
325
365
  maker: PublicKey | null;
326
366
  makerOrderId: BN | null;
367
+ makerOrderDirection: PositionDirection | null;
327
368
  makerOrderBaseAssetAmount: BN | null;
328
369
  makerOrderBaseAssetAmountFilled: BN | null;
329
370
  makerOrderQuoteAssetAmountFilled: BN | null;
@@ -348,7 +389,8 @@ export type StateAccount = {
348
389
  fullLiquidationPenaltyPercentageDenominator: BN;
349
390
  partialLiquidationLiquidatorShareDenominator: BN;
350
391
  fullLiquidationLiquidatorShareDenominator: BN;
351
- feeStructure: FeeStructure;
392
+ perpFeeStructure: FeeStructure;
393
+ spotFeeStructure: FeeStructure;
352
394
  totalFee: BN;
353
395
  totalFeeWithdrawn: BN;
354
396
  whitelistMint: PublicKey;
@@ -356,16 +398,20 @@ export type StateAccount = {
356
398
  oracleGuardRails: OracleGuardRails;
357
399
  maxDeposit: BN;
358
400
  numberOfMarkets: BN;
359
- numberOfBanks: BN;
401
+ numberOfSpotMarkets: BN;
360
402
  minOrderQuoteAssetAmount: BN;
361
403
  signer: PublicKey;
362
404
  signerNonce: number;
363
405
  maxAuctionDuration: number;
364
406
  minAuctionDuration: number;
407
+ liquidationMarginBufferRatio: number;
365
408
  };
366
409
 
367
- export type MarketAccount = {
368
- initialized: boolean;
410
+ export type PerpMarketAccount = {
411
+ status: MarketStatus;
412
+ contractType: ContractType;
413
+ expiryTs: BN;
414
+ settlementPrice: BN;
369
415
  marketIndex: BN;
370
416
  pubkey: PublicKey;
371
417
  amm: AMM;
@@ -380,12 +426,18 @@ export type MarketAccount = {
380
426
  liquidationFee: BN;
381
427
  imfFactor: BN;
382
428
  unrealizedImfFactor: BN;
429
+ unrealizedMaxImbalance: BN;
383
430
  unrealizedInitialAssetWeight: number;
384
431
  unrealizedMaintenanceAssetWeight: number;
432
+ revenueWithdrawSinceLastSettle: BN;
433
+ maxRevenueWithdrawPerPeriod: BN;
434
+ lastRevenueWithdrawTs: BN;
435
+ quoteSettledInsurance: BN;
436
+ quoteMaxInsurance: BN;
385
437
  };
386
438
 
387
- export type BankAccount = {
388
- bankIndex: BN;
439
+ export type SpotMarketAccount = {
440
+ marketIndex: BN;
389
441
  pubkey: PublicKey;
390
442
  mint: PublicKey;
391
443
  vault: PublicKey;
@@ -423,6 +475,13 @@ export type BankAccount = {
423
475
  depositTokenTwap: BN;
424
476
  borrowTokenTwap: BN;
425
477
  utilizationTwap: BN;
478
+
479
+ orderStepSize: BN;
480
+ nextFillRecordId: BN;
481
+ spotFeePool: {
482
+ balance: BN;
483
+ };
484
+ totalSpotFee: BN;
426
485
  };
427
486
 
428
487
  export type PoolBalance = {
@@ -477,21 +536,24 @@ export type AMM = {
477
536
  totalMmFee: BN;
478
537
  netRevenueSinceLastFunding: BN;
479
538
  lastUpdateSlot: BN;
539
+ lastOracleValid: boolean;
480
540
  lastBidPriceTwap: BN;
481
541
  lastAskPriceTwap: BN;
482
542
  longSpread: BN;
483
543
  shortSpread: BN;
484
544
  maxSpread: number;
485
- marketPosition: UserPosition;
486
- marketPositionPerLp: UserPosition;
545
+ marketPosition: PerpPosition;
546
+ marketPositionPerLp: PerpPosition;
487
547
  ammJitIntensity: number;
488
548
  maxBaseAssetReserve: BN;
489
549
  minBaseAssetReserve: BN;
550
+ cumulativeSocialLoss: BN;
490
551
  };
491
552
 
492
553
  // # User Account Types
493
- export type UserPosition = {
554
+ export type PerpPosition = {
494
555
  baseAssetAmount: BN;
556
+ remainderBaseAssetAmount: BN;
495
557
  lastCumulativeFundingRate: BN;
496
558
  marketIndex: BN;
497
559
  quoteAssetAmount: BN;
@@ -531,8 +593,8 @@ export type UserAccount = {
531
593
  authority: PublicKey;
532
594
  name: number[];
533
595
  userId: number;
534
- bankBalances: UserBankBalance[];
535
- positions: UserPosition[];
596
+ spotPositions: SpotPosition[];
597
+ perpPositions: PerpPosition[];
536
598
  orders: Order[];
537
599
  beingLiquidated: boolean;
538
600
  bankrupt: boolean;
@@ -540,15 +602,19 @@ export type UserAccount = {
540
602
  nextOrderId: BN;
541
603
  };
542
604
 
543
- export type UserBankBalance = {
544
- bankIndex: BN;
545
- balanceType: BankBalanceType;
605
+ export type SpotPosition = {
606
+ marketIndex: BN;
607
+ balanceType: SpotBalanceType;
546
608
  balance: BN;
609
+ openOrders: number;
610
+ openBids: BN;
611
+ openAsks: BN;
547
612
  };
548
613
 
549
614
  export type Order = {
550
615
  status: OrderStatus;
551
616
  orderType: OrderType;
617
+ marketType: MarketType;
552
618
  ts: BN;
553
619
  slot: BN;
554
620
  orderId: BN;
@@ -576,6 +642,7 @@ export type Order = {
576
642
 
577
643
  export type OrderParams = {
578
644
  orderType: OrderType;
645
+ marketType: MarketType;
579
646
  userOrderId: number;
580
647
  direction: PositionDirection;
581
648
  baseAssetAmount: BN;
@@ -590,10 +657,6 @@ export type OrderParams = {
590
657
  oraclePriceOffset: BN;
591
658
  padding0: boolean;
592
659
  padding1: BN;
593
- optionalAccounts: {
594
- discountToken: boolean;
595
- referrer: boolean;
596
- };
597
660
  };
598
661
 
599
662
  export type NecessaryOrderParams = {
@@ -609,6 +672,7 @@ export type OptionalOrderParams = {
609
672
 
610
673
  export const DefaultOrderParams = {
611
674
  orderType: OrderType.MARKET,
675
+ marketType: MarketType.PERP,
612
676
  userOrderId: 0,
613
677
  direction: PositionDirection.LONG,
614
678
  baseAssetAmount: ZERO,
@@ -623,10 +687,6 @@ export const DefaultOrderParams = {
623
687
  oraclePriceOffset: ZERO,
624
688
  padding0: ZERO,
625
689
  padding1: ZERO,
626
- optionalAccounts: {
627
- discountToken: false,
628
- referrer: false,
629
- },
630
690
  };
631
691
 
632
692
  export type MakerInfo = {
@@ -713,7 +773,7 @@ export type OrderFillerRewardStructure = {
713
773
  export type MarginCategory = 'Initial' | 'Maintenance';
714
774
 
715
775
  export type InsuranceFundStake = {
716
- bankIndex: BN;
776
+ marketIndex: BN;
717
777
  authority: PublicKey;
718
778
 
719
779
  ifShares: BN;
@@ -723,3 +783,20 @@ export type InsuranceFundStake = {
723
783
  lastWithdrawRequestValue: BN;
724
784
  lastWithdrawRequestTs: BN;
725
785
  };
786
+
787
+ export type SerumV3FulfillmentConfigAccount = {
788
+ fulfillmentType: SpotFulfillmentType;
789
+ status: SpotFulfillmentStatus;
790
+ pubkey: PublicKey;
791
+ marketIndex: BN;
792
+ serumProgramId: PublicKey;
793
+ serumMarket: PublicKey;
794
+ serumRequestQueue: PublicKey;
795
+ serumEventQueue: PublicKey;
796
+ serumBids: PublicKey;
797
+ serumAsks: PublicKey;
798
+ serumBaseVault: PublicKey;
799
+ serumQuoteVault: PublicKey;
800
+ serumOpenOrders: PublicKey;
801
+ serumSignerNonce: BN;
802
+ };
package/src/userName.js CHANGED
@@ -1,20 +1,20 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.__esModule = true;
3
3
  exports.decodeName = exports.encodeName = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
4
4
  exports.MAX_NAME_LENGTH = 32;
5
5
  exports.DEFAULT_USER_NAME = 'Main Account';
6
6
  function encodeName(name) {
7
7
  if (name.length > exports.MAX_NAME_LENGTH) {
8
- throw Error(`User name (${name}) longer than 32 characters`);
8
+ throw Error("User name (".concat(name, ") longer than 32 characters"));
9
9
  }
10
- const buffer = Buffer.alloc(32);
10
+ var buffer = Buffer.alloc(32);
11
11
  buffer.fill(name);
12
12
  buffer.fill(' ', name.length);
13
- return Array(...buffer);
13
+ return Array.apply(void 0, buffer);
14
14
  }
15
15
  exports.encodeName = encodeName;
16
16
  function decodeName(bytes) {
17
- const buffer = Buffer.from(bytes);
17
+ var buffer = Buffer.from(bytes);
18
18
  return buffer.toString('utf8').trim();
19
19
  }
20
20
  exports.decodeName = decodeName;
@@ -8,20 +8,55 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
12
39
  exports.findComputeUnitConsumption = void 0;
13
- function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
14
- return __awaiter(this, void 0, void 0, function* () {
15
- const tx = yield connection.getTransaction(txSignature, { commitment });
16
- const computeUnits = [];
17
- const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
18
- tx.meta.logMessages.forEach((logMessage) => {
19
- const match = logMessage.match(regex);
20
- if (match && match[1]) {
21
- computeUnits.push(match[1]);
40
+ function findComputeUnitConsumption(programId, connection, txSignature, commitment) {
41
+ if (commitment === void 0) { commitment = 'confirmed'; }
42
+ return __awaiter(this, void 0, void 0, function () {
43
+ var tx, computeUnits, regex;
44
+ return __generator(this, function (_a) {
45
+ switch (_a.label) {
46
+ case 0: return [4 /*yield*/, connection.getTransaction(txSignature, { commitment: commitment })];
47
+ case 1:
48
+ tx = _a.sent();
49
+ computeUnits = [];
50
+ regex = new RegExp("Program ".concat(programId.toString(), " consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units"));
51
+ tx.meta.logMessages.forEach(function (logMessage) {
52
+ var match = logMessage.match(regex);
53
+ if (match && match[1]) {
54
+ computeUnits.push(match[1]);
55
+ }
56
+ });
57
+ return [2 /*return*/, computeUnits];
22
58
  }
23
59
  });
24
- return computeUnits;
25
60
  });
26
61
  }
27
62
  exports.findComputeUnitConsumption = findComputeUnitConsumption;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.__esModule = true;
3
3
  exports.promiseTimeout = void 0;
4
4
  function promiseTimeout(promise, timeoutMs) {
5
- let timeoutId;
6
- const timeoutPromise = new Promise((resolve) => {
7
- timeoutId = setTimeout(() => resolve(null), timeoutMs);
5
+ var timeoutId;
6
+ var timeoutPromise = new Promise(function (resolve) {
7
+ timeoutId = setTimeout(function () { return resolve(null); }, timeoutMs);
8
8
  });
9
- return Promise.race([promise, timeoutPromise]).then((result) => {
9
+ return Promise.race([promise, timeoutPromise]).then(function (result) {
10
10
  clearTimeout(timeoutId);
11
11
  return result;
12
12
  });
package/src/util/tps.js CHANGED
@@ -8,20 +8,54 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
12
39
  exports.estimateTps = void 0;
13
40
  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));
41
+ return __awaiter(this, void 0, void 0, function () {
42
+ var signatures, numberOfSignatures;
43
+ return __generator(this, function (_a) {
44
+ switch (_a.label) {
45
+ case 0: return [4 /*yield*/, connection.getSignaturesForAddress(programId, undefined, 'finalized')];
46
+ case 1:
47
+ signatures = _a.sent();
48
+ if (failed) {
49
+ signatures = signatures.filter(function (signature) { return signature.err; });
50
+ }
51
+ numberOfSignatures = signatures.length;
52
+ if (numberOfSignatures === 0) {
53
+ return [2 /*return*/, 0];
54
+ }
55
+ return [2 /*return*/, (numberOfSignatures /
56
+ (signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime))];
57
+ }
58
+ });
25
59
  });
26
60
  }
27
61
  exports.estimateTps = estimateTps;
package/src/wallet.js CHANGED
@@ -8,28 +8,65 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
12
39
  exports.Wallet = void 0;
13
- class Wallet {
14
- constructor(payer) {
40
+ var Wallet = /** @class */ (function () {
41
+ function Wallet(payer) {
15
42
  this.payer = payer;
16
43
  }
17
- signTransaction(tx) {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- tx.partialSign(this.payer);
20
- return tx;
44
+ Wallet.prototype.signTransaction = function (tx) {
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ return __generator(this, function (_a) {
47
+ tx.partialSign(this.payer);
48
+ return [2 /*return*/, tx];
49
+ });
21
50
  });
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;
51
+ };
52
+ Wallet.prototype.signAllTransactions = function (txs) {
53
+ return __awaiter(this, void 0, void 0, function () {
54
+ var _this = this;
55
+ return __generator(this, function (_a) {
56
+ return [2 /*return*/, txs.map(function (t) {
57
+ t.partialSign(_this.payer);
58
+ return t;
59
+ })];
28
60
  });
29
61
  });
30
- }
31
- get publicKey() {
32
- return this.payer.publicKey;
33
- }
34
- }
62
+ };
63
+ Object.defineProperty(Wallet.prototype, "publicKey", {
64
+ get: function () {
65
+ return this.payer.publicKey;
66
+ },
67
+ enumerable: false,
68
+ configurable: true
69
+ });
70
+ return Wallet;
71
+ }());
35
72
  exports.Wallet = Wallet;
@@ -1,15 +0,0 @@
1
- /// <reference types="bn.js" />
2
- import { BankAccount, BankBalanceType, MarginCategory } from '../types';
3
- import { BN } from '@project-serum/anchor';
4
- export declare function getBalance(tokenAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
5
- export declare function getTokenAmount(balanceAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
6
- export declare function calculateAssetWeight(balanceAmount: BN, bank: BankAccount, marginCategory: MarginCategory): BN;
7
- export declare function calculateLiabilityWeight(balanceAmount: BN, bank: BankAccount, marginCategory: MarginCategory): BN;
8
- export declare function calculateInterestAccumulated(bank: BankAccount, now: BN): {
9
- borrowInterest: BN;
10
- depositInterest: BN;
11
- };
12
- export declare function calculateWithdrawLimit(bank: BankAccount, now: BN): {
13
- borrowLimit: BN;
14
- withdrawLimit: BN;
15
- };