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

program: add cancelOrdersByIds #540

Merged
merged 3 commits into from
Jul 18, 2023
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
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
Loading