@drift-labs/common 1.0.46 → 1.0.47

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepSignAndSendSwiftOrder = exports.sendSwiftOrder = exports.signSwiftOrderMsg = exports.AuctionSlotExpiredError = exports.prepSwiftOrder = exports.MINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.USER_SIGNING_MESSAGE_BUFFER_SLOTS = void 0;
3
+ exports.prepSignAndSendSwiftOrder = exports.prepSwiftOrderMessage = exports.sendSwiftOrder = exports.signSwiftOrderMsg = exports.AuctionSlotExpiredError = exports.prepSwiftOrder = exports.MINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.USER_SIGNING_MESSAGE_BUFFER_SLOTS = void 0;
4
4
  const sdk_1 = require("@drift-labs/sdk");
5
5
  const sdk_2 = require("@drift-labs/sdk");
6
6
  const utils_1 = require("../../../../../../utils");
@@ -100,6 +100,7 @@ const prepSwiftOrder = ({ driftClient, takerUserAccount, currentSlot, isDelegate
100
100
  signedMsgOrderParamsMessage,
101
101
  slotForSignedMsg: auctionStartSlot,
102
102
  signedMsgOrderUuid,
103
+ resolvedUserSigningSlotBuffer: userSigningSlotBuffer,
103
104
  };
104
105
  };
105
106
  exports.prepSwiftOrder = prepSwiftOrder;
