Skip to content

Commit 2db9320

Browse files
committed
merge master
2 parents d1060ff + 030a422 commit 2db9320

32 files changed

+1057
-207
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Features
1111

12+
- program: support scaled ui extension ([#1894](https://github.com/drift-labs/protocol-v2/pull/1894))
13+
1214
### Fixes
1315

1416
### Breaking
@@ -20,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2022
- program: post only respects reduce only ([#1878](https://github.com/drift-labs/protocol-v2/pull/1878))
2123
- program: add sequence id to exchange/mm oracle ([#1834](https://github.com/drift-labs/protocol-v2/pull/1834))
2224
- program: perp position max margin ratio ([#1847](https://github.com/drift-labs/protocol-v2/pull/1847))
25+
- program: add padding to swift messages ([#1845](https://github.com/drift-labs/protocol-v2/pull/1845))
2326
- program: rm lp ([#1755](https://github.com/drift-labs/protocol-v2/pull/1755))
2427

2528
### Fixes

package.json

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,51 @@
4848
"prepare": "husky install",
4949
"prettify": "prettier --check './sdk/src/**/*.ts' './tests/**.ts' './cli/**.ts'",
5050
"prettify:fix": "prettier --write './sdk/src/**/*.ts' './tests/**.ts' './cli/**.ts'",
51-
"lint": "eslint . --ext ts --quiet",
51+
"lint": "eslint . --ext ts --quiet --format unix",
5252
"lint:fix": "eslint . --ext ts --fix",
5353
"update-idl": "cp target/idl/drift.json sdk/src/idl/drift.json"
5454
},
5555
"engines": {
5656
"node": ">=12"
57+
},
58+
"resolutions": {
59+
"chalk": "4.1.2",
60+
"debug": "<4.4.2",
61+
"ansi-styles": "4.3.0",
62+
"supports-color": "7.2.0",
63+
"strip-ansi": "6.0.1",
64+
"ansi-regex": "5.0.1",
65+
"wrap-ansi": "7.0.0",
66+
"color-convert": "<3.1.1",
67+
"color-name": "<2.0.1",
68+
"color-string": "<2.1.1",
69+
"simple-swizzle": "<0.2.3",
70+
"is-arrayish": "<0.3.3",
71+
"slice-ansi": "3.0.0",
72+
"error-ex": "<1.3.3",
73+
"backslash": "<0.2.1",
74+
"chalk-template": "<1.1.1",
75+
"supports-hyperlinks": "<4.1.1",
76+
"has-ansi": "<6.0.1"
77+
},
78+
"overrides": {
79+
"chalk": "4.1.2",
80+
"debug": "<4.4.2",
81+
"ansi-styles": "4.3.0",
82+
"supports-color": "7.2.0",
83+
"strip-ansi": "6.0.1",
84+
"ansi-regex": "5.0.1",
85+
"wrap-ansi": "7.0.0",
86+
"color-convert": "<3.1.1",
87+
"color-name": "<2.0.1",
88+
"color-string": "<2.1.1",
89+
"simple-swizzle": "<0.2.3",
90+
"is-arrayish": "<0.3.3",
91+
"slice-ansi": "3.0.0",
92+
"error-ex": "<1.3.3",
93+
"backslash": "<0.2.1",
94+
"chalk-template": "<1.1.1",
95+
"supports-hyperlinks": "<4.1.1",
96+
"has-ansi": "<6.0.1"
5797
}
5898
}

programs/drift/src/controller/insurance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ pub fn handle_if_begin_swap(
973973
out_insurance_fund_vault_amount: u64,
974974
in_spot_market: &mut SpotMarket,
975975
out_spot_market: &mut SpotMarket,
976-
in_amount: u64,
976+
_in_amount: u64,
977977
now: i64,
978978
) -> DriftResult<()> {
979979
if now

programs/drift/src/controller/orders.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -983,23 +983,12 @@ pub fn fill_perp_order(
983983
.position(|order| order.order_id == order_id && order.status == OrderStatus::Open)
984984
.ok_or_else(print_error!(ErrorCode::OrderDoesNotExist))?;
985985

986-
let (
987-
order_status,
988-
market_index,
989-
order_market_type,
990-
order_price,
991-
order_oracle_price_offset,
992-
order_direction,
993-
order_auction_duration,
994-
) = get_struct_values!(
986+
let (order_status, market_index, order_market_type, order_direction) = get_struct_values!(
995987
user.orders[order_index],
996988
status,
997989
market_index,
998990
market_type,
999-
price,
1000-
oracle_price_offset,
1001-
direction,
1002-
auction_duration
991+
direction
1003992
);
1004993

1005994
validate!(
@@ -2244,7 +2233,7 @@ pub fn fulfill_perp_order_with_amm(
22442233
filler_reward,
22452234
referee_discount,
22462235
referrer_reward,
2247-
fee_to_market_for_lp,
2236+
fee_to_market_for_lp: _fee_to_market_for_lp,
22482237
maker_rebate,
22492238
} = fees::calculate_fee_for_fulfillment_with_amm(
22502239
user_stats,
@@ -2261,9 +2250,6 @@ pub fn fulfill_perp_order_with_amm(
22612250
user.is_high_leverage_mode(MarginRequirementType::Initial),
22622251
)?;
22632252

2264-
let user_position_delta =
2265-
get_position_delta_for_fill(base_asset_amount, quote_asset_amount, order_direction)?;
2266-
22672253
// Increment the protocol's total fee variables
22682254
market.amm.total_fee = market.amm.total_fee.safe_add(fee_to_market.cast()?)?;
22692255
market.amm.total_exchange_fee = market.amm.total_exchange_fee.safe_add(user_fee.cast()?)?;

programs/drift/src/controller/pnl.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ use crate::math::oracle::{is_oracle_valid_for_action, DriftAction};
1414

1515
use crate::math::casting::Cast;
1616
use crate::math::margin::{
17-
calculate_margin_requirement_and_total_collateral_and_liability_info,
1817
meets_maintenance_margin_requirement, meets_settle_pnl_maintenance_margin_requirement,
19-
MarginRequirementType,
2018
};
2119
use crate::math::position::calculate_base_asset_value_with_expiry_price;
2220
use crate::math::safe_math::SafeMath;
2321
use crate::math::spot_balance::get_token_amount;
24-
use crate::state::margin_calculation::MarginContext;
2522

2623
use crate::msg;
2724
use crate::state::events::{OrderActionExplanation, SettlePnlExplanation, SettlePnlRecord};

programs/drift/src/controller/repeg.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::cmp::min;
33
use crate::math::oracle::LogMode;
44
use crate::msg;
55
use crate::state::oracle::MMOraclePriceData;
6-
use crate::state::oracle::OraclePriceData;
76
use anchor_lang::prelude::AccountInfo;
87
use anchor_lang::prelude::*;
98

programs/drift/src/controller/token.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,21 @@ pub fn transfer_checked_with_transfer_hook<'info>(
273273

274274
solana_program::program::invoke_signed(&ix, &account_infos, signer_seeds).map_err(Into::into)
275275
}
276+
277+
pub fn initialize_token_account<'info>(
278+
token_program: &Interface<'info, TokenInterface>,
279+
account: &AccountInfo<'info>,
280+
owner: &AccountInfo<'info>,
281+
mint: &InterfaceAccount<'info, Mint>,
282+
) -> Result<()> {
283+
let cpi_program = token_program.to_account_info();
284+
let accounts = ::anchor_spl::token_interface::InitializeAccount3 {
285+
account: account.to_account_info(),
286+
mint: mint.to_account_info(),
287+
authority: owner.to_account_info(),
288+
};
289+
let cpi_ctx = anchor_lang::context::CpiContext::new(cpi_program, accounts);
290+
::anchor_spl::token_interface::initialize_account3(cpi_ctx)?;
291+
292+
Ok(())
293+
}

programs/drift/src/instructions/admin.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serum_dex::state::ToAlignedBytes;
99
use std::convert::{identity, TryInto};
1010
use std::mem::size_of;
1111

12-
use crate::controller::token::close_vault;
12+
use crate::controller::token::{close_vault, initialize_token_account};
1313
use crate::error::ErrorCode;
1414
use crate::ids::{admin_hot_wallet, amm_spread_adjust_wallet, mm_oracle_crank_wallet};
1515
use crate::instructions::constraints::*;
@@ -150,15 +150,19 @@ pub fn handle_initialize_spot_market(
150150
let state = &mut ctx.accounts.state;
151151
let spot_market_pubkey = ctx.accounts.spot_market.key();
152152

153-
// protocol must be authority of collateral vault
154-
if ctx.accounts.spot_market_vault.owner != state.signer {
155-
return Err(ErrorCode::InvalidSpotMarketAuthority.into());
156-
}
153+
initialize_token_account(
154+
&ctx.accounts.token_program,
155+
&ctx.accounts.spot_market_vault,
156+
&ctx.accounts.drift_signer,
157+
&ctx.accounts.spot_market_mint,
158+
)?;
157159

158-
// protocol must be authority of collateral vault
159-
if ctx.accounts.insurance_fund_vault.owner != state.signer {
160-
return Err(ErrorCode::InvalidInsuranceFundAuthority.into());
161-
}
160+
initialize_token_account(
161+
&ctx.accounts.token_program,
162+
&ctx.accounts.insurance_fund_vault,
163+
&ctx.accounts.drift_signer,
164+
&ctx.accounts.spot_market_mint,
165+
)?;
162166

163167
validate_borrow_rate(optimal_utilization, optimal_borrow_rate, max_borrow_rate, 0)?;
164168

@@ -284,7 +288,7 @@ pub fn handle_initialize_spot_market(
284288
historical_oracle_data: historical_oracle_data_default,
285289
historical_index_data: historical_index_data_default,
286290
mint: ctx.accounts.spot_market_mint.key(),
287-
vault: *ctx.accounts.spot_market_vault.to_account_info().key,
291+
vault: ctx.accounts.spot_market_vault.key(),
288292
revenue_pool: PoolBalance {
289293
scaled_balance: 0,
290294
market_index: spot_market_index,
@@ -341,7 +345,7 @@ pub fn handle_initialize_spot_market(
341345
pool_id: 0,
342346
padding: [0; 40],
343347
insurance_fund: InsuranceFund {
344-
vault: *ctx.accounts.insurance_fund_vault.to_account_info().key,
348+
vault: ctx.accounts.insurance_fund_vault.key(),
345349
unstaking_period: THIRTEEN_DAY,
346350
total_factor: if_total_factor,
347351
user_factor: if_total_factor / 2,
@@ -1626,7 +1630,6 @@ pub fn handle_recenter_perp_market_amm_crank(
16261630
depth: Option<u128>,
16271631
) -> Result<()> {
16281632
let perp_market = &mut load_mut!(ctx.accounts.perp_market)?;
1629-
let spot_market = &mut load!(ctx.accounts.spot_market)?;
16301633

16311634
let clock = Clock::get()?;
16321635
let price_oracle = &ctx.accounts.oracle;
@@ -4861,9 +4864,6 @@ pub fn handle_initialize_if_rebalance_config(
48614864
ctx: Context<InitializeIfRebalanceConfig>,
48624865
params: IfRebalanceConfigParams,
48634866
) -> Result<()> {
4864-
let clock = Clock::get()?;
4865-
let now = clock.unix_timestamp;
4866-
48674867
let pubkey = ctx.accounts.if_rebalance_config.to_account_info().key;
48684868
let mut config = ctx.accounts.if_rebalance_config.load_init()?;
48694869

@@ -5099,25 +5099,30 @@ pub struct InitializeSpotMarket<'info> {
50995099
payer = admin
51005100
)]
51015101
pub spot_market: AccountLoader<'info, SpotMarket>,
5102+
#[account(
5103+
mint::token_program = token_program,
5104+
)]
51025105
pub spot_market_mint: Box<InterfaceAccount<'info, Mint>>,
51035106
#[account(
51045107
init,
51055108
seeds = [b"spot_market_vault".as_ref(), state.number_of_spot_markets.to_le_bytes().as_ref()],
51065109
bump,
51075110
payer = admin,
5108-
token::mint = spot_market_mint,
5109-
token::authority = drift_signer
5111+
space = get_vault_len(&spot_market_mint)?,
5112+
owner = token_program.key()
51105113
)]
5111-
pub spot_market_vault: Box<InterfaceAccount<'info, TokenAccount>>,
5114+
/// CHECK: checked in `initialize_spot_market`
5115+
pub spot_market_vault: AccountInfo<'info>,
51125116
#[account(
51135117
init,
51145118
seeds = [b"insurance_fund_vault".as_ref(), state.number_of_spot_markets.to_le_bytes().as_ref()],
51155119
bump,
51165120
payer = admin,
5117-
token::mint = spot_market_mint,
5118-
token::authority = drift_signer
5121+
space = get_vault_len(&spot_market_mint)?,
5122+
owner = token_program.key()
51195123
)]
5120-
pub insurance_fund_vault: Box<InterfaceAccount<'info, TokenAccount>>,
5124+
/// CHECK: checked in `initialize_spot_market`
5125+
pub insurance_fund_vault: AccountInfo<'info>,
51215126
#[account(
51225127
constraint = state.signer.eq(&drift_signer.key())
51235128
)]

programs/drift/src/instructions/constraints.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use anchor_lang::accounts::account::Account;
22
use anchor_lang::accounts::account_loader::AccountLoader;
33
use anchor_lang::accounts::signer::Signer;
4+
use anchor_lang::prelude::*;
45
use anchor_lang::prelude::{AccountInfo, Pubkey};
6+
use anchor_spl::token_interface::Mint;
57

68
use crate::error::ErrorCode;
79
use crate::msg;
@@ -145,3 +147,31 @@ pub fn exchange_not_paused(state: &Account<State>) -> anchor_lang::Result<()> {
145147
}
146148
Ok(())
147149
}
150+
151+
pub fn get_vault_len(mint: &InterfaceAccount<Mint>) -> anchor_lang::Result<usize> {
152+
let mint_info = mint.to_account_info();
153+
let len = if *mint_info.owner == ::anchor_spl::token_2022::Token2022::id() {
154+
use ::anchor_spl::token_2022::spl_token_2022::extension::{
155+
BaseStateWithExtensions, ExtensionType, StateWithExtensions,
156+
};
157+
use ::anchor_spl::token_2022::spl_token_2022::state::{Account, Mint};
158+
let mint_data = mint_info.try_borrow_data()?;
159+
let mint_state = StateWithExtensions::<Mint>::unpack(&mint_data)?;
160+
let mint_extensions = match mint_state.get_extension_types() {
161+
Ok(extensions) => extensions,
162+
// If we cant deserialize the mint, we use the default token account length
163+
// Init token will fail if this size doesnt work, so worst case init account just fails
164+
Err(_) => {
165+
msg!("Failed to deserialize mint. Falling back to default token account length");
166+
return Ok(::anchor_spl::token::TokenAccount::LEN);
167+
}
168+
};
169+
let required_extensions =
170+
ExtensionType::get_required_init_account_extensions(&mint_extensions);
171+
ExtensionType::try_calculate_account_len::<Account>(&required_extensions)?
172+
} else {
173+
::anchor_spl::token::TokenAccount::LEN
174+
};
175+
176+
Ok(len)
177+
}

programs/drift/src/instructions/if_staker.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,7 @@ pub fn handle_begin_insurance_fund_swap<'c: 'info, 'info>(
375375

376376
let remaining_accounts_iter = &mut ctx.remaining_accounts.iter().peekable();
377377
let AccountMaps {
378-
perp_market_map,
379-
spot_market_map,
380-
mut oracle_map,
378+
spot_market_map, ..
381379
} = load_maps(
382380
remaining_accounts_iter,
383381
&MarketSet::new(),
@@ -602,14 +600,13 @@ pub fn handle_end_insurance_fund_swap<'c: 'info, 'info>(
602600
) -> Result<()> {
603601
let state = &ctx.accounts.state;
604602
let clock = Clock::get()?;
605-
let slot = clock.slot;
606603
let now = clock.unix_timestamp;
607604

608605
let remaining_accounts = &mut ctx.remaining_accounts.iter().peekable();
609606
let AccountMaps {
610-
perp_market_map,
611607
spot_market_map,
612608
mut oracle_map,
609+
..
613610
} = load_maps(
614611
remaining_accounts,
615612
&MarketSet::new(),

0 commit comments

Comments
 (0)