Skip to content

Commit 065b633

Browse files
authored
program: add immutable owner support for token 22 vaults (#1904)
* program: add immutable owner support for token 22 vaults * cargo fmt -- * CHANGELOG
1 parent f941e0d commit 065b633

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

CHANGELOG.md

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

1010
### Features
1111

12+
- program: all token 22 use immutable owner ([#1904](https://github.com/drift-labs/protocol-v2/pull/1904))
1213
- program: allow resolve perp pnl deficit if pnl pool isnt 0 but at deficit ([#1909](https://github.com/drift-labs/protocol-v2/pull/1909))
1314
- program: auction order params account for twap divergence ([#1882](https://github.com/drift-labs/protocol-v2/pull/1882))
1415
- program: add delegate stake if ([#1859](https://github.com/drift-labs/protocol-v2/pull/1859))

programs/drift/src/controller/token.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,16 @@ pub fn initialize_token_account<'info>(
217217

218218
Ok(())
219219
}
220+
221+
pub fn initialize_immutable_owner<'info>(
222+
token_program: &Interface<'info, TokenInterface>,
223+
account: &AccountInfo<'info>,
224+
) -> Result<()> {
225+
let accounts = ::anchor_spl::token_interface::InitializeImmutableOwner {
226+
account: account.to_account_info(),
227+
};
228+
let cpi_ctx = anchor_lang::context::CpiContext::new(token_program.to_account_info(), accounts);
229+
::anchor_spl::token_interface::initialize_immutable_owner(cpi_ctx)?;
230+
231+
Ok(())
232+
}

programs/drift/src/instructions/admin.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use pyth_solana_receiver_sdk::cpi::accounts::InitPriceUpdate;
1010
use pyth_solana_receiver_sdk::program::PythSolanaReceiver;
1111
use serum_dex::state::ToAlignedBytes;
1212

13-
use crate::controller::token::{close_vault, initialize_token_account};
13+
use crate::controller::token::{close_vault, initialize_immutable_owner, initialize_token_account};
1414
use crate::error::ErrorCode;
1515
use crate::ids::{admin_hot_wallet, amm_spread_adjust_wallet, mm_oracle_crank_wallet};
1616
use crate::instructions::constraints::*;
@@ -148,6 +148,16 @@ pub fn handle_initialize_spot_market(
148148
let state = &mut ctx.accounts.state;
149149
let spot_market_pubkey = ctx.accounts.spot_market.key();
150150

151+
let is_token_2022 = *ctx.accounts.spot_market_mint.to_account_info().owner == Token2022::id();
152+
if is_token_2022 {
153+
initialize_immutable_owner(&ctx.accounts.token_program, &ctx.accounts.spot_market_vault)?;
154+
155+
initialize_immutable_owner(
156+
&ctx.accounts.token_program,
157+
&ctx.accounts.insurance_fund_vault,
158+
)?;
159+
}
160+
151161
initialize_token_account(
152162
&ctx.accounts.token_program,
153163
&ctx.accounts.spot_market_vault,

programs/drift/src/instructions/constraints.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,13 @@ pub fn get_vault_len(mint: &InterfaceAccount<Mint>) -> anchor_lang::Result<usize
159159
let mint_state = StateWithExtensions::<Mint>::unpack(&mint_data)?;
160160
let mint_extensions = match mint_state.get_extension_types() {
161161
Ok(extensions) => extensions,
162-
// If we cant deserialize the mint, we use the default token account length
162+
// If we cant deserialize the mint, try assuming no extensions
163163
// 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-
}
164+
Err(_) => vec![],
168165
};
169-
let required_extensions =
166+
let mut required_extensions =
170167
ExtensionType::get_required_init_account_extensions(&mint_extensions);
168+
required_extensions.push(ExtensionType::ImmutableOwner);
171169
ExtensionType::try_calculate_account_len::<Account>(&required_extensions)?
172170
} else {
173171
::anchor_spl::token::TokenAccount::LEN

0 commit comments

Comments
 (0)