@drift-labs/sdk 2.23.0 → 2.25.0-beta.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 (124) hide show
  1. package/README.md +1 -1
  2. package/lib/accounts/fetch.d.ts +1 -1
  3. package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +1 -1
  4. package/lib/accounts/pollingOracleAccountSubscriber.d.ts +1 -1
  5. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +1 -1
  6. package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
  7. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +1 -1
  8. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
  9. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +1 -1
  10. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +1 -1
  11. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +1 -1
  12. package/lib/addresses/pda.d.ts +1 -2
  13. package/lib/addresses/pda.js +1 -1
  14. package/lib/adminClient.d.ts +3 -3
  15. package/lib/adminClient.js +16 -4
  16. package/lib/constants/numericConstants.d.ts +59 -61
  17. package/lib/constants/perpMarkets.js +21 -1
  18. package/lib/constants/spotMarkets.d.ts +0 -1
  19. package/lib/dlob/DLOB.d.ts +0 -1
  20. package/lib/dlob/DLOBNode.d.ts +0 -1
  21. package/lib/dlob/DLOBOrders.d.ts +1 -1
  22. package/lib/dlob/DLOBOrders.js +1 -1
  23. package/lib/dlob/NodeList.d.ts +0 -1
  24. package/lib/driftClient.d.ts +58 -24
  25. package/lib/driftClient.js +152 -136
  26. package/lib/events/eventSubscriber.d.ts +1 -1
  27. package/lib/events/eventSubscriber.js +3 -2
  28. package/lib/events/fetchLogs.d.ts +1 -1
  29. package/lib/events/fetchLogs.js +3 -2
  30. package/lib/examples/loadDlob.js +1 -1
  31. package/lib/examples/makeTradeExample.js +1 -1
  32. package/lib/factory/bigNum.d.ts +8 -9
  33. package/lib/factory/bigNum.js +1 -1
  34. package/lib/factory/oracleClient.js +1 -1
  35. package/lib/idl/drift.json +216 -14
  36. package/lib/index.d.ts +1 -1
  37. package/lib/index.js +1 -1
  38. package/lib/math/amm.d.ts +2 -3
  39. package/lib/math/amm.js +1 -1
  40. package/lib/math/auction.d.ts +0 -1
  41. package/lib/math/conversion.d.ts +1 -2
  42. package/lib/math/funding.d.ts +1 -2
  43. package/lib/math/funding.js +1 -1
  44. package/lib/math/insurance.d.ts +1 -1
  45. package/lib/math/insurance.js +17 -1
  46. package/lib/math/margin.d.ts +1 -2
  47. package/lib/math/margin.js +1 -1
  48. package/lib/math/market.d.ts +1 -2
  49. package/lib/math/market.js +1 -1
  50. package/lib/math/oracles.d.ts +0 -1
  51. package/lib/math/oracles.js +2 -3
  52. package/lib/math/orders.d.ts +1 -2
  53. package/lib/math/orders.js +1 -1
  54. package/lib/math/position.d.ts +0 -1
  55. package/lib/math/repeg.d.ts +1 -2
  56. package/lib/math/repeg.js +1 -1
  57. package/lib/math/spotBalance.d.ts +1 -2
  58. package/lib/math/spotBalance.js +1 -1
  59. package/lib/math/spotMarket.d.ts +1 -2
  60. package/lib/math/spotMarket.js +1 -1
  61. package/lib/math/spotPosition.d.ts +1 -2
  62. package/lib/math/trade.d.ts +1 -2
  63. package/lib/math/trade.js +1 -1
  64. package/lib/math/utils.d.ts +0 -1
  65. package/lib/oracles/pythClient.d.ts +2 -3
  66. package/lib/oracles/pythClient.js +1 -1
  67. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  68. package/lib/oracles/types.d.ts +1 -2
  69. package/lib/orderParams.d.ts +1 -2
  70. package/lib/serum/serumSubscriber.d.ts +1 -2
  71. package/lib/serum/serumSubscriber.js +1 -1
  72. package/lib/tokenFaucet.d.ts +2 -3
  73. package/lib/tokenFaucet.js +2 -2
  74. package/lib/tx/retryTxSender.d.ts +1 -1
  75. package/lib/tx/types.d.ts +1 -1
  76. package/lib/types.d.ts +9 -3
  77. package/lib/types.js +2 -1
  78. package/lib/user.d.ts +0 -1
  79. package/package.json +2 -2
  80. package/src/accounts/fetch.ts +1 -1
  81. package/src/accounts/pollingDriftClientAccountSubscriber.ts +1 -1
  82. package/src/accounts/pollingOracleAccountSubscriber.ts +1 -1
  83. package/src/accounts/pollingTokenAccountSubscriber.ts +1 -1
  84. package/src/accounts/pollingUserAccountSubscriber.ts +1 -1
  85. package/src/accounts/pollingUserStatsAccountSubscriber.ts +1 -1
  86. package/src/accounts/webSocketAccountSubscriber.ts +1 -1
  87. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +1 -1
  88. package/src/accounts/webSocketUserAccountSubscriber.ts +1 -1
  89. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +1 -1
  90. package/src/addresses/pda.ts +2 -2
  91. package/src/adminClient.ts +30 -2
  92. package/src/constants/perpMarkets.ts +21 -1
  93. package/src/dlob/DLOBOrders.ts +2 -2
  94. package/src/driftClient.ts +266 -163
  95. package/src/events/eventSubscriber.ts +7 -3
  96. package/src/events/fetchLogs.ts +7 -3
  97. package/src/examples/loadDlob.ts +1 -1
  98. package/src/examples/makeTradeExample.ts +1 -1
  99. package/src/factory/bigNum.ts +1 -1
  100. package/src/factory/oracleClient.ts +1 -1
  101. package/src/idl/drift.json +216 -14
  102. package/src/index.ts +1 -1
  103. package/src/math/amm.ts +1 -1
  104. package/src/math/funding.ts +1 -1
  105. package/src/math/insurance.ts +29 -0
  106. package/src/math/margin.ts +1 -1
  107. package/src/math/market.ts +1 -1
  108. package/src/math/oracles.ts +2 -3
  109. package/src/math/orders.ts +1 -1
  110. package/src/math/repeg.ts +1 -1
  111. package/src/math/spotBalance.ts +1 -1
  112. package/src/math/spotMarket.ts +1 -1
  113. package/src/math/spotPosition.ts +1 -1
  114. package/src/math/trade.ts +1 -1
  115. package/src/oracles/pythClient.ts +1 -1
  116. package/src/oracles/quoteAssetOracleClient.ts +1 -1
  117. package/src/oracles/types.ts +1 -1
  118. package/src/orderParams.ts +1 -1
  119. package/src/serum/serumSubscriber.ts +1 -1
  120. package/src/tokenFaucet.ts +2 -2
  121. package/src/tx/retryTxSender.ts +1 -1
  122. package/src/tx/types.ts +1 -1
  123. package/src/types.ts +8 -1
  124. package/src/userMap/userStatsMap.ts +1 -1
