Skip to content

Commit

Permalink
Add reserved bytes in accounts to better support upgradability
Browse files Browse the repository at this point in the history
  • Loading branch information
EquilateralDelta committed Nov 24, 2023
1 parent 7c6bf21 commit 0ea694a
Show file tree
Hide file tree
Showing 63 changed files with 209 additions and 148 deletions.
18 changes: 9 additions & 9 deletions psyoptions-american-instrument/program/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ESCROW_SEED: &str = "escrow";
pub struct ValidateData<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,

/// user provided
pub american_meta: Account<'info, OptionMarket>,
Expand All @@ -25,9 +25,9 @@ pub struct ValidateData<'info> {
pub struct PrepareToSettle<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,
pub response: Box<Account<'info, Response>>,

/// user provided
#[account(mut)]
Expand All @@ -50,8 +50,8 @@ pub struct PrepareToSettle<'info> {
pub struct Settle<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub rfq: Account<'info, Rfq>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,

/// user provided
Expand All @@ -68,8 +68,8 @@ pub struct Settle<'info> {
pub struct RevertPreparation<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub rfq: Account<'info, Rfq>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,

/// user provided
Expand All @@ -86,8 +86,8 @@ pub struct RevertPreparation<'info> {
pub struct CleanUp<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub rfq: Account<'info, Rfq>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,

/// user provided
Expand Down
12 changes: 6 additions & 6 deletions psyoptions-european-instrument/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn close_escrow_account<'info>(
pub struct ValidateData<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,

/// user provided
pub euro_meta: Account<'info, EuroMeta>,
Expand All @@ -340,9 +340,9 @@ pub struct ValidateData<'info> {
pub struct PrepareToSettle<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,
pub response: Box<Account<'info, Response>>,

/// user provided
#[account(mut)]
Expand All @@ -366,7 +366,7 @@ pub struct PrepareToSettle<'info> {
pub struct Settle<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,

Expand All @@ -384,7 +384,7 @@ pub struct Settle<'info> {
pub struct RevertPreparation<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,

Expand All @@ -402,7 +402,7 @@ pub struct RevertPreparation<'info> {
pub struct CleanUp<'info> {
/// protocol provided
#[account(signer)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
pub response: Account<'info, Response>,

Expand Down
4 changes: 2 additions & 2 deletions rfq/program/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
interfaces::instrument::validate_leg_instrument_data,
seeds::COLLATERAL_SEED,
state::{
AuthoritySide, BaseAssetInfo, CollateralInfo, Leg, ProtocolState, Response, Rfq,
ApiLeg, AuthoritySide, BaseAssetInfo, CollateralInfo, ProtocolState, Response, Rfq,
StoredResponseState,
},
};
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn update_state_after_preparation(
}

pub fn validate_legs<'a, 'info: 'a>(
legs: &[Leg],
legs: &[ApiLeg],
protocol: &Account<'info, ProtocolState>,
remaining_accounts: &mut impl Iterator<Item = &'a AccountInfo<'info>>,
) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/collateral/fund_collateral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct FundCollateralAccounts<'info> {
pub user_tokens: Account<'info, TokenAccount>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(seeds = [COLLATERAL_SEED.as_bytes(), user.key().as_ref()], bump = collateral_info.bump)]
pub collateral_info: Account<'info, CollateralInfo>,
#[account(mut, seeds = [COLLATERAL_TOKEN_SEED.as_bytes(), user.key().as_ref()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct InitializeCollateralAccounts<'info> {
pub user: Signer<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(init, payer = user, space = 8 + mem::size_of::<CollateralInfo>(),
seeds = [COLLATERAL_SEED.as_bytes(), user.key().as_ref()], bump)]
pub collateral_info: Account<'info, CollateralInfo>,
Expand Down Expand Up @@ -43,6 +43,7 @@ pub fn initialize_collateral_instruction(ctx: Context<InitializeCollateralAccoun
user: user.key(),
token_account_bump: *ctx.bumps.get("collateral_token").unwrap(),
locked_tokens_amount: 0,
reserved: [0; 256],
});

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct WithdrawCollateralAccounts<'info> {
pub user_tokens: Account<'info, TokenAccount>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(seeds = [COLLATERAL_SEED.as_bytes(), user.key().as_ref()], bump = collateral_info.bump)]
pub collateral_info: Account<'info, CollateralInfo>,
#[account(mut, seeds = [COLLATERAL_TOKEN_SEED.as_bytes(), user.key().as_ref()],
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/protocol/add_base_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct AddBaseAssetAccounts<'info> {
#[account(mut, constraint = protocol.authority == authority.key() @ ProtocolError::NotAProtocolAuthority)]
pub authority: Signer<'info>,
#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(init, payer = authority, space = 8 + mem::size_of::<BaseAssetInfo>() + ticker.as_bytes().len(),
seeds = [BASE_ASSET_INFO_SEED.as_bytes(), &u16::from(index).to_le_bytes()], bump)]
pub base_asset: Account<'info, BaseAssetInfo>,
Expand Down
3 changes: 2 additions & 1 deletion rfq/program/src/instructions/protocol/add_instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct AddInstrumentAccounts<'info> {
#[account(constraint = protocol.authority == authority.key() @ ProtocolError::NotAProtocolAuthority)]
pub authority: Signer<'info>,
#[account(mut, seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
/// CHECK: is a valid instrument program id
#[account(executable)]
pub instrument_program: UncheckedAccount<'info>,
Expand Down Expand Up @@ -60,6 +60,7 @@ pub fn add_instrument_instruction(
settle_account_amount,
revert_preparation_account_amount,
clean_up_account_amount,
reserved: [0; 32],
});

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct ChangeBaseAssetParametersAccounts<'info> {
#[account(constraint = protocol.authority == authority.key() @ ProtocolError::NotAProtocolAuthority)]
pub authority: Signer<'info>,
#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(mut)]
pub base_asset: Account<'info, BaseAssetInfo>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct ChangeProtocolFeesAccounts<'info> {
#[account(constraint = protocol.authority == authority.key() @ ProtocolError::NotAProtocolAuthority)]
pub authority: Signer<'info>,
#[account(mut, seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
}

