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 ↗| Name | Type | Default |
|---|---|---|
orderParamsMessage | SignedMsgOrderParamsMessage | SignedMsgOrderParamsDelegateMessage | |
delegateSigner | boolean |
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 ↗| Name | Type | Default |
|---|---|---|
programId | PublicKey | |
authority | PublicKey |
const [txSig, signedMsgUserAccount] =
await driftClient.initializeSignedMsgUserOrders(authority, 8);Method DriftClient.initializeSignedMsgUserOrdersReference ↗| Name | Type | Default |
|---|---|---|
authority | PublicKey | |
numOrders | number | |
txParams | TxParams |
Decode signed messages
const signedMessage = driftClient.decodeSignedMsgOrderParamsMessage(
Buffer.from(orderMessageHex, "hex"),
isDelegateSigner
);Method DriftClient.decodeSignedMsgOrderParamsMessageReference ↗| Name | Type | Default |
|---|---|---|
encodedMessage | Buffer | |
delegateSigner | boolean |
Instruction builders (Swift taker + maker)
const ixs = await driftClient.getPlaceSignedMsgTakerPerpOrderIxs(
{ orderParams: orderMessageHex, signature },
marketIndex,
takerInfo
);Method DriftClient.getPlaceSignedMsgTakerPerpOrderIxsReference ↗const ixs = await driftClient.getPlaceAndMakeSignedMsgPerpOrderIxs(
signedMsgOrderParams,
signedMsgOrderUuid,
takerInfo,
makerOrderParams
);Method DriftClient.getPlaceAndMakeSignedMsgPerpOrderIxsReference ↗Helpers commonly used in Swift flows
import { generateSignedMsgUuid } from "@drift-labs/sdk";
const uuid = generateSignedMsgUuid();Function generateSignedMsgUuidReference ↗This function does not accept any parameters.
import { digestSignature } from "@drift-labs/sdk";
const hash = digestSignature(Uint8Array.from(signature));Function digestSignatureReference ↗| Name | Type | Default |
|---|---|---|
signature | Uint8Array<ArrayBufferLike> |
import { getUserStatsAccountPublicKey } from "@drift-labs/sdk";
const userStats = getUserStatsAccountPublicKey(
driftClient.program.programId,
authority
);Function getUserStatsAccountPublicKeyReference ↗| Name | Type | Default |
|---|---|---|
programId | PublicKey | |
authority | PublicKey |
Last updated on