From 2f726f3071b9431a6884e0f512c7fc505f90f2f3 Mon Sep 17 00:00:00 2001 From: Nathaniel Parke Date: Wed, 26 Aug 2020 18:27:03 +0800 Subject: [PATCH] Add makePlaceOrderInstruction (#3) --- src/market.ts | 63 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/src/market.ts b/src/market.ts index af8b202..5788c1a 100644 --- a/src/market.ts +++ b/src/market.ts @@ -263,29 +263,58 @@ export class Market { } else { openOrdersAddress = openOrdersAccounts[0].address; } + const placeOrderInstruction = this.makePlaceOrderInstruction( + connection, + { + owner, + payer, + side, + price, + size, + orderType, + clientId, + }, + openOrdersAddress, + ); + transaction.add(placeOrderInstruction); + return { transaction, signers }; + } + + makePlaceOrderInstruction( + connection: Connection, + { + owner, + payer, + side, + price, + size, + orderType = 'limit', + clientId, + }: OrderParams, + openOrdersAddress, + ): TransactionInstruction { + // @ts-ignore + const ownerAddress: PublicKey = owner.publicKey ?? owner; if (this.baseSizeNumberToLots(size).lte(new BN(0))) { throw new Error('size too small'); } if (this.priceNumberToLots(price).lte(new BN(0))) { throw new Error('invalid price'); } - transaction.add( - DexInstructions.newOrder({ - market: this.address, - requestQueue: this._decoded.requestQueue, - baseVault: this._decoded.baseVault, - quoteVault: this._decoded.quoteVault, - openOrders: openOrdersAddress, - owner: ownerAddress, - payer, - side, - limitPrice: this.priceNumberToLots(price), - maxQuantity: this.baseSizeNumberToLots(size), - orderType, - clientId, - }), - ); - return { transaction, signers }; + return DexInstructions.newOrder({ + market: this.address, + requestQueue: this._decoded.requestQueue, + baseVault: this._decoded.baseVault, + quoteVault: this._decoded.quoteVault, + openOrders: openOrdersAddress, + owner: ownerAddress, + payer, + side, + limitPrice: this.priceNumberToLots(price), + maxQuantity: this.baseSizeNumberToLots(size), + orderType, + clientId, + }); } private async _sendTransaction(