Skip to content

Commit

Permalink
program: add fee adjustment to spot market (#987)
Browse files Browse the repository at this point in the history
* program: add fee adjustment to spot market

* CHANGELOG
  • Loading branch information
crispheaney committed Apr 1, 2024
1 parent 695b210 commit a0052b2
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- program: add fee adjustment to spot market ([#987](https://github.com/drift-labs/protocol-v2/pull/987))
- program: allow multiple makers to be passed into for spot fills ([#946](https://github.com/drift-labs/protocol-v2/pull/946))
- ts-sdk: add fn to get admin ix ([#980](https://github.com/drift-labs/protocol-v2/pull/980))
- program: add invariant check boolean for attempt settle revenue to insurance ([#937](https://github.com/drift-labs/protocol-v2/pull/937))
Expand Down
4 changes: 2 additions & 2 deletions programs/drift/src/controller/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4238,7 +4238,7 @@ pub fn fulfill_spot_order_with_match(
false,
&None,
&MarketType::Spot,
0,
base_market.fee_adjustment,
)?;

// Update taker state
Expand Down Expand Up @@ -4528,7 +4528,7 @@ pub fn fulfill_spot_order_with_external_market(
external_market_fee,
unsettled_referrer_rebate,
fee_pool_amount.cast()?,
0,
base_market.fee_adjustment,
)?;

let quote_spot_position_delta = match quote_update_direction {
Expand Down
25 changes: 24 additions & 1 deletion programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ pub fn handle_initialize_spot_market(
total_spot_fee: 0,
orders_enabled: spot_market_index != 0,
paused_operations: 0,
padding1: [0; 5],
padding2: 0,
fee_adjustment: 0,
padding1: [0; 2],
flash_loan_amount: 0,
flash_loan_initial_token_amount: 0,
total_swap_fee: 0,
Expand Down Expand Up @@ -2308,6 +2310,27 @@ pub fn handle_update_perp_market_fee_adjustment(
Ok(())
}

#[access_control(
spot_market_valid(&ctx.accounts.spot_market)
)]
pub fn handle_update_spot_market_fee_adjustment(
ctx: Context<AdminUpdateSpotMarket>,
fee_adjustment: i16,
) -> Result<()> {
let spot = &mut load_mut!(ctx.accounts.spot_market)?;

validate!(
fee_adjustment.unsigned_abs().cast::<u64>()? <= FEE_ADJUSTMENT_MAX,
ErrorCode::DefaultError,
"fee adjustment {} greater than max {}",
fee_adjustment,
FEE_ADJUSTMENT_MAX
)?;

spot.fee_adjustment = fee_adjustment;
Ok(())
}

pub fn handle_update_admin(ctx: Context<AdminUpdateState>, admin: Pubkey) -> Result<()> {
ctx.accounts.state.admin = admin;
Ok(())
Expand Down
7 changes: 7 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,13 @@ pub mod drift {
handle_update_perp_market_fee_adjustment(ctx, fee_adjustment)
}

pub fn update_spot_market_fee_adjustment(
ctx: Context<AdminUpdateSpotMarket>,
fee_adjustment: i16,
) -> Result<()> {
handle_update_spot_market_fee_adjustment(ctx, fee_adjustment)
}

pub fn update_admin(ctx: Context<AdminUpdateState>, admin: Pubkey) -> Result<()> {
handle_update_admin(ctx, admin)
}
Expand Down
8 changes: 6 additions & 2 deletions programs/drift/src/state/spot_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ pub struct SpotMarket {
/// The asset tier affects how a deposit can be used as collateral and the priority for a borrow being liquidated
pub asset_tier: AssetTier,
pub paused_operations: u8,
pub padding1: [u8; 5],
pub padding2: u8,
pub fee_adjustment: i16,
pub padding1: [u8; 2],
/// For swaps, the amount of token loaned out in the begin_swap ix
/// precision: token mint precision
pub flash_loan_amount: u64,
Expand Down Expand Up @@ -230,7 +232,9 @@ impl Default for SpotMarket {
status: MarketStatus::default(),
asset_tier: AssetTier::default(),
paused_operations: 0,
padding1: [0; 5],
padding2: 0,
fee_adjustment: 0,
padding1: [0; 2],
flash_loan_amount: 0,
flash_loan_initial_token_amount: 0,
total_swap_fee: 0,
Expand Down
38 changes: 38 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,44 @@ export class AdminClient extends DriftClient {
);
}

public async updateSpotMarketFeeAdjustment(
perpMarketIndex: number,
feeAdjustment: number
): Promise<TransactionSignature> {
const updateSpotMarketFeeAdjustmentIx =
await this.getUpdateSpotMarketFeeAdjustmentIx(
perpMarketIndex,
feeAdjustment
);

const tx = await this.buildTransaction(updateSpotMarketFeeAdjustmentIx);

const { txSig } = await this.sendTransaction(tx, [], this.opts);

return txSig;
}

public async getUpdateSpotMarketFeeAdjustmentIx(
spotMarketIndex: number,
feeAdjustment: number
): Promise<TransactionInstruction> {
return await this.program.instruction.updateSpotMarketFeeAdjustment(
feeAdjustment,
{
accounts: {
admin: this.isSubscribed
? this.getStateAccount().admin
: this.wallet.publicKey,
state: await this.getStatePublicKey(),
spotMarket: await getSpotMarketPublicKey(
this.program.programId,
spotMarketIndex
),
},
}
);
}

public async updateSerumVault(
srmVault: PublicKey
): Promise<TransactionSignature> {
Expand Down
36 changes: 35 additions & 1 deletion sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -4784,6 +4784,32 @@
}
]
},
{
"name": "updateSpotMarketFeeAdjustment",
"accounts": [
{
"name": "admin",
"isMut": false,
"isSigner": true
},
{
"name": "state",
"isMut": false,
"isSigner": false
},
{
"name": "spotMarket",
"isMut": true,
"isSigner": false
}
],
"args": [
{
"name": "feeAdjustment",
"type": "i16"
}
]
},
{
"name": "updateAdmin",
"accounts": [
Expand Down Expand Up @@ -6030,12 +6056,20 @@
"name": "pausedOperations",
"type": "u8"
},
{
"name": "padding2",
"type": "u8"
},
{
"name": "feeAdjustment",
"type": "i16"
},
{
"name": "padding1",
"type": {
"array": [
"u8",
5
2
]
}
},
Expand Down

0 comments on commit a0052b2

Please sign in to comment.