Skip to content

Commit

Permalink
Add backwards compatibility for NewOrderV3
Browse files Browse the repository at this point in the history
  • Loading branch information
jhlx committed Apr 5, 2022
1 parent 35d39a9 commit f989240
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions packages/serum/src/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ INSTRUCTION_LAYOUT.inner.addVariant(
orderTypeLayout('orderType'),
u64('clientId'),
u16('limit'),
i64('maxTs'),
]),
'newOrderV3',
);
Expand Down Expand Up @@ -117,11 +116,36 @@ INSTRUCTION_LAYOUT.inner.addVariant(
'cancelOrdersByClientIds',
);

export const INSTRUCTION_LAYOUT_V2 = new VersionedLayout(
0,
union(u32('instruction')),
);
INSTRUCTION_LAYOUT_V2.inner.addVariant(
10,
struct([
sideLayout('side'),
u64('limitPrice'),
u64('maxBaseQuantity'),
u64('maxQuoteQuantity'),
selfTradeBehaviorLayout('selfTradeBehavior'),
orderTypeLayout('orderType'),
u64('clientId'),
u16('limit'),
i64('maxTs'),
]),
'newOrderV3',
);

export function encodeInstruction(instruction) {
const b = Buffer.alloc(100);
return b.slice(0, INSTRUCTION_LAYOUT.encode(instruction, b));
}

export function encodeInstructionV2(instruction) {
const b = Buffer.alloc(100);
return b.slice(0, INSTRUCTION_LAYOUT_V2.encode(instruction, b));
}

export function decodeInstruction(message) {
return INSTRUCTION_LAYOUT.decode(message);
}
Expand Down Expand Up @@ -284,10 +308,12 @@ export class DexInstructions {
isWritable: false,
});
}

const encoder = maxTs ? encodeInstructionV2 : encodeInstruction;
return new TransactionInstruction({
keys,
programId,
data: encodeInstruction({
data: encoder({
newOrderV3: {
side,
limitPrice,
Expand Down

0 comments on commit f989240

Please sign in to comment.