Skip to Content
DevelopersDrift SDKSwift (off-chain signed orders)

Swift (off-chain signed orders)

Swift lets users sign an order message off-chain and submit it to a Swift endpoint. The SDK helps you build and sign the message; the HTTP POST is up to you.

Sign a Swift order message

// `orderParams` is a normal Drift `OrderParams` object. const slot = await driftClient.connection.getSlot(); const orderMessage = { signedMsgOrderParams: orderParams, subAccountId: driftClient.activeSubAccountId, slot, uuid: "<UUID>", stopLossOrderParams: null, takeProfitOrderParams: null, }; const { orderParams: message, signature } = driftClient.signSignedMsgOrderParamsMessage(orderMessage);
Method DriftClient.signSignedMsgOrderParamsMessageReference ↗
ParameterTypeRequired
orderParamsMessage
SignedMsgOrderParamsMessage | SignedMsgOrderParamsDelegateMessage
Yes
delegateSigner
boolean
No
Returns
SignedMsgOrderParams

Submit to the Swift API

const swiftUrl = "https://swift.drift.trade/orders"; const response = await fetch(swiftUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ market_index: orderParams.marketIndex, market_type: "perp", message: message.toString("hex"), signature: signature.toString("hex"), taker_authority: driftClient.wallet.publicKey.toBase58(), }), }); if (!response.ok) { const errorText = await response.text(); throw new Error("Swift error: " + response.status + " " + errorText); }
Example Swift API submitReference ↗
TypeScript docs unavailable for Swift API submit.

Signed message accounts (delegate flow)

import { getSignedMsgUserAccountPublicKey } from "@drift-labs/sdk"; const pda = getSignedMsgUserAccountPublicKey(driftClient.program.programId, authority);
Function getSignedMsgUserAccountPublicKeyReference ↗
ParameterTypeRequired
programId
PublicKey
Yes
authority
PublicKey
Yes
Returns
PublicKey
const [txSig, signedMsgUserAccount] = await driftClient.initializeSignedMsgUserOrders(authority, 8);
Method DriftClient.initializeSignedMsgUserOrdersReference ↗
ParameterTypeRequired
authority
PublicKey
Yes
numOrders
number
Yes
txParams
TxParams
No
Returns
Promise<[string, PublicKey]>

Decode signed messages

const signedMessage = driftClient.decodeSignedMsgOrderParamsMessage( Buffer.from(orderMessageHex, "hex"), isDelegateSigner );
Method DriftClient.decodeSignedMsgOrderParamsMessageReference ↗
ParameterTypeRequired
encodedMessage
Buffer
Yes
delegateSigner
boolean
No
Returns
SignedMsgOrderParamsMessage | SignedMsgOrderParamsDelegateMessage

Instruction builders (Swift taker + maker)

const ixs = await driftClient.getPlaceSignedMsgTakerPerpOrderIxs( { orderParams: orderMessageHex, signature }, marketIndex, takerInfo );
Method DriftClient.getPlaceSignedMsgTakerPerpOrderIxsReference ↗
ParameterTypeRequired
signedSignedMsgOrderParams
SignedMsgOrderParams
Yes
marketIndex
number
Yes
takerInfo
{ taker: PublicKey; takerStats: PublicKey; takerUserAccount: UserAccount; signingAuthority: PublicKey; }
Yes
precedingIxs
TransactionInstruction[]
No
overrideCustomIxIndex
number
No
Returns
Promise<TransactionInstruction[]>
const ixs = await driftClient.getPlaceAndMakeSignedMsgPerpOrderIxs( signedMsgOrderParams, signedMsgOrderUuid, takerInfo, makerOrderParams );
Method DriftClient.getPlaceAndMakeSignedMsgPerpOrderIxsReference ↗
ParameterTypeRequired
signedSignedMsgOrderParams
SignedMsgOrderParams
Yes
signedMsgOrderUuid
Uint8Array<ArrayBufferLike>
Yes
takerInfo
{ taker: PublicKey; takerStats: PublicKey; takerUserAccount: UserAccount; signingAuthority: PublicKey; }
Yes
orderParams
OptionalOrderParams
Yes
referrerInfo
ReferrerInfo
No
subAccountId
number
No
precedingIxs
TransactionInstruction[]
No
overrideCustomIxIndex
number
No
Returns
Promise<TransactionInstruction[]>

Helpers commonly used in Swift flows

import { generateSignedMsgUuid } from "@drift-labs/sdk"; const uuid = generateSignedMsgUuid();
Function generateSignedMsgUuidReference ↗
Returns
Uint8Array<ArrayBufferLike>
import { digestSignature } from "@drift-labs/sdk"; const hash = digestSignature(Uint8Array.from(signature));
Function digestSignatureReference ↗
ParameterTypeRequired
signature
Uint8Array<ArrayBufferLike>
Yes
Returns
string
import { getUserStatsAccountPublicKey } from "@drift-labs/sdk"; const userStats = getUserStatsAccountPublicKey( driftClient.program.programId, authority );
Function getUserStatsAccountPublicKeyReference ↗
ParameterTypeRequired
programId
PublicKey
Yes
authority
PublicKey
Yes
Returns
PublicKey
Last updated on