From ab1053e5965098cf07f7b47db9eaa8088044ee02 Mon Sep 17 00:00:00 2001 From: dd Date: Fri, 19 Feb 2021 09:33:11 -0500 Subject: [PATCH] added makeCancelOrderInstruction to instruction.ts as an example of instruction factory function --- src/client.ts | 40 ++++++++++++++++------------------------ src/instruction.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 src/instruction.ts diff --git a/src/client.ts b/src/client.ts index ec43eef..c095d1a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -31,6 +31,7 @@ import { Market, OpenOrders, Orderbook } from '@project-serum/serum'; import { SRM_DECIMALS, TOKEN_PROGRAM_ID } from '@project-serum/serum/lib/token-instructions'; import { Order } from '@project-serum/serum/lib/market'; import Wallet from '@project-serum/sol-wallet-adapter'; +import { makeCancelOrderInstruction } from './instruction'; export class MangoGroup { @@ -808,6 +809,7 @@ export class MangoClient { return await this.sendTransaction(connection, transaction, owner, additionalSigners) } + async cancelOrder( connection: Connection, programId: PublicKey, @@ -817,30 +819,20 @@ export class MangoClient { spotMarket: Market, order: Order, ): Promise { - const keys = [ - { isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey}, - { isSigner: true, isWritable: false, pubkey: owner.publicKey }, - { isSigner: false, isWritable: true, pubkey: marginAccount.publicKey }, - { isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }, - { isSigner: false, isWritable: false, pubkey: mangoGroup.dexProgramId }, - { isSigner: false, isWritable: true, pubkey: spotMarket.publicKey }, - { isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].bids }, - { isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].asks }, - { isSigner: false, isWritable: true, pubkey: order.openOrdersAddress }, - { isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey }, - { isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].eventQueue }, - ] - - const data = encodeMangoInstruction({ - CancelOrder: { - side: order.side, - orderId: order.orderId, - } - }) - - - const instruction = new TransactionInstruction( { keys, data, programId }) - + const instruction = makeCancelOrderInstruction( + programId, + mangoGroup.publicKey, + owner.publicKey, + marginAccount.publicKey, + spotMarket.programId, + spotMarket.publicKey, + spotMarket['_decoded'].bids, + spotMarket['_decoded'].asks, + order.openOrdersAddress, + mangoGroup.signerKey, + spotMarket['_decoded'].eventQueue, + order + ) const transaction = new Transaction() transaction.add(instruction) const additionalSigners = [] diff --git a/src/instruction.ts b/src/instruction.ts new file mode 100644 index 0000000..ef74673 --- /dev/null +++ b/src/instruction.ts @@ -0,0 +1,40 @@ +import { PublicKey, SYSVAR_CLOCK_PUBKEY, TransactionInstruction } from '@solana/web3.js'; +import { Order } from '@project-serum/serum/lib/market'; +import { encodeMangoInstruction } from './layout'; + +export function makeCancelOrderInstruction( + programId: PublicKey, + mangoGroupPk: PublicKey, + ownerPk: PublicKey, + marginAccountPk: PublicKey, + dexProgramId: PublicKey, + spotMarketPk: PublicKey, + bidsPk: PublicKey, + asksPk: PublicKey, + openOrdersPk: PublicKey, + signerKey: PublicKey, + eventQueuePk: PublicKey, + order: Order +): TransactionInstruction { + const keys = [ + { isSigner: false, isWritable: true, pubkey: mangoGroupPk}, + { isSigner: true, isWritable: false, pubkey: ownerPk }, + { isSigner: false, isWritable: true, pubkey: marginAccountPk }, + { isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }, + { isSigner: false, isWritable: false, pubkey: dexProgramId }, + { isSigner: false, isWritable: true, pubkey: spotMarketPk }, + { isSigner: false, isWritable: true, pubkey: bidsPk }, + { isSigner: false, isWritable: true, pubkey: asksPk }, + { isSigner: false, isWritable: true, pubkey: openOrdersPk }, + { isSigner: false, isWritable: false, pubkey: signerKey }, + { isSigner: false, isWritable: true, pubkey: eventQueuePk }, + ] + + const data = encodeMangoInstruction({ + CancelOrder: { + side: order.side, + orderId: order.orderId, + } + }) + return new TransactionInstruction( { keys, data, programId }) +} \ No newline at end of file