@@ -185,42 +186,76 @@ const sendSwiftOrder = ({ driftClient, marketId, hexEncodedSwiftOrderMessageStri
185
186
  };
186
187
  exports.sendSwiftOrder = sendSwiftOrder;
187
188
  /**
188
- * Handles the full flow of the swift order, from preparing to signing and sending to the Swift server.
189
- * Callbacks can be provided to handle the events of the Swift order.
190
- * Returns a promise that resolves when the Swift order has reached a terminal state (i.e. confirmed, expired, or errored).
189
+ * Computes the timing parameters for a SWIFT order:
190
+ * - slotsTillAuctionEnd: how many slots until the auction is considered ended
191
+ * - expirationTimeMs: how long (in ms) the user has to sign before the window expires
192
+ *
193
+ * For market orders, auction duration + signing buffer is used directly.
194
+ * For non-market orders, a minimum is enforced because limit auctions can have
195
+ * very small durations but the order is still valid after the auction ends.
191
196
  */
192
- const prepSignAndSendSwiftOrder = async ({ driftClient, subAccountId, userAccountPubKey, marketIndex, userSigningSlotBuffer, swiftOptions, orderParams, builderParams, confirmationMultiplier, }) => {
193
- var _a, _b, _c, _d, _e, _f;
197
+ const computeSwiftOrderTiming = (mainOrderParams, userSigningSlotBuffer) => {
198
+ const isMarketOrder = utils_1.ENUM_UTILS.match(mainOrderParams.orderType, sdk_1.OrderType.ORACLE) ||
199
+ utils_1.ENUM_UTILS.match(mainOrderParams.orderType, sdk_1.OrderType.MARKET);
200
+ const slotsTillAuctionEnd = mainOrderParams.auctionDuration
201
+ ? isMarketOrder
202
+ ? userSigningSlotBuffer + mainOrderParams.auctionDuration
203
+ : Math.max(exports.MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS, userSigningSlotBuffer + mainOrderParams.auctionDuration)
204
+ : exports.MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS;
205
+ const expirationTimeMs = Math.max(slotsTillAuctionEnd - exports.SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS, exports.MINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS) * sdk_1.SLOT_TIME_ESTIMATE_MS;
206
+ return { slotsTillAuctionEnd, expirationTimeMs };
207
+ };
208
+ /**
209
+ * Prepares a SWIFT order message without signing or sending it.
210
+ * Returns all data needed for the consumer to sign and send the order themselves.
211
+ *
212
+ * This is useful for server-side contexts (e.g., CentralServerDrift) where
213
+ * the server prepares the message but the client handles signing and sending.
214
+ */
215
+ const prepSwiftOrderMessage = async ({ driftClient, subAccountId, userAccountPubKey, marketIndex, userSigningSlotBuffer, isDelegate = false, orderParams, builderParams, }) => {
194
216
  const currentSlot = await driftClient.connection.getSlot('confirmed');
195
- const { hexEncodedSwiftOrderMessage, signedMsgOrderUuid, signedMsgOrderParamsMessage, } = (0, exports.prepSwiftOrder)({
217
+ const { hexEncodedSwiftOrderMessage, signedMsgOrderUuid, signedMsgOrderParamsMessage, slotForSignedMsg, resolvedUserSigningSlotBuffer, } = (0, exports.prepSwiftOrder)({
196
218
  driftClient,
197
219
  takerUserAccount: {
198
220
  pubKey: userAccountPubKey,
199
- subAccountId: subAccountId,
221
+ subAccountId,
200
222
  },
201
223
  currentSlot,
202
- isDelegate: swiftOptions.isDelegate || false,
224
+ isDelegate,
203
225
  orderParams,
204
226
  userSigningSlotBuffer,
205
227
  builderParams,
206
228
  });
229
+ const { slotsTillAuctionEnd, expirationTimeMs } = computeSwiftOrderTiming(orderParams.main, resolvedUserSigningSlotBuffer);
230
+ return {
231
+ hexEncodedSwiftOrderMessage,
232
+ signedMsgOrderParamsMessage,
233
+ slotForSignedMsg,
234
+ signedMsgOrderUuid,
235
+ marketIndex,
236
+ slotsTillAuctionEnd,
237
+ expirationTimeMs,
238
+ };
239
+ };
240
+ exports.prepSwiftOrderMessage = prepSwiftOrderMessage;
241
+ /**
242
+ * Handles the full flow of the swift order, from preparing to signing and sending to the Swift server.
243
+ * Callbacks can be provided to handle the events of the Swift order.
244
+ * Returns a promise that resolves when the Swift order has reached a terminal state (i.e. confirmed, expired, or errored).
245
+ */
246
+ const prepSignAndSendSwiftOrder = async ({ driftClient, subAccountId, userAccountPubKey, marketIndex, userSigningSlotBuffer, swiftOptions, orderParams, builderParams, confirmationMultiplier, }) => {
247
+ var _a, _b, _c, _d, _e, _f;
248
+ const { hexEncodedSwiftOrderMessage, signedMsgOrderUuid, signedMsgOrderParamsMessage, slotsTillAuctionEnd, expirationTimeMs, } = await (0, exports.prepSwiftOrderMessage)({
249
+ driftClient,
250
+ subAccountId,
251
+ userAccountPubKey,
252
+ marketIndex,
253
+ userSigningSlotBuffer,
254
+ isDelegate: swiftOptions.isDelegate || false,
255
+ orderParams,
256
+ builderParams,
257
+ });
207
258
  (_b = (_a = swiftOptions.callbacks) === null || _a === void 0 ? void 0 : _a.onOrderParamsMessagePrepped) === null || _b === void 0 ? void 0 : _b.call(_a, signedMsgOrderParamsMessage);
208
- // both market and non-market orders can have auction durations.
209
- //
210
- // for market orders, the auction duration + user signing slot buffer is a good gauge for slots until auction end
211
- //
212
- // for non-market orders, if they have an auction duration, we use the maximum of MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS
213
- // and the (auction duration + user signing slot buffer). This is because limit auctions can have very small auction durations,
214
- // but the limit order is still valid to be placed even after the auction ends, hence we enforce a minimum duration so that
215
- // short limit auction durations can still be placed
216
- const isMarketOrder = utils_1.ENUM_UTILS.match(orderParams.main.orderType, sdk_1.OrderType.ORACLE) ||
217
- utils_1.ENUM_UTILS.match(orderParams.main.orderType, sdk_1.OrderType.MARKET);
218
- const slotsTillAuctionEnd = orderParams.main.auctionDuration
219
- ? isMarketOrder
220
- ? userSigningSlotBuffer + orderParams.main.auctionDuration
221
- : Math.max(exports.MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS, userSigningSlotBuffer + orderParams.main.auctionDuration)
222
- : exports.MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS;
223
- const expirationTimeMs = Math.max(slotsTillAuctionEnd - exports.SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS, exports.MINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS) * sdk_1.SLOT_TIME_ESTIMATE_MS;
224
259
  // Ensure that the user signs the message before the expiration time
225
260
  const signedMessage = await (0, exports.signSwiftOrderMsg)({
226
261
  wallet: swiftOptions.wallet,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.ts"],"names":[],"mappings":";;;AAAA,yCAMyB;AACzB,yCAOyB;AACzB,mDAGiC;AACjC,uEAO+C;AAC/C,mDAAmD;AAGnD,uEAA0E;AAC1E,6CAA6C;AAE7C;;GAEG;AACU,QAAA,iCAAiC,GAAG,CAAC,CAAC;AAEnD;;GAEG;AACU,QAAA,+DAA+D,GAAG,EAAE,CAAC;AAElF;;GAEG;AACU,QAAA,2CAA2C,GAAG,CAAC,CAAC;AAChD,QAAA,mDAAmD,GAAG,CAAC,CAAC;AAuHrE;;;;;;;;;;;;;;;;GAgBG;AACI,MAAM,cAAc,GAAG,CAAC,EAC9B,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,WAAW,EACX,qBAAqB,EACrB,aAAa,GACS,EAUrB,EAAE;;IACH,MAAM,eAAe,GAAG,IAAA,oBAAc,EAAC;QACtC,GAAG,WAAW,CAAC,IAAI;QACnB,eAAe,EAAE,WAAW,CAAC,IAAI,CAAC,eAAe,IAAI,IAAI,EAAE,6EAA6E;KACxI,CAAC,CAAC;IAEH,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC5B,qBAAqB,GAAG,eAAe,CAAC,eAAe;YACtD,CAAC,CAAC,yCAAiC;YACnC,CAAC,CAAC,uEAA+D,CAAC;IACpE,CAAC;IAED,uEAAuE;IACvE,IAAI,CAAC,eAAe,CAAC,eAAe,EAAE,CAAC;QACtC,qBAAqB,GAAG,IAAI,CAAC,GAAG,CAC/B,qBAAqB,EACrB,uEAA+D,CAC/D,CAAC;IACH,CAAC;IAED,gFAAgF;IAChF,MAAM,gBAAgB,GAA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SignedMsgUserAccountPublicKey,\n\tOrderType,\n\tSignedMsgOrderParamsDelegateMessage,\n\tSignedMsgOrderParamsMessage,\n\tSLOT_TIME_ESTIMATE_MS,\n} from '@drift-labs/sdk';\nimport {\n\tBN,\n\tDriftClient,\n\tgenerateSignedMsgUuid,\n\tgetOrderParams,\n\tOptionalOrderParams,\n\tPublicKey,\n} from '@drift-labs/sdk';\nimport {\n\tENUM_UTILS,\n\tgetSwiftConfirmationTimeoutMs,\n} from '../../../../../../utils';\nimport {\n\tSwiftClient,\n\tSwiftOrderConfirmedEvent,\n\tSwiftOrderErroredEvent,\n\tSwiftOrderEvent,\n\tSwiftOrderEventWithParams,\n\tSwiftOrderSentEvent,\n} from '../../../../../../clients/swiftClient';\nimport { MarketId } from '../../../../../../types';\nimport { Observable, Subscription } from 'rxjs';\nimport { OptionalTriggerOrderParams } from '../types';\nimport { TRADING_UTILS } from '../../../../../../common-ui-utils/trading';\nimport { Connection } from '@solana/web3.js';\n\n/**\n * Buffer slots to account for signing of message by the user (default: 7 slots ~3 second, assumes user have to approves the signing in a UI wallet).\n */\nexport const USER_SIGNING_MESSAGE_BUFFER_SLOTS = 7;\n\n/**\n * Orders without auction require a higher buffer (kink of the SWIFT server handling non-auction orders)\n */\nexport const MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 35;\n\n/**\n * Buffer slots from the end of the auction to prevent the signing of the order message.\n */\nexport const SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 5;\nexport const MINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 5;\n\nexport interface SwiftOrderOptions {\n\twallet: {\n\t\tsignMessage: (message: Uint8Array) => Promise<Uint8Array>;\n\t\ttakerAuthority: PublicKey;\n\t\tsigningAuthority?: PublicKey;\n\t};\n\tswiftServerUrl: string;\n\t/**\n\t * Buffer slots to account for the user to sign the message. Affects the auction start slot.\n\t * If order is not an auction order, it is not encouraged to use this buffer.\n\t */\n\tuserSigningSlotBuffer?: number;\n\tisDelegate?: boolean;\n\t/**\n\t * Multiplier for the SWIFT confirmation timeout (after sending SWIFT order). Default is 1.\n\t */\n\tconfirmationMultiplier?: number;\n\tcallbacks?: {\n\t\tonOrderParamsMessagePrepped?: (\n\t\t\torderParamsMessage:\n\t\t\t\t| SignedMsgOrderParamsMessage\n\t\t\t\t| SignedMsgOrderParamsDelegateMessage\n\t\t) => void;\n\t\tonSigningExpiry?: (\n\t\t\torderParamsMessage:\n\t\t\t\t| SignedMsgOrderParamsMessage\n\t\t\t\t| SignedMsgOrderParamsDelegateMessage\n\t\t) => void;\n\t\tonSigningSuccess?: (\n\t\t\tsignedMessage: Uint8Array,\n\t\t\t// we add the following here, because the onSigningSuccess callback is called before the order is sent to the swift server\n\t\t\torderUuid: Uint8Array,\n\t\t\torderParamsMessage:\n\t\t\t\t| SignedMsgOrderParamsMessage\n\t\t\t\t| SignedMsgOrderParamsDelegateMessage\n\t\t) => void;\n\t\tonSent?: (\n\t\t\tswiftSentEvent: SwiftOrderEventWithParams<SwiftOrderSentEvent>\n\t\t) => void;\n\t\tonConfirmed?: (\n\t\t\tswiftConfirmedEvent: SwiftOrderEventWithParams<SwiftOrderConfirmedEvent>\n\t\t) => void;\n\t\tonExpired?: (\n\t\t\tswiftExpiredEvent: SwiftOrderEventWithParams<SwiftOrderErroredEvent>\n\t\t) => void;\n\t\tonErrored?: (\n\t\t\tswiftErroredEvent: SwiftOrderEventWithParams<SwiftOrderErroredEvent>\n\t\t) => void;\n\t};\n\t/**\n\t * Used for internal tracking of the source of the swift order.\n\t */\n\tsource?: string;\n}\n\nexport type SwiftOrderObservable = Observable<SwiftOrderEvent>;\n\ninterface PrepSwiftOrderParams {\n\t/** The Drift client instance */\n\tdriftClient: DriftClient;\n\t/** The taker user account information */\n\ttakerUserAccount: {\n\t\t/** Public key of the user account */\n\t\tpubKey: PublicKey;\n\t\t/** User account ID */\n\t\tsubAccountId: number;\n\t};\n\t/** Current blockchain slot number */\n\tcurrentSlot: number;\n\t/** Whether this is a delegate order */\n\tisDelegate: boolean;\n\t/** Order parameters including main order and optional stop loss/take profit */\n\torderParams: {\n\t\t/** Main order parameters */\n\t\tmain: OptionalOrderParams;\n\t\t/** Optional stop loss order parameters */\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t\t/** Optional take profit order parameters */\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\t/** Optional max leverage for the position */\n\t\tpositionMaxLeverage?: number;\n\t\t/** Optional isolated position deposit amount */\n\t\tisolatedPositionDeposit?: BN;\n\t};\n\t/**\n\t * Buffer slots to account for the user to sign the message. Affects the auction start slot.\n\t * If order is not an auction order, it is not encouraged to use this buffer.\n\t */\n\tuserSigningSlotBuffer?: number;\n\t/**\n\t * Optional builder code parameters for revenue sharing.\n\t * If provided, the builder will receive a portion of the trading fees.\n\t *\n\t * Prerequisites:\n\t * - User must have initialized a RevenueShareEscrow account\n\t * - Builder must be in the user's approved_builders list\n\t * - builderFeeTenthBps must not exceed the builder's max_fee_tenth_bps\n\t */\n\tbuilderParams?: {\n\t\t/**\n\t\t * Index of the builder in the user's approved_builders list.\n\t\t * This is the position (0-indexed) of the builder in the RevenueShareEscrow.approved_builders array.\n\t\t */\n\t\tbuilderIdx: number;\n\t\t/**\n\t\t * Fee to charge for this order, in tenths of basis points.\n\t\t * Must be <= the builder's max_fee_tenth_bps.\n\t\t *\n\t\t * Examples:\n\t\t * - 10 = 1 bps = 0.01%\n\t\t * - 50 = 5 bps = 0.05%\n\t\t * - 100 = 10 bps = 0.1%\n\t\t */\n\t\tbuilderFeeTenthBps: number;\n\t};\n}\n\n/**\n * Prepares a swift order by encoding the order parameters into a message format\n * suitable for signing and sending to the Swift server.\n *\n * @param driftClient - The Drift client instance\n * @param takerUserAccount - The taker user account information\n * @param currentSlot - Current blockchain slot number\n * @param isDelegate - Whether this is a delegate order\n * @param orderParams - Order parameters including main order and optional stop loss/take profit\n * @param userSigningSlotBuffer - Buffer slots to account for the user to sign the message. Affects the auction start slot. If order is not an auction order, it is not encouraged to use this buffer.\n *\n * @returns An object containing:\n * - `hexEncodedSwiftOrderMessage`: The encoded order message in both Uint8Array and string formats. The Uint8Array format is for a wallet to sign, while the string format is used to send to the SWIFT server.\n * - `signedMsgOrderParamsMessage`: The signed message order parameters\n * - `slotForSignedMsg`: The slot number for the signed message\n * - `signedMsgOrderUuid`: Unique identifier for the signed message order\n */\nexport const prepSwiftOrder = ({\n\tdriftClient,\n\ttakerUserAccount,\n\tcurrentSlot,\n\tisDelegate,\n\torderParams,\n\tuserSigningSlotBuffer,\n\tbuilderParams,\n}: PrepSwiftOrderParams): {\n\thexEncodedSwiftOrderMessage: {\n\t\tuInt8Array: Uint8Array;\n\t\tstring: string;\n\t};\n\tsignedMsgOrderParamsMessage:\n\t\t| SignedMsgOrderParamsMessage\n\t\t| SignedMsgOrderParamsDelegateMessage;\n\tslotForSignedMsg: BN;\n\tsignedMsgOrderUuid: Uint8Array;\n} => {\n\tconst mainOrderParams = getOrderParams({\n\t\t...orderParams.main,\n\t\tauctionDuration: orderParams.main.auctionDuration || null, // swift server expects auctionDuration to be null if not set, won't handle 0\n\t});\n\n\tif (!userSigningSlotBuffer) {\n\t\tuserSigningSlotBuffer = mainOrderParams.auctionDuration\n\t\t\t? USER_SIGNING_MESSAGE_BUFFER_SLOTS\n\t\t\t: MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS;\n\t}\n\n\t// if it is not an auction order, there should be a minimum buffer used\n\tif (!mainOrderParams.auctionDuration) {\n\t\tuserSigningSlotBuffer = Math.max(\n\t\t\tuserSigningSlotBuffer,\n\t\t\tMINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS\n\t\t);\n\t}\n\n\t// buffer for time the user takes to sign a message and send to the swift server\n\tconst auctionStartSlot = new BN(currentSlot + userSigningSlotBuffer);\n\n\tconst signedMsgOrderUuid = generateSignedMsgUuid();\n\n\tconsole.log(\n\t\t'DEBUG swift prep orderParams.isolatedPositionDeposit',\n\t\torderParams.isolatedPositionDeposit?.toString()\n\t);\n\n\tconst baseSignedMsgOrderParamsMessage = {\n\t\tsignedMsgOrderParams: mainOrderParams,\n\t\tuuid: signedMsgOrderUuid,\n\t\tslot: auctionStartSlot,\n\t\tstopLossOrderParams: orderParams.stopLoss\n\t\t\t? {\n\t\t\t\t\tbaseAssetAmount: orderParams.stopLoss.baseAssetAmount,\n\t\t\t\t\ttriggerPrice: orderParams.stopLoss.triggerPrice,\n\t\t\t }\n\t\t\t: null,\n\t\ttakeProfitOrderParams: orderParams.takeProfit\n\t\t\t? {\n\t\t\t\t\tbaseAssetAmount: orderParams.takeProfit.baseAssetAmount,\n\t\t\t\t\ttriggerPrice: orderParams.takeProfit.triggerPrice,\n\t\t\t }\n\t\t\t: null,\n\t\tmaxMarginRatio: orderParams.positionMaxLeverage\n\t\t\t? TRADING_UTILS.convertLeverageToMarginRatio(\n\t\t\t\t\torderParams.positionMaxLeverage\n\t\t\t )\n\t\t\t: null,\n\t\tisolatedPositionDeposit: orderParams.isolatedPositionDeposit ?? null,\n\t\t// Include builder params if provided\n\t\tbuilderIdx: builderParams?.builderIdx ?? null,\n\t\tbuilderFeeTenthBps: builderParams?.builderFeeTenthBps ?? null,\n\t};\n\n\tconst signedMsgOrderParamsMessage:\n\t\t| SignedMsgOrderParamsMessage\n\t\t| SignedMsgOrderParamsDelegateMessage = isDelegate\n\t\t? {\n\t\t\t\t...baseSignedMsgOrderParamsMessage,\n\t\t\t\ttakerPubkey: takerUserAccount.pubKey,\n\t\t }\n\t\t: {\n\t\t\t\t...baseSignedMsgOrderParamsMessage,\n\t\t\t\tsubAccountId: takerUserAccount.subAccountId,\n\t\t };\n\n\tconst encodedOrderMessage = driftClient.encodeSignedMsgOrderParamsMessage(\n\t\tsignedMsgOrderParamsMessage,\n\t\tisDelegate\n\t);\n\tconst hexEncodedSwiftOrderMessage = Buffer.from(\n\t\tencodedOrderMessage.toString('hex')\n\t);\n\n\treturn {\n\t\thexEncodedSwiftOrderMessage: {\n\t\t\tuInt8Array: new Uint8Array(hexEncodedSwiftOrderMessage),\n\t\t\tstring: hexEncodedSwiftOrderMessage.toString(),\n\t\t},\n\t\tsignedMsgOrderParamsMessage,\n\t\tslotForSignedMsg: auctionStartSlot,\n\t\tsignedMsgOrderUuid,\n\t};\n};\n\n/**\n * Error thrown when an auction slot has expired\n */\nexport class AuctionSlotExpiredError extends Error {\n\tname = 'AuctionSlotExpiredError';\n\n\t/**\n\t * Creates an instance of AuctionSlotExpiredError\n\t * @param message - Error message (default: 'Auction slot expired')\n\t */\n\tconstructor(message: string = 'Auction slot expired') {\n\t\tsuper(message);\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, AuctionSlotExpiredError);\n\t\t}\n\t}\n}\n\ninterface SignOrderMsgParams {\n\t/** Wallet instance with message signing capability */\n\twallet: {\n\t\t/** Function to sign a message */\n\t\tsignMessage: (message: Uint8Array) => Promise<Uint8Array>;\n\t};\n\t/** Hex-encoded swift order message to sign */\n\thexEncodedSwiftOrderMessage: Uint8Array;\n\t/** Time in milliseconds till the auction expires */\n\texpirationTimeMs: number;\n\t/** Callback function called when the auction expires */\n\tonExpired?: () => void;\n}\n\n/**\n * Signs a swift order message with slot expiration monitoring.\n * Continuously monitors the current slot and rejects with AuctionSlotExpiredError\n * if the auction slot expires before signing is complete.\n *\n * @param wallet - Wallet instance with message signing capability\n * @param hexEncodedSwiftOrderMessage - Hex-encoded swift order message to sign\n * @param expirationTimeMs - Time in milliseconds till the auction expires\n * @param onExpired - Callback function called when the auction expires\n *\n * @returns Promise resolving to the signed message as Uint8Array\n * @throws {AuctionSlotExpiredError} When the auction slot expires before signing completes\n */\nexport const signSwiftOrderMsg = async ({\n\twallet,\n\thexEncodedSwiftOrderMessage,\n\texpirationTimeMs,\n\tonExpired,\n}: SignOrderMsgParams): Promise<Uint8Array> => {\n\tlet timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n\ttry {\n\t\t// Sign the message\n\t\tconst signedMessagePromise = wallet.signMessage(\n\t\t\thexEncodedSwiftOrderMessage\n\t\t);\n\n\t\tconst signingExpiredPromise = new Promise<never>((_resolve, reject) => {\n\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\tonExpired?.();\n\t\t\t\treject(new AuctionSlotExpiredError());\n\t\t\t}, expirationTimeMs);\n\t\t});\n\n\t\t// Ensure that the user signs the message before the expiration time\n\t\tconst signedMessage = await Promise.race([\n\t\t\tsignedMessagePromise,\n\t\t\tsigningExpiredPromise,\n\t\t]);\n\n\t\treturn signedMessage;\n\t} finally {\n\t\tif (timeoutId) {\n\t\t\tclearTimeout(timeoutId);\n\t\t}\n\t}\n};\n\n/**\n * Parameters for sending a swift order to the Swift server\n * @interface SendSwiftOrderParams\n */\ninterface SendSwiftOrderParams {\n\t/** The Drift client instance */\n\tdriftClient: DriftClient;\n\t/** Market identifier for the order */\n\tmarketId: MarketId;\n\t/** Hex-encoded swift order message as string */\n\thexEncodedSwiftOrderMessageString: string;\n\t/** The signed message from the wallet */\n\tsignedMessage: Uint8Array;\n\t/** Unique identifier for the signed message order */\n\tsignedMsgOrderUuid: Uint8Array;\n\t/** Public key of the taker authority */\n\ttakerAuthority: PublicKey;\n\t/** Public key of the signing authority */\n\tsigningAuthority: PublicKey;\n\t/** Number of slots till the end of the auction (optional) */\n\tslotsTillAuctionEnd: number;\n\t/** Multiplier for the SWIFT confirmation timeout (after sending SWIFT order) */\n\tconfirmationMultiplier?: number;\n\t/** Optionally send a different connection for the confirmation step, possibly for a faster commitment */\n\tconfirmationConnection?: Connection;\n}\n\n/**\n * Sends a swift order to the Swift server and handles the response.\n * Monitors the order status and calls appropriate callback functions based on the response type.\n *\n * @param driftClient - The Drift client instance\n * @param marketId - Market identifier for the order\n * @param hexEncodedSwiftOrderMessageString - Hex-encoded swift order message as string\n * @param signedMessage - The signed message from the wallet\n * @param signedMsgOrderUuid - Unique identifier for the signed message order\n * @param takerAuthority - Public key of the taker authority\n * @param signingAuthority - Public key of the signing authority\n * @param slotsTillAuctionEnd - Number of slots till the end of the auction (optional)\n * @param swiftConfirmationSlotBuffer - Slot buffer for swift server confirmation time (default: 15)\n * @param onExpired - Callback function called when the order expires\n * @param onErrored - Callback function called when the order encounters an error\n * @param onConfirmed - Callback function called when the order is confirmed\n *\n * @returns Promise that resolves when the order processing is complete\n *\n */\nexport const sendSwiftOrder = ({\n\tdriftClient,\n\tmarketId,\n\thexEncodedSwiftOrderMessageString,\n\tsignedMessage,\n\tsignedMsgOrderUuid,\n\ttakerAuthority,\n\tsigningAuthority,\n\tslotsTillAuctionEnd,\n\tconfirmationMultiplier,\n\tconfirmationConnection,\n}: SendSwiftOrderParams): SwiftOrderObservable => {\n\tconst signedMsgUserOrdersAccountPubkey = getSignedMsgUserAccountPublicKey(\n\t\tdriftClient.program.programId,\n\t\ttakerAuthority\n\t);\n\n\tconst swiftOrderObservable = SwiftClient.sendAndConfirmSwiftOrderWS(\n\t\tconfirmationConnection ?? driftClient.connection,\n\t\tdriftClient,\n\t\tmarketId.marketIndex,\n\t\tmarketId.marketType,\n\t\thexEncodedSwiftOrderMessageString,\n\t\tBuffer.from(signedMessage),\n\t\ttakerAuthority,\n\t\tsignedMsgUserOrdersAccountPubkey,\n\t\tsignedMsgOrderUuid,\n\t\tgetSwiftConfirmationTimeoutMs(slotsTillAuctionEnd, confirmationMultiplier),\n\t\tsigningAuthority\n\t);\n\n\treturn swiftOrderObservable;\n};\n\ntype PrepSignAndSendSwiftOrderParams = {\n\tdriftClient: DriftClient;\n\tsubAccountId: number;\n\tuserAccountPubKey: PublicKey;\n\tmarketIndex: number;\n\tuserSigningSlotBuffer: number;\n\tswiftOptions: SwiftOrderOptions;\n\t/** Multiplier for the SWIFT confirmation timeout (after sending SWIFT order). Default is 1.\n\t *\n\t * Higher multiplier means longer confirmation timeout.\n\t */\n\tconfirmationMultiplier?: number;\n\tconfirmationConnection?: Connection;\n\torderParams: {\n\t\tmain: OptionalOrderParams;\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t\t/**\n\t\t * Adjusts the max leverage of a position.\n\t\t */\n\t\tpositionMaxLeverage?: number;\n\t\t/**\n\t\t * Optional isolated position deposit amount for isolated positions.\n\t\t */\n\t\tisolatedPositionDeposit?: BN;\n\t};\n\t/**\n\t * Optional builder code parameters for revenue sharing.\n\t * If provided, the builder will receive a portion of the trading fees.\n\t *\n\t * Prerequisites:\n\t * - User must have initialized a RevenueShareEscrow account\n\t * - Builder must be in the user's approved_builders list\n\t * - builderFeeTenthBps must not exceed the builder's max_fee_tenth_bps\n\t *\n\t * @example\n\t * ```typescript\n\t * builderParams: {\n\t * builderIdx: 0, // First builder in approved list\n\t * builderFeeTenthBps: 50 // 5 bps = 0.05%\n\t * }\n\t * ```\n\t */\n\tbuilderParams?: {\n\t\t/**\n\t\t * Index of the builder in the user's approved_builders list.\n\t\t */\n\t\tbuilderIdx: number;\n\t\t/**\n\t\t * Fee to charge for this order, in tenths of basis points.\n\t\t * Must be <= the builder's max_fee_tenth_bps.\n\t\t */\n\t\tbuilderFeeTenthBps: number;\n\t};\n};\n\n/**\n * Handles the full flow of the swift order, from preparing to signing and sending to the Swift server.\n * Callbacks can be provided to handle the events of the Swift order.\n * Returns a promise that resolves when the Swift order has reached a terminal state (i.e. confirmed, expired, or errored).\n */\nexport const prepSignAndSendSwiftOrder = async ({\n\tdriftClient,\n\tsubAccountId,\n\tuserAccountPubKey,\n\tmarketIndex,\n\tuserSigningSlotBuffer,\n\tswiftOptions,\n\torderParams,\n\tbuilderParams,\n\tconfirmationMultiplier,\n}: PrepSignAndSendSwiftOrderParams): Promise<void> => {\n\tconst currentSlot = await driftClient.connection.getSlot('confirmed');\n\n\tconst {\n\t\thexEncodedSwiftOrderMessage,\n\t\tsignedMsgOrderUuid,\n\t\tsignedMsgOrderParamsMessage,\n\t} = prepSwiftOrder({\n\t\tdriftClient,\n\t\ttakerUserAccount: {\n\t\t\tpubKey: userAccountPubKey,\n\t\t\tsubAccountId: subAccountId,\n\t\t},\n\t\tcurrentSlot,\n\t\tisDelegate: swiftOptions.isDelegate || false,\n\t\torderParams,\n\t\tuserSigningSlotBuffer,\n\t\tbuilderParams,\n\t});\n\n\tswiftOptions.callbacks?.onOrderParamsMessagePrepped?.(\n\t\tsignedMsgOrderParamsMessage\n\t);\n\n\t// both market and non-market orders can have auction durations.\n\t//\n\t// for market orders, the auction duration + user signing slot buffer is a good gauge for slots until auction end\n\t//\n\t// for non-market orders, if they have an auction duration, we use the maximum of MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS\n\t// and the (auction duration + user signing slot buffer). This is because limit auctions can have very small auction durations,\n\t// but the limit order is still valid to be placed even after the auction ends, hence we enforce a minimum duration so that\n\t// short limit auction durations can still be placed\n\tconst isMarketOrder =\n\t\tENUM_UTILS.match(orderParams.main.orderType, OrderType.ORACLE) ||\n\t\tENUM_UTILS.match(orderParams.main.orderType, OrderType.MARKET);\n\tconst slotsTillAuctionEnd = orderParams.main.auctionDuration\n\t\t? isMarketOrder\n\t\t\t? userSigningSlotBuffer + orderParams.main.auctionDuration\n\t\t\t: Math.max(\n\t\t\t\t\tMINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS,\n\t\t\t\t\tuserSigningSlotBuffer + orderParams.main.auctionDuration\n\t\t\t )\n\t\t: MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS;\n\tconst expirationTimeMs =\n\t\tMath.max(\n\t\t\tslotsTillAuctionEnd - SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS,\n\t\t\tMINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS\n\t\t) * SLOT_TIME_ESTIMATE_MS;\n\n\t// Ensure that the user signs the message before the expiration time\n\tconst signedMessage = await signSwiftOrderMsg({\n\t\twallet: swiftOptions.wallet,\n\t\thexEncodedSwiftOrderMessage: hexEncodedSwiftOrderMessage.uInt8Array,\n\t\texpirationTimeMs,\n\t\tonExpired: () =>\n\t\t\tswiftOptions.callbacks?.onSigningExpiry?.(signedMsgOrderParamsMessage),\n\t});\n\n\tswiftOptions.callbacks?.onSigningSuccess?.(\n\t\tsignedMessage,\n\t\tsignedMsgOrderUuid,\n\t\tsignedMsgOrderParamsMessage\n\t);\n\n\t// Initialize SwiftClient (required before using sendSwiftOrder)\n\tSwiftClient.init(swiftOptions.swiftServerUrl, swiftOptions.source ?? '');\n\n\t// Create a promise-based wrapper for the sendSwiftOrder callback-based API\n\tconst swiftOrderObservable = sendSwiftOrder({\n\t\tdriftClient,\n\t\tmarketId: MarketId.createPerpMarket(marketIndex),\n\t\thexEncodedSwiftOrderMessageString: hexEncodedSwiftOrderMessage.string,\n\t\tsignedMessage,\n\t\tsignedMsgOrderUuid,\n\t\ttakerAuthority: swiftOptions.wallet.takerAuthority,\n\t\tsigningAuthority:\n\t\t\tswiftOptions.wallet.signingAuthority ??\n\t\t\tswiftOptions.wallet.takerAuthority,\n\t\tslotsTillAuctionEnd,\n\t\tconfirmationMultiplier,\n\t\tconfirmationConnection: new Connection(\n\t\t\tdriftClient.connection.rpcEndpoint,\n\t\t\t'processed'\n\t\t),\n\t});\n\n\tconst wrapSwiftOrderEvent = <T extends SwiftOrderEvent>(\n\t\tswiftOrderEvent: T\n\t) => {\n\t\treturn {\n\t\t\t...swiftOrderEvent,\n\t\t\tswiftOrderUuid: signedMsgOrderUuid,\n\t\t\torderParamsMessage: signedMsgOrderParamsMessage,\n\t\t};\n\t};\n\n\tlet promiseResolver: (value: void | PromiseLike<void>) => void;\n\tconst promise = new Promise<void>((resolve) => {\n\t\tpromiseResolver = resolve;\n\t});\n\n\tconst handleTerminalEvent = (subscription: Subscription) => {\n\t\tsubscription.unsubscribe();\n\t\tpromiseResolver();\n\t};\n\n\tconst subscription = swiftOrderObservable.subscribe((swiftOrderEvent) => {\n\t\tif (swiftOrderEvent.type === 'sent') {\n\t\t\tswiftOptions.callbacks?.onSent?.(wrapSwiftOrderEvent(swiftOrderEvent));\n\t\t}\n\t\tif (swiftOrderEvent.type === 'confirmed') {\n\t\t\tswiftOptions.callbacks?.onConfirmed?.(\n\t\t\t\twrapSwiftOrderEvent(swiftOrderEvent)\n\t\t\t);\n\t\t\thandleTerminalEvent(subscription);\n\t\t}\n\t\tif (swiftOrderEvent.type === 'expired') {\n\t\t\tswiftOptions.callbacks?.onExpired?.(wrapSwiftOrderEvent(swiftOrderEvent));\n\t\t\thandleTerminalEvent(subscription);\n\t\t}\n\t\tif (swiftOrderEvent.type === 'errored') {\n\t\t\tswiftOptions.callbacks?.onErrored?.(wrapSwiftOrderEvent(swiftOrderEvent));\n\t\t\thandleTerminalEvent(subscription);\n\t\t}\n\t});\n\n\treturn promise;\n};\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.ts"],"names":[],"mappings":";;;AAAA,yCAMyB;AACzB,yCAOyB;AACzB,mDAGiC;AACjC,uEAO+C;AAC/C,mDAAmD;AAGnD,uEAA0E;AAC1E,6CAA6C;AAE7C;;GAEG;AACU,QAAA,iCAAiC,GAAG,CAAC,CAAC;AAEnD;;GAEG;AACU,QAAA,+DAA+D,GAAG,EAAE,CAAC;AAElF;;GAEG;AACU,QAAA,2CAA2C,GAAG,CAAC,CAAC;AAChD,QAAA,mDAAmD,GAAG,CAAC,CAAC;AAqJrE;;;;;;;;;;;;;;;;GAgBG;AACI,MAAM,cAAc,GAAG,CAAC,EAC9B,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,WAAW,EACX,qBAAqB,EACrB,aAAa,GACS,EAWrB,EAAE;;IACH,MAAM,eAAe,GAAG,IAAA,oBAAc,EAAC;QACtC,GAAG,WAAW,CAAC,IAAI;QACnB,eAA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SignedMsgOrderParamsMessage,\n\tSLOT_TIME_ESTIMATE_MS,\n} from '@drift-labs/sdk';\nimport {\n\tBN,\n\tDriftClient,\n\tgenerateSignedMsgUuid,\n\tgetOrderParams,\n\tOptionalOrderParams,\n\tPublicKey,\n} from '@drift-labs/sdk';\nimport {\n\tENUM_UTILS,\n\tgetSwiftConfirmationTimeoutMs,\n} from '../../../../../../utils';\nimport {\n\tSwiftClient,\n\tSwiftOrderConfirmedEvent,\n\tSwiftOrderErroredEvent,\n\tSwiftOrderEvent,\n\tSwiftOrderEventWithParams,\n\tSwiftOrderSentEvent,\n} from '../../../../../../clients/swiftClient';\nimport { MarketId } from '../../../../../../types';\nimport { Observable, Subscription } from 'rxjs';\nimport { OptionalTriggerOrderParams } from '../types';\nimport { TRADING_UTILS } from '../../../../../../common-ui-utils/trading';\nimport { Connection } from '@solana/web3.js';\n\n/**\n * Buffer slots to account for signing of message by the user (default: 7 slots ~3 second, assumes user have to approves the signing in a UI wallet).\n */\nexport const USER_SIGNING_MESSAGE_BUFFER_SLOTS = 7;\n\n/**\n * Orders without auction require a higher buffer (kink of the SWIFT server handling non-auction orders)\n */\nexport const MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 35;\n\n/**\n * Buffer slots from the end of the auction to prevent the signing of the order message.\n */\nexport const SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 5;\nexport const MINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 5;\n\nexport interface SwiftOrderOptions {\n\twallet: {\n\t\tsignMessage: (message: Uint8Array) => Promise<Uint8Array>;\n\t\ttakerAuthority: PublicKey;\n\t\tsigningAuthority?: PublicKey;\n\t};\n\tswiftServerUrl: string;\n\t/**\n\t * Buffer slots to account for the user to sign the message. Affects the auction start slot.\n\t * If order is not an auction order, it is not encouraged to use this buffer.\n\t */\n\tuserSigningSlotBuffer?: number;\n\tisDelegate?: boolean;\n\t/**\n\t * Multiplier for the SWIFT confirmation timeout (after sending SWIFT order). Default is 1.\n\t */\n\tconfirmationMultiplier?: number;\n\tcallbacks?: {\n\t\tonOrderParamsMessagePrepped?: (\n\t\t\torderParamsMessage:\n\t\t\t\t| SignedMsgOrderParamsMessage\n\t\t\t\t| SignedMsgOrderParamsDelegateMessage\n\t\t) => void;\n\t\tonSigningExpiry?: (\n\t\t\torderParamsMessage:\n\t\t\t\t| SignedMsgOrderParamsMessage\n\t\t\t\t| SignedMsgOrderParamsDelegateMessage\n\t\t) => void;\n\t\tonSigningSuccess?: (\n\t\t\tsignedMessage: Uint8Array,\n\t\t\t// we add the following here, because the onSigningSuccess callback is called before the order is sent to the swift server\n\t\t\torderUuid: Uint8Array,\n\t\t\torderParamsMessage:\n\t\t\t\t| SignedMsgOrderParamsMessage\n\t\t\t\t| SignedMsgOrderParamsDelegateMessage\n\t\t) => void;\n\t\tonSent?: (\n\t\t\tswiftSentEvent: SwiftOrderEventWithParams<SwiftOrderSentEvent>\n\t\t) => void;\n\t\tonConfirmed?: (\n\t\t\tswiftConfirmedEvent: SwiftOrderEventWithParams<SwiftOrderConfirmedEvent>\n\t\t) => void;\n\t\tonExpired?: (\n\t\t\tswiftExpiredEvent: SwiftOrderEventWithParams<SwiftOrderErroredEvent>\n\t\t) => void;\n\t\tonErrored?: (\n\t\t\tswiftErroredEvent: SwiftOrderEventWithParams<SwiftOrderErroredEvent>\n\t\t) => void;\n\t};\n\t/**\n\t * Used for internal tracking of the source of the swift order.\n\t */\n\tsource?: string;\n}\n\n/**\n * Represents a prepared SWIFT order message that is ready to be signed and sent.\n * This is the output of the \"prep\" step, before signing occurs.\n *\n * Consumers should:\n * 1. Sign `hexEncodedSwiftOrderMessage.uInt8Array` with the user's wallet\n * 2. Send the signed message along with the other fields to the SWIFT server\n */\nexport interface SwiftOrderMessage {\n\t/** The encoded order message in both Uint8Array (for signing) and string (for sending) formats */\n\thexEncodedSwiftOrderMessage: {\n\t\tuInt8Array: Uint8Array;\n\t\tstring: string;\n\t};\n\t/** The order parameters message that was encoded */\n\tsignedMsgOrderParamsMessage:\n\t\t| SignedMsgOrderParamsMessage\n\t\t| SignedMsgOrderParamsDelegateMessage;\n\t/** The slot number used for the signed message */\n\tslotForSignedMsg: BN;\n\t/** Unique identifier for the signed message order */\n\tsignedMsgOrderUuid: Uint8Array;\n\t/** The market index this order is for */\n\tmarketIndex: number;\n\t/** Number of slots till the auction ends (used for confirmation timeout) */\n\tslotsTillAuctionEnd: number;\n\t/** Time in milliseconds before the signing window expires */\n\texpirationTimeMs: number;\n}\n\nexport type SwiftOrderObservable = Observable<SwiftOrderEvent>;\n\ninterface PrepSwiftOrderParams {\n\t/** The Drift client instance */\n\tdriftClient: DriftClient;\n\t/** The taker user account information */\n\ttakerUserAccount: {\n\t\t/** Public key of the user account */\n\t\tpubKey: PublicKey;\n\t\t/** User account ID */\n\t\tsubAccountId: number;\n\t};\n\t/** Current blockchain slot number */\n\tcurrentSlot: number;\n\t/** Whether this is a delegate order */\n\tisDelegate: boolean;\n\t/** Order parameters including main order and optional stop loss/take profit */\n\torderParams: {\n\t\t/** Main order parameters */\n\t\tmain: OptionalOrderParams;\n\t\t/** Optional stop loss order parameters */\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t\t/** Optional take profit order parameters */\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\t/** Optional max leverage for the position */\n\t\tpositionMaxLeverage?: number;\n\t\t/** Optional isolated position deposit amount */\n\t\tisolatedPositionDeposit?: BN;\n\t};\n\t/**\n\t * Buffer slots to account for the user to sign the message. Affects the auction start slot.\n\t * If order is not an auction order, it is not encouraged to use this buffer.\n\t */\n\tuserSigningSlotBuffer?: number;\n\t/**\n\t * Optional builder code parameters for revenue sharing.\n\t * If provided, the builder will receive a portion of the trading fees.\n\t *\n\t * Prerequisites:\n\t * - User must have initialized a RevenueShareEscrow account\n\t * - Builder must be in the user's approved_builders list\n\t * - builderFeeTenthBps must not exceed the builder's max_fee_tenth_bps\n\t */\n\tbuilderParams?: {\n\t\t/**\n\t\t * Index of the builder in the user's approved_builders list.\n\t\t * This is the position (0-indexed) of the builder in the RevenueShareEscrow.approved_builders array.\n\t\t */\n\t\tbuilderIdx: number;\n\t\t/**\n\t\t * Fee to charge for this order, in tenths of basis points.\n\t\t * Must be <= the builder's max_fee_tenth_bps.\n\t\t *\n\t\t * Examples:\n\t\t * - 10 = 1 bps = 0.01%\n\t\t * - 50 = 5 bps = 0.05%\n\t\t * - 100 = 10 bps = 0.1%\n\t\t */\n\t\tbuilderFeeTenthBps: number;\n\t};\n}\n\n/**\n * Prepares a swift order by encoding the order parameters into a message format\n * suitable for signing and sending to the Swift server.\n *\n * @param driftClient - The Drift client instance\n * @param takerUserAccount - The taker user account information\n * @param currentSlot - Current blockchain slot number\n * @param isDelegate - Whether this is a delegate order\n * @param orderParams - Order parameters including main order and optional stop loss/take profit\n * @param userSigningSlotBuffer - Buffer slots to account for the user to sign the message. Affects the auction start slot. If order is not an auction order, it is not encouraged to use this buffer.\n *\n * @returns An object containing:\n * - `hexEncodedSwiftOrderMessage`: The encoded order message in both Uint8Array and string formats. The Uint8Array format is for a wallet to sign, while the string format is used to send to the SWIFT server.\n * - `signedMsgOrderParamsMessage`: The signed message order parameters\n * - `slotForSignedMsg`: The slot number for the signed message\n * - `signedMsgOrderUuid`: Unique identifier for the signed message order\n */\nexport const prepSwiftOrder = ({\n\tdriftClient,\n\ttakerUserAccount,\n\tcurrentSlot,\n\tisDelegate,\n\torderParams,\n\tuserSigningSlotBuffer,\n\tbuilderParams,\n}: PrepSwiftOrderParams): {\n\thexEncodedSwiftOrderMessage: {\n\t\tuInt8Array: Uint8Array;\n\t\tstring: string;\n\t};\n\tsignedMsgOrderParamsMessage:\n\t\t| SignedMsgOrderParamsMessage\n\t\t| SignedMsgOrderParamsDelegateMessage;\n\tslotForSignedMsg: BN;\n\tsignedMsgOrderUuid: Uint8Array;\n\tresolvedUserSigningSlotBuffer: number;\n} => {\n\tconst mainOrderParams = getOrderParams({\n\t\t...orderParams.main,\n\t\tauctionDuration: orderParams.main.auctionDuration || null, // swift server expects auctionDuration to be null if not set, won't handle 0\n\t});\n\n\tif (!userSigningSlotBuffer) {\n\t\tuserSigningSlotBuffer = mainOrderParams.auctionDuration\n\t\t\t? USER_SIGNING_MESSAGE_BUFFER_SLOTS\n\t\t\t: MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS;\n\t}\n\n\t// if it is not an auction order, there should be a minimum buffer used\n\tif (!mainOrderParams.auctionDuration) {\n\t\tuserSigningSlotBuffer = Math.max(\n\t\t\tuserSigningSlotBuffer,\n\t\t\tMINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS\n\t\t);\n\t}\n\n\t// buffer for time the user takes to sign a message and send to the swift server\n\tconst auctionStartSlot = new BN(currentSlot + userSigningSlotBuffer);\n\n\tconst signedMsgOrderUuid = generateSignedMsgUuid();\n\n\tconsole.log(\n\t\t'DEBUG swift prep orderParams.isolatedPositionDeposit',\n\t\torderParams.isolatedPositionDeposit?.toString()\n\t);\n\n\tconst baseSignedMsgOrderParamsMessage = {\n\t\tsignedMsgOrderParams: mainOrderParams,\n\t\tuuid: signedMsgOrderUuid,\n\t\tslot: auctionStartSlot,\n\t\tstopLossOrderParams: orderParams.stopLoss\n\t\t\t? {\n\t\t\t\t\tbaseAssetAmount: orderParams.stopLoss.baseAssetAmount,\n\t\t\t\t\ttriggerPrice: orderParams.stopLoss.triggerPrice,\n\t\t\t }\n\t\t\t: null,\n\t\ttakeProfitOrderParams: orderParams.takeProfit\n\t\t\t? {\n\t\t\t\t\tbaseAssetAmount: orderParams.takeProfit.baseAssetAmount,\n\t\t\t\t\ttriggerPrice: orderParams.takeProfit.triggerPrice,\n\t\t\t }\n\t\t\t: null,\n\t\tmaxMarginRatio: orderParams.positionMaxLeverage\n\t\t\t? TRADING_UTILS.convertLeverageToMarginRatio(\n\t\t\t\t\torderParams.positionMaxLeverage\n\t\t\t )\n\t\t\t: null,\n\t\tisolatedPositionDeposit: orderParams.isolatedPositionDeposit ?? null,\n\t\t// Include builder params if provided\n\t\tbuilderIdx: builderParams?.builderIdx ?? null,\n\t\tbuilderFeeTenthBps: builderParams?.builderFeeTenthBps ?? null,\n\t};\n\n\tconst signedMsgOrderParamsMessage:\n\t\t| SignedMsgOrderParamsMessage\n\t\t| SignedMsgOrderParamsDelegateMessage = isDelegate\n\t\t? {\n\t\t\t\t...baseSignedMsgOrderParamsMessage,\n\t\t\t\ttakerPubkey: takerUserAccount.pubKey,\n\t\t }\n\t\t: {\n\t\t\t\t...baseSignedMsgOrderParamsMessage,\n\t\t\t\tsubAccountId: takerUserAccount.subAccountId,\n\t\t };\n\n\tconst encodedOrderMessage = driftClient.encodeSignedMsgOrderParamsMessage(\n\t\tsignedMsgOrderParamsMessage,\n\t\tisDelegate\n\t);\n\tconst hexEncodedSwiftOrderMessage = Buffer.from(\n\t\tencodedOrderMessage.toString('hex')\n\t);\n\n\treturn {\n\t\thexEncodedSwiftOrderMessage: {\n\t\t\tuInt8Array: new Uint8Array(hexEncodedSwiftOrderMessage),\n\t\t\tstring: hexEncodedSwiftOrderMessage.toString(),\n\t\t},\n\t\tsignedMsgOrderParamsMessage,\n\t\tslotForSignedMsg: auctionStartSlot,\n\t\tsignedMsgOrderUuid,\n\t\tresolvedUserSigningSlotBuffer: userSigningSlotBuffer,\n\t};\n};\n\n/**\n * Error thrown when an auction slot has expired\n */\nexport class AuctionSlotExpiredError extends Error {\n\tname = 'AuctionSlotExpiredError';\n\n\t/**\n\t * Creates an instance of AuctionSlotExpiredError\n\t * @param message - Error message (default: 'Auction slot expired')\n\t */\n\tconstructor(message: string = 'Auction slot expired') {\n\t\tsuper(message);\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, AuctionSlotExpiredError);\n\t\t}\n\t}\n}\n\ninterface SignOrderMsgParams {\n\t/** Wallet instance with message signing capability */\n\twallet: {\n\t\t/** Function to sign a message */\n\t\tsignMessage: (message: Uint8Array) => Promise<Uint8Array>;\n\t};\n\t/** Hex-encoded swift order message to sign */\n\thexEncodedSwiftOrderMessage: Uint8Array;\n\t/** Time in milliseconds till the auction expires */\n\texpirationTimeMs: number;\n\t/** Callback function called when the auction expires */\n\tonExpired?: () => void;\n}\n\n/**\n * Signs a swift order message with slot expiration monitoring.\n * Continuously monitors the current slot and rejects with AuctionSlotExpiredError\n * if the auction slot expires before signing is complete.\n *\n * @param wallet - Wallet instance with message signing capability\n * @param hexEncodedSwiftOrderMessage - Hex-encoded swift order message to sign\n * @param expirationTimeMs - Time in milliseconds till the auction expires\n * @param onExpired - Callback function called when the auction expires\n *\n * @returns Promise resolving to the signed message as Uint8Array\n * @throws {AuctionSlotExpiredError} When the auction slot expires before signing completes\n */\nexport const signSwiftOrderMsg = async ({\n\twallet,\n\thexEncodedSwiftOrderMessage,\n\texpirationTimeMs,\n\tonExpired,\n}: SignOrderMsgParams): Promise<Uint8Array> => {\n\tlet timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n\ttry {\n\t\t// Sign the message\n\t\tconst signedMessagePromise = wallet.signMessage(\n\t\t\thexEncodedSwiftOrderMessage\n\t\t);\n\n\t\tconst signingExpiredPromise = new Promise<never>((_resolve, reject) => {\n\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\tonExpired?.();\n\t\t\t\treject(new AuctionSlotExpiredError());\n\t\t\t}, expirationTimeMs);\n\t\t});\n\n\t\t// Ensure that the user signs the message before the expiration time\n\t\tconst signedMessage = await Promise.race([\n\t\t\tsignedMessagePromise,\n\t\t\tsigningExpiredPromise,\n\t\t]);\n\n\t\treturn signedMessage;\n\t} finally {\n\t\tif (timeoutId) {\n\t\t\tclearTimeout(timeoutId);\n\t\t}\n\t}\n};\n\n/**\n * Parameters for sending a swift order to the Swift server\n * @interface SendSwiftOrderParams\n */\ninterface SendSwiftOrderParams {\n\t/** The Drift client instance */\n\tdriftClient: DriftClient;\n\t/** Market identifier for the order */\n\tmarketId: MarketId;\n\t/** Hex-encoded swift order message as string */\n\thexEncodedSwiftOrderMessageString: string;\n\t/** The signed message from the wallet */\n\tsignedMessage: Uint8Array;\n\t/** Unique identifier for the signed message order */\n\tsignedMsgOrderUuid: Uint8Array;\n\t/** Public key of the taker authority */\n\ttakerAuthority: PublicKey;\n\t/** Public key of the signing authority */\n\tsigningAuthority: PublicKey;\n\t/** Number of slots till the end of the auction (optional) */\n\tslotsTillAuctionEnd: number;\n\t/** Multiplier for the SWIFT confirmation timeout (after sending SWIFT order) */\n\tconfirmationMultiplier?: number;\n\t/** Optionally send a different connection for the confirmation step, possibly for a faster commitment */\n\tconfirmationConnection?: Connection;\n}\n\n/**\n * Sends a swift order to the Swift server and handles the response.\n * Monitors the order status and calls appropriate callback functions based on the response type.\n *\n * @param driftClient - The Drift client instance\n * @param marketId - Market identifier for the order\n * @param hexEncodedSwiftOrderMessageString - Hex-encoded swift order message as string\n * @param signedMessage - The signed message from the wallet\n * @param signedMsgOrderUuid - Unique identifier for the signed message order\n * @param takerAuthority - Public key of the taker authority\n * @param signingAuthority - Public key of the signing authority\n * @param slotsTillAuctionEnd - Number of slots till the end of the auction (optional)\n * @param swiftConfirmationSlotBuffer - Slot buffer for swift server confirmation time (default: 15)\n * @param onExpired - Callback function called when the order expires\n * @param onErrored - Callback function called when the order encounters an error\n * @param onConfirmed - Callback function called when the order is confirmed\n *\n * @returns Promise that resolves when the order processing is complete\n *\n */\nexport const sendSwiftOrder = ({\n\tdriftClient,\n\tmarketId,\n\thexEncodedSwiftOrderMessageString,\n\tsignedMessage,\n\tsignedMsgOrderUuid,\n\ttakerAuthority,\n\tsigningAuthority,\n\tslotsTillAuctionEnd,\n\tconfirmationMultiplier,\n\tconfirmationConnection,\n}: SendSwiftOrderParams): SwiftOrderObservable => {\n\tconst signedMsgUserOrdersAccountPubkey = getSignedMsgUserAccountPublicKey(\n\t\tdriftClient.program.programId,\n\t\ttakerAuthority\n\t);\n\n\tconst swiftOrderObservable = SwiftClient.sendAndConfirmSwiftOrderWS(\n\t\tconfirmationConnection ?? driftClient.connection,\n\t\tdriftClient,\n\t\tmarketId.marketIndex,\n\t\tmarketId.marketType,\n\t\thexEncodedSwiftOrderMessageString,\n\t\tBuffer.from(signedMessage),\n\t\ttakerAuthority,\n\t\tsignedMsgUserOrdersAccountPubkey,\n\t\tsignedMsgOrderUuid,\n\t\tgetSwiftConfirmationTimeoutMs(slotsTillAuctionEnd, confirmationMultiplier),\n\t\tsigningAuthority\n\t);\n\n\treturn swiftOrderObservable;\n};\n\n/**\n * Computes the timing parameters for a SWIFT order:\n * - slotsTillAuctionEnd: how many slots until the auction is considered ended\n * - expirationTimeMs: how long (in ms) the user has to sign before the window expires\n *\n * For market orders, auction duration + signing buffer is used directly.\n * For non-market orders, a minimum is enforced because limit auctions can have\n * very small durations but the order is still valid after the auction ends.\n */\nconst computeSwiftOrderTiming = (\n\tmainOrderParams: OptionalOrderParams,\n\tuserSigningSlotBuffer: number\n): { slotsTillAuctionEnd: number; expirationTimeMs: number } => {\n\tconst isMarketOrder =\n\t\tENUM_UTILS.match(mainOrderParams.orderType, OrderType.ORACLE) ||\n\t\tENUM_UTILS.match(mainOrderParams.orderType, OrderType.MARKET);\n\tconst slotsTillAuctionEnd = mainOrderParams.auctionDuration\n\t\t? isMarketOrder\n\t\t\t? userSigningSlotBuffer + mainOrderParams.auctionDuration\n\t\t\t: Math.max(\n\t\t\t\t\tMINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS,\n\t\t\t\t\tuserSigningSlotBuffer + mainOrderParams.auctionDuration\n\t\t\t )\n\t\t: MINIMUM_SWIFT_NON_AUCTION_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS;\n\n\tconst expirationTimeMs =\n\t\tMath.max(\n\t\t\tslotsTillAuctionEnd - SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS,\n\t\t\tMINIMUM_SWIFT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS\n\t\t) * SLOT_TIME_ESTIMATE_MS;\n\n\treturn { slotsTillAuctionEnd, expirationTimeMs };\n};\n\ntype PrepSwiftOrderMessageParams = {\n\tdriftClient: DriftClient;\n\tsubAccountId: number;\n\tuserAccountPubKey: PublicKey;\n\tmarketIndex: number;\n\tuserSigningSlotBuffer: number;\n\tisDelegate?: boolean;\n\torderParams: {\n\t\tmain: OptionalOrderParams;\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t\tpositionMaxLeverage?: number;\n\t\tisolatedPositionDeposit?: BN;\n\t};\n\tbuilderParams?: {\n\t\tbuilderIdx: number;\n\t\tbuilderFeeTenthBps: number;\n\t};\n};\n\n/**\n * Prepares a SWIFT order message without signing or sending it.\n * Returns all data needed for the consumer to sign and send the order themselves.\n *\n * This is useful for server-side contexts (e.g., CentralServerDrift) where\n * the server prepares the message but the client handles signing and sending.\n */\nexport const prepSwiftOrderMessage = async ({\n\tdriftClient,\n\tsubAccountId,\n\tuserAccountPubKey,\n\tmarketIndex,\n\tuserSigningSlotBuffer,\n\tisDelegate = false,\n\torderParams,\n\tbuilderParams,\n}: PrepSwiftOrderMessageParams): Promise<SwiftOrderMessage> => {\n\tconst currentSlot = await driftClient.connection.getSlot('confirmed');\n\n\tconst {\n\t\thexEncodedSwiftOrderMessage,\n\t\tsignedMsgOrderUuid,\n\t\tsignedMsgOrderParamsMessage,\n\t\tslotForSignedMsg,\n\t\tresolvedUserSigningSlotBuffer,\n\t} = prepSwiftOrder({\n\t\tdriftClient,\n\t\ttakerUserAccount: {\n\t\t\tpubKey: userAccountPubKey,\n\t\t\tsubAccountId,\n\t\t},\n\t\tcurrentSlot,\n\t\tisDelegate,\n\t\torderParams,\n\t\tuserSigningSlotBuffer,\n\t\tbuilderParams,\n\t});\n\n\tconst { slotsTillAuctionEnd, expirationTimeMs } = computeSwiftOrderTiming(\n\t\torderParams.main,\n\t\tresolvedUserSigningSlotBuffer\n\t);\n\n\treturn {\n\t\thexEncodedSwiftOrderMessage,\n\t\tsignedMsgOrderParamsMessage,\n\t\tslotForSignedMsg,\n\t\tsignedMsgOrderUuid,\n\t\tmarketIndex,\n\t\tslotsTillAuctionEnd,\n\t\texpirationTimeMs,\n\t};\n};\n\ntype PrepSignAndSendSwiftOrderParams = {\n\tdriftClient: DriftClient;\n\tsubAccountId: number;\n\tuserAccountPubKey: PublicKey;\n\tmarketIndex: number;\n\tuserSigningSlotBuffer: number;\n\tswiftOptions: SwiftOrderOptions;\n\t/** Multiplier for the SWIFT confirmation timeout (after sending SWIFT order). Default is 1.\n\t *\n\t * Higher multiplier means longer confirmation timeout.\n\t */\n\tconfirmationMultiplier?: number;\n\tconfirmationConnection?: Connection;\n\torderParams: {\n\t\tmain: OptionalOrderParams;\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t\t/**\n\t\t * Adjusts the max leverage of a position.\n\t\t */\n\t\tpositionMaxLeverage?: number;\n\t\t/**\n\t\t * Optional isolated position deposit amount for isolated positions.\n\t\t */\n\t\tisolatedPositionDeposit?: BN;\n\t};\n\t/**\n\t * Optional builder code parameters for revenue sharing.\n\t * If provided, the builder will receive a portion of the trading fees.\n\t *\n\t * Prerequisites:\n\t * - User must have initialized a RevenueShareEscrow account\n\t * - Builder must be in the user's approved_builders list\n\t * - builderFeeTenthBps must not exceed the builder's max_fee_tenth_bps\n\t *\n\t * @example\n\t * ```typescript\n\t * builderParams: {\n\t * builderIdx: 0, // First builder in approved list\n\t * builderFeeTenthBps: 50 // 5 bps = 0.05%\n\t * }\n\t * ```\n\t */\n\tbuilderParams?: {\n\t\t/**\n\t\t * Index of the builder in the user's approved_builders list.\n\t\t */\n\t\tbuilderIdx: number;\n\t\t/**\n\t\t * Fee to charge for this order, in tenths of basis points.\n\t\t * Must be <= the builder's max_fee_tenth_bps.\n\t\t */\n\t\tbuilderFeeTenthBps: number;\n\t};\n};\n\n/**\n * Handles the full flow of the swift order, from preparing to signing and sending to the Swift server.\n * Callbacks can be provided to handle the events of the Swift order.\n * Returns a promise that resolves when the Swift order has reached a terminal state (i.e. confirmed, expired, or errored).\n */\nexport const prepSignAndSendSwiftOrder = async ({\n\tdriftClient,\n\tsubAccountId,\n\tuserAccountPubKey,\n\tmarketIndex,\n\tuserSigningSlotBuffer,\n\tswiftOptions,\n\torderParams,\n\tbuilderParams,\n\tconfirmationMultiplier,\n}: PrepSignAndSendSwiftOrderParams): Promise<void> => {\n\tconst {\n\t\thexEncodedSwiftOrderMessage,\n\t\tsignedMsgOrderUuid,\n\t\tsignedMsgOrderParamsMessage,\n\t\tslotsTillAuctionEnd,\n\t\texpirationTimeMs,\n\t} = await prepSwiftOrderMessage({\n\t\tdriftClient,\n\t\tsubAccountId,\n\t\tuserAccountPubKey,\n\t\tmarketIndex,\n\t\tuserSigningSlotBuffer,\n\t\tisDelegate: swiftOptions.isDelegate || false,\n\t\torderParams,\n\t\tbuilderParams,\n\t});\n\n\tswiftOptions.callbacks?.onOrderParamsMessagePrepped?.(\n\t\tsignedMsgOrderParamsMessage\n\t);\n\n\t// Ensure that the user signs the message before the expiration time\n\tconst signedMessage = await signSwiftOrderMsg({\n\t\twallet: swiftOptions.wallet,\n\t\thexEncodedSwiftOrderMessage: hexEncodedSwiftOrderMessage.uInt8Array,\n\t\texpirationTimeMs,\n\t\tonExpired: () =>\n\t\t\tswiftOptions.callbacks?.onSigningExpiry?.(signedMsgOrderParamsMessage),\n\t});\n\n\tswiftOptions.callbacks?.onSigningSuccess?.(\n\t\tsignedMessage,\n\t\tsignedMsgOrderUuid,\n\t\tsignedMsgOrderParamsMessage\n\t);\n\n\t// Initialize SwiftClient (required before using sendSwiftOrder)\n\tSwiftClient.init(swiftOptions.swiftServerUrl, swiftOptions.source ?? '');\n\n\t// Create a promise-based wrapper for the sendSwiftOrder callback-based API\n\tconst swiftOrderObservable = sendSwiftOrder({\n\t\tdriftClient,\n\t\tmarketId: MarketId.createPerpMarket(marketIndex),\n\t\thexEncodedSwiftOrderMessageString: hexEncodedSwiftOrderMessage.string,\n\t\tsignedMessage,\n\t\tsignedMsgOrderUuid,\n\t\ttakerAuthority: swiftOptions.wallet.takerAuthority,\n\t\tsigningAuthority:\n\t\t\tswiftOptions.wallet.signingAuthority ??\n\t\t\tswiftOptions.wallet.takerAuthority,\n\t\tslotsTillAuctionEnd,\n\t\tconfirmationMultiplier,\n\t\tconfirmationConnection: new Connection(\n\t\t\tdriftClient.connection.rpcEndpoint,\n\t\t\t'processed'\n\t\t),\n\t});\n\n\tconst wrapSwiftOrderEvent = <T extends SwiftOrderEvent>(\n\t\tswiftOrderEvent: T\n\t) => {\n\t\treturn {\n\t\t\t...swiftOrderEvent,\n\t\t\tswiftOrderUuid: signedMsgOrderUuid,\n\t\t\torderParamsMessage: signedMsgOrderParamsMessage,\n\t\t};\n\t};\n\n\tlet promiseResolver: (value: void | PromiseLike<void>) => void;\n\tconst promise = new Promise<void>((resolve) => {\n\t\tpromiseResolver = resolve;\n\t});\n\n\tconst handleTerminalEvent = (subscription: Subscription) => {\n\t\tsubscription.unsubscribe();\n\t\tpromiseResolver();\n\t};\n\n\tconst subscription = swiftOrderObservable.subscribe((swiftOrderEvent) => {\n\t\tif (swiftOrderEvent.type === 'sent') {\n\t\t\tswiftOptions.callbacks?.onSent?.(wrapSwiftOrderEvent(swiftOrderEvent));\n\t\t}\n\t\tif (swiftOrderEvent.type === 'confirmed') {\n\t\t\tswiftOptions.callbacks?.onConfirmed?.(\n\t\t\t\twrapSwiftOrderEvent(swiftOrderEvent)\n\t\t\t);\n\t\t\thandleTerminalEvent(subscription);\n\t\t}\n\t\tif (swiftOrderEvent.type === 'expired') {\n\t\t\tswiftOptions.callbacks?.onExpired?.(wrapSwiftOrderEvent(swiftOrderEvent));\n\t\t\thandleTerminalEvent(subscription);\n\t\t}\n\t\tif (swiftOrderEvent.type === 'errored') {\n\t\t\tswiftOptions.callbacks?.onErrored?.(wrapSwiftOrderEvent(swiftOrderEvent));\n\t\t\thandleTerminalEvent(subscription);\n\t\t}\n\t});\n\n\treturn promise;\n};\n"]}
package/lib/drift/cli.js CHANGED
@@ -32,6 +32,9 @@ const CentralServerDrift_1 = require("./Drift/clients/CentralServerDrift");
32
32
  const utils_1 = require("../utils");
33
33
  const apiUrls_1 = require("./constants/apiUrls");
34
34
  const path = __importStar(require("path"));
35
+ const openSwiftOrder_1 = require("./base/actions/trade/openPerpOrder/openSwiftOrder");
36
+ const swiftClient_1 = require("../clients/swiftClient");
37
+ const types_1 = require("../types");
35
38
  // Load environment variables from .env file
36
39
  const dotenv = require('dotenv');
37
40
  dotenv.config({ path: path.resolve(__dirname, '.env') });
@@ -258,37 +261,65 @@ async function executeTransaction(txn, transactionType) {
258
261
  console.log(`📋 Transaction Signature: ${txSig === null || txSig === void 0 ? void 0 : txSig.toString()}`);
259
262
  console.log(`🔍 View on Solscan: https://solscan.io/tx/${txSig === null || txSig === void 0 ? void 0 : txSig.toString()}`);
260
263
  }
261
- const createSwiftOrderCallbacks = (orderType) => {
262
- const terminalCall = () => {
263
- console.log('🏁 Order monitoring completed');
264
- };
265
- return {
266
- onSigningExpiry: () => {
267
- console.log('Swift order signing expired');
268
- },
269
- onSigningSuccess: (signedMessage, orderUuid, orderParamsMessage) => {
270
- console.log('Swift order signed successfully. Signed message:', signedMessage, orderUuid, orderParamsMessage);
271
- },
272
- onSent: () => {
273
- console.log(`✅ ${orderType} Swift order submitted successfully`);
274
- },
275
- onConfirmed: (event) => {
276
- console.log('✅ Order confirmed!');
277
- console.log(`📋 Order ID: ${event.orderId}`);
278
- console.log(`📋 Hash: ${event.hash}`);
279
- terminalCall();
280
- },
281
- onExpired: (event) => {
282
- console.error('⏰ Order expired:', event.message);
283
- terminalCall();
284
- },
285
- onErrored: (event) => {
286
- console.error('❌ Order failed:', event.message);
287
- console.error(`📋 Status: ${event.status}`);
288
- terminalCall();
264
+ /**
265
+ * Signs and sends a SWIFT order message that was prepared by CentralServerDrift.
266
+ */
267
+ async function signAndSendSwiftOrderMessage(swiftMessage, wallet, centralServerDrift, swiftServerUrl, orderType) {
268
+ console.log(`\n📝 Signing ${orderType} SWIFT message...`);
269
+ const signedMessage = await (0, openSwiftOrder_1.signSwiftOrderMsg)({
270
+ wallet: {
271
+ signMessage: async (message) => {
272
+ const signature = tweetnacl_1.sign.detached(message, wallet.payer.secretKey);
273
+ return new Uint8Array(signature);
274
+ },
289
275
  },
290
- };
291
- };
276
+ hexEncodedSwiftOrderMessage: swiftMessage.hexEncodedSwiftOrderMessage.uInt8Array,
277
+ expirationTimeMs: swiftMessage.expirationTimeMs,
278
+ });
279
+ console.log('✅ Message signed successfully');
280
+ swiftClient_1.SwiftClient.init(swiftServerUrl, 'common-ts-cli');
281
+ const swiftOrderObservable = (0, openSwiftOrder_1.sendSwiftOrder)({
282
+ driftClient: centralServerDrift.driftClient,
283
+ marketId: types_1.MarketId.createPerpMarket(swiftMessage.marketIndex),
284
+ hexEncodedSwiftOrderMessageString: swiftMessage.hexEncodedSwiftOrderMessage.string,
285
+ signedMessage,
286
+ signedMsgOrderUuid: swiftMessage.signedMsgOrderUuid,
287
+ takerAuthority: wallet.publicKey,
288
+ signingAuthority: wallet.publicKey,
289
+ slotsTillAuctionEnd: swiftMessage.slotsTillAuctionEnd,
290
+ });
291
+ await new Promise((resolve, reject) => {
292
+ const subscription = swiftOrderObservable.subscribe({
293
+ next: (event) => {
294
+ if (event.type === 'sent') {
295
+ console.log(`✅ ${orderType} Swift order submitted successfully`);
296
+ }
297
+ if (event.type === 'confirmed') {
298
+ console.log('✅ Order confirmed!');
299
+ console.log(`📋 Order ID: ${event.orderId}`);
300
+ console.log(`📋 Hash: ${event.hash}`);
301
+ subscription.unsubscribe();
302
+ resolve();
303
+ }
304
+ if (event.type === 'expired') {
305
+ console.error('⏰ Order expired:', event.message);
306
+ subscription.unsubscribe();
307
+ resolve();
308
+ }
309
+ if (event.type === 'errored') {
310
+ console.error('❌ Order failed:', event.message);
311
+ subscription.unsubscribe();
312
+ resolve();
313
+ }
314
+ },
315
+ error: (err) => {
316
+ console.error('❌ Observable error:', err);
317
+ reject(err);
318
+ },
319
+ });
320
+ });
321
+ console.log('🏁 Order monitoring completed');
322
+ }
292
323
  /**
293
324
  * CLI Command: deposit
294
325
  */
@@ -526,26 +557,16 @@ async function openPerpMarketOrderSwiftCommand(args) {
526
557
  console.log(`🔑 Wallet Public Key: ${wallet.publicKey.toString()}`);
527
558
  console.log('\n👁️ Monitoring order status...');
528
559
  try {
529
- await centralServerDrift.getOpenPerpMarketOrderTxn({
560
+ const swiftMessage = await centralServerDrift.getOpenPerpMarketOrderTxn({
530
561
  userAccountPublicKey: userAccountPubkey,
531
562
  assetType: assetType,
532
563
  marketIndex,
533
564
  direction: directionEnum,
534
565
  amount: amountBN,
535
566
  useSwift: true,
536
- swiftOptions: {
537
- wallet: {
538
- signMessage: async (message) => {
539
- const signature = tweetnacl_1.sign.detached(message, wallet.payer.secretKey);
540
- return new Uint8Array(signature);
541
- },
542
- takerAuthority: wallet.publicKey,
543
- signingAuthority: wallet.publicKey,
544
- },
545
- callbacks: createSwiftOrderCallbacks('Open Perp Order'),
546
- },
547
567
  positionMaxLeverage: 10,
548
568
  });
569
+ await signAndSendSwiftOrderMessage(swiftMessage, wallet, centralServerDrift, swiftServerUrl, 'Open Perp Order');
549
570
  console.log('✅ [CLI] Swift order finished');
550
571
  }
551
572
  catch (error) {
@@ -715,20 +736,8 @@ async function openPerpNonMarketOrderSwiftCommand(args) {
715
736
  console.log(`⚡ Swift Server: ${swiftServerUrl}`);
716
737
  console.log(`🔑 Wallet Public Key: ${wallet.publicKey.toString()}`);
717
738
  try {
718
- const swiftOptions = {
719
- wallet: {
720
- signMessage: async (message) => {
721
- const signature = tweetnacl_1.sign.detached(message, wallet.payer.secretKey);
722
- return new Uint8Array(signature);
723
- },
724
- takerAuthority: wallet.publicKey,
725
- signingAuthority: wallet.publicKey,
726
- },
727
- callbacks: createSwiftOrderCallbacks('Open Perp Non-Market Order'),
728
- };
729
739
  console.log('\n👁️ Monitoring order status...');
730
- // Use the main method - it handles both approaches internally
731
- await centralServerDrift.getOpenPerpNonMarketOrderTxn({
740
+ const swiftMessage = await centralServerDrift.getOpenPerpNonMarketOrderTxn({
732
741
  userAccountPublicKey: userAccountPubkey,
733
742
  marketIndex,
734
743
  direction: directionEnum,
@@ -738,9 +747,9 @@ async function openPerpNonMarketOrderSwiftCommand(args) {
738
747
  assetType: assetType,
739
748
  amount: amountBN,
740
749
  useSwift: true,
741
- swiftOptions,
742
750
  positionMaxLeverage: 10,
743
751
  });
752
+ await signAndSendSwiftOrderMessage(swiftMessage, wallet, centralServerDrift, swiftServerUrl, 'Open Perp Non-Market Order');
744
753
  console.log('✅ [CLI] Swift order finished');
745
754
  }
746
755
  catch (error) {