@@ -27,11 +27,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.DriftClient = void 0;
30
- const anchor_1 = require("@project-serum/anchor");
30
+ const anchor_1 = require("@coral-xyz/anchor");
31
31
  const bs58_1 = __importDefault(require("bs58"));
32
32
  const spl_token_1 = require("@solana/spl-token");
33
33
  const types_1 = require("./types");
34
- const anchor = __importStar(require("@project-serum/anchor"));
34
+ const anchor = __importStar(require("@coral-xyz/anchor"));
35
35
  const drift_json_1 = __importDefault(require("./idl/drift.json"));
36
36
  const web3_js_1 = require("@solana/web3.js");
37
37
  const pda_1 = require("./addresses/pda");
@@ -458,6 +458,18 @@ class DriftClient {
458
458
  ]);
459
459
  return programAccounts.map((programAccount) => programAccount.account);
460
460
  }
461
+ async getUserAccountsAndAddressesForAuthority(authority) {
462
+ const programAccounts = await this.program.account.user.all([
463
+ {
464
+ memcmp: {
465
+ offset: 8,
466
+ /** data to match, as base-58 encoded string and limited to less than 129 bytes */
467
+ bytes: bs58_1.default.encode(authority.toBuffer()),
468
+ },
469
+ },
470
+ ]);
471
+ return programAccounts.map((programAccount) => programAccount);
472
+ }
461
473
  async getUserAccountsForAuthority(authority) {
462
474
  const programAccounts = await this.program.account.user.all([
463
475
  {
@@ -1988,153 +2000,107 @@ class DriftClient {
1988
2000
  /**
1989
2001
  * Modifies an open order (spot or perp) by closing it and replacing it with a new order.
1990
2002
  * @param orderId: The open order to modify
1991
- * @param newBaseAmount: The new base amount for the order. One of [newBaseAmount|newLimitPrice|newOraclePriceOffset] must be provided.
1992
- * @param newLimitPice: The new limit price for the order. One of [newBaseAmount|newLimitPrice|newOraclePriceOffset] must be provided.
1993
- * @param newOraclePriceOffset: The new oracle price offset for the order. One of [newBaseAmount|newLimitPrice|newOraclePriceOffset] must be provided.
1994
- * @param newOrderType: Optional - New order type for the order.
2003
+ * @param newDirection: The new direction for the order
2004
+ * @param newBaseAmount: The new base amount for the order
2005
+ * @param newLimitPice: The new limit price for the order
2006
+ * @param newOraclePriceOffset: The new oracle price offset for the order
1995
2007
  * @param newTriggerPrice: Optional - Thew new trigger price for the order.
1996
- * @param isSpot: Optional - Set to true if the order is a spot order
1997
- * @param auctionDuration: Only required if order type changed to market from something else
1998
- * @param auctionStartPrice: Only required if order type changed to market from something else
1999
- * @param auctionEndPrice: Only required if order type changed to market from something else
2008
+ * @param auctionDuration:
2009
+ * @param auctionStartPrice:
2010
+ * @param auctionEndPrice:
2011
+ * @param reduceOnly:
2012
+ * @param postOnly:
2013
+ * @param immediateOrCancel:
2014
+ * @param maxTs:
2000
2015
  * @returns
2001
2016
  */
2002
- async modifyOrder({ orderId, newBaseAmount, newLimitPrice, newOraclePriceOffset, newOrderType, newTriggerPrice, newTriggerCondition, isSpot, auctionDuration, auctionStartPrice, auctionEndPrice, }) {
2003
- if (!newBaseAmount && !newLimitPrice && !newOraclePriceOffset) {
2004
- throw new Error(`Must provide newBaseAmount or newLimitPrice or newOraclePriceOffset to modify order`);
2005
- }
2006
- const openOrder = this.getUser().getOrder(orderId);
2007
- if (!openOrder) {
2008
- throw new Error(`No open order with id ${orderId.toString()}`);
2009
- }
2010
- const cancelOrderIx = await this.getCancelOrderIx(orderId);
2011
- const orderTypeHasTrigger = newOrderType
2012
- ? (0, types_1.isOneOfVariant)(newOrderType, ['triggerlimit', 'triggerMarket'])
2013
- : (0, types_1.isOneOfVariant)(openOrder.orderType, ['triggerLimit', 'triggerMarket']);
2014
- const orderTypeHasLimitPrice = newOrderType
2015
- ? (0, types_1.isOneOfVariant)(newOrderType, ['triggerLimit', 'limit'])
2016
- : (0, types_1.isOneOfVariant)(openOrder.orderType, ['triggerLimit', 'limit']);
2017
- const newOrderParams = {
2018
- orderType: newOrderType || openOrder.orderType,
2019
- marketType: openOrder.marketType,
2020
- direction: openOrder.direction,
2021
- baseAssetAmount: newBaseAmount || openOrder.baseAssetAmount,
2022
- price: orderTypeHasLimitPrice
2023
- ? newLimitPrice || openOrder.price
2024
- : undefined,
2025
- marketIndex: openOrder.marketIndex,
2026
- reduceOnly: openOrder.reduceOnly,
2027
- postOnly: openOrder.postOnly
2028
- ? types_1.PostOnlyParams.MUST_POST_ONLY
2029
- : types_1.PostOnlyParams.NONE,
2030
- immediateOrCancel: openOrder.immediateOrCancel,
2031
- triggerPrice: orderTypeHasTrigger
2032
- ? newTriggerPrice || openOrder.triggerPrice
2033
- : undefined,
2034
- triggerCondition: orderTypeHasTrigger
2035
- ? newTriggerCondition || openOrder.triggerCondition
2036
- : undefined,
2037
- oraclePriceOffset: newOraclePriceOffset || openOrder.oraclePriceOffset,
2038
- auctionDuration: auctionDuration !== null && auctionDuration !== void 0 ? auctionDuration : openOrder.auctionDuration,
2039
- maxTs: openOrder.maxTs,
2040
- auctionStartPrice: auctionStartPrice || openOrder.auctionStartPrice,
2041
- auctionEndPrice: auctionEndPrice || openOrder.auctionEndPrice,
2042
- userOrderId: openOrder.userOrderId,
2017
+ async modifyOrder({ orderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, immediateOrCancel, maxTs, txParams, }) {
2018
+ const orderParams = {
2019
+ baseAssetAmount: newBaseAmount || null,
2020
+ direction: newDirection || null,
2021
+ price: newLimitPrice || null,
2022
+ oraclePriceOffset: newOraclePriceOffset || null,
2023
+ triggerPrice: newTriggerPrice || null,
2024
+ triggerCondition: newTriggerCondition || null,
2025
+ auctionDuration: auctionDuration || null,
2026
+ auctionStartPrice: auctionStartPrice || null,
2027
+ auctionEndPrice: auctionEndPrice || null,
2028
+ reduceOnly: reduceOnly || null,
2029
+ postOnly: postOnly || null,
2030
+ immediateOrCancel: immediateOrCancel || null,
2031
+ maxTs: maxTs || null,
2043
2032
  };
2044
- const placeOrderIx = isSpot
2045
- ? await this.getPlaceSpotOrderIx(newOrderParams)
2046
- : await this.getPlacePerpOrderIx(newOrderParams);
2047
- const tx = new web3_js_1.Transaction();
2048
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
2049
- units: 1000000,
2050
- }));
2051
- tx.add(cancelOrderIx);
2052
- tx.add(placeOrderIx);
2053
- const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
2054
- if (isSpot) {
2055
- this.spotMarketLastSlotCache.set(newOrderParams.marketIndex, slot);
2056
- this.spotMarketLastSlotCache.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, slot);
2057
- }
2058
- else {
2059
- this.perpMarketLastSlotCache.set(newOrderParams.marketIndex, slot);
2060
- }
2033
+ const { txSig } = await this.sendTransaction((0, utils_1.wrapInTx)(await this.getModifyOrderIx(orderId, orderParams), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice), [], this.opts);
2061
2034
  return txSig;
2062
2035
  }
2036
+ async getModifyOrderIx(orderId, orderParams) {
2037
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
2038
+ const remainingAccounts = this.getRemainingAccounts({
2039
+ userAccounts: [this.getUserAccount()],
2040
+ useMarketLastSlotCache: true,
2041
+ });
2042
+ return await this.program.instruction.modifyOrder(orderId, orderParams, {
2043
+ accounts: {
2044
+ state: await this.getStatePublicKey(),
2045
+ user: userAccountPublicKey,
2046
+ userStats: this.getUserStatsAccountPublicKey(),
2047
+ authority: this.wallet.publicKey,
2048
+ },
2049
+ remainingAccounts,
2050
+ });
2051
+ }
2063
2052
  /**
2064
2053
  * Modifies an open order by closing it and replacing it with a new order.
2065
2054
  * @param userOrderId: The open order to modify
2066
- * @param newBaseAmount: The new base amount for the order. One of [newBaseAmount|newLimitPrice|newOraclePriceOffset] must be provided.
2067
- * @param newLimitPice: The new limit price for the order. One of [newBaseAmount|newLimitPrice|newOraclePriceOffset] must be provided.
2068
- * @param newOraclePriceOffset: The new oracle price offset for the order. One of [newBaseAmount|newLimitPrice|newOraclePriceOffset] must be provided.
2069
- * @param newOrderType: Optional - New order type for the order.
2055
+ * @param newDirection: The new direction for the order
2056
+ * @param newBaseAmount: The new base amount for the order
2057
+ * @param newLimitPice: The new limit price for the order
2058
+ * @param newOraclePriceOffset: The new oracle price offset for the order
2070
2059
  * @param newTriggerPrice: Optional - Thew new trigger price for the order.
2071
- * @param isSpot: Set to true if the order is a spot order
2072
2060
  * @param auctionDuration: Only required if order type changed to market from something else
2073
2061
  * @param auctionStartPrice: Only required if order type changed to market from something else
2074
2062
  * @param auctionEndPrice: Only required if order type changed to market from something else
2063
+ * @param reduceOnly:
2064
+ * @param postOnly:
2065
+ * @param immediateOrCancel:
2066
+ * @param maxTs:
2075
2067
  * @returns
2076
2068
  */
2077
- async modifyOrderByUserOrderId({ userOrderId, newBaseAmount, newLimitPrice, newOraclePriceOffset, newOrderType, newTriggerPrice, newTriggerCondition, isSpot, auctionDuration, auctionStartPrice, auctionEndPrice, }) {
2078
- if (!newBaseAmount && !newLimitPrice && !newOraclePriceOffset) {
2079
- throw new Error(`Must provide newBaseAmount or newLimitPrice or newOraclePriceOffset to modify order`);
2080
- }
2081
- const openOrder = this.getUser().getOrderByUserOrderId(userOrderId);
2082
- if (!openOrder) {
2083
- throw new Error(`No open order with user order id ${userOrderId.toString()}`);
2084
- }
2085
- const cancelOrderIx = await this.getCancelOrderByUserIdIx(userOrderId);
2086
- const orderTypeHasTrigger = newOrderType
2087
- ? (0, types_1.isOneOfVariant)(newOrderType, ['triggerlimit', 'triggerMarket'])
2088
- : (0, types_1.isOneOfVariant)(openOrder.orderType, ['triggerLimit', 'triggerMarket']);
2089
- const orderTypeHasLimitPrice = newOrderType
2090
- ? (0, types_1.isOneOfVariant)(newOrderType, ['triggerLimit', 'limit'])
2091
- : (0, types_1.isOneOfVariant)(openOrder.orderType, ['triggerLimit', 'limit']);
2092
- const newOrderParams = {
2093
- orderType: newOrderType || openOrder.orderType,
2094
- marketType: openOrder.marketType,
2095
- direction: openOrder.direction,
2096
- baseAssetAmount: newBaseAmount || openOrder.baseAssetAmount,
2097
- price: orderTypeHasLimitPrice
2098
- ? newLimitPrice || openOrder.price
2099
- : undefined,
2100
- marketIndex: openOrder.marketIndex,
2101
- reduceOnly: openOrder.reduceOnly,
2102
- postOnly: openOrder.postOnly
2103
- ? types_1.PostOnlyParams.MUST_POST_ONLY
2104
- : types_1.PostOnlyParams.NONE,
2105
- immediateOrCancel: openOrder.immediateOrCancel,
2106
- triggerPrice: orderTypeHasTrigger
2107
- ? newTriggerPrice || openOrder.triggerPrice
2108
- : undefined,
2109
- triggerCondition: orderTypeHasTrigger
2110
- ? newTriggerCondition || openOrder.triggerCondition
2111
- : undefined,
2112
- oraclePriceOffset: newOraclePriceOffset || openOrder.oraclePriceOffset,
2113
- auctionDuration: auctionDuration || openOrder.auctionDuration,
2114
- maxTs: openOrder.maxTs,
2115
- auctionStartPrice: auctionStartPrice || openOrder.auctionStartPrice,
2116
- auctionEndPrice: auctionEndPrice || openOrder.auctionEndPrice,
2117
- userOrderId: openOrder.userOrderId,
2069
+ async modifyOrderByUserOrderId({ userOrderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, immediateOrCancel, maxTs, txParams, }) {
2070
+ const orderParams = {
2071
+ baseAssetAmount: newBaseAmount || null,
2072
+ direction: newDirection || null,
2073
+ price: newLimitPrice || null,
2074
+ oraclePriceOffset: newOraclePriceOffset || null,
2075
+ triggerPrice: newTriggerPrice || null,
2076
+ triggerCondition: newTriggerCondition || null,
2077
+ auctionDuration: auctionDuration || null,
2078
+ auctionStartPrice: auctionStartPrice || null,
2079
+ auctionEndPrice: auctionEndPrice || null,
2080
+ reduceOnly: reduceOnly || null,
2081
+ postOnly: postOnly || null,
2082
+ immediateOrCancel: immediateOrCancel || null,
2083
+ maxTs: maxTs || null,
2118
2084
  };
2119
- const placeOrderIx = isSpot
2120
- ? await this.getPlaceSpotOrderIx(newOrderParams)
2121
- : await this.getPlacePerpOrderIx(newOrderParams);
2122
- const tx = new web3_js_1.Transaction();
2123
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
2124
- units: 1000000,
2125
- }));
2126
- tx.add(cancelOrderIx);
2127
- tx.add(placeOrderIx);
2128
- const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
2129
- if (isSpot) {
2130
- this.spotMarketLastSlotCache.set(newOrderParams.marketIndex, slot);
2131
- this.spotMarketLastSlotCache.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, slot);
2132
- }
2133
- else {
2134
- this.perpMarketLastSlotCache.set(newOrderParams.marketIndex, slot);
2135
- }
2085
+ const { txSig } = await this.sendTransaction((0, utils_1.wrapInTx)(await this.getModifyOrderByUserIdIx(userOrderId, orderParams), txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice), [], this.opts);
2136
2086
  return txSig;
2137
2087
  }
2088
+ async getModifyOrderByUserIdIx(userOrderId, orderParams) {
2089
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
2090
+ const remainingAccounts = this.getRemainingAccounts({
2091
+ userAccounts: [this.getUserAccount()],
2092
+ useMarketLastSlotCache: true,
2093
+ });
2094
+ return await this.program.instruction.modifyOrderByUserId(userOrderId, orderParams, {
2095
+ accounts: {
2096
+ state: await this.getStatePublicKey(),
2097
+ user: userAccountPublicKey,
2098
+ userStats: this.getUserStatsAccountPublicKey(),
2099
+ authority: this.wallet.publicKey,
2100
+ },
2101
+ remainingAccounts,
2102
+ });
2103
+ }
2138
2104
  async settlePNLs(users, marketIndexes) {
2139
2105
  const ixs = [];
2140
2106
  for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
@@ -2406,7 +2372,7 @@ class DriftClient {
2406
2372
  },
2407
2373
  });
