Skip to content

Commit

Permalink
Use a signed swift order params type (#1420)
Browse files Browse the repository at this point in the history
* Use a signed swift order params helper
  • Loading branch information
jordy25519 authored Jan 9, 2025
1 parent ae9bf75 commit 35cf587
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 76 deletions.
51 changes: 27 additions & 24 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ import { gprcDriftClientAccountSubscriber } from './accounts/grpcDriftClientAcco
import nacl from 'tweetnacl';
import { Slothash } from './slot/SlothashSubscriber';
import { getOracleId } from './oracles/oracleId';
import { SignedSwiftOrderParams } from './swift/types';

type RemainingAccountParams = {
userAccounts: UserAccount[];
Expand Down Expand Up @@ -5882,23 +5883,30 @@ export class DriftClient {

public signSwiftOrderParamsMessage(
orderParamsMessage: SwiftOrderParamsMessage
): Buffer {
const takerOrderParamsMessage =
this.encodeSwiftOrderParamsMessage(orderParamsMessage);
return this.signMessage(takerOrderParamsMessage);
): SignedSwiftOrderParams {
const borshBuf = this.encodeSwiftOrderParamsMessage(orderParamsMessage);
const orderParams = Buffer.from(borshBuf.toString('hex'));
return {
orderParams,
signature: this.signMessage(Buffer.from(borshBuf.toString('hex'))),
};
}

// encode the swift order for use in program Ix/signing
/*
* Borsh encode swift taker order params
*/
public encodeSwiftOrderParamsMessage(
orderParamsMessage: SwiftOrderParamsMessage
): Buffer {
const borshBuf = this.program.coder.types.encode(
return this.program.coder.types.encode(
'SwiftOrderParamsMessage',
orderParamsMessage
);
return Buffer.from(borshBuf.toString('hex'));
}

/*
* Decode swift taker order params from borsh buffer
*/
public decodeSwiftOrderParamsMessage(
encodedMessage: Buffer
): SwiftOrderParamsMessage {
Expand All @@ -5916,8 +5924,7 @@ export class DriftClient {
}

public async placeSwiftTakerOrder(
swiftOrderParamsMessage: Buffer,
swiftOrderParamsSignature: Buffer,
signedSwiftOrderParams: SignedSwiftOrderParams,
marketIndex: number,
takerInfo: {
taker: PublicKey;
Expand All @@ -5929,8 +5936,7 @@ export class DriftClient {
txParams?: TxParams
): Promise<TransactionSignature> {
const ixs = await this.getPlaceSwiftTakerPerpOrderIxs(
swiftOrderParamsMessage,
swiftOrderParamsSignature,
signedSwiftOrderParams,
marketIndex,
takerInfo,
undefined,
Expand All @@ -5946,8 +5952,7 @@ export class DriftClient {
}

public async getPlaceSwiftTakerPerpOrderIxs(
encodedSwiftOrderParamsMessage: Buffer,
swiftOrderParamsSignature: Buffer,
signedSwiftOrderParams: SignedSwiftOrderParams,
marketIndex: number,
takerInfo: {
taker: PublicKey;
Expand All @@ -5971,13 +5976,15 @@ export class DriftClient {
const authorityToUse = authority || takerInfo.takerUserAccount.authority;

const messageLengthBuffer = Buffer.alloc(2);
messageLengthBuffer.writeUInt16LE(encodedSwiftOrderParamsMessage.length);
messageLengthBuffer.writeUInt16LE(
signedSwiftOrderParams.orderParams.length
);

const swiftIxData = Buffer.concat([
swiftOrderParamsSignature,
signedSwiftOrderParams.signature,
authorityToUse.toBytes(),
messageLengthBuffer,
encodedSwiftOrderParamsMessage,
signedSwiftOrderParams.orderParams,
]);

const swiftOrderParamsSignatureIx = createMinimalEd25519VerifyIx(
Expand Down Expand Up @@ -6007,8 +6014,7 @@ export class DriftClient {
}

public async placeAndMakeSwiftPerpOrder(
encodedSwiftOrderParamsMessage: Buffer,
swiftOrderParamsSignature: Buffer,
signedSwiftOrderParams: SignedSwiftOrderParams,
swiftOrderUuid: Uint8Array,
takerInfo: {
taker: PublicKey;
Expand All @@ -6023,8 +6029,7 @@ export class DriftClient {
overrideIxCount?: number
): Promise<TransactionSignature> {
const ixs = await this.getPlaceAndMakeSwiftPerpOrderIxs(
encodedSwiftOrderParamsMessage,
swiftOrderParamsSignature,
signedSwiftOrderParams,
swiftOrderUuid,
takerInfo,
orderParams,
Expand All @@ -6044,8 +6049,7 @@ export class DriftClient {
}

public async getPlaceAndMakeSwiftPerpOrderIxs(
encodedSwiftOrderParamsMessage: Buffer,
swiftOrderParamsSignature: Buffer,
signedSwiftOrderParams: SignedSwiftOrderParams,
swiftOrderUuid: Uint8Array,
takerInfo: {
taker: PublicKey;
Expand All @@ -6060,8 +6064,7 @@ export class DriftClient {
): Promise<TransactionInstruction[]> {
const [swiftOrderSignatureIx, placeTakerSwiftPerpOrderIx] =
await this.getPlaceSwiftTakerPerpOrderIxs(
encodedSwiftOrderParamsMessage,
swiftOrderParamsSignature,
signedSwiftOrderParams,
orderParams.marketIndex,
takerInfo,
undefined,
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -14651,5 +14651,8 @@
"name": "InvalidLiquidateSpotWithSwap",
"msg": "InvalidLiquidateSpotWithSwap"
}
]
],
"metadata": {
"address": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
}
}
12 changes: 5 additions & 7 deletions sdk/src/swift/swiftOrderSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class SwiftOrderSubscriber {
const order = JSON.parse(message['order']);
const swiftOrderParamsBuf = Buffer.from(
order['order_message'],
'base64'
'hex'
);
const swiftOrderParamsMessage: SwiftOrderParamsMessage =
this.driftClient.program.coder.types.decode(
Expand Down Expand Up @@ -153,10 +153,6 @@ export class SwiftOrderSubscriber {
swiftOrderParamsMessage: SwiftOrderParamsMessage,
makerOrderParams: OptionalOrderParams
): Promise<TransactionInstruction[]> {
const swiftOrderParamsBuf = Buffer.from(
orderMessageRaw['order_message'],
'base64'
);
const takerAuthority = new PublicKey(orderMessageRaw['taker_authority']);
const takerUserPubkey = await getUserAccountPublicKey(
this.driftClient.program.programId,
Expand All @@ -167,8 +163,10 @@ export class SwiftOrderSubscriber {
await this.userMap.mustGet(takerUserPubkey.toString())
).getUserAccount();
const ixs = await this.driftClient.getPlaceAndMakeSwiftPerpOrderIxs(
swiftOrderParamsBuf,
Buffer.from(orderMessageRaw['order_signature'], 'base64'),
{
orderParams: orderMessageRaw['order_message'],
signature: Buffer.from(orderMessageRaw['order_signature'], 'base64'),
},
decodeUTF8(orderMessageRaw['uuid']),
{
taker: takerUserPubkey,
Expand Down
16 changes: 16 additions & 0 deletions sdk/src/swift/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Buffer } from 'buffer';

/**
* Represents proof of a swift taker order
* It can be provided to drift program to fill a swift order
*/
export interface SignedSwiftOrderParams {
/**
* The encoded order params that were signed (borsh encoded then hexified).
*/
orderParams: Buffer;
/**
* The signature generated for the orderParams
*/
signature: Buffer;
}
24 changes: 16 additions & 8 deletions sdk/tests/dlob/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,16 @@ function printOrderNode(
slot: number | undefined
) {
console.log(
` . vAMMNode? ${node.isVammNode()},\t${node.order ? getVariant(node.order?.orderType) : '~'
} ${node.order ? getVariant(node.order?.direction) : '~'}\t, slot: ${node.order?.slot.toString() || '~'
}, orderId: ${node.order?.orderId.toString() || '~'},\tnode.getPrice: ${oracle ? node.getPrice(oracle, slot!) : '~'
}, node.price: ${node.order?.price.toString() || '~'}, priceOffset: ${node.order?.oraclePriceOffset.toString() || '~'
} quantity: ${node.order?.baseAssetAmountFilled.toString() || '~'}/${node.order?.baseAssetAmount.toString() || '~'
` . vAMMNode? ${node.isVammNode()},\t${
node.order ? getVariant(node.order?.orderType) : '~'
} ${node.order ? getVariant(node.order?.direction) : '~'}\t, slot: ${
node.order?.slot.toString() || '~'
}, orderId: ${node.order?.orderId.toString() || '~'},\tnode.getPrice: ${
oracle ? node.getPrice(oracle, slot!) : '~'
}, node.price: ${node.order?.price.toString() || '~'}, priceOffset: ${
node.order?.oraclePriceOffset.toString() || '~'
} quantity: ${node.order?.baseAssetAmountFilled.toString() || '~'}/${
node.order?.baseAssetAmount.toString() || '~'
}`
);
}
Expand Down Expand Up @@ -210,7 +215,8 @@ function printBookState(

function printCrossedNodes(n: NodeToFill, slot: number) {
console.log(
`Cross Found, takerExists: ${n.node.order !== undefined}, makerExists: ${n.makerNodes !== undefined
`Cross Found, takerExists: ${n.node.order !== undefined}, makerExists: ${
n.makerNodes !== undefined
}`
);
console.log(
Expand All @@ -232,8 +238,10 @@ function printCrossedNodes(n: NodeToFill, slot: number) {
console.log(
` orderId: ${o.orderId}, ${getVariant(o.orderType)}, ${getVariant(
o.direction
)},\texpired: ${isOrderExpired(o, slot)}, postOnly: ${o.postOnly
}, reduceOnly: ${o.reduceOnly
)},\texpired: ${isOrderExpired(o, slot)}, postOnly: ${
o.postOnly
}, reduceOnly: ${
o.reduceOnly
}, price: ${o.price.toString()}, priceOffset: ${o.oraclePriceOffset.toString()}, baseAmtFileld: ${o.baseAssetAmountFilled.toString()}/${o.baseAssetAmount.toString()}`
);
};
Expand Down
15 changes: 5 additions & 10 deletions tests/placeAndMakeSwiftPerp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('place and make swift order', () => {
immediateOrCancel: true,
});

const takerOrderParamsSig = takerDriftClient.signSwiftOrderParamsMessage(
const signedOrderParams = takerDriftClient.signSwiftOrderParamsMessage(
takerOrderParamsMessage
);

Expand All @@ -251,8 +251,7 @@ describe('place and make swift order', () => {
];
ixs.push(
...(await makerDriftClient.getPlaceAndMakeSwiftPerpOrderIxs(
takerDriftClient.encodeSwiftOrderParamsMessage(takerOrderParamsMessage),
takerOrderParamsSig,
signedOrderParams,
uuid,
{
taker: await takerDriftClient.getUserAccountPublicKey(),
Expand Down Expand Up @@ -366,11 +365,8 @@ describe('place and make swift order', () => {
immediateOrCancel: true,
});

const takerOrderParamsMessageEncoded =
takerDriftClient.encodeSwiftOrderParamsMessage(takerOrderParamsMessage);
const takerOrderParamsSig = takerDriftClient.signMessage(
takerOrderParamsMessageEncoded,
makerDriftClient.wallet.payer
const signedOrderParams = takerDriftClient.signSwiftOrderParamsMessage(
takerOrderParamsMessage
);

const ixs = [
Expand All @@ -380,8 +376,7 @@ describe('place and make swift order', () => {
];
ixs.push(
...(await makerDriftClient.getPlaceAndMakeSwiftPerpOrderIxs(
takerDriftClient.encodeSwiftOrderParamsMessage(takerOrderParamsMessage),
takerOrderParamsSig,
signedOrderParams,
uuid,
{
taker: await takerDriftClient.getUserAccountPublicKey(),
Expand Down
Loading

0 comments on commit 35cf587

Please sign in to comment.