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 { SRM_DECIMALS, TOKEN_PROGRAM_ID } from '@project-serum/serum/lib/token-instructions';
import { Order } from '@project-serum/serum/lib/market'; import { Order } from '@project-serum/serum/lib/market';
import Wallet from '@project-serum/sol-wallet-adapter'; import Wallet from '@project-serum/sol-wallet-adapter';
import { makeCancelOrderInstruction } from './instruction';
export class MangoGroup { export class MangoGroup {
@ -808,6 +809,7 @@ export class MangoClient {
return await this.sendTransaction(connection, transaction, owner, additionalSigners) return await this.sendTransaction(connection, transaction, owner, additionalSigners)
} }
async cancelOrder( async cancelOrder(
connection: Connection, connection: Connection,
programId: PublicKey, programId: PublicKey,
@ -817,30 +819,20 @@ export class MangoClient {
spotMarket: Market, spotMarket: Market,
order: Order, order: Order,
): Promise<TransactionSignature> { ): Promise<TransactionSignature> {
const keys = [ const instruction = makeCancelOrderInstruction(
{ isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey}, programId,
{ isSigner: true, isWritable: false, pubkey: owner.publicKey }, mangoGroup.publicKey,
{ isSigner: false, isWritable: true, pubkey: marginAccount.publicKey }, owner.publicKey,
{ isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY }, marginAccount.publicKey,
{ isSigner: false, isWritable: false, pubkey: mangoGroup.dexProgramId }, spotMarket.programId,
{ isSigner: false, isWritable: true, pubkey: spotMarket.publicKey }, spotMarket.publicKey,
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].bids }, spotMarket['_decoded'].bids,
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].asks }, spotMarket['_decoded'].asks,
{ isSigner: false, isWritable: true, pubkey: order.openOrdersAddress }, order.openOrdersAddress,
{ isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey }, mangoGroup.signerKey,
{ isSigner: false, isWritable: true, pubkey: spotMarket['_decoded'].eventQueue }, spotMarket['_decoded'].eventQueue,
] order
)
const data = encodeMangoInstruction({
CancelOrder: {
side: order.side,
orderId: order.orderId,
}
})
const instruction = new TransactionInstruction( { keys, data, programId })
const transaction = new Transaction() const transaction = new Transaction()
transaction.add(instruction) transaction.add(instruction)
const additionalSigners = [] 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 })
}