2408
2374
  }
2409
- async addInsuranceFundStake(marketIndex, amount, collateralAccountPublicKey) {
2375
+ async getAddInsuranceFundStakeIx(marketIndex, amount, collateralAccountPublicKey) {
2410
2376
  const spotMarket = this.getSpotMarketAccount(marketIndex);
2411
2377
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
2412
2378
  const remainingAccounts = this.getRemainingAccounts({
@@ -2414,7 +2380,7 @@ class DriftClient {
2414
2380
  useMarketLastSlotCache: true,
2415
2381
  writableSpotMarketIndexes: [marketIndex],
2416
2382
  });
2417
- const tx = await this.program.transaction.addInsuranceFundStake(marketIndex, amount, {
2383
+ const ix = this.program.instruction.addInsuranceFundStake(marketIndex, amount, {
2418
2384
  accounts: {
2419
2385
  state: await this.getStatePublicKey(),
2420
2386
  spotMarket: spotMarket.pubkey,
@@ -2429,7 +2395,39 @@ class DriftClient {
2429
2395
  },
2430
2396
  remainingAccounts,
2431
2397
  });
2432
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
2398
+ return ix;
2399
+ }
2400
+ /**
2401
+ * Add to an insurance fund stake and optionally initialize the account
2402
+ */
2403
+ async addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, }) {
2404
+ const tx = new web3_js_1.Transaction();
2405
+ const additionalSigners = [];
2406
+ const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
2407
+ const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
2408
+ const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
2409
+ let tokenAccount;
2410
+ if (createWSOLTokenAccount) {
2411
+ const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, true);
2412
+ tokenAccount = pubkey;
2413
+ ixs.forEach((ix) => {
2414
+ tx.add(ix);
2415
+ });
2416
+ signers.forEach((signer) => additionalSigners.push(signer));
2417
+ }
2418
+ else {
2419
+ tokenAccount = collateralAccountPublicKey;
2420
+ }
2421
+ if (initializeStakeAccount) {
2422
+ const initializeIx = await this.getInitializeInsuranceFundStakeIx(marketIndex);
2423
+ tx.add(initializeIx);
2424
+ }
2425
+ const addFundsIx = await this.getAddInsuranceFundStakeIx(marketIndex, amount, tokenAccount);
2426
+ tx.add(addFundsIx);
2427
+ if (createWSOLTokenAccount) {
2428
+ tx.add(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
2429
+ }
2430
+ const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
2433
2431
  return txSig;
2434
2432
  }
2435
2433
  async requestRemoveInsuranceFundStake(marketIndex, amount) {
@@ -2477,14 +2475,31 @@ class DriftClient {
2477
2475
  return txSig;
2478
2476
  }
2479
2477
  async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey) {
2478
+ const tx = new web3_js_1.Transaction();
2480
2479
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
2481
2480
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
2481
+ const tokenAccount = collateralAccountPublicKey;
2482
+ // Todo wsol remove iF stake... how do we determine the amount?
2483
+ // const amount = // get balance here...?
2484
+ // const additionalSigners: Array<Signer> = [];
2485
+ // const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);
2486
+ // const createWSOLTokenAccount =
2487
+ // isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
2488
+ // if (createWSOLTokenAccount) {
2489
+ // const { ixs, signers, pubkey } =
2490
+ // await this.getWrappedSolAccountCreationIxs(amount, true);
2491
+ // tokenAccount = pubkey;
2492
+ // ixs.forEach((ix) => {
2493
+ // tx.add(ix);
2494
+ // });
2495
+ // signers.forEach((signer) => additionalSigners.push(signer));
2496
+ // }
2482
2497
  const remainingAccounts = this.getRemainingAccounts({
2483
2498
  userAccounts: [this.getUserAccount()],
2484
2499
  useMarketLastSlotCache: true,
2485
2500
  writableSpotMarketIndexes: [marketIndex],
2486
2501
  });
2487
- const tx = await this.program.transaction.removeInsuranceFundStake(marketIndex, {
2502
+ const removeStakeIx = await this.program.instruction.removeInsuranceFundStake(marketIndex, {
2488
2503
  accounts: {
2489
2504
  state: await this.getStatePublicKey(),
2490
2505
  spotMarket: spotMarketAccount.pubkey,
@@ -2493,11 +2508,12 @@ class DriftClient {
2493
2508
  authority: this.wallet.publicKey,
2494
2509
  insuranceFundVault: spotMarketAccount.insuranceFund.vault,
2495
2510
  driftSigner: this.getSignerPublicKey(),
2496
- userTokenAccount: collateralAccountPublicKey,
2511
+ userTokenAccount: tokenAccount,
2497
2512
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
2498
2513
  },
2499
2514
  remainingAccounts,
2500
2515
  });
2516
+ tx.add(removeStakeIx);
2501
2517
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
2502
2518
  return txSig;
2503
2519
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { Connection, TransactionSignature } from '@solana/web3.js';
3
- import { Program } from '@project-serum/anchor';
3
+ import { Program } from '@coral-xyz/anchor';
4
4
  import { EventSubscriptionOptions, EventType, WrappedEvents, EventMap, EventSubscriberEvents } from './types';
5
5
  import { EventList } from './eventList';
6
6
  import { EventEmitter } from 'events';
@@ -97,7 +97,8 @@ class EventSubscriber {
97
97
  parseEventsFromLogs(txSig, slot, logs) {
98
98
  const records = [];
99
99
  // @ts-ignore
100
- this.program._events._eventParser.parseLogs(logs, (event) => {
100
+ const eventGenerator = this.program._events._eventParser.parseLogs(logs, false);
101
+ for (const event of eventGenerator) {
101
102
  const expectRecordType = this.eventListMap.has(event.name);
102
103
  if (expectRecordType) {
103
104
  event.data.txSig = txSig;
@@ -105,7 +106,7 @@ class EventSubscriber {
105
106
  event.data.eventType = event.name;
106
107
  records.push(event.data);
107
108
  }
108
- });
109
+ }
109
110
  return records;
110
111
  }
111
112
  awaitTx(txSig) {
@@ -1,4 +1,4 @@
1
- import { Program } from '@project-serum/anchor';
1
+ import { Program } from '@coral-xyz/anchor';
2
2
  import { Connection, Finality, PublicKey, TransactionResponse, TransactionSignature } from '@solana/web3.js';
3
3
  import { WrappedEvents } from './types';
4
4
  type Log = {
@@ -61,12 +61,13 @@ class LogParser {
61
61
  parseEventsFromLogs(event) {
62
62
  const records = [];
63
63
  // @ts-ignore
64
- this.program._events._eventParser.parseLogs(event.logs, (eventLog) => {
64
+ const eventGenerator = this.program._events._eventParser.parseLogs(event.logs, false);
65
+ for (const eventLog of eventGenerator) {
65
66
  eventLog.data.txSig = event.txSig;
66
67
  eventLog.data.slot = event.slot;
67
68
  eventLog.data.eventType = eventLog.name;
68
69
  records.push(eventLog.data);
69
- });
70
+ }
70
71
  return records;
71
72
  }
72
73
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const anchor_1 = require("@project-serum/anchor");
3
+ const anchor_1 = require("@coral-xyz/anchor");
4
4
  const __1 = require("..");
5
5
  const web3_js_1 = require("@solana/web3.js");
6
6
  const __2 = require("..");
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getTokenAddress = void 0;
4
- const anchor_1 = require("@project-serum/anchor");
4
+ const anchor_1 = require("@coral-xyz/anchor");
5
5
  const __1 = require("..");
6
6
  const spl_token_1 = require("@solana/spl-token");
7
7
  const web3_js_1 = require("@solana/web3.js");
@@ -1,5 +1,4 @@
1
- /// <reference types="bn.js" />
2
- import { BN } from '@project-serum/anchor';
1
+ import { BN } from '@coral-xyz/anchor';
3
2
  export declare class BigNum {
4
3
  val: BN;
5
4
  precision: BN;
@@ -44,11 +43,11 @@ export declare class BigNum {
44
43
  gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
45
44
  lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
46
45
  eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
47
- eqZero(): boolean;
48
- gtZero(): boolean;
49
- ltZero(): boolean;
50
- gteZero(): boolean;
51
- lteZero(): boolean;
46
+ eqZero(): any;
47
+ gtZero(): any;
48
+ ltZero(): any;
49
+ gteZero(): any;
50
+ lteZero(): any;
52
51
  abs(): BigNum;
53
52
  neg(): BigNum;
54
53
  toString: (base?: number | 'hex', length?: number) => string;
@@ -88,8 +87,8 @@ export declare class BigNum {
88
87
  toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
89
88
  toMillified(precision?: number, rounded?: boolean): string;
90
89
  toJSON(): {
91
- val: string;
92
- precision: string;
90
+ val: any;
91
+ precision: any;
93
92
  };
94
93
  isNeg(): boolean;
95
94
  isPos(): boolean;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BigNum = void 0;
4
- const anchor_1 = require("@project-serum/anchor");
4
+ const anchor_1 = require("@coral-xyz/anchor");
5
5
  const assert_1 = require("../assert/assert");
6
6
  const numericConstants_1 = require("./../constants/numericConstants");
7
7
  class BigNum {
@@ -5,7 +5,7 @@ const types_1 = require("../types");
5
5
  const pythClient_1 = require("../oracles/pythClient");
6
6
  // import { SwitchboardClient } from '../oracles/switchboardClient';
7
7
  const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
8
- const anchor_1 = require("@project-serum/anchor");
8
+ const anchor_1 = require("@coral-xyz/anchor");
9
9
  function getOracleClient(oracleSource, connection) {
10
10
  if ((0, types_1.isVariant)(oracleSource, 'pyth')) {
11
11
  return new pythClient_1.PythClient(connection);