Skip to content

Commit

Permalink
program: add cancel_orders_by_ids (#540)
Browse files Browse the repository at this point in the history
* program: add cancelOrdersByIds

* CHANGELOG
  • Loading branch information
crispheaney committed Jul 18, 2023
1 parent 2c61eda commit f44d1dc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: add cancel orders by ids ([#540](https://github.com/drift-labs/protocol-v2/pull/540))
- program: add post only slide for perps ([#541](https://github.com/drift-labs/protocol-v2/pull/541))
- program: allow up to 10000 users

Expand Down
33 changes: 33 additions & 0 deletions programs/drift/src/instructions/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,39 @@ pub fn handle_cancel_order_by_user_id(ctx: Context<CancelOrder>, user_order_id:
Ok(())
}

#[access_control(
exchange_not_paused(&ctx.accounts.state)
)]
pub fn handle_cancel_orders_by_ids(ctx: Context<CancelOrder>, order_ids: Vec<u32>) -> Result<()> {
let clock = &Clock::get()?;
let state = &ctx.accounts.state;

let AccountMaps {
perp_market_map,
spot_market_map,
mut oracle_map,
} = load_maps(
&mut ctx.remaining_accounts.iter().peekable(),
&MarketSet::new(),
&MarketSet::new(),
clock.slot,
Some(state.oracle_guard_rails),
)?;

for order_id in order_ids {
controller::orders::cancel_order_by_order_id(
order_id,
&ctx.accounts.user,
&perp_market_map,
&spot_market_map,
&mut oracle_map,
clock,
)?;
}

Ok(())
}

#[access_control(
exchange_not_paused(&ctx.accounts.state)
)]
Expand Down
4 changes: 4 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pub mod drift {
handle_cancel_orders(ctx, market_type, market_index, direction)
}

pub fn cancel_orders_by_ids(ctx: Context<CancelOrder>, order_ids: Vec<u32>) -> Result<()> {
handle_cancel_orders_by_ids(ctx, order_ids)
}

pub fn modify_order(
ctx: Context<CancelOrder>,
order_id: Option<u32>,
Expand Down
35 changes: 35 additions & 0 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,41 @@ export class DriftClient {
});
}

public async cancelOrdersByIds(
orderIds?: number[],
txParams?: TxParams
): Promise<TransactionSignature> {
const { txSig } = await this.sendTransaction(
await this.buildTransaction(
await this.getCancelOrdersByIdsIx(orderIds),
txParams
),
[],
this.opts
);
return txSig;
}

public async getCancelOrdersByIdsIx(
orderIds?: number[]
): Promise<TransactionInstruction> {
const userAccountPublicKey = await this.getUserAccountPublicKey();

const remainingAccounts = this.getRemainingAccounts({
userAccounts: [this.getUserAccount()],
useMarketLastSlotCache: true,
});

return await this.program.instruction.cancelOrdersByIds(orderIds, {
accounts: {
state: await this.getStatePublicKey(),
user: userAccountPublicKey,
authority: this.wallet.publicKey,
},
remainingAccounts,
});
}

public async cancelOrders(
marketType?: MarketType,
marketIndex?: number,
Expand Down
28 changes: 28 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,34 @@
}
]
},
{
"name": "cancelOrdersByIds",
"accounts": [
{
"name": "state",
"isMut": false,
"isSigner": false
},
{
"name": "user",
"isMut": true,
"isSigner": false
},
{
"name": "authority",
"isMut": false,
"isSigner": true
}
],
"args": [
{
"name": "orderIds",
"type": {
"vec": "u32"
}
}
]
},
{
"name": "modifyOrder",
"accounts": [
Expand Down

0 comments on commit f44d1dc

Please sign in to comment.