Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add place_orders #221

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions idl/openbook_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,120 @@
"option": "u128"
}
},
{
"name": "placeOrders",
"docs": [
"Place multiple orders"
],
"accounts": [
{
"name": "signer",
"isMut": false,
"isSigner": true
},
{
"name": "openOrdersAccount",
"isMut": true,
"isSigner": false
},
{
"name": "openOrdersAdmin",
"isMut": false,
"isSigner": true,
"isOptional": true
},
{
"name": "userQuoteAccount",
"isMut": true,
"isSigner": false
},
{
"name": "userBaseAccount",
"isMut": true,
"isSigner": false
},
{
"name": "market",
"isMut": true,
"isSigner": false
},
{
"name": "bids",
"isMut": true,
"isSigner": false
},
{
"name": "asks",
"isMut": true,
"isSigner": false
},
{
"name": "eventHeap",
"isMut": true,
"isSigner": false
},
{
"name": "marketQuoteVault",
"isMut": true,
"isSigner": false
},
{
"name": "marketBaseVault",
"isMut": true,
"isSigner": false
},
{
"name": "oracleA",
"isMut": false,
"isSigner": false,
"isOptional": true
},
{
"name": "oracleB",
"isMut": false,
"isSigner": false,
"isOptional": true
},
{
"name": "tokenProgram",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "ordersType",
"type": {
"defined": "PlaceOrderType"
}
},
{
"name": "bids",
"type": {
"vec": {
"defined": "PlaceMultipleOrdersArgs"
}
}
},
{
"name": "asks",
"type": {
"vec": {
"defined": "PlaceMultipleOrdersArgs"
}
}
},
{
"name": "limit",
"type": "u8"
}
],
"returns": {
"vec": {
"option": "u128"
}
}
},
{
"name": "cancelAllAndPlaceOrders",
"docs": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openbook-dex/openbook-v2",
"version": "0.1.8",
"version": "0.1.9",
"description": "Typescript Client for openbook-v2 program.",
"repository": "https://github.com/openbook-dex/openbook-v2/",
"author": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::token_utils::*;
#[allow(clippy::too_many_arguments)]
pub fn cancel_all_and_place_orders(
ctx: Context<CancelAllAndPlaceOrders>,
cancel: bool,
mut orders: Vec<Order>,
limit: u8,
) -> Result<Vec<Option<u128>>> {
Expand Down Expand Up @@ -39,7 +40,9 @@ pub fn cancel_all_and_place_orders(
clock.slot,
)?;

book.cancel_all_orders(&mut open_orders_account, *market, u8::MAX, None)?;
if cancel {
book.cancel_all_orders(&mut open_orders_account, *market, u8::MAX, None)?;
}

let mut base_amount = 0_u64;
let mut quote_amount = 0_u64;
Expand Down
50 changes: 49 additions & 1 deletion programs/openbook-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,54 @@ pub mod openbook_v2 {
Ok(None)
}

/// Place multiple orders
pub fn place_orders(
ctx: Context<CancelAllAndPlaceOrders>,
orders_type: PlaceOrderType,
bids: Vec<PlaceMultipleOrdersArgs>,
asks: Vec<PlaceMultipleOrdersArgs>,
limit: u8,
) -> Result<Vec<Option<u128>>> {
let n_bids = bids.len();

let mut orders = vec![];
for (i, order) in bids.into_iter().chain(asks).enumerate() {
require_gte!(order.price_lots, 1, OpenBookError::InvalidInputPriceLots);

let time_in_force = match Order::tif_from_expiry(order.expiry_timestamp) {
Some(t) => t,
None => {
msg!("Order is already expired");
continue;
}
};
orders.push(Order {
side: if i < n_bids { Side::Bid } else { Side::Ask },
max_base_lots: i64::MIN, // this will be overriden to max_base_lots
max_quote_lots_including_fees: order.max_quote_lots_including_fees,
client_order_id: i as u64,
time_in_force,
self_trade_behavior: SelfTradeBehavior::CancelProvide,
params: match orders_type {
PlaceOrderType::Market => OrderParams::Market,
PlaceOrderType::ImmediateOrCancel => OrderParams::ImmediateOrCancel {
price_lots: order.price_lots,
},
_ => OrderParams::Fixed {
price_lots: order.price_lots,
order_type: orders_type.to_post_order_type()?,
},
},
});
}

#[cfg(feature = "enable-gpl")]
return instructions::cancel_all_and_place_orders(ctx, false, orders, limit);

#[cfg(not(feature = "enable-gpl"))]
Ok(vec![])
}

/// Cancel orders and place multiple orders.
pub fn cancel_all_and_place_orders(
ctx: Context<CancelAllAndPlaceOrders>,
Expand Down Expand Up @@ -298,7 +346,7 @@ pub mod openbook_v2 {
}

#[cfg(feature = "enable-gpl")]
return instructions::cancel_all_and_place_orders(ctx, orders, limit);
return instructions::cancel_all_and_place_orders(ctx, true, orders, limit);

#[cfg(not(feature = "enable-gpl"))]
Ok(vec![])
Expand Down
43 changes: 43 additions & 0 deletions ts/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,49 @@ export class OpenBookV2Client {
return [ix, signers];
}

// Use OrderType from './utils/utils' for orderType
public async placeOrdersIx(
openOrdersPublicKey: PublicKey,
marketPublicKey: PublicKey,
market: MarketAccount,
userBaseAccount: PublicKey,
userQuoteAccount: PublicKey,
openOrdersAdmin: PublicKey | null,
orderType: PlaceOrderType,
bids: PlaceMultipleOrdersArgs[],
asks: PlaceMultipleOrdersArgs[],
limit: number = 12,
openOrdersDelegate?: Keypair,
): Promise<[TransactionInstruction, Signer[]]> {
const ix = await this.program.methods
.placeOrders(orderType, bids, asks, limit)
.accounts({
signer:
openOrdersDelegate != null
? openOrdersDelegate.publicKey
: this.walletPk,
asks: market.asks,
bids: market.bids,
marketQuoteVault: market.marketQuoteVault,
marketBaseVault: market.marketBaseVault,
eventHeap: market.eventHeap,
market: marketPublicKey,
openOrdersAccount: openOrdersPublicKey,
oracleA: market.oracleA.key,
oracleB: market.oracleB.key,
userBaseAccount,
userQuoteAccount,
tokenProgram: TOKEN_PROGRAM_ID,
openOrdersAdmin,
})
.instruction();
const signers: Signer[] = [];
if (openOrdersDelegate != null) {
signers.push(openOrdersDelegate);
}
return [ix, signers];
}

public async cancelOrderById(
openOrdersPublicKey: PublicKey,
openOrdersAccount: OpenOrdersAccount,
Expand Down
Loading
Loading