Skip to content

Commit

Permalink
program: ix to revert fill (#391)
Browse files Browse the repository at this point in the history
* program: ix to revert fill

* CHANGELOG
  • Loading branch information
crispheaney committed Mar 15, 2023
1 parent 134ecab commit 59fe94e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: revert fill ix ([#391](https://github.com/drift-labs/protocol-v2/pull/391))
- program: flag users as idle on-chain ([#386](https://github.com/drift-labs/protocol-v2/pull/386))

### Fixes
Expand Down
8 changes: 8 additions & 0 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ pub fn fill_perp_order(
filler.as_deref_mut(),
market.deref_mut(),
state.perp_fee_structure.flat_filler_fee,
slot,
)?
};

Expand Down Expand Up @@ -913,6 +914,7 @@ pub fn fill_perp_order(
filler.as_deref_mut(),
market.deref_mut(),
state.perp_fee_structure.flat_filler_fee,
slot,
)?
};

Expand Down Expand Up @@ -1148,6 +1150,7 @@ fn get_maker_orders_info(
filler.as_deref_mut(),
market.deref_mut(),
filler_reward,
slot,
)?
};

Expand Down Expand Up @@ -1732,6 +1735,7 @@ pub fn fulfill_perp_order_with_amm(
.safe_unwrap()?
.update_filler_volume(quote_asset_amount, now)?;
}
filler.update_last_active_slot(slot);
}

update_order_after_fill(
Expand Down Expand Up @@ -2096,6 +2100,7 @@ pub fn fulfill_perp_order_with_match(
.safe_unwrap()?
.update_filler_volume(quote_asset_amount, now)?;
}
filler.update_last_active_slot(slot);
}

if let (Some(referrer), Some(referrer_stats)) = (referrer.as_mut(), referrer_stats.as_mut()) {
Expand Down Expand Up @@ -2358,6 +2363,7 @@ pub fn trigger_order(
filler.as_deref_mut(),
&mut perp_market,
state.perp_fee_structure.flat_filler_fee,
slot,
)?;

let order_action_record = get_order_action_record(
Expand Down Expand Up @@ -2531,6 +2537,7 @@ pub fn pay_keeper_flat_reward_for_perps(
filler: Option<&mut User>,
market: &mut PerpMarket,
filler_reward: u64,
slot: u64,
) -> DriftResult<u64> {
let filler_reward = if let Some(filler) = filler {
let user_position = user.get_perp_position_mut(market.market_index)?;
Expand All @@ -2540,6 +2547,7 @@ pub fn pay_keeper_flat_reward_for_perps(
-filler_reward.cast()?,
)?;

filler.update_last_active_slot(slot);
// Dont throw error if filler doesnt have position available
let filler_position = match filler.force_get_perp_position_mut(market.market_index) {
Ok(position) => position,
Expand Down
2 changes: 2 additions & 0 deletions programs/drift/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ pub enum ErrorCode {
UnableToLoadUserStatsAccount,
#[msg("User Not Inactive")]
UserNotInactive,
#[msg("RevertFill")]
RevertFill,
}

#[macro_export]
Expand Down
34 changes: 34 additions & 0 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ fn fill_order(ctx: Context<FillOrder>, order_id: u32, market_index: u16) -> Resu
Ok(())
}

#[access_control(
fill_not_paused(&ctx.accounts.state)
)]
pub fn handle_revert_fill<'info>(ctx: Context<RevertFill>) -> Result<()> {
let filler = load_mut!(ctx.accounts.filler)?;
let clock = Clock::get()?;

validate!(
filler.last_active_slot == clock.slot,
ErrorCode::RevertFill,
"filler last active slot ({}) != current slot ({})",
filler.last_active_slot,
clock.slot
)?;

Ok(())
}

#[derive(Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Debug, Eq)]
pub enum SpotFulfillmentType {
SerumV3,
Expand Down Expand Up @@ -1299,6 +1317,22 @@ pub struct FillOrder<'info> {
pub user_stats: AccountLoader<'info, UserStats>,
}

#[derive(Accounts)]
pub struct RevertFill<'info> {
pub state: Box<Account<'info, State>>,
pub authority: Signer<'info>,
#[account(
mut,
constraint = can_sign_for_user(&filler, &authority)?
)]
pub filler: AccountLoader<'info, User>,
#[account(
mut,
constraint = is_stats_for_user(&filler, &filler_stats)?
)]
pub filler_stats: AccountLoader<'info, UserStats>,
}

#[derive(Accounts)]
pub struct TriggerOrder<'info> {
pub state: Box<Account<'info, 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 @@ -217,6 +217,10 @@ pub mod drift {
handle_fill_perp_order(ctx, order_id)
}

pub fn revert_fill(ctx: Context<RevertFill>) -> Result<()> {
handle_revert_fill(ctx)
}

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

public async getRevertFillIx(): Promise<TransactionInstruction> {
const fillerPublicKey = await this.getUserAccountPublicKey();
const fillerStatsPublicKey = this.getUserStatsAccountPublicKey();

return this.program.instruction.revertFill({
accounts: {
state: await this.getStatePublicKey(),
filler: fillerPublicKey,
fillerStats: fillerStatsPublicKey,
authority: this.wallet.publicKey,
},
});
}

public async placeSpotOrder(
orderParams: OptionalOrderParams,
txParams?: TxParams
Expand Down
31 changes: 31 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,32 @@
}
]
},
{
"name": "revertFill",
"accounts": [
{
"name": "state",
"isMut": false,
"isSigner": false
},
{
"name": "authority",
"isMut": false,
"isSigner": true
},
{
"name": "filler",
"isMut": true,
"isSigner": false
},
{
"name": "fillerStats",
"isMut": true,
"isSigner": false
}
],
"args": []
},
{
"name": "fillSpotOrder",
"accounts": [
Expand Down Expand Up @@ -8768,6 +8794,11 @@
"code": 6238,
"name": "UserNotInactive",
"msg": "User Not Inactive"
},
{
"code": 6239,
"name": "RevertFill",
"msg": "RevertFill"
}
]
}

0 comments on commit 59fe94e

Please sign in to comment.