@drift-labs/common 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/clients/swiftClient.d.ts +5 -1
- package/lib/clients/swiftClient.js +17 -7
- package/lib/clients/swiftClient.js.map +1 -1
- package/lib/clients/tvFeed.d.ts +3 -1
- package/lib/clients/tvFeed.js +27 -13
- package/lib/clients/tvFeed.js.map +1 -1
- package/lib/common-ui-utils/commonUiUtils.d.ts +7 -0
- package/lib/common-ui-utils/market.d.ts +6 -1
- package/lib/common-ui-utils/market.js +46 -0
- package/lib/common-ui-utils/market.js.map +1 -1
- package/lib/common-ui-utils/order.d.ts +8 -1
- package/lib/common-ui-utils/order.js +39 -0
- package/lib/common-ui-utils/order.js.map +1 -1
- package/lib/common-ui-utils/user.js +1 -0
- package/lib/common-ui-utils/user.js.map +1 -1
- package/lib/constants/markets.d.ts +1 -0
- package/lib/constants/markets.js +2 -1
- package/lib/constants/markets.js.map +1 -1
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.d.ts +1 -6
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.js +8 -22
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/index.js.map +1 -1
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/types.d.ts +2 -2
- package/lib/drift/Drift/clients/AuthorityDrift/DriftOperations/types.js.map +1 -1
- package/lib/drift/Drift/clients/AuthorityDrift/index.d.ts +1 -2
- package/lib/drift/Drift/clients/AuthorityDrift/index.js.map +1 -1
- package/lib/drift/Drift/clients/CentralServerDrift.d.ts +10 -5
- package/lib/drift/Drift/clients/CentralServerDrift.js +5 -48
- package/lib/drift/Drift/clients/CentralServerDrift.js.map +1 -1
- package/lib/drift/Drift/constants/errors.d.ts +9 -1
- package/lib/drift/Drift/constants/errors.js +13 -2
- package/lib/drift/Drift/constants/errors.js.map +1 -1
- package/lib/drift/Drift/data/PollingDlob.d.ts +0 -20
- package/lib/drift/Drift/data/PollingDlob.js +2 -78
- package/lib/drift/Drift/data/PollingDlob.js.map +1 -1
- package/lib/drift/base/actions/perp/settlePnl.d.ts +3 -3
- package/lib/drift/base/actions/perp/settlePnl.js +3 -3
- package/lib/drift/base/actions/perp/settlePnl.js.map +1 -1
- package/lib/drift/base/actions/spot/index.d.ts +0 -1
- package/lib/drift/base/actions/spot/index.js +0 -1
- package/lib/drift/base/actions/spot/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/dlobServer/index.d.ts +81 -0
- package/lib/drift/base/actions/trade/openPerpOrder/dlobServer/index.js +255 -0
- package/lib/drift/base/actions/trade/openPerpOrder/dlobServer/index.js.map +1 -0
- package/lib/drift/base/actions/trade/openPerpOrder/index.d.ts +2 -0
- package/lib/drift/base/actions/trade/openPerpOrder/index.js +2 -0
- package/lib/drift/base/actions/trade/openPerpOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.d.ts +53 -43
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.js +180 -201
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.d.ts +37 -9
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.js +194 -83
- package/lib/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.d.ts +40 -27
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.js +111 -32
- package/lib/drift/base/actions/trade/openPerpOrder/openSwiftOrder/index.js.map +1 -1
- package/lib/drift/base/actions/trade/openPerpOrder/types.d.ts +64 -0
- package/lib/drift/base/actions/trade/openPerpOrder/types.js +3 -0
- package/lib/drift/base/actions/trade/openPerpOrder/types.js.map +1 -0
- package/lib/drift/base/actions/user/create.js +9 -3
- package/lib/drift/base/actions/user/create.js.map +1 -1
- package/lib/drift/cli.js +114 -71
- package/lib/drift/cli.js.map +1 -1
- package/lib/drift/index.d.ts +1 -0
- package/lib/drift/index.js +1 -0
- package/lib/drift/index.js.map +1 -1
- package/lib/drift/utils/auctionParamsResponseMapper.d.ts +25 -23
- package/lib/drift/utils/auctionParamsResponseMapper.js +4 -6
- package/lib/drift/utils/auctionParamsResponseMapper.js.map +1 -1
- package/lib/drift/utils/index.d.ts +1 -1
- package/lib/drift/utils/index.js +1 -4
- package/lib/drift/utils/index.js.map +1 -1
- package/lib/drift/utils/orderParams.d.ts +3 -28
- package/lib/drift/utils/orderParams.js +9 -1
- package/lib/drift/utils/orderParams.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/types/user.d.ts +1 -0
- package/lib/types/user.js.map +1 -1
- package/lib/utils/fetch.d.ts +1 -0
- package/lib/utils/fetch.js +11 -0
- package/lib/utils/fetch.js.map +1 -0
- package/lib/utils/orderbook/index.d.ts +18 -0
- package/lib/utils/orderbook/index.js +68 -1
- package/lib/utils/orderbook/index.js.map +1 -1
- package/lib/utils/priceImpact.d.ts +14 -0
- package/lib/utils/priceImpact.js +71 -0
- package/lib/utils/priceImpact.js.map +1 -0
- package/package.json +1 -1
- package/lib/drift/base/actions/spot/borrow.d.ts +0 -1
- package/lib/drift/base/actions/spot/borrow.js +0 -8
- package/lib/drift/base/actions/spot/borrow.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.ts"],"names":[],"mappings":";;;AAAA,yCAOyB;AAMzB,mDAAqD;AACrD,kGAI0D;AAC1D,sDAM2B;AAC3B,mDAAmD;AACnD,uEAAoE;AACpE,kEAA6E;AAsD7E;;GAEG;AACH,KAAK,UAAU,0BAA0B,CAAC,EACzC,SAAS,EACT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,CAAC;QAC5B,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,WAAW,CAAC,UAAU;QAClC,mBAAmB,EAAE,WAAW;QAChC,iCAAiC,EAChC,WAAW,CAAC,2BAA2B,CAAC,IAAI,CAAC,WAAW,CAAC;KAC1D,CAAC,CAAC;IAEH,OAAO,sBAE8B,CAAC;AACvC,CAAC,CAAC;AAnGW,QAAA,4BAA4B,gCAmGvC;AAEF;;;;;;GAMG;AACI,MAAM,0BAA0B,GAAG,KAAK,EAC9C,MAEC,EAC2B,EAAE;IAC9B,OAAO,MAAM,gBAAgB,CAAC;QAC7B,GAAG,MAAM;KACT,CAAC,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,0BAA0B,8BAQrC","sourcesContent":["import {\n\tDriftClient,\n\tUser,\n\tBN,\n\tPositionDirection,\n\tOptionalOrderParams,\n\tMarketType,\n} from '@drift-labs/sdk';\nimport {\n\tTransaction,\n\tTransactionInstruction,\n\tVersionedTransaction,\n} from '@solana/web3.js';\nimport { ENUM_UTILS } from '../../../../../../utils';\nimport {\n\tmapAuctionParamsResponse,\n\tServerAuctionParamsResponse,\n\tMappedAuctionParams,\n} from '../../../../../utils/auctionParamsResponseMapper';\nimport {\n\tOptionalTriggerOrderParams,\n\tprepSwiftOrder,\n\tsendSwiftOrder,\n\tSwiftOrderOptions,\n\tSwiftOrderResult,\n} from '../openSwiftOrder';\nimport { MarketId } from '../../../../../../types';\nimport { SwiftClient } from '../../../../../../clients/swiftClient';\nimport { buildNonMarketOrderParams } from '../../../../../utils/orderParams';\n\nexport interface AuctionParamsRequestOptions {\n\t// Optional parameters that can override defaults or provide additional configuration\n\tmaxLeverageSelected?: boolean;\n\tmaxLeverageOrderSize?: BN;\n\treduceOnly?: boolean;\n\tauctionDuration?: number;\n\tauctionStartPriceOffset?: number;\n\tauctionEndPriceOffset?: number;\n\tauctionStartPriceOffsetFrom?: string; // TradeOffsetPrice\n\tauctionEndPriceOffsetFrom?: string; // TradeOffsetPrice\n\tslippageTolerance?: number | 'dynamic';\n\tauctionPriceCaps?: {\n\t\tmin: BN;\n\t\tmax: BN;\n\t};\n\tisOracleOrder?: boolean;\n\tadditionalEndPriceBuffer?: BN;\n\tforceUpToSlippage?: boolean;\n\torderType?: 'market' | 'oracle';\n}\n\nexport type OpenPerpMarketOrderParams<T extends boolean = boolean> = {\n\tdriftClient: DriftClient;\n\tuser: User;\n\tassetType: 'base' | 'quote';\n\tmarketIndex: number;\n\tdirection: PositionDirection;\n\tamount: BN;\n\tauctionParamsOptions?: AuctionParamsRequestOptions;\n\tdlobServerHttpUrl: string;\n\tbracketOrders?: {\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t};\n\tmarketType?: MarketType;\n\tuseSwift: T;\n} & (T extends true\n\t? { swiftOptions: SwiftOrderOptions }\n\t: { swiftOptions?: never });\n\ninterface RegularOrderParams {\n\tdriftClient: DriftClient;\n\tuser: User;\n\tassetType: 'base' | 'quote';\n\tmarketType?: MarketType;\n\tmarketIndex: number;\n\tdirection: PositionDirection;\n\tamount: BN;\n\tauctionParamsOptions?: AuctionParamsRequestOptions;\n\tdlobServerHttpUrl: string;\n}\n\n/**\n * Fetches order parameters from the auction params server\n */\nasync function fetchOrderParamsFromServer({\n\tassetType,\n\tmarketIndex,\n\tmarketType = MarketType.PERP,\n\tdirection,\n\tamount,\n\tdlobServerHttpUrl,\n\tauctionParamsOptions = {},\n}: RegularOrderParams): Promise<OptionalOrderParams> {\n\t// Extract optional parameters (no defaults except for what's required by server)\n\tconst {\n\t\tmaxLeverageSelected,\n\t\tmaxLeverageOrderSize,\n\t\treduceOnly,\n\t\tauctionDuration,\n\t\tauctionStartPriceOffset,\n\t\tauctionEndPriceOffset,\n\t\tauctionStartPriceOffsetFrom,\n\t\tauctionEndPriceOffsetFrom,\n\t\tslippageTolerance,\n\t\tisOracleOrder,\n\t\torderType,\n\t\t...restOptions\n\t} = auctionParamsOptions;\n\n\t// Build URL parameters for server request\n\tconst urlParamsObject: Record<string, string> = {\n\t\t// Required fields\n\t\tassetType,\n\t\tmarketType: ENUM_UTILS.toStr(marketType),\n\t\tmarketIndex: marketIndex.toString(),\n\t\tdirection: ENUM_UTILS.toStr(direction),\n\t\tamount: amount.toString(),\n\t};\n\n\t// Add optional parameters only if they are provided\n\tconst optionalParams = {\n\t\tmaxLeverageSelected,\n\t\tmaxLeverageOrderSize,\n\t\treduceOnly,\n\t\tauctionDuration,\n\t\tauctionStartPriceOffset,\n\t\tauctionEndPriceOffset,\n\t\tauctionStartPriceOffsetFrom,\n\t\tauctionEndPriceOffsetFrom,\n\t\tslippageTolerance,\n\t\tisOracleOrder,\n\t\torderType,\n\t\tadditionalEndPriceBuffer: restOptions.additionalEndPriceBuffer,\n\t\tforceUpToSlippage: restOptions.forceUpToSlippage,\n\t};\n\n\t// Add defined optional parameters\n\tObject.entries(optionalParams).forEach(([key, value]) => {\n\t\tif (value !== undefined) {\n\t\t\turlParamsObject[key] = value.toString();\n\t\t}\n\t});\n\n\tconst urlParams = new URLSearchParams(urlParamsObject);\n\n\t// Get order params from server\n\tconst requestUrl = `${dlobServerHttpUrl}/auctionParams?${urlParams.toString()}`;\n\tconst response = await fetch(requestUrl);\n\n\tif (!response.ok) {\n\t\tthrow new Error(\n\t\t\t`Server responded with ${response.status}: ${response.statusText}`\n\t\t);\n\t}\n\n\tconst serverResponse: ServerAuctionParamsResponse = await response.json();\n\tconst mappedParams: MappedAuctionParams =\n\t\tmapAuctionParamsResponse(serverResponse);\n\n\t// Convert MappedAuctionParams to OptionalOrderParams\n\treturn {\n\t\torderType: mappedParams.orderType,\n\t\tmarketType: mappedParams.marketType,\n\t\tuserOrderId: mappedParams.userOrderId,\n\t\tdirection: mappedParams.direction,\n\t\tbaseAssetAmount: mappedParams.baseAssetAmount,\n\t\tmarketIndex: mappedParams.marketIndex,\n\t\treduceOnly: mappedParams.reduceOnly,\n\t\tpostOnly: mappedParams.postOnly,\n\t\ttriggerPrice: mappedParams.triggerPrice,\n\t\ttriggerCondition: mappedParams.triggerCondition,\n\t\toraclePriceOffset: mappedParams.oraclePriceOffset?.toNumber() || null,\n\t\tauctionDuration: mappedParams.auctionDuration,\n\t\tmaxTs: mappedParams.maxTs,\n\t\tauctionStartPrice: mappedParams.auctionStartPrice,\n\t\tauctionEndPrice: mappedParams.auctionEndPrice,\n\t};\n}\n\n/**\n * Creates and submits a Swift (signed message) order. Only available for perp orders.\n */\nasync function createSwiftOrder({\n\tdriftClient,\n\tuser,\n\tassetType,\n\tmarketIndex,\n\tdirection,\n\tamount,\n\tbracketOrders,\n\tdlobServerHttpUrl,\n\tauctionParamsOptions,\n\tswiftOptions,\n}: Omit<OpenPerpMarketOrderParams, 'useSwift'> & {\n\tswiftOptions: SwiftOrderOptions;\n}): Promise<SwiftOrderResult> {\n\t// Get order parameters from server\n\tconst orderParams = await fetchOrderParamsFromServer({\n\t\tdriftClient,\n\t\tuser,\n\t\tassetType,\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tamount,\n\t\tdlobServerHttpUrl,\n\t\tauctionParamsOptions,\n\t});\n\n\t// Fetch current slot programmatically\n\tconst currentSlot = await driftClient.connection.getSlot();\n\tconst userAccount = user.getUserAccount();\n\n\t// Use the existing prepSwiftOrder helper function\n\tconst { hexEncodedSwiftOrderMessage, signedMsgOrderUuid } = prepSwiftOrder({\n\t\tdriftClient,\n\t\ttakerUserAccount: {\n\t\t\tpubKey: swiftOptions.wallet.publicKey,\n\t\t\tsubAccountId: userAccount.subAccountId,\n\t\t},\n\t\tcurrentSlot,\n\t\tisDelegate: swiftOptions.isDelegate || false,\n\t\torderParams: {\n\t\t\tmain: orderParams,\n\t\t\ttakeProfit: bracketOrders?.takeProfit,\n\t\t\tstopLoss: bracketOrders?.stopLoss,\n\t\t},\n\t\tslotBuffer: swiftOptions.signedMessageOrderSlotBuffer || 30,\n\t});\n\n\t// Sign the message\n\tconst signedMessage = await swiftOptions.wallet.signMessage(\n\t\thexEncodedSwiftOrderMessage.uInt8Array\n\t);\n\n\t// Initialize SwiftClient (required before using sendSwiftOrder)\n\tSwiftClient.init(swiftOptions.swiftServerUrl);\n\n\tconst swiftOrderResult = 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.publicKey,\n\t\tsigningAuthority: swiftOptions.wallet.publicKey,\n\t\tauctionDurationSlot: orderParams.auctionDuration || undefined,\n\t\tswiftConfirmationSlotBuffer: 15,\n\t});\n\n\treturn swiftOrderResult;\n}\n\n/**\n * Creates transaction instructions for opening a perp market order.\n * If swiftOptions is provided, it will create a Swift (signed message) order instead.\n *\n * @param driftClient - The Drift client instance for interacting with the protocol\n * @param user - The user account that will place the order\n * @param assetType - Whether the amount is in base or quote units\n * @param marketIndex - The perp market index to trade\n * @param direction - The direction of the trade (long/short)\n * @param amount - The amount to trade\n * @param dlobServerHttpUrl - Server URL for the auction params endpoint\n * @param auctionParamsOptions - Optional parameters for auction params endpoint and order configuration\n * @param useSwift - Whether to use Swift (signed message) orders instead of regular transactions\n * @param swiftOptions - Options for Swift (signed message) orders. Required if useSwift is true\n *\n * @returns Promise resolving to an array of transaction instructions for regular orders, or empty array for Swift orders\n */\nexport const createOpenPerpMarketOrderIx = async ({\n\tdriftClient,\n\tuser,\n\tassetType,\n\tmarketIndex,\n\tdirection,\n\tamount,\n\tdlobServerHttpUrl,\n\tauctionParamsOptions = {},\n\tuseSwift = false,\n\tswiftOptions,\n}: OpenPerpMarketOrderParams): Promise<TransactionInstruction[]> => {\n\tif (!amount || amount.isZero()) {\n\t\tthrow new Error('Amount must be greater than zero');\n\t}\n\n\t// First, get order parameters from server (same for both Swift and regular orders)\n\tconst orderParams = await fetchOrderParamsFromServer({\n\t\tdriftClient,\n\t\tuser,\n\t\tassetType,\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tamount,\n\t\tdlobServerHttpUrl,\n\t\tauctionParamsOptions,\n\t});\n\n\t// If useSwift is true, use prepSwiftOrder and return empty array\n\tif (useSwift) {\n\t\tif (!swiftOptions) {\n\t\t\tthrow new Error('swiftOptions is required when useSwift is true');\n\t\t}\n\n\t\tconst currentSlot = await driftClient.connection.getSlot();\n\t\tconst userAccount = user.getUserAccount();\n\n\t\t// Use the existing prepSwiftOrder helper function\n\t\tprepSwiftOrder({\n\t\t\tdriftClient,\n\t\t\ttakerUserAccount: {\n\t\t\t\tpubKey: swiftOptions.wallet.publicKey,\n\t\t\t\tsubAccountId: userAccount.subAccountId,\n\t\t\t},\n\t\t\tcurrentSlot,\n\t\t\tisDelegate: swiftOptions.isDelegate || false,\n\t\t\torderParams: {\n\t\t\t\tmain: orderParams,\n\t\t\t\t// TODO: Add support for stopLoss and takeProfit\n\t\t\t},\n\t\t\tslotBuffer: swiftOptions.signedMessageOrderSlotBuffer || 30,\n\t\t});\n\n\t\t// Swift orders don't return transaction instructions\n\t\treturn [];\n\t}\n\n\t// Regular order flow - create transaction instruction\n\tconst placeOrderIx = await driftClient.getPlaceOrdersIx([orderParams]);\n\treturn [placeOrderIx];\n};\n\n/**\n * Creates a complete transaction for opening a perp market order.\n *\n * @param driftClient - The Drift client instance for interacting with the protocol\n * @param user - The user account that will place the order\n * @param marketIndex - The perp market index to trade\n * @param direction - The direction of the trade (long/short)\n * @param amount - The amount to trade\n * @param auctionParamsOptions - Optional parameters for auction params endpoint and order configuration\n * @param dlobServerHttpUrl - Server URL for the auction params endpoint\n *\n * @returns Promise resolving to a built transaction ready for signing (Transaction or VersionedTransaction)\n */\nexport const createOpenPerpMarketOrderTxn = async <T extends boolean>({\n\tdriftClient,\n\tuser,\n\tassetType,\n\tmarketIndex,\n\tdirection,\n\tamount,\n\tdlobServerHttpUrl,\n\tauctionParamsOptions,\n\tbracketOrders,\n\tuseSwift,\n\tswiftOptions,\n}: OpenPerpMarketOrderParams<T>): Promise<\n\tT extends true ? SwiftOrderResult : Transaction | VersionedTransaction\n> => {\n\tif (!amount || amount.isZero()) {\n\t\tthrow new Error('Amount must be greater than zero');\n\t}\n\n\t// First, get order parameters from server (same for both Swift and regular orders)\n\tconst orderParams = await fetchOrderParamsFromServer({\n\t\tdriftClient,\n\t\tuser,\n\t\tassetType,\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tamount,\n\t\tdlobServerHttpUrl,\n\t\tauctionParamsOptions,\n\t});\n\n\t// If useSwift is true, return the Swift result directly\n\tif (useSwift) {\n\t\tif (!swiftOptions) {\n\t\t\tthrow new Error('swiftOptions is required when useSwift is true');\n\t\t}\n\t\treturn (await createSwiftOrder({\n\t\t\tdriftClient,\n\t\t\tuser,\n\t\t\tassetType,\n\t\t\tmarketIndex,\n\t\t\tdirection,\n\t\t\tamount,\n\t\t\tdlobServerHttpUrl,\n\t\t\tbracketOrders,\n\t\t\tauctionParamsOptions,\n\t\t\tswiftOptions,\n\t\t})) as T extends true\n\t\t\t? SwiftOrderResult\n\t\t\t: Transaction | VersionedTransaction;\n\t}\n\n\tconst allOrders: OptionalOrderParams[] = [orderParams];\n\n\tif (bracketOrders?.takeProfit) {\n\t\tconst takeProfitParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: bracketOrders.takeProfit.direction,\n\t\t\tbaseAssetAmount: amount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'takeProfit',\n\t\t\t\ttriggerPrice: bracketOrders.takeProfit.triggerPrice,\n\t\t\t},\n\t\t\treduceOnly: true,\n\t\t});\n\t\tallOrders.push(takeProfitParams);\n\t}\n\n\tif (bracketOrders?.stopLoss) {\n\t\tconst stopLossParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: bracketOrders.stopLoss.direction,\n\t\t\tbaseAssetAmount: amount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'stopLoss',\n\t\t\t\ttriggerPrice: bracketOrders.stopLoss.triggerPrice,\n\t\t\t},\n\t\t\treduceOnly: true,\n\t\t});\n\t\tallOrders.push(stopLossParams);\n\t}\n\n\t// Regular order flow - create transaction instruction and build transaction\n\tconst placeOrderIx = await driftClient.getPlaceOrdersIx(allOrders);\n\tconst openPerpMarketOrderTxn = await driftClient.txHandler.buildTransaction({\n\t\tinstructions: [placeOrderIx],\n\t\ttxVersion: 0,\n\t\tconnection: driftClient.connection,\n\t\tpreFlightCommitment: 'confirmed',\n\t\tfetchAllMarketLookupTableAccounts:\n\t\t\tdriftClient.fetchAllLookupTableAccounts.bind(driftClient),\n\t});\n\n\treturn openPerpMarketOrderTxn as T extends true\n\t\t? SwiftOrderResult\n\t\t: Transaction | VersionedTransaction;\n};\n\n/**\n * Creates a Swift (signed message) order directly.\n * This is a convenience function for when you only want to create Swift orders.\n *\n * @param params - All the parameters needed for creating a Swift order\n * @returns Promise resolving to SwiftOrderResult with observable and order UUID\n */\nexport const createSwiftPerpMarketOrder = async (\n\tparams: Omit<OpenPerpMarketOrderParams, 'useSwift'> & {\n\t\tswiftOptions: SwiftOrderOptions;\n\t}\n): Promise<SwiftOrderResult> => {\n\treturn await createSwiftOrder({\n\t\t...params,\n\t});\n};\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/drift/base/actions/trade/openPerpOrder/openPerpMarketOrder/index.ts"],"names":[],"mappings":";;;AAAA,yCAUyB;AAMzB,mDAAqD;AACrD,sDAG2B;AAC3B,kEAA6E;AAC7E,8CAIuB;AACvB,mEAA6E;AAE7E,kEAAyE;AAiCzE;;GAEG;AACI,KAAK,UAAU,sBAAsB,CAAC,EAC5C,WAAW,EACX,IAAI,EACJ,SAAS,EACT,WAAW,EACX,SAAS,EACT,MAAM,EACN,aAAa,EACb,iBAAiB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B,CAAC;IACvC,CAAC;IAED,MAAM,sBAAsB,GAAG,MAAM,IAAA,oCAA4B,EAAC,IAAI,CAAC,CAAC;IAExE,OAAO,sBAE8B,CAAC;AACvC,CAAC,CAAC;AA1BW,QAAA,yBAAyB,6BA0BpC","sourcesContent":["import {\n\tDriftClient,\n\tUser,\n\tBN,\n\tPositionDirection,\n\tOptionalOrderParams,\n\tMarketType,\n\tgetUserStatsAccountPublicKey,\n\tReferrerInfo,\n\tPRICE_PRECISION,\n} from '@drift-labs/sdk';\nimport {\n\tTransaction,\n\tTransactionInstruction,\n\tVersionedTransaction,\n} from '@solana/web3.js';\nimport { ENUM_UTILS } from '../../../../../../utils';\nimport {\n\tprepSignAndSendSwiftOrder,\n\tSwiftOrderOptions,\n} from '../openSwiftOrder';\nimport { buildNonMarketOrderParams } from '../../../../../utils/orderParams';\nimport {\n\tfetchAuctionOrderParams,\n\tfetchTopMakers,\n\tOptionalAuctionParamsRequestInputs,\n} from '../dlobServer';\nimport { ORDER_COMMON_UTILS } from '../../../../../../common-ui-utils/order';\nimport { TxnOrSwiftResult, WithTxnParams } from '../types';\nimport { NoTopMakersError } from '../../../../../Drift/constants/errors';\nimport { PlaceAndTakeParams, OptionalTriggerOrderParams } from '../types';\n\nexport interface OpenPerpMarketOrderBaseParams {\n\tdriftClient: DriftClient;\n\tuser: User;\n\tassetType: 'base' | 'quote';\n\tmarketIndex: number;\n\tdirection: PositionDirection;\n\tamount: BN;\n\tdlobServerHttpUrl: string;\n\t// mainly used for UI order identification\n\tuserOrderId?: number;\n\tplaceAndTake?: PlaceAndTakeParams;\n\toptionalAuctionParamsInputs?: OptionalAuctionParamsRequestInputs;\n\tbracketOrders?: {\n\t\ttakeProfit?: OptionalTriggerOrderParams;\n\t\tstopLoss?: OptionalTriggerOrderParams;\n\t};\n}\n\nexport interface OpenPerpMarketOrderBaseParamsWithSwift\n\textends Omit<OpenPerpMarketOrderBaseParams, 'placeAndTake'> {\n\tswiftOptions: SwiftOrderOptions;\n}\n\nexport interface OpenPerpMarketOrderParams<T extends boolean = boolean>\n\textends OpenPerpMarketOrderBaseParams {\n\tuseSwift: T;\n\tswiftOptions?: T extends true ? SwiftOrderOptions : never;\n\tplaceAndTake?: T extends true ? never : PlaceAndTakeParams;\n}\n\n/**\n * Creates and submits a Swift (signed message) order. Only available for perp orders.\n */\nexport async function createSwiftMarketOrder({\n\tdriftClient,\n\tuser,\n\tassetType,\n\tmarketIndex,\n\tdirection,\n\tamount,\n\tbracketOrders,\n\tdlobServerHttpUrl,\n\toptionalAuctionParamsInputs,\n\tswiftOptions,\n\tuserOrderId = 0,\n}: OpenPerpMarketOrderBaseParamsWithSwift): Promise<void> {\n\tif (amount.isZero()) {\n\t\tthrow new Error('Amount must be greater than zero');\n\t}\n\n\t// Get order parameters from server\n\tconst fetchedOrderParams = await fetchAuctionOrderParams({\n\t\tdriftClient,\n\t\tuser,\n\t\tassetType,\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tamount,\n\t\tdlobServerHttpUrl,\n\t\toptionalAuctionParamsInputs,\n\t});\n\n\tconst oraclePrice = driftClient.getOracleDataForPerpMarket(marketIndex).price;\n\tconst totalQuoteAmount = amount.mul(oraclePrice).div(PRICE_PRECISION);\n\n\tconst bitFlags = ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(\n\t\tmarketIndex,\n\t\tdriftClient,\n\t\tuser,\n\t\ttotalQuoteAmount,\n\t\tdirection\n\t);\n\n\tconst orderParams = {\n\t\t...fetchedOrderParams,\n\t\tuserOrderId,\n\t\tbitFlags,\n\t};\n\n\tconst userAccount = user.getUserAccount();\n\tconst slotBuffer = swiftOptions.signedMessageOrderSlotBuffer || 7;\n\n\tawait prepSignAndSendSwiftOrder({\n\t\tdriftClient,\n\t\tsubAccountId: userAccount.subAccountId,\n\t\tmarketIndex,\n\t\tslotBuffer,\n\t\tswiftOptions,\n\t\torderParams: {\n\t\t\tmain: orderParams,\n\t\t\ttakeProfit: bracketOrders?.takeProfit,\n\t\t\tstopLoss: bracketOrders?.stopLoss,\n\t\t},\n\t});\n}\n\n/**\n * Creates a placeAndTake transaction instruction.\n * Fallbacks to a regular market order if no top makers are found.\n */\nexport const createPlaceAndTakePerpMarketOrderIx = async ({\n\tassetType,\n\tdirection,\n\tdlobServerHttpUrl,\n\tmarketIndex,\n\tdriftClient,\n\tuser,\n\tuserOrderId,\n\tamount,\n\treferrerInfo,\n\tauctionDurationPercentage,\n\toptionalAuctionParamsInputs,\n}: OpenPerpMarketOrderBaseParams & {\n\tdirection: PositionDirection;\n\tdlobServerHttpUrl: string;\n\tmarketIndex: number;\n\tdriftClient: DriftClient;\n\tuser: User;\n\treferrerInfo?: ReferrerInfo;\n\tauctionDurationPercentage?: number;\n}) => {\n\tconst counterPartySide = ENUM_UTILS.match(direction, PositionDirection.LONG)\n\t\t? 'ask'\n\t\t: 'bid';\n\n\tconst [fetchedOrderParams, topMakersResult] = await Promise.all([\n\t\tfetchAuctionOrderParams({\n\t\t\tdriftClient,\n\t\t\tuser,\n\t\t\tassetType,\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection,\n\t\t\tamount,\n\t\t\tdlobServerHttpUrl,\n\t\t\toptionalAuctionParamsInputs,\n\t\t}),\n\t\tfetchTopMakers({\n\t\t\tdlobServerHttpUrl,\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tside: counterPartySide,\n\t\t\tlimit: 4,\n\t\t}),\n\t]);\n\n\tconst oraclePrice = driftClient.getOracleDataForPerpMarket(marketIndex).price;\n\tconst totalQuoteAmount = amount.mul(oraclePrice).div(PRICE_PRECISION);\n\n\tconst bitFlags = ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(\n\t\tmarketIndex,\n\t\tdriftClient,\n\t\tuser,\n\t\ttotalQuoteAmount,\n\t\tdirection\n\t);\n\tfetchedOrderParams.bitFlags = bitFlags;\n\tfetchedOrderParams.userOrderId = userOrderId;\n\n\tif (!topMakersResult || topMakersResult.length === 0) {\n\t\tthrow new NoTopMakersError('No top makers found', fetchedOrderParams);\n\t}\n\n\tconst topMakersInfo = topMakersResult.map((maker) => ({\n\t\tmaker: maker.userAccountPubKey,\n\t\tmakerUserAccount: maker.userAccount,\n\t\tmakerStats: getUserStatsAccountPublicKey(\n\t\t\tdriftClient.program.programId,\n\t\t\tmaker.userAccount.authority\n\t\t),\n\t}));\n\n\tconst placeAndTakeIx = await driftClient.getPlaceAndTakePerpOrderIx(\n\t\tfetchedOrderParams,\n\t\ttopMakersInfo,\n\t\treferrerInfo,\n\t\tundefined,\n\t\tauctionDurationPercentage,\n\t\tuser.getUserAccount().subAccountId\n\t);\n\n\treturn placeAndTakeIx;\n};\n\n/**\n * Creates transaction instructions for opening a perp market order.\n * If swiftOptions is provided, it will create a Swift (signed message) order instead.\n *\n * @param driftClient - The Drift client instance for interacting with the protocol\n * @param user - The user account that will place the order\n * @param assetType - Whether the amount is in base or quote units\n * @param marketIndex - The perp market index to trade\n * @param direction - The direction of the trade (long/short)\n * @param amount - The amount to trade\n * @param dlobServerHttpUrl - Server URL for the auction params endpoint\n * @param optionalAuctionParamsInputs - Optional parameters for auction params endpoint and order configuration\n *\n * @returns Promise resolving to an array of transaction instructions for regular orders\n */\nexport const createOpenPerpMarketOrderIxs = async ({\n\tdriftClient,\n\tuser,\n\tassetType,\n\tmarketIndex,\n\tdirection,\n\tamount,\n\tbracketOrders,\n\tdlobServerHttpUrl,\n\tplaceAndTake,\n\tuserOrderId,\n\toptionalAuctionParamsInputs = {},\n}: OpenPerpMarketOrderBaseParams): Promise<TransactionInstruction[]> => {\n\tif (!amount || amount.isZero()) {\n\t\tthrow new Error('Amount must be greater than zero');\n\t}\n\n\tconst allOrders: OptionalOrderParams[] = [];\n\tconst allIxs: TransactionInstruction[] = [];\n\n\tif (placeAndTake?.enable) {\n\t\ttry {\n\t\t\tconst placeAndTakeIx = await createPlaceAndTakePerpMarketOrderIx({\n\t\t\t\tassetType,\n\t\t\t\tamount,\n\t\t\t\tdirection,\n\t\t\t\tdlobServerHttpUrl,\n\t\t\t\tmarketIndex,\n\t\t\t\tdriftClient,\n\t\t\t\tuser,\n\t\t\t\tuserOrderId,\n\t\t\t\treferrerInfo: placeAndTake.referrerInfo,\n\t\t\t\tauctionDurationPercentage: placeAndTake.auctionDurationPercentage,\n\t\t\t\toptionalAuctionParamsInputs,\n\t\t\t});\n\t\t\tallIxs.push(placeAndTakeIx);\n\t\t} catch (e) {\n\t\t\tif (e instanceof NoTopMakersError) {\n\t\t\t\t// fallback to regular order\n\t\t\t\tallOrders.push(e.orderParams);\n\t\t\t} else {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t}\n\t} else {\n\t\tconst fetchedOrderParams = await fetchAuctionOrderParams({\n\t\t\tdriftClient,\n\t\t\tuser,\n\t\t\tassetType,\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection,\n\t\t\tamount,\n\t\t\tdlobServerHttpUrl,\n\t\t\toptionalAuctionParamsInputs,\n\t\t});\n\n\t\tconst oraclePrice =\n\t\t\tdriftClient.getOracleDataForPerpMarket(marketIndex).price;\n\t\tconst totalQuoteAmount = amount.mul(oraclePrice).div(PRICE_PRECISION);\n\n\t\tconst bitFlags = ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(\n\t\t\tmarketIndex,\n\t\t\tdriftClient,\n\t\t\tuser,\n\t\t\ttotalQuoteAmount,\n\t\t\tdirection\n\t\t);\n\n\t\tconst orderParams = {\n\t\t\t...fetchedOrderParams,\n\t\t\tuserOrderId,\n\t\t\tbitFlags,\n\t\t};\n\n\t\tallOrders.push(orderParams);\n\t}\n\n\tconst bracketOrdersDirection = ENUM_UTILS.match(\n\t\tdirection,\n\t\tPositionDirection.LONG\n\t)\n\t\t? PositionDirection.SHORT\n\t\t: PositionDirection.LONG;\n\n\tif (bracketOrders?.takeProfit) {\n\t\tconst takeProfitParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: bracketOrdersDirection,\n\t\t\tbaseAssetAmount: bracketOrders.takeProfit.baseAssetAmount ?? amount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'takeProfit',\n\t\t\t\ttriggerPrice: bracketOrders.takeProfit.triggerPrice,\n\t\t\t\tlimitPrice: bracketOrders.takeProfit.limitPrice,\n\t\t\t},\n\t\t\treduceOnly: bracketOrders.takeProfit.reduceOnly ?? true,\n\t\t});\n\t\tallOrders.push(takeProfitParams);\n\t}\n\n\tif (bracketOrders?.stopLoss) {\n\t\tconst stopLossParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: bracketOrdersDirection,\n\t\t\tbaseAssetAmount: bracketOrders.stopLoss.baseAssetAmount ?? amount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'stopLoss',\n\t\t\t\ttriggerPrice: bracketOrders.stopLoss.triggerPrice,\n\t\t\t\tlimitPrice: bracketOrders.stopLoss.limitPrice,\n\t\t\t},\n\t\t\treduceOnly: bracketOrders.stopLoss.reduceOnly ?? true,\n\t\t});\n\t\tallOrders.push(stopLossParams);\n\t}\n\n\t// Regular order flow - create transaction instruction\n\tif (allOrders.length > 0) {\n\t\tconst placeOrderIx = await driftClient.getPlaceOrdersIx(allOrders);\n\t\tallIxs.push(placeOrderIx);\n\t}\n\n\treturn allIxs;\n};\n\n/**\n * Creates a complete transaction for opening a perp market order.\n *\n * @param driftClient - The Drift client instance for interacting with the protocol\n * @param user - The user account that will place the order\n * @param marketIndex - The perp market index to trade\n * @param direction - The direction of the trade (long/short)\n * @param amount - The amount to trade\n * @param optionalAuctionParamsInputs - Optional parameters for auction params endpoint and order configuration\n * @param dlobServerHttpUrl - Server URL for the auction params endpoint\n *\n * @returns Promise resolving to a built transaction ready for signing (Transaction or VersionedTransaction)\n */\nexport const createOpenPerpMarketOrderTxn = async (\n\tparams: WithTxnParams<OpenPerpMarketOrderBaseParams>\n): Promise<Transaction | VersionedTransaction> => {\n\tconst { driftClient } = params;\n\n\t// Regular order flow - create transaction instruction and build transaction\n\tconst placeOrderIx = await createOpenPerpMarketOrderIxs(params);\n\tconst openPerpMarketOrderTxn = await driftClient.txHandler.buildTransaction({\n\t\tinstructions: placeOrderIx,\n\t\ttxVersion: 0,\n\t\tconnection: driftClient.connection,\n\t\tpreFlightCommitment: 'confirmed',\n\t\tfetchAllMarketLookupTableAccounts:\n\t\t\tdriftClient.fetchAllLookupTableAccounts.bind(driftClient),\n\t\ttxParams: params.txParams,\n\t});\n\n\treturn openPerpMarketOrderTxn;\n};\n\n/**\n * Creates a transaction or swift order for a perp market order.\n *\n * @param driftClient - The Drift client instance for interacting with the protocol\n * @param user - The user account that will place the order\n * @param marketIndex - The perp market index to trade\n * @param direction - The direction of the trade (long/short)\n * @param amount - The amount to trade\n * @param optionalAuctionParamsInputs - Optional parameters for auction params endpoint and order configuration\n * @param dlobServerHttpUrl - Server URL for the auction params endpoint\n * @param useSwift - Whether to use Swift (signed message) orders instead of regular transactions\n * @param swiftOptions - Options for Swift (signed message) orders. Required if useSwift is true\n * @param userOrderId - The user order id for UI identification\n *\n * @returns Promise resolving to a built transaction ready for signing (Transaction or VersionedTransaction)\n */\nexport const createOpenPerpMarketOrder = async <T extends boolean>(\n\tparams: WithTxnParams<OpenPerpMarketOrderParams<T>>\n): Promise<TxnOrSwiftResult<T>> => {\n\tconst { useSwift, swiftOptions, ...rest } = params;\n\n\t// If useSwift is true, return the Swift result directly\n\tif (useSwift) {\n\t\tif (!swiftOptions) {\n\t\t\tthrow new Error('swiftOptions is required when useSwift is true');\n\t\t}\n\n\t\tconst swiftOrderResult = await createSwiftMarketOrder({\n\t\t\t...rest,\n\t\t\tswiftOptions,\n\t\t});\n\n\t\treturn swiftOrderResult as T extends true\n\t\t\t? void\n\t\t\t: Transaction | VersionedTransaction;\n\t}\n\n\tconst openPerpMarketOrderTxn = await createOpenPerpMarketOrderTxn(rest);\n\n\treturn openPerpMarketOrderTxn as T extends true\n\t\t? void\n\t\t: Transaction | VersionedTransaction;\n};\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { DriftClient, User, BN, PostOnlyParams,
|
|
1
|
+
import { DriftClient, User, BN, PostOnlyParams, PositionDirection } from '@drift-labs/sdk';
|
|
2
2
|
import { Transaction, TransactionInstruction, VersionedTransaction } from '@solana/web3.js';
|
|
3
|
-
import { SwiftOrderOptions
|
|
4
|
-
import { NonMarketOrderParamsConfig } from '
|
|
5
|
-
export interface
|
|
3
|
+
import { SwiftOrderOptions } from '../openSwiftOrder';
|
|
4
|
+
import { TxnOrSwiftResult, LimitOrderParamsOrderConfig, NonMarketOrderParamsConfig, WithTxnParams } from '../types';
|
|
5
|
+
export interface OpenPerpNonMarketOrderBaseParams extends Omit<NonMarketOrderParamsConfig, 'marketType' | 'baseAssetAmount'> {
|
|
6
6
|
driftClient: DriftClient;
|
|
7
7
|
user: User;
|
|
8
8
|
amount?: BN;
|
|
@@ -10,10 +10,38 @@ export interface OpenPerpNonMarketOrderParams<T extends boolean = boolean> exten
|
|
|
10
10
|
baseAssetAmount?: BN;
|
|
11
11
|
reduceOnly?: boolean;
|
|
12
12
|
postOnly?: PostOnlyParams;
|
|
13
|
-
|
|
13
|
+
userOrderId?: number;
|
|
14
|
+
autoEnterHighLeverageModeBufferPct?: number;
|
|
15
|
+
}
|
|
16
|
+
export interface OpenPerpNonMarketOrderParamsWithSwift extends OpenPerpNonMarketOrderBaseParams {
|
|
17
|
+
swiftOptions: SwiftOrderOptions;
|
|
18
|
+
}
|
|
19
|
+
export interface OpenPerpNonMarketOrderParams<T extends boolean = boolean> extends OpenPerpNonMarketOrderBaseParams {
|
|
20
|
+
useSwift: T;
|
|
14
21
|
swiftOptions?: T extends true ? SwiftOrderOptions : never;
|
|
15
22
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Creates a transaction instruction to open multiple non-market orders.
|
|
25
|
+
*/
|
|
26
|
+
export declare const createMultipleOpenPerpNonMarketOrderIx: (params: {
|
|
27
|
+
driftClient: DriftClient;
|
|
28
|
+
user: User;
|
|
29
|
+
marketIndex: number;
|
|
30
|
+
direction: PositionDirection;
|
|
31
|
+
orderParamsConfigs: NonMarketOrderParamsConfig[];
|
|
32
|
+
enterHighLeverageMode?: boolean;
|
|
33
|
+
}) => Promise<TransactionInstruction>;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a transaction instruction to open a non-market order.
|
|
36
|
+
* Allows for bracket orders to be opened in the same transaction.
|
|
37
|
+
*
|
|
38
|
+
* If `limitAuction` is enabled, a placeAndTake order is created to simulate a market auction order,
|
|
39
|
+
* with the end price being the limit price.
|
|
40
|
+
*/
|
|
41
|
+
export declare const createOpenPerpNonMarketOrderIxs: (params: OpenPerpNonMarketOrderBaseParams) => Promise<TransactionInstruction[]>;
|
|
42
|
+
export declare const MINIMUM_SWIFT_LIMIT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 35;
|
|
43
|
+
export declare const createSwiftLimitOrder: (params: OpenPerpNonMarketOrderParamsWithSwift & {
|
|
44
|
+
orderConfig: LimitOrderParamsOrderConfig;
|
|
45
|
+
}) => Promise<void>;
|
|
46
|
+
export declare const createOpenPerpNonMarketOrderTxn: (params: WithTxnParams<OpenPerpNonMarketOrderBaseParams>) => Promise<Transaction | VersionedTransaction>;
|
|
47
|
+
export declare const createOpenPerpNonMarketOrder: <T extends boolean>(params: WithTxnParams<OpenPerpNonMarketOrderParams<T>>) => Promise<TxnOrSwiftResult<T>>;
|
|
@@ -1,71 +1,209 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createOpenPerpNonMarketOrderTxn = exports.
|
|
6
|
+
exports.createOpenPerpNonMarketOrder = exports.createOpenPerpNonMarketOrderTxn = exports.createSwiftLimitOrder = exports.MINIMUM_SWIFT_LIMIT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = exports.createOpenPerpNonMarketOrderIxs = exports.createMultipleOpenPerpNonMarketOrderIx = void 0;
|
|
4
7
|
const sdk_1 = require("@drift-labs/sdk");
|
|
5
8
|
const openSwiftOrder_1 = require("../openSwiftOrder");
|
|
6
|
-
const types_1 = require("../../../../../../types");
|
|
7
|
-
const swiftClient_1 = require("../../../../../../clients/swiftClient");
|
|
8
9
|
const orderParams_1 = require("../../../../../utils/orderParams");
|
|
9
10
|
const utils_1 = require("../../../../../../utils");
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const dlobServer_1 = require("../dlobServer");
|
|
12
|
+
const common_ui_utils_1 = require("../../../../../../common-ui-utils");
|
|
13
|
+
const openPerpMarketOrder_1 = require("../openPerpMarketOrder");
|
|
14
|
+
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
|
15
|
+
const getLimitAuctionOrderParams = async ({ driftClient, user, marketIndex, direction, baseAssetAmount, userOrderId = 0, reduceOnly = false, postOnly = sdk_1.PostOnlyParams.NONE, orderConfig, }) => {
|
|
16
|
+
const orderParams = await (0, dlobServer_1.fetchAuctionOrderParams)({
|
|
17
|
+
driftClient,
|
|
18
|
+
user,
|
|
19
|
+
assetType: 'base',
|
|
20
|
+
marketIndex,
|
|
21
|
+
marketType: sdk_1.MarketType.PERP,
|
|
22
|
+
direction,
|
|
23
|
+
amount: baseAssetAmount,
|
|
24
|
+
dlobServerHttpUrl: orderConfig.limitAuction.dlobServerHttpUrl,
|
|
25
|
+
optionalAuctionParamsInputs: orderConfig.limitAuction.optionalLimitAuctionParams,
|
|
26
|
+
});
|
|
27
|
+
const perpMarketAccount = driftClient.getPerpMarketAccount(marketIndex);
|
|
28
|
+
(0, tiny_invariant_1.default)(perpMarketAccount, 'Perp market account not found');
|
|
29
|
+
(0, tiny_invariant_1.default)(orderConfig.limitAuction.oraclePrice, 'Oracle price not found');
|
|
30
|
+
(0, tiny_invariant_1.default)(orderParams.auctionStartPrice, 'Auction start price not found');
|
|
31
|
+
const oraclePriceBands = orderConfig.limitAuction.oraclePrice
|
|
32
|
+
? (0, sdk_1.oraclePriceBands)(perpMarketAccount, {
|
|
33
|
+
price: orderConfig.limitAuction.oraclePrice,
|
|
34
|
+
})
|
|
35
|
+
: undefined;
|
|
36
|
+
const auctionDuration = common_ui_utils_1.ORDER_COMMON_UTILS.getPerpAuctionDuration(orderConfig.limitPrice.sub(orderParams.auctionStartPrice).abs(), orderConfig.limitAuction.oraclePrice, perpMarketAccount.contractTier);
|
|
37
|
+
const limitAuctionParams = common_ui_utils_1.COMMON_UI_UTILS.getLimitAuctionParams({
|
|
38
|
+
direction,
|
|
39
|
+
inputPrice: sdk_1.BigNum.from(orderConfig.limitPrice, sdk_1.PRICE_PRECISION_EXP),
|
|
40
|
+
startPriceFromSettings: orderParams.auctionStartPrice,
|
|
41
|
+
duration: auctionDuration,
|
|
42
|
+
auctionStartPriceOffset: orderConfig.limitAuction.auctionStartPriceOffset,
|
|
43
|
+
oraclePriceBands,
|
|
44
|
+
});
|
|
45
|
+
const limitAuctionOrderParams = (0, sdk_1.getLimitOrderParams)({
|
|
46
|
+
marketIndex,
|
|
47
|
+
marketType: sdk_1.MarketType.PERP,
|
|
48
|
+
direction,
|
|
49
|
+
baseAssetAmount,
|
|
50
|
+
reduceOnly,
|
|
51
|
+
postOnly,
|
|
52
|
+
price: orderConfig.limitPrice,
|
|
53
|
+
userOrderId,
|
|
54
|
+
...limitAuctionParams,
|
|
55
|
+
});
|
|
56
|
+
const oraclePrice = driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
57
|
+
const totalQuoteAmount = baseAssetAmount
|
|
58
|
+
.mul(oraclePrice)
|
|
59
|
+
.div(sdk_1.PRICE_PRECISION);
|
|
60
|
+
const bitFlags = common_ui_utils_1.ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(marketIndex, driftClient, user, totalQuoteAmount, direction);
|
|
61
|
+
return {
|
|
62
|
+
...limitAuctionOrderParams,
|
|
63
|
+
bitFlags,
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Creates a transaction instruction to open multiple non-market orders.
|
|
68
|
+
*/
|
|
69
|
+
const createMultipleOpenPerpNonMarketOrderIx = async (params) => {
|
|
70
|
+
const { driftClient, orderParamsConfigs } = params;
|
|
71
|
+
const orderParams = orderParamsConfigs.map(orderParams_1.buildNonMarketOrderParams);
|
|
72
|
+
if (params.enterHighLeverageMode && orderParams.length > 0) {
|
|
73
|
+
orderParams[0].bitFlags = sdk_1.OrderParamsBitFlag.UpdateHighLeverageMode;
|
|
74
|
+
}
|
|
75
|
+
const placeOrderIx = await driftClient.getPlaceOrdersIx(orderParams);
|
|
76
|
+
return placeOrderIx;
|
|
77
|
+
};
|
|
78
|
+
exports.createMultipleOpenPerpNonMarketOrderIx = createMultipleOpenPerpNonMarketOrderIx;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a transaction instruction to open a non-market order.
|
|
81
|
+
* Allows for bracket orders to be opened in the same transaction.
|
|
82
|
+
*
|
|
83
|
+
* If `limitAuction` is enabled, a placeAndTake order is created to simulate a market auction order,
|
|
84
|
+
* with the end price being the limit price.
|
|
85
|
+
*/
|
|
86
|
+
const createOpenPerpNonMarketOrderIxs = async (params) => {
|
|
87
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
88
|
+
const { driftClient, user, marketIndex, direction, reduceOnly = false, postOnly = sdk_1.PostOnlyParams.NONE, orderConfig, userOrderId = 0, } = params;
|
|
13
89
|
// Support both new (amount + assetType) and legacy (baseAssetAmount) approaches
|
|
14
90
|
const finalBaseAssetAmount = (0, orderParams_1.resolveBaseAssetAmount)({
|
|
15
91
|
amount: 'amount' in params ? params.amount : undefined,
|
|
16
92
|
assetType: 'assetType' in params ? params.assetType : undefined,
|
|
17
93
|
baseAssetAmount: 'baseAssetAmount' in params ? params.baseAssetAmount : undefined,
|
|
18
|
-
limitPrice: 'limitPrice' in params.orderConfig
|
|
94
|
+
limitPrice: 'limitPrice' in params.orderConfig
|
|
95
|
+
? params.orderConfig.limitPrice
|
|
96
|
+
: undefined,
|
|
19
97
|
});
|
|
20
98
|
if (!finalBaseAssetAmount || finalBaseAssetAmount.isZero()) {
|
|
21
99
|
throw new Error('Final base asset amount must be greater than zero');
|
|
22
100
|
}
|
|
23
101
|
const allOrders = [];
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
102
|
+
const allIxs = [];
|
|
103
|
+
// handle limit auction
|
|
104
|
+
if (orderConfig.orderType === 'limit' && ((_a = orderConfig.limitAuction) === null || _a === void 0 ? void 0 : _a.enable)) {
|
|
105
|
+
const limitAuctionOrderParams = await getLimitAuctionOrderParams({
|
|
106
|
+
...params,
|
|
107
|
+
baseAssetAmount: finalBaseAssetAmount,
|
|
108
|
+
orderConfig: orderConfig,
|
|
109
|
+
});
|
|
110
|
+
let createdPlaceAndTakeIx = false;
|
|
111
|
+
// if it is a limit auction order, we create a placeAndTake order to simulate a market order.
|
|
112
|
+
// this is useful when a limit order is crossing, and we want to achieve the best fill price through a placeAndTake.
|
|
113
|
+
// falls back to limit order with auction params if the placeAndTake order creation fails
|
|
114
|
+
if (limitAuctionOrderParams.auctionDuration &&
|
|
115
|
+
limitAuctionOrderParams.auctionDuration > 0 &&
|
|
116
|
+
((_c = (_b = orderConfig.limitAuction) === null || _b === void 0 ? void 0 : _b.usePlaceAndTake) === null || _c === void 0 ? void 0 : _c.enable)) {
|
|
117
|
+
try {
|
|
118
|
+
const placeAndTakeIx = await (0, openPerpMarketOrder_1.createPlaceAndTakePerpMarketOrderIx)({
|
|
119
|
+
assetType: 'base',
|
|
120
|
+
amount: finalBaseAssetAmount,
|
|
121
|
+
direction,
|
|
122
|
+
dlobServerHttpUrl: orderConfig.limitAuction.dlobServerHttpUrl,
|
|
123
|
+
marketIndex,
|
|
124
|
+
driftClient,
|
|
125
|
+
user,
|
|
126
|
+
userOrderId,
|
|
127
|
+
optionalAuctionParamsInputs: orderConfig.limitAuction.optionalLimitAuctionParams,
|
|
128
|
+
auctionDurationPercentage: orderConfig.limitAuction.usePlaceAndTake.auctionDurationPercentage,
|
|
129
|
+
referrerInfo: orderConfig.limitAuction.usePlaceAndTake.referrerInfo,
|
|
130
|
+
});
|
|
131
|
+
allIxs.push(placeAndTakeIx);
|
|
132
|
+
createdPlaceAndTakeIx = true;
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
console.error('Failed to create placeAndTake order for limit auction order', e);
|
|
136
|
+
createdPlaceAndTakeIx = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// fallback to normal limit order with auction params
|
|
140
|
+
if (!createdPlaceAndTakeIx) {
|
|
141
|
+
allOrders.push(limitAuctionOrderParams);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const orderParams = (0, orderParams_1.buildNonMarketOrderParams)({
|
|
36
146
|
marketIndex,
|
|
37
147
|
marketType: sdk_1.MarketType.PERP,
|
|
38
|
-
direction
|
|
148
|
+
direction,
|
|
39
149
|
baseAssetAmount: finalBaseAssetAmount,
|
|
150
|
+
orderConfig,
|
|
151
|
+
reduceOnly,
|
|
152
|
+
postOnly,
|
|
153
|
+
userOrderId,
|
|
154
|
+
});
|
|
155
|
+
const oraclePrice = driftClient.getOracleDataForPerpMarket(marketIndex).price;
|
|
156
|
+
const totalQuoteAmount = finalBaseAssetAmount
|
|
157
|
+
.mul(oraclePrice)
|
|
158
|
+
.div(sdk_1.PRICE_PRECISION);
|
|
159
|
+
const bitFlags = common_ui_utils_1.ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(marketIndex, driftClient, user, totalQuoteAmount, direction);
|
|
160
|
+
orderParams.bitFlags = bitFlags;
|
|
161
|
+
allOrders.push(orderParams);
|
|
162
|
+
}
|
|
163
|
+
const bracketOrdersDirection = utils_1.ENUM_UTILS.match(direction, sdk_1.PositionDirection.LONG)
|
|
164
|
+
? sdk_1.PositionDirection.SHORT
|
|
165
|
+
: sdk_1.PositionDirection.LONG;
|
|
166
|
+
if ('bracketOrders' in orderConfig && ((_d = orderConfig.bracketOrders) === null || _d === void 0 ? void 0 : _d.takeProfit)) {
|
|
167
|
+
const takeProfitParams = (0, orderParams_1.buildNonMarketOrderParams)({
|
|
168
|
+
marketIndex,
|
|
169
|
+
marketType: sdk_1.MarketType.PERP,
|
|
170
|
+
direction: bracketOrdersDirection,
|
|
171
|
+
baseAssetAmount: (_e = orderConfig.bracketOrders.takeProfit.baseAssetAmount) !== null && _e !== void 0 ? _e : finalBaseAssetAmount,
|
|
40
172
|
orderConfig: {
|
|
41
173
|
orderType: 'takeProfit',
|
|
42
174
|
triggerPrice: orderConfig.bracketOrders.takeProfit.triggerPrice,
|
|
175
|
+
limitPrice: orderConfig.bracketOrders.takeProfit.limitPrice,
|
|
43
176
|
},
|
|
44
|
-
reduceOnly: true,
|
|
177
|
+
reduceOnly: (_f = orderConfig.bracketOrders.takeProfit.reduceOnly) !== null && _f !== void 0 ? _f : true,
|
|
45
178
|
});
|
|
46
179
|
allOrders.push(takeProfitParams);
|
|
47
180
|
}
|
|
48
|
-
if ('bracketOrders' in orderConfig && ((
|
|
181
|
+
if ('bracketOrders' in orderConfig && ((_g = orderConfig.bracketOrders) === null || _g === void 0 ? void 0 : _g.stopLoss)) {
|
|
49
182
|
const stopLossParams = (0, orderParams_1.buildNonMarketOrderParams)({
|
|
50
183
|
marketIndex,
|
|
51
184
|
marketType: sdk_1.MarketType.PERP,
|
|
52
|
-
direction:
|
|
53
|
-
baseAssetAmount: finalBaseAssetAmount,
|
|
185
|
+
direction: bracketOrdersDirection,
|
|
186
|
+
baseAssetAmount: (_h = orderConfig.bracketOrders.stopLoss.baseAssetAmount) !== null && _h !== void 0 ? _h : finalBaseAssetAmount,
|
|
54
187
|
orderConfig: {
|
|
55
188
|
orderType: 'stopLoss',
|
|
56
189
|
triggerPrice: orderConfig.bracketOrders.stopLoss.triggerPrice,
|
|
190
|
+
limitPrice: orderConfig.bracketOrders.stopLoss.limitPrice,
|
|
57
191
|
},
|
|
58
|
-
reduceOnly: true,
|
|
192
|
+
reduceOnly: (_j = orderConfig.bracketOrders.stopLoss.reduceOnly) !== null && _j !== void 0 ? _j : true,
|
|
59
193
|
});
|
|
60
194
|
allOrders.push(stopLossParams);
|
|
61
195
|
}
|
|
62
|
-
|
|
63
|
-
|
|
196
|
+
if (allOrders.length > 0) {
|
|
197
|
+
const placeOrderIx = await driftClient.getPlaceOrdersIx(allOrders);
|
|
198
|
+
allIxs.push(placeOrderIx);
|
|
199
|
+
}
|
|
200
|
+
return allIxs;
|
|
64
201
|
};
|
|
65
|
-
exports.
|
|
66
|
-
|
|
202
|
+
exports.createOpenPerpNonMarketOrderIxs = createOpenPerpNonMarketOrderIxs;
|
|
203
|
+
exports.MINIMUM_SWIFT_LIMIT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 35;
|
|
204
|
+
const createSwiftLimitOrder = async (params) => {
|
|
67
205
|
var _a, _b;
|
|
68
|
-
const { driftClient, user, marketIndex,
|
|
206
|
+
const { driftClient, user, marketIndex, swiftOptions, orderConfig } = params;
|
|
69
207
|
const limitPrice = orderConfig.limitPrice;
|
|
70
208
|
if (limitPrice.isZero()) {
|
|
71
209
|
throw new Error('LIMIT orders require limitPrice');
|
|
@@ -77,85 +215,58 @@ const createSwiftOrder = async (params) => {
|
|
|
77
215
|
baseAssetAmount: 'baseAssetAmount' in params ? params.baseAssetAmount : undefined,
|
|
78
216
|
limitPrice,
|
|
79
217
|
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
marketIndex,
|
|
83
|
-
marketType: sdk_1.MarketType.PERP,
|
|
84
|
-
direction,
|
|
218
|
+
const orderParams = await getLimitAuctionOrderParams({
|
|
219
|
+
...params,
|
|
85
220
|
baseAssetAmount: finalBaseAssetAmount,
|
|
86
|
-
|
|
87
|
-
reduceOnly,
|
|
88
|
-
postOnly: sdk_1.PostOnlyParams.NONE, // we don't allow post only orders for SWIFT
|
|
221
|
+
orderConfig: orderConfig,
|
|
89
222
|
});
|
|
90
|
-
console.log('🔧 Order params:', JSON.stringify(orderParams, null, 2));
|
|
91
|
-
// Fetch current slot programmatically
|
|
92
|
-
const currentSlot = await driftClient.connection.getSlot();
|
|
93
223
|
const userAccount = user.getUserAccount();
|
|
94
|
-
|
|
95
|
-
|
|
224
|
+
const slotBuffer = Math.max((swiftOptions.signedMessageOrderSlotBuffer || 0) +
|
|
225
|
+
(orderParams.auctionDuration || 0), exports.MINIMUM_SWIFT_LIMIT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS); // limit orders require a much larger buffer, to replace the auction duration usually found in market orders
|
|
226
|
+
await (0, openSwiftOrder_1.prepSignAndSendSwiftOrder)({
|
|
96
227
|
driftClient,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
currentSlot,
|
|
102
|
-
isDelegate: swiftOptions.isDelegate || false,
|
|
228
|
+
subAccountId: userAccount.subAccountId,
|
|
229
|
+
marketIndex,
|
|
230
|
+
slotBuffer,
|
|
231
|
+
swiftOptions,
|
|
103
232
|
orderParams: {
|
|
104
233
|
main: orderParams,
|
|
105
234
|
takeProfit: (_a = orderConfig.bracketOrders) === null || _a === void 0 ? void 0 : _a.takeProfit,
|
|
106
235
|
stopLoss: (_b = orderConfig.bracketOrders) === null || _b === void 0 ? void 0 : _b.stopLoss,
|
|
107
236
|
},
|
|
108
|
-
slotBuffer: swiftOptions.signedMessageOrderSlotBuffer || 50,
|
|
109
|
-
});
|
|
110
|
-
// Sign the message
|
|
111
|
-
const signedMessage = await swiftOptions.wallet.signMessage(hexEncodedSwiftOrderMessage.uInt8Array);
|
|
112
|
-
// Initialize SwiftClient (required before using sendSwiftOrder)
|
|
113
|
-
swiftClient_1.SwiftClient.init(swiftOptions.swiftServerUrl);
|
|
114
|
-
// Create a promise-based wrapper for the sendSwiftOrder callback-based API
|
|
115
|
-
const swiftOrderResult = (0, openSwiftOrder_1.sendSwiftOrder)({
|
|
116
|
-
driftClient,
|
|
117
|
-
marketId: types_1.MarketId.createPerpMarket(marketIndex),
|
|
118
|
-
hexEncodedSwiftOrderMessageString: hexEncodedSwiftOrderMessage.string,
|
|
119
|
-
signedMessage,
|
|
120
|
-
signedMsgOrderUuid,
|
|
121
|
-
takerAuthority: swiftOptions.wallet.publicKey,
|
|
122
|
-
signingAuthority: swiftOptions.wallet.publicKey,
|
|
123
|
-
auctionDurationSlot: orderParams.auctionDuration || undefined,
|
|
124
|
-
swiftConfirmationSlotBuffer: 30,
|
|
125
237
|
});
|
|
126
|
-
return swiftOrderResult;
|
|
127
238
|
};
|
|
239
|
+
exports.createSwiftLimitOrder = createSwiftLimitOrder;
|
|
128
240
|
const createOpenPerpNonMarketOrderTxn = async (params) => {
|
|
129
|
-
const { driftClient
|
|
241
|
+
const { driftClient } = params;
|
|
242
|
+
const instructions = await (0, exports.createOpenPerpNonMarketOrderIxs)(params);
|
|
243
|
+
const openPerpNonMarketOrderTxn = await driftClient.buildTransaction(instructions, params.txParams);
|
|
244
|
+
return openPerpNonMarketOrderTxn;
|
|
245
|
+
};
|
|
246
|
+
exports.createOpenPerpNonMarketOrderTxn = createOpenPerpNonMarketOrderTxn;
|
|
247
|
+
const createOpenPerpNonMarketOrder = async (params) => {
|
|
248
|
+
const { swiftOptions, useSwift, orderConfig } = params;
|
|
130
249
|
// If useSwift is true, return the Swift result directly
|
|
131
250
|
if (useSwift) {
|
|
132
|
-
if (!swiftOptions) {
|
|
133
|
-
throw new Error('swiftOptions is required when useSwift is true');
|
|
134
|
-
}
|
|
135
251
|
if (orderConfig.orderType !== 'limit') {
|
|
136
252
|
throw new Error('Only limit orders are supported with Swift');
|
|
137
253
|
}
|
|
254
|
+
if (!swiftOptions) {
|
|
255
|
+
throw new Error('swiftOptions is required when useSwift is true');
|
|
256
|
+
}
|
|
138
257
|
if (params.postOnly &&
|
|
139
258
|
!utils_1.ENUM_UTILS.match(params.postOnly, sdk_1.PostOnlyParams.NONE)) {
|
|
140
259
|
throw new Error('Post only orders are not supported with Swift');
|
|
141
260
|
}
|
|
142
|
-
const swiftOrderResult = await
|
|
261
|
+
const swiftOrderResult = await (0, exports.createSwiftLimitOrder)({
|
|
143
262
|
...params,
|
|
144
263
|
swiftOptions,
|
|
145
264
|
orderConfig,
|
|
146
265
|
});
|
|
147
266
|
return swiftOrderResult;
|
|
148
267
|
}
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
instructions,
|
|
152
|
-
txVersion: 0,
|
|
153
|
-
connection: driftClient.connection,
|
|
154
|
-
preFlightCommitment: 'confirmed',
|
|
155
|
-
fetchAllMarketLookupTableAccounts: driftClient.fetchAllLookupTableAccounts.bind(driftClient),
|
|
156
|
-
txParams: params.txParams,
|
|
157
|
-
});
|
|
158
|
-
return openPerpNonMarketOrderTxn;
|
|
268
|
+
const marketOrderTxn = await (0, exports.createOpenPerpNonMarketOrderTxn)(params);
|
|
269
|
+
return marketOrderTxn;
|
|
159
270
|
};
|
|
160
|
-
exports.
|
|
271
|
+
exports.createOpenPerpNonMarketOrder = createOpenPerpNonMarketOrder;
|
|
161
272
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.ts"],"names":[],"mappings":";;;AAAA,yCASyB;AAMzB,sDAK2B;AAC3B,mDAAmD;AACnD,uEAAoE;AACpE,kEAI0C;AAC1C,mDAAqD;AAmB9C,MAAM,8BAA8B,GAA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CAA4C,CAAC,CAAC;QAC/D,CAAC;QAED,IACC,MAAM,CAAC,QAAQ;YACf,CAAC,kBAAU,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,oBAAc,CAAC,IAAI,CAAC,EACtD,CAAC;YACF,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;QAClE,CAAC;QAED,MAAM,gBAAgB,GAAG,MAAM,gBAAgB,CAAC;YAC/C,GAAG,MAAM;YACT,YAAY;YACZ,WAAW;SACX,CAAC,CAAC;QAEH,OAAO,gBAE8B,CAAC;IACvC,CAAC;IAED,MAAM,YAAY,GAAG,MAAM,IAAA,sCAA8B,EAAC,MAAM,CAAC,CAAC;IAElE,MAAM,yBAAyB,GAC9B,MAAM,WAAW,CAAC,SAAS,CAAC,gBAAgB,CAAC;QAC5C,YAAY;QACZ,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,WAAW,CAAC,UAAU;QAClC,mBAAmB,EAAE,WAAW;QAChC,iCAAiC,EAChC,WAAW,CAAC,2BAA2B,CAAC,IAAI,CAAC,WAAW,CAAC;QAC1D,QAAQ,EAAE,MAAM,CAAC,QAAQ;KACzB,CAAC,CAAC;IAEJ,OAAO,yBAE8B,CAAC;AACvC,CAAC,CAAC;AAnDW,QAAA,+BAA+B,mCAmD1C","sourcesContent":["import {\n\tDriftClient,\n\tUser,\n\tBN,\n\tMarketType,\n\tPostOnlyParams,\n\tgetLimitOrderParams,\n\tTxParams,\n\tOptionalOrderParams,\n} from '@drift-labs/sdk';\nimport {\n\tTransaction,\n\tTransactionInstruction,\n\tVersionedTransaction,\n} from '@solana/web3.js';\nimport {\n\tprepSwiftOrder,\n\tsendSwiftOrder,\n\tSwiftOrderOptions,\n\tSwiftOrderResult,\n} from '../openSwiftOrder';\nimport { MarketId } from '../../../../../../types';\nimport { SwiftClient } from '../../../../../../clients/swiftClient';\nimport {\n\tbuildNonMarketOrderParams,\n\tNonMarketOrderParamsConfig,\n\tresolveBaseAssetAmount,\n} from '../../../../../utils/orderParams';\nimport { ENUM_UTILS } from '../../../../../../utils';\n\nexport interface OpenPerpNonMarketOrderParams<T extends boolean = boolean>\n\textends Omit<NonMarketOrderParamsConfig, 'marketType' | 'baseAssetAmount'> {\n\tdriftClient: DriftClient;\n\tuser: User;\n\t// Either new approach\n\tamount?: BN;\n\tassetType?: 'base' | 'quote';\n\t// Or legacy approach\n\tbaseAssetAmount?: BN;\n\t// Common optional params\n\treduceOnly?: boolean;\n\tpostOnly?: PostOnlyParams;\n\t// Swift\n\tuseSwift?: T;\n\tswiftOptions?: T extends true ? SwiftOrderOptions : never;\n}\n\nexport const createOpenPerpNonMarketOrderIx = async (\n\tparams: Omit<OpenPerpNonMarketOrderParams, 'useSwift' | 'swiftOptions'>\n): Promise<TransactionInstruction> => {\n\tconst {\n\t\tdriftClient,\n\t\tuser: _user,\n\t\tmarketIndex,\n\t\tdirection,\n\t\treduceOnly = false,\n\t\tpostOnly = PostOnlyParams.NONE,\n\t\torderConfig,\n\t} = params;\n\t// Support both new (amount + assetType) and legacy (baseAssetAmount) approaches\n\tconst finalBaseAssetAmount = resolveBaseAssetAmount({\n\t\tamount: 'amount' in params ? params.amount : undefined,\n\t\tassetType: 'assetType' in params ? params.assetType : undefined,\n\t\tbaseAssetAmount:\n\t\t\t'baseAssetAmount' in params ? params.baseAssetAmount : undefined,\n\t\tlimitPrice:\n\t\t\t'limitPrice' in params.orderConfig && params.orderConfig.limitPrice,\n\t});\n\n\tif (!finalBaseAssetAmount || finalBaseAssetAmount.isZero()) {\n\t\tthrow new Error('Final base asset amount must be greater than zero');\n\t}\n\n\tconst allOrders: OptionalOrderParams[] = [];\n\n\tconst orderParams = buildNonMarketOrderParams({\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\torderConfig,\n\t\treduceOnly,\n\t\tpostOnly,\n\t});\n\tallOrders.push(orderParams);\n\n\tif ('bracketOrders' in orderConfig && orderConfig.bracketOrders?.takeProfit) {\n\t\tconst takeProfitParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: orderConfig.bracketOrders.takeProfit.direction,\n\t\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'takeProfit',\n\t\t\t\ttriggerPrice: orderConfig.bracketOrders.takeProfit.triggerPrice,\n\t\t\t},\n\t\t\treduceOnly: true,\n\t\t});\n\t\tallOrders.push(takeProfitParams);\n\t}\n\n\tif ('bracketOrders' in orderConfig && orderConfig.bracketOrders?.stopLoss) {\n\t\tconst stopLossParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: orderConfig.bracketOrders.stopLoss.direction,\n\t\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'stopLoss',\n\t\t\t\ttriggerPrice: orderConfig.bracketOrders.stopLoss.triggerPrice,\n\t\t\t},\n\t\t\treduceOnly: true,\n\t\t});\n\t\tallOrders.push(stopLossParams);\n\t}\n\n\tconst placeOrderIx = await driftClient.getPlaceOrdersIx(allOrders);\n\treturn placeOrderIx;\n};\n\nconst createSwiftOrder = async (\n\tparams: OpenPerpNonMarketOrderParams & {\n\t\tswiftOptions: SwiftOrderOptions;\n\t} & {\n\t\torderConfig: {\n\t\t\torderType: 'limit';\n\t\t\tlimitPrice: BN;\n\t\t};\n\t}\n): Promise<SwiftOrderResult> => {\n\tconst {\n\t\tdriftClient,\n\t\tuser,\n\t\tmarketIndex,\n\t\tdirection,\n\t\treduceOnly = false,\n\t\tswiftOptions,\n\t\torderConfig,\n\t} = params;\n\n\tconst limitPrice = orderConfig.limitPrice;\n\n\tif (limitPrice.isZero()) {\n\t\tthrow new Error('LIMIT orders require limitPrice');\n\t}\n\n\t// Support both new (amount + assetType) and legacy (baseAssetAmount) approaches\n\tconst finalBaseAssetAmount = resolveBaseAssetAmount({\n\t\tamount: 'amount' in params ? params.amount : undefined,\n\t\tassetType: 'assetType' in params ? params.assetType : undefined,\n\t\tbaseAssetAmount:\n\t\t\t'baseAssetAmount' in params ? params.baseAssetAmount : undefined,\n\t\tlimitPrice,\n\t});\n\n\t// Build limit order parameters directly like the UI does\n\tconst orderParams = getLimitOrderParams({\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\tprice: limitPrice,\n\t\treduceOnly,\n\t\tpostOnly: PostOnlyParams.NONE, // we don't allow post only orders for SWIFT\n\t});\n\n\tconsole.log('🔧 Order params:', JSON.stringify(orderParams, null, 2));\n\n\t// Fetch current slot programmatically\n\tconst currentSlot = await driftClient.connection.getSlot();\n\tconst userAccount = user.getUserAccount();\n\n\t// Use the existing prepSwiftOrder helper function\n\tconst { hexEncodedSwiftOrderMessage, signedMsgOrderUuid } = prepSwiftOrder({\n\t\tdriftClient,\n\t\ttakerUserAccount: {\n\t\t\tpubKey: swiftOptions.wallet.publicKey,\n\t\t\tsubAccountId: userAccount.subAccountId,\n\t\t},\n\t\tcurrentSlot,\n\t\tisDelegate: swiftOptions.isDelegate || false,\n\t\torderParams: {\n\t\t\tmain: orderParams,\n\t\t\ttakeProfit: orderConfig.bracketOrders?.takeProfit,\n\t\t\tstopLoss: orderConfig.bracketOrders?.stopLoss,\n\t\t},\n\t\tslotBuffer: swiftOptions.signedMessageOrderSlotBuffer || 50,\n\t});\n\n\t// Sign the message\n\tconst signedMessage = await swiftOptions.wallet.signMessage(\n\t\thexEncodedSwiftOrderMessage.uInt8Array\n\t);\n\n\t// Initialize SwiftClient (required before using sendSwiftOrder)\n\tSwiftClient.init(swiftOptions.swiftServerUrl);\n\n\t// Create a promise-based wrapper for the sendSwiftOrder callback-based API\n\tconst swiftOrderResult = 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.publicKey,\n\t\tsigningAuthority: swiftOptions.wallet.publicKey,\n\t\tauctionDurationSlot: orderParams.auctionDuration || undefined,\n\t\tswiftConfirmationSlotBuffer: 30,\n\t});\n\n\treturn swiftOrderResult;\n};\n\nexport const createOpenPerpNonMarketOrderTxn = async <T extends boolean>(\n\tparams: OpenPerpNonMarketOrderParams<T> & { txParams?: TxParams }\n): Promise<\n\tT extends true ? SwiftOrderResult : Transaction | VersionedTransaction\n> => {\n\tconst { driftClient, swiftOptions, useSwift, orderConfig } = params;\n\n\t// If useSwift is true, return the Swift result directly\n\tif (useSwift) {\n\t\tif (!swiftOptions) {\n\t\t\tthrow new Error('swiftOptions is required when useSwift is true');\n\t\t}\n\n\t\tif (orderConfig.orderType !== 'limit') {\n\t\t\tthrow new Error('Only limit orders are supported with Swift');\n\t\t}\n\n\t\tif (\n\t\t\tparams.postOnly &&\n\t\t\t!ENUM_UTILS.match(params.postOnly, PostOnlyParams.NONE)\n\t\t) {\n\t\t\tthrow new Error('Post only orders are not supported with Swift');\n\t\t}\n\n\t\tconst swiftOrderResult = await createSwiftOrder({\n\t\t\t...params,\n\t\t\tswiftOptions,\n\t\t\torderConfig,\n\t\t});\n\n\t\treturn swiftOrderResult as T extends true\n\t\t\t? SwiftOrderResult\n\t\t\t: Transaction | VersionedTransaction;\n\t}\n\n\tconst instructions = await createOpenPerpNonMarketOrderIx(params);\n\n\tconst openPerpNonMarketOrderTxn =\n\t\tawait driftClient.txHandler.buildTransaction({\n\t\t\tinstructions,\n\t\t\ttxVersion: 0,\n\t\t\tconnection: driftClient.connection,\n\t\t\tpreFlightCommitment: 'confirmed',\n\t\t\tfetchAllMarketLookupTableAccounts:\n\t\t\t\tdriftClient.fetchAllLookupTableAccounts.bind(driftClient),\n\t\t\ttxParams: params.txParams,\n\t\t});\n\n\treturn openPerpNonMarketOrderTxn as T extends true\n\t\t? SwiftOrderResult\n\t\t: Transaction | VersionedTransaction;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/drift/base/actions/trade/openPerpOrder/openPerpNonMarketOrder/index.ts"],"names":[],"mappings":";;;;;;AAAA,yCAcyB;AAMzB,sDAG2B;AAC3B,kEAG0C;AAC1C,mDAAqD;AACrD,8CAAwD;AACxD,uEAG2C;AAC3C,gEAA6E;AAC7E,oEAAuC;AAoCvC,MAAM,0BAA0B,GAAG,KAAK,EAAE,EACzC,WAAW,EACX,IAAI,EACJ,WAAW,EACX,SAAS,EACT,eAAe,EACf,WAAW,GAAG,CAAC,EACf,UAAU,GAAG,KAAK,EAClB,QAAQ,GAAG,oBAAc,CAAC,IAAI,EAC9B,WAAW,GAMX,EAAgC,EAAE;IAClC,MAAM,WAAW,GAAG,MAAM,IAAA,oCAAuB,EAAC;QACjD,WAAW;QACX,IAAI;QACJ,SAAS,EAAE,MAAM;QACjB,WAAW;QACX,UAAU,EAAE,gBAAU,CAAC,IAAI;QAC3B,SAAS;QACT,MAAM,EAAE,eAAe;QACvB,iBAAiB,EAAE,WAAW,CAAC,YAAY,CAAC,iBAAiB;QAC7D,2BAA2B,EAC1B,WAAW,CAAC,YAAY,CAAC,0BAA0B;KACpD,CAAC,CAAC;IAEH,MAAM,iBAAiB,GAAG,WAAW,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;IAExE,IAAA,wBAAS,EAAC,iBAAiB,EAAE,+BAA+B,CAAC,CAAC;IAC9D,IAAA,wBAAS,EAAC,WAAW,CAAC,YAAY,CAAC,WAAW,EAAE,wBAAwB,CAAC,CAAC;IAC1E,IAAA,wBAAS,EAAC,WAAW,CAAC,iBAAiB,EAAE,+BAA+B,CAAC,CAAC;IAE1E,MAAM,gBAAgB,GAAG,WAAW,CAAC,YAAY,CAAC,WAAW;QAC5D,CAAC,CAAC,IAAA,sBAAm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C;YACF,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;QAClE,CAAC;QAED,MAAM,gBAAgB,GAAG,MAAM,IAAA,6BAAqB,EAAC;YACpD,GAAG,MAAM;YACT,YAAY;YACZ,WAAW;SACX,CAAC,CAAC;QAEH,OAAO,gBAAuC,CAAC;IAChD,CAAC;IAED,MAAM,cAAc,GAAG,MAAM,IAAA,uCAA+B,EAAC,MAAM,CAAC,CAAC;IAErE,OAAO,cAAqC,CAAC;AAC9C,CAAC,CAAC;AAlCW,QAAA,4BAA4B,gCAkCvC","sourcesContent":["import {\n\tDriftClient,\n\tUser,\n\tBN,\n\tMarketType,\n\tPostOnlyParams,\n\tgetLimitOrderParams,\n\tOptionalOrderParams,\n\tPRICE_PRECISION_EXP,\n\tBigNum,\n\toraclePriceBands as getOraclePriceBands,\n\tPositionDirection,\n\tOrderParamsBitFlag,\n\tPRICE_PRECISION,\n} from '@drift-labs/sdk';\nimport {\n\tTransaction,\n\tTransactionInstruction,\n\tVersionedTransaction,\n} from '@solana/web3.js';\nimport {\n\tprepSignAndSendSwiftOrder,\n\tSwiftOrderOptions,\n} from '../openSwiftOrder';\nimport {\n\tbuildNonMarketOrderParams,\n\tresolveBaseAssetAmount,\n} from '../../../../../utils/orderParams';\nimport { ENUM_UTILS } from '../../../../../../utils';\nimport { fetchAuctionOrderParams } from '../dlobServer';\nimport {\n\tCOMMON_UI_UTILS,\n\tORDER_COMMON_UTILS,\n} from '../../../../../../common-ui-utils';\nimport { createPlaceAndTakePerpMarketOrderIx } from '../openPerpMarketOrder';\nimport invariant from 'tiny-invariant';\nimport {\n\tTxnOrSwiftResult,\n\tLimitAuctionConfig,\n\tLimitOrderParamsOrderConfig,\n\tNonMarketOrderParamsConfig,\n\tWithTxnParams,\n} from '../types';\n\nexport interface OpenPerpNonMarketOrderBaseParams\n\textends Omit<NonMarketOrderParamsConfig, 'marketType' | 'baseAssetAmount'> {\n\tdriftClient: DriftClient;\n\tuser: User;\n\t// Either new approach\n\tamount?: BN;\n\tassetType?: 'base' | 'quote';\n\t// Or legacy approach\n\tbaseAssetAmount?: BN;\n\t// Common optional params\n\treduceOnly?: boolean;\n\tpostOnly?: PostOnlyParams;\n\tuserOrderId?: number;\n\tautoEnterHighLeverageModeBufferPct?: number;\n}\n\nexport interface OpenPerpNonMarketOrderParamsWithSwift\n\textends OpenPerpNonMarketOrderBaseParams {\n\tswiftOptions: SwiftOrderOptions;\n}\n\nexport interface OpenPerpNonMarketOrderParams<T extends boolean = boolean>\n\textends OpenPerpNonMarketOrderBaseParams {\n\tuseSwift: T;\n\tswiftOptions?: T extends true ? SwiftOrderOptions : never;\n}\n\nconst getLimitAuctionOrderParams = async ({\n\tdriftClient,\n\tuser,\n\tmarketIndex,\n\tdirection,\n\tbaseAssetAmount,\n\tuserOrderId = 0,\n\treduceOnly = false,\n\tpostOnly = PostOnlyParams.NONE,\n\torderConfig,\n}: OpenPerpNonMarketOrderBaseParams & {\n\tbaseAssetAmount: BN;\n\torderConfig: LimitOrderParamsOrderConfig & {\n\t\tlimitAuction: LimitAuctionConfig;\n\t};\n}): Promise<OptionalOrderParams> => {\n\tconst orderParams = await fetchAuctionOrderParams({\n\t\tdriftClient,\n\t\tuser,\n\t\tassetType: 'base',\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tamount: baseAssetAmount,\n\t\tdlobServerHttpUrl: orderConfig.limitAuction.dlobServerHttpUrl,\n\t\toptionalAuctionParamsInputs:\n\t\t\torderConfig.limitAuction.optionalLimitAuctionParams,\n\t});\n\n\tconst perpMarketAccount = driftClient.getPerpMarketAccount(marketIndex);\n\n\tinvariant(perpMarketAccount, 'Perp market account not found');\n\tinvariant(orderConfig.limitAuction.oraclePrice, 'Oracle price not found');\n\tinvariant(orderParams.auctionStartPrice, 'Auction start price not found');\n\n\tconst oraclePriceBands = orderConfig.limitAuction.oraclePrice\n\t\t? getOraclePriceBands(perpMarketAccount, {\n\t\t\t\tprice: orderConfig.limitAuction.oraclePrice,\n\t\t })\n\t\t: undefined;\n\tconst auctionDuration = ORDER_COMMON_UTILS.getPerpAuctionDuration(\n\t\torderConfig.limitPrice.sub(orderParams.auctionStartPrice).abs(),\n\t\torderConfig.limitAuction.oraclePrice,\n\t\tperpMarketAccount.contractTier\n\t);\n\tconst limitAuctionParams = COMMON_UI_UTILS.getLimitAuctionParams({\n\t\tdirection,\n\t\tinputPrice: BigNum.from(orderConfig.limitPrice, PRICE_PRECISION_EXP),\n\t\tstartPriceFromSettings: orderParams.auctionStartPrice,\n\t\tduration: auctionDuration,\n\t\tauctionStartPriceOffset: orderConfig.limitAuction.auctionStartPriceOffset,\n\t\toraclePriceBands,\n\t});\n\n\tconst limitAuctionOrderParams = getLimitOrderParams({\n\t\tmarketIndex,\n\t\tmarketType: MarketType.PERP,\n\t\tdirection,\n\t\tbaseAssetAmount,\n\t\treduceOnly,\n\t\tpostOnly,\n\t\tprice: orderConfig.limitPrice,\n\t\tuserOrderId,\n\t\t...limitAuctionParams,\n\t});\n\n\tconst oraclePrice = driftClient.getOracleDataForPerpMarket(marketIndex).price;\n\tconst totalQuoteAmount = baseAssetAmount\n\t\t.mul(oraclePrice)\n\t\t.div(PRICE_PRECISION);\n\n\tconst bitFlags = ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(\n\t\tmarketIndex,\n\t\tdriftClient,\n\t\tuser,\n\t\ttotalQuoteAmount,\n\t\tdirection\n\t);\n\n\treturn {\n\t\t...limitAuctionOrderParams,\n\t\tbitFlags,\n\t};\n};\n\n/**\n * Creates a transaction instruction to open multiple non-market orders.\n */\nexport const createMultipleOpenPerpNonMarketOrderIx = async (params: {\n\tdriftClient: DriftClient;\n\tuser: User;\n\tmarketIndex: number;\n\tdirection: PositionDirection;\n\torderParamsConfigs: NonMarketOrderParamsConfig[];\n\tenterHighLeverageMode?: boolean;\n}): Promise<TransactionInstruction> => {\n\tconst { driftClient, orderParamsConfigs } = params;\n\n\tconst orderParams = orderParamsConfigs.map(buildNonMarketOrderParams);\n\n\tif (params.enterHighLeverageMode && orderParams.length > 0) {\n\t\torderParams[0].bitFlags = OrderParamsBitFlag.UpdateHighLeverageMode;\n\t}\n\n\tconst placeOrderIx = await driftClient.getPlaceOrdersIx(orderParams);\n\treturn placeOrderIx;\n};\n\n/**\n * Creates a transaction instruction to open a non-market order.\n * Allows for bracket orders to be opened in the same transaction.\n *\n * If `limitAuction` is enabled, a placeAndTake order is created to simulate a market auction order,\n * with the end price being the limit price.\n */\nexport const createOpenPerpNonMarketOrderIxs = async (\n\tparams: OpenPerpNonMarketOrderBaseParams\n): Promise<TransactionInstruction[]> => {\n\tconst {\n\t\tdriftClient,\n\t\tuser,\n\t\tmarketIndex,\n\t\tdirection,\n\t\treduceOnly = false,\n\t\tpostOnly = PostOnlyParams.NONE,\n\t\torderConfig,\n\t\tuserOrderId = 0,\n\t} = params;\n\t// Support both new (amount + assetType) and legacy (baseAssetAmount) approaches\n\tconst finalBaseAssetAmount = resolveBaseAssetAmount({\n\t\tamount: 'amount' in params ? params.amount : undefined,\n\t\tassetType: 'assetType' in params ? params.assetType : undefined,\n\t\tbaseAssetAmount:\n\t\t\t'baseAssetAmount' in params ? params.baseAssetAmount : undefined,\n\t\tlimitPrice:\n\t\t\t'limitPrice' in params.orderConfig\n\t\t\t\t? params.orderConfig.limitPrice\n\t\t\t\t: undefined,\n\t});\n\n\tif (!finalBaseAssetAmount || finalBaseAssetAmount.isZero()) {\n\t\tthrow new Error('Final base asset amount must be greater than zero');\n\t}\n\n\tconst allOrders: OptionalOrderParams[] = [];\n\tconst allIxs: TransactionInstruction[] = [];\n\n\t// handle limit auction\n\tif (orderConfig.orderType === 'limit' && orderConfig.limitAuction?.enable) {\n\t\tconst limitAuctionOrderParams = await getLimitAuctionOrderParams({\n\t\t\t...params,\n\t\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\t\torderConfig: orderConfig as LimitOrderParamsOrderConfig & {\n\t\t\t\tlimitAuction: LimitAuctionConfig;\n\t\t\t},\n\t\t});\n\n\t\tlet createdPlaceAndTakeIx = false;\n\n\t\t// if it is a limit auction order, we create a placeAndTake order to simulate a market order.\n\t\t// this is useful when a limit order is crossing, and we want to achieve the best fill price through a placeAndTake.\n\t\t// falls back to limit order with auction params if the placeAndTake order creation fails\n\t\tif (\n\t\t\tlimitAuctionOrderParams.auctionDuration &&\n\t\t\tlimitAuctionOrderParams.auctionDuration > 0 &&\n\t\t\torderConfig.limitAuction?.usePlaceAndTake?.enable\n\t\t) {\n\t\t\ttry {\n\t\t\t\tconst placeAndTakeIx = await createPlaceAndTakePerpMarketOrderIx({\n\t\t\t\t\tassetType: 'base',\n\t\t\t\t\tamount: finalBaseAssetAmount,\n\t\t\t\t\tdirection,\n\t\t\t\t\tdlobServerHttpUrl: orderConfig.limitAuction.dlobServerHttpUrl,\n\t\t\t\t\tmarketIndex,\n\t\t\t\t\tdriftClient,\n\t\t\t\t\tuser,\n\t\t\t\t\tuserOrderId,\n\t\t\t\t\toptionalAuctionParamsInputs:\n\t\t\t\t\t\torderConfig.limitAuction.optionalLimitAuctionParams,\n\t\t\t\t\tauctionDurationPercentage:\n\t\t\t\t\t\torderConfig.limitAuction.usePlaceAndTake.auctionDurationPercentage,\n\t\t\t\t\treferrerInfo: orderConfig.limitAuction.usePlaceAndTake.referrerInfo,\n\t\t\t\t});\n\t\t\t\tallIxs.push(placeAndTakeIx);\n\t\t\t\tcreatedPlaceAndTakeIx = true;\n\t\t\t} catch (e) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t'Failed to create placeAndTake order for limit auction order',\n\t\t\t\t\te\n\t\t\t\t);\n\t\t\t\tcreatedPlaceAndTakeIx = false;\n\t\t\t}\n\t\t}\n\n\t\t// fallback to normal limit order with auction params\n\t\tif (!createdPlaceAndTakeIx) {\n\t\t\tallOrders.push(limitAuctionOrderParams);\n\t\t}\n\t} else {\n\t\tconst orderParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection,\n\t\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\t\torderConfig,\n\t\t\treduceOnly,\n\t\t\tpostOnly,\n\t\t\tuserOrderId,\n\t\t});\n\n\t\tconst oraclePrice =\n\t\t\tdriftClient.getOracleDataForPerpMarket(marketIndex).price;\n\t\tconst totalQuoteAmount = finalBaseAssetAmount\n\t\t\t.mul(oraclePrice)\n\t\t\t.div(PRICE_PRECISION);\n\n\t\tconst bitFlags = ORDER_COMMON_UTILS.getPerpOrderParamsBitFlags(\n\t\t\tmarketIndex,\n\t\t\tdriftClient,\n\t\t\tuser,\n\t\t\ttotalQuoteAmount,\n\t\t\tdirection\n\t\t);\n\t\torderParams.bitFlags = bitFlags;\n\n\t\tallOrders.push(orderParams);\n\t}\n\n\tconst bracketOrdersDirection = ENUM_UTILS.match(\n\t\tdirection,\n\t\tPositionDirection.LONG\n\t)\n\t\t? PositionDirection.SHORT\n\t\t: PositionDirection.LONG;\n\n\tif ('bracketOrders' in orderConfig && orderConfig.bracketOrders?.takeProfit) {\n\t\tconst takeProfitParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: bracketOrdersDirection,\n\t\t\tbaseAssetAmount:\n\t\t\t\torderConfig.bracketOrders.takeProfit.baseAssetAmount ??\n\t\t\t\tfinalBaseAssetAmount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'takeProfit',\n\t\t\t\ttriggerPrice: orderConfig.bracketOrders.takeProfit.triggerPrice,\n\t\t\t\tlimitPrice: orderConfig.bracketOrders.takeProfit.limitPrice,\n\t\t\t},\n\t\t\treduceOnly: orderConfig.bracketOrders.takeProfit.reduceOnly ?? true,\n\t\t});\n\t\tallOrders.push(takeProfitParams);\n\t}\n\n\tif ('bracketOrders' in orderConfig && orderConfig.bracketOrders?.stopLoss) {\n\t\tconst stopLossParams = buildNonMarketOrderParams({\n\t\t\tmarketIndex,\n\t\t\tmarketType: MarketType.PERP,\n\t\t\tdirection: bracketOrdersDirection,\n\t\t\tbaseAssetAmount:\n\t\t\t\torderConfig.bracketOrders.stopLoss.baseAssetAmount ??\n\t\t\t\tfinalBaseAssetAmount,\n\t\t\torderConfig: {\n\t\t\t\torderType: 'stopLoss',\n\t\t\t\ttriggerPrice: orderConfig.bracketOrders.stopLoss.triggerPrice,\n\t\t\t\tlimitPrice: orderConfig.bracketOrders.stopLoss.limitPrice,\n\t\t\t},\n\t\t\treduceOnly: orderConfig.bracketOrders.stopLoss.reduceOnly ?? true,\n\t\t});\n\t\tallOrders.push(stopLossParams);\n\t}\n\n\tif (allOrders.length > 0) {\n\t\tconst placeOrderIx = await driftClient.getPlaceOrdersIx(allOrders);\n\t\tallIxs.push(placeOrderIx);\n\t}\n\n\treturn allIxs;\n};\n\nexport const MINIMUM_SWIFT_LIMIT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS = 35;\n\nexport const createSwiftLimitOrder = async (\n\tparams: OpenPerpNonMarketOrderParamsWithSwift & {\n\t\torderConfig: LimitOrderParamsOrderConfig;\n\t}\n): Promise<void> => {\n\tconst { driftClient, user, marketIndex, swiftOptions, orderConfig } = params;\n\n\tconst limitPrice = orderConfig.limitPrice;\n\n\tif (limitPrice.isZero()) {\n\t\tthrow new Error('LIMIT orders require limitPrice');\n\t}\n\n\t// Support both new (amount + assetType) and legacy (baseAssetAmount) approaches\n\tconst finalBaseAssetAmount = resolveBaseAssetAmount({\n\t\tamount: 'amount' in params ? params.amount : undefined,\n\t\tassetType: 'assetType' in params ? params.assetType : undefined,\n\t\tbaseAssetAmount:\n\t\t\t'baseAssetAmount' in params ? params.baseAssetAmount : undefined,\n\t\tlimitPrice,\n\t});\n\n\tconst orderParams = await getLimitAuctionOrderParams({\n\t\t...params,\n\t\tbaseAssetAmount: finalBaseAssetAmount,\n\t\torderConfig: orderConfig as LimitOrderParamsOrderConfig & {\n\t\t\tlimitAuction: LimitAuctionConfig;\n\t\t},\n\t});\n\n\tconst userAccount = user.getUserAccount();\n\tconst slotBuffer = Math.max(\n\t\t(swiftOptions.signedMessageOrderSlotBuffer || 0) +\n\t\t\t(orderParams.auctionDuration || 0),\n\t\tMINIMUM_SWIFT_LIMIT_ORDER_SIGNING_EXPIRATION_BUFFER_SLOTS\n\t); // limit orders require a much larger buffer, to replace the auction duration usually found in market orders\n\n\tawait prepSignAndSendSwiftOrder({\n\t\tdriftClient,\n\t\tsubAccountId: userAccount.subAccountId,\n\t\tmarketIndex,\n\t\tslotBuffer,\n\t\tswiftOptions,\n\t\torderParams: {\n\t\t\tmain: orderParams,\n\t\t\ttakeProfit: orderConfig.bracketOrders?.takeProfit,\n\t\t\tstopLoss: orderConfig.bracketOrders?.stopLoss,\n\t\t},\n\t});\n};\n\nexport const createOpenPerpNonMarketOrderTxn = async (\n\tparams: WithTxnParams<OpenPerpNonMarketOrderBaseParams>\n): Promise<Transaction | VersionedTransaction> => {\n\tconst { driftClient } = params;\n\n\tconst instructions = await createOpenPerpNonMarketOrderIxs(params);\n\n\tconst openPerpNonMarketOrderTxn = await driftClient.buildTransaction(\n\t\tinstructions,\n\t\tparams.txParams\n\t);\n\n\treturn openPerpNonMarketOrderTxn;\n};\n\nexport const createOpenPerpNonMarketOrder = async <T extends boolean>(\n\tparams: WithTxnParams<OpenPerpNonMarketOrderParams<T>>\n): Promise<TxnOrSwiftResult<T>> => {\n\tconst { swiftOptions, useSwift, orderConfig } = params;\n\n\t// If useSwift is true, return the Swift result directly\n\tif (useSwift) {\n\t\tif (orderConfig.orderType !== 'limit') {\n\t\t\tthrow new Error('Only limit orders are supported with Swift');\n\t\t}\n\n\t\tif (!swiftOptions) {\n\t\t\tthrow new Error('swiftOptions is required when useSwift is true');\n\t\t}\n\n\t\tif (\n\t\t\tparams.postOnly &&\n\t\t\t!ENUM_UTILS.match(params.postOnly, PostOnlyParams.NONE)\n\t\t) {\n\t\t\tthrow new Error('Post only orders are not supported with Swift');\n\t\t}\n\n\t\tconst swiftOrderResult = await createSwiftLimitOrder({\n\t\t\t...params,\n\t\t\tswiftOptions,\n\t\t\torderConfig,\n\t\t});\n\n\t\treturn swiftOrderResult as TxnOrSwiftResult<T>;\n\t}\n\n\tconst marketOrderTxn = await createOpenPerpNonMarketOrderTxn(params);\n\n\treturn marketOrderTxn as TxnOrSwiftResult<T>;\n};\n"]}
|