Skip to Content
DevelopersDrift TS 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 ↗
Parameters:
NameTypeDefault
orderParamsMessageSignedMsgOrderParamsMessage | SignedMsgOrderParamsDelegateMessage
delegateSignerboolean
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(), signature: signature.toString("base64"), taker_authority: driftClient.wallet.publicKey.toBase58(), }), }); if (!response.ok) { throw new Error(`Swift error: ${response.status}`); }

Signed message accounts (delegate flow)

import { getSignedMsgUserAccountPublicKey } from "@drift-labs/sdk"; const pda = getSignedMsgUserAccountPublicKey(driftClient.program.programId, authority);
Function getSignedMsgUserAccountPublicKeyReference ↗
Parameters:
NameTypeDefault
programIdPublicKey
authorityPublicKey
Returns:
PublicKey
const [txSig, signedMsgUserAccount] = await driftClient.initializeSignedMsgUserOrders(authority, 8);
Method DriftClient.initializeSignedMsgUserOrdersReference ↗
Parameters:
NameTypeDefault
authorityPublicKey
numOrdersnumber
txParamsTxParams
Returns:
Promise<[string, PublicKey]>

Decode signed messages

const signedMessage = driftClient.decodeSignedMsgOrderParamsMessage( Buffer.from(orderMessageHex, "hex"), isDelegateSigner );
Method DriftClient.decodeSignedMsgOrderParamsMessageReference ↗
Parameters:
NameTypeDefault
encodedMessageBuffer
delegateSignerboolean
Returns:
SignedMsgOrderParamsMessage | SignedMsgOrderParamsDelegateMessage

Instruction builders (Swift taker + maker)

const ixs = await driftClient.getPlaceSignedMsgTakerPerpOrderIxs( { orderParams: orderMessageHex, signature }, marketIndex, takerInfo );
Method DriftClient.getPlaceSignedMsgTakerPerpOrderIxsReference ↗
Parameters:
NameTypeDefault
signedSignedMsgOrderParamsSignedMsgOrderParams
marketIndexnumber
takerInfo{ taker: PublicKey; takerStats: PublicKey; takerUserAccount: UserAccount; signingAuthority: PublicKey; }
precedingIxsTransactionInstruction[]
overrideCustomIxIndexnumber
Returns:
Promise<TransactionInstruction[]>
const ixs = await driftClient.getPlaceAndMakeSignedMsgPerpOrderIxs( signedMsgOrderParams, signedMsgOrderUuid, takerInfo, makerOrderParams );
Method DriftClient.getPlaceAndMakeSignedMsgPerpOrderIxsReference ↗
Parameters:
NameTypeDefault
signedSignedMsgOrderParamsSignedMsgOrderParams
signedMsgOrderUuidUint8Array<ArrayBufferLike>
takerInfo{ taker: PublicKey; takerStats: PublicKey; takerUserAccount: UserAccount; signingAuthority: PublicKey; }
orderParamsOptionalOrderParams
referrerInfoReferrerInfo
subAccountIdnumber
precedingIxsTransactionInstruction[]
overrideCustomIxIndexnumber
Returns:
Promise<TransactionInstruction[]>

Helpers commonly used in Swift flows

import { generateSignedMsgUuid } from "@drift-labs/sdk"; const uuid = generateSignedMsgUuid();
Function generateSignedMsgUuidReference ↗
Parameters:
This function does not accept any parameters.
Returns:
Uint8Array<ArrayBufferLike>
import { digestSignature } from "@drift-labs/sdk"; const hash = digestSignature(Uint8Array.from(signature));
Function digestSignatureReference ↗
Parameters:
NameTypeDefault
signatureUint8Array<ArrayBufferLike>
Returns:
string
import { getUserStatsAccountPublicKey } from "@drift-labs/sdk"; const userStats = getUserStatsAccountPublicKey( driftClient.program.programId, authority );
Function getUserStatsAccountPublicKeyReference ↗
Parameters:
NameTypeDefault
programIdPublicKey
authorityPublicKey
Returns:
PublicKey
Last updated on