added makeCancelOrderInstruction to instruction.ts as an example of instruction factory function

This commit is contained in:
dd 2021-02-19 09:33:11 -05:00
parent f670846d4d
commit ab1053e596
2 changed files with 56 additions and 24 deletions

View File

@ -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<TransactionSignature> {
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 = []

40
src/instruction.ts Normal file
View File

@ -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 })
}