fn validate(settle_fees: Option<FeeParameters>, default_fees: Option<FeeParameters>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ pub struct CloseProtocolStateAccounts<'info> {
seeds = [PROTOCOL_SEED.as_bytes()],
bump
)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
}
3 changes: 2 additions & 1 deletion rfq/program/src/instructions/protocol/initialize_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct InitializeProtocolAccounts<'info> {
space = ProtocolState::get_allocated_size(),
bump
)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
/// CHECK: is a valid risk engine program id
#[account(executable)]
pub risk_engine: AccountInfo<'info>,
Expand Down Expand Up @@ -53,6 +53,7 @@ pub fn initialize_protocol_instruction(
risk_engine: risk_engine.key(),
collateral_mint: collateral_mint.key(),
instruments: Default::default(),
reserved: [0; 1024],
});

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/protocol/register_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct RegisterMintAccounts<'info> {
#[account(mut, constraint = protocol.authority == authority.key() @ ProtocolError::NotAProtocolAuthority)]
pub authority: Signer<'info>,
#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(init, payer = authority, space = 8 + mem::size_of::<MintInfo>(),
seeds = [MINT_INFO_SEED.as_bytes(), mint.key().as_ref()], bump)]
pub mint_info: Account<'info, MintInfo>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct SetInstrumentEnabledStatusAccounts<'info> {
#[account(constraint = protocol.authority == authority.key() @ ProtocolError::NotAProtocolAuthority)]
pub authority: Signer<'info>,
#[account(mut, seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
}

fn validate(
Expand Down
12 changes: 7 additions & 5 deletions rfq/program/src/instructions/rfq/add_legs_to_rfq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
common::validate_legs,
errors::ProtocolError,
seeds::PROTOCOL_SEED,
state::{Leg, ProtocolState, Rfq, RfqState},
state::{ApiLeg, ProtocolState, Rfq, RfqState},
};
use anchor_lang::prelude::*;

Expand All @@ -12,14 +12,14 @@ pub struct AddLegsToRfqAccounts<'info> {
pub taker: Signer<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(mut)]
pub rfq: Box<Account<'info, Rfq>>,
}

fn validate<'info>(
ctx: &Context<'_, '_, '_, 'info, AddLegsToRfqAccounts<'info>>,
legs: &Vec<Leg>,
legs: &Vec<ApiLeg>,
) -> Result<()> {
let AddLegsToRfqAccounts { protocol, rfq, .. } = &ctx.accounts;
let mut remaining_accounts = ctx.remaining_accounts.iter();
Expand All @@ -39,13 +39,15 @@ fn validate<'info>(

pub fn add_legs_to_rfq_instruction<'info>(
ctx: Context<'_, '_, '_, 'info, AddLegsToRfqAccounts<'info>>,
mut legs: Vec<Leg>,
legs: Vec<ApiLeg>,
) -> Result<()> {
validate(&ctx, &legs)?;

let AddLegsToRfqAccounts { rfq, .. } = ctx.accounts;

rfq.legs.append(&mut legs);
for leg in legs {
rfq.legs.push(leg.into());
}

Ok(())
}
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/rfq/cancel_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct CancelResponseAccounts<'info> {
pub maker: Signer<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
#[account(mut, constraint = response.rfq == rfq.key() @ ProtocolError::ResponseForAnotherRfq)]
pub response: Account<'info, Response>,
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/rfq/cancel_rfq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct CancelRfqAccounts<'info> {
pub taker: Signer<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(mut)]
pub rfq: Box<Account<'info, Rfq>>,
}
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/rfq/clean_up_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct CleanUpResponseAccounts<'info> {
pub maker: UncheckedAccount<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(mut)]
pub rfq: Box<Account<'info, Rfq>>,
#[account(mut, close = maker, constraint = response.rfq == rfq.key() @ ProtocolError::ResponseForAnotherRfq)]
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/rfq/clean_up_response_legs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anchor_lang::prelude::*;
#[derive(Accounts)]
pub struct CleanUpResponseLegsAccounts<'info> {
#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
pub rfq: Box<Account<'info, Rfq>>,
#[account(mut, constraint = response.rfq == rfq.key() @ ProtocolError::ResponseForAnotherRfq)]
pub response: Account<'info, Response>,
Expand Down
2 changes: 1 addition & 1 deletion rfq/program/src/instructions/rfq/clean_up_rfq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct CleanUpRfqAccounts<'info> {
pub taker: UncheckedAccount<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(mut, close = taker)]
pub rfq: Box<Account<'info, Rfq>>,
}
Expand Down
4 changes: 2 additions & 2 deletions rfq/program/src/instructions/rfq/confirm_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub struct ConfirmResponseAccounts<'info> {
pub taker: Signer<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(mut)]
pub rfq: Box<Account<'info, Rfq>>,
#[account(mut, constraint = response.rfq == rfq.key() @ ProtocolError::ResponseForAnotherRfq)]
pub response: Account<'info, Response>,
pub response: Box<Account<'info, Response>>,
#[account(mut, seeds = [COLLATERAL_SEED.as_bytes(), taker.key().as_ref()],
bump = collateral_info.bump)]
pub collateral_info: Account<'info, CollateralInfo>,
Expand Down
13 changes: 7 additions & 6 deletions rfq/program/src/instructions/rfq/create_rfq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
errors::ProtocolError,
interfaces::instrument::validate_quote_instrument_data,
seeds::{PROTOCOL_SEED, RFQ_SEED},
state::{rfq::QuoteAsset, FixedSize, Leg, OrderType, ProtocolState, Rfq, StoredRfqState},
state::{rfq::QuoteAsset, ApiLeg, FixedSize, OrderType, ProtocolState, Rfq, StoredRfqState},
};
use anchor_lang::prelude::*;
use solana_program::hash::hash;
Expand All @@ -16,7 +16,7 @@ const RECENT_TIMESTAMP_VALIDITY: u64 = 120; // slightly higher then the recent b
#[instruction(
expected_legs_size: u16,
expected_legs_hash: [u8; 32],
_legs: Vec<Leg>,
_legs: Vec<ApiLeg>,
order_type: OrderType,
quote_asset: QuoteAsset,
fixed_size: FixedSize,
Expand All @@ -29,7 +29,7 @@ pub struct CreateRfqAccounts<'info> {
pub taker: Signer<'info>,

#[account(seeds = [PROTOCOL_SEED.as_bytes()], bump = protocol.bump)]
pub protocol: Account<'info, ProtocolState>,
pub protocol: Box<Account<'info, ProtocolState>>,
#[account(init, payer = taker, space = 8 + mem::size_of::<Rfq>() + expected_legs_size as usize, seeds = [
RFQ_SEED.as_bytes(),
taker.key().as_ref(),
Expand Down Expand Up @@ -68,7 +68,7 @@ fn validate_legs<'a, 'info: 'a>(
protocol: &Account<'info, ProtocolState>,
remaining_accounts: &mut impl Iterator<Item = &'a AccountInfo<'info>>,
expected_leg_size: u16,
legs: &[Leg],
legs: &[ApiLeg],
) -> Result<()> {
require!(
legs.len() <= Rfq::MAX_LEGS_AMOUNT as usize,
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn create_rfq_instruction<'info>(
ctx: Context<'_, '_, '_, 'info, CreateRfqAccounts<'info>>,
expected_legs_size: u16,
expected_legs_hash: [u8; 32],
legs: Vec<Leg>,
legs: Vec<ApiLeg>,
order_type: OrderType,
quote_asset: QuoteAsset,
fixed_size: FixedSize,
Expand Down Expand Up @@ -133,7 +133,8 @@ pub fn create_rfq_instruction<'info>(
total_responses: 0,
cleared_responses: 0,
confirmed_responses: 0,
legs,
reserved: [0; 256],
legs: legs.into_iter().map(Into::into).collect(),
});

Ok(())
Expand Down
Loading

0 comments on commit 0ea694a

Please sign in to comment.