Skip to content

Commit

Permalink
Merge pull request #20 from marinade-finance/a27-token-system-stake-c…
Browse files Browse the repository at this point in the history
…heck-address

remove explicit check address for type Program <System>,<Token>,<Stake>
  • Loading branch information
aankor authored May 11, 2023
2 parents b7caea8 + 8b98088 commit 873d3e4
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 102 deletions.
5 changes: 4 additions & 1 deletion programs/marinade-finance/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub fn check_address(
reference_address,
actual_address
);
Err(Error::from(ProgramError::InvalidArgument).with_source(source!()))
Err(Error::from(ProgramError::InvalidArgument)
.with_account_name(field_name)
.with_pubkeys((*actual_address, *reference_address))
.with_source(source!()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use anchor_lang::solana_program::{
};
use anchor_spl::stake::{Stake, StakeAccount};

use crate::checks::{check_address, check_stake_amount_and_validator};
use crate::checks::{check_stake_amount_and_validator};

#[derive(Accounts)]
pub struct DeactivateStake<'info> {
Expand Down Expand Up @@ -61,12 +61,6 @@ impl<'info> DeactivateStake<'info> {
.check_stake_deposit_authority(self.stake_deposit_authority.key)?;
self.state
.check_stake_deposit_authority(self.stake_deposit_authority.key)?;
check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;
check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;

let mut stake = self.state.stake_system.get_checked(
&self.stake_list.data.as_ref().borrow(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anchor_spl::stake::{Stake, StakeAccount};

use crate::state::stake_system::StakeSystemHelpers;
use crate::State;
use crate::{checks::check_address, error::MarinadeError};
use crate::{error::MarinadeError};

#[derive(Accounts)]
pub struct MergeStakes<'info> {
Expand Down Expand Up @@ -54,7 +54,6 @@ impl<'info> MergeStakes<'info> {
self.state
.check_stake_withdraw_authority(self.stake_withdraw_authority.to_account_info().key)?;

check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;
let mut validator = self
.state
.validator_system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use anchor_lang::solana_program::{
self,
state::{Authorized, Lockup, StakeState},
},
system_instruction, system_program,
system_instruction,
sysvar::stake_history,
};
use anchor_spl::stake::{Stake, StakeAccount};
Expand Down Expand Up @@ -45,6 +45,7 @@ pub struct StakeReserve<'info> {
/// CHECK: have no CPU budget to parse
pub stake_history: UncheckedAccount<'info>,
/// CHECK: CPI
#[account(address = stake::config::ID)]
pub stake_config: UncheckedAccount<'info>,

pub system_program: Program<'info, System>,
Expand Down Expand Up @@ -98,14 +99,6 @@ impl<'info> StakeReserve<'info> {
return Err(Error::from(ProgramError::InvalidAccountData).with_source(source!()));
}

check_address(self.stake_config.key, &stake::config::ID, "stake_config")?;
check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;
check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;

let staker = self.state.stake_deposit_authority();
let withdrawer = self.state.stake_withdraw_authority();

Expand Down
13 changes: 3 additions & 10 deletions programs/marinade-finance/src/instructions/crank/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::ops::{Deref, DerefMut};

use anchor_lang::prelude::*;
use anchor_lang::solana_program::{
program::invoke_signed, stake, system_instruction, system_program,
program::invoke_signed, stake, system_instruction,
};
use anchor_spl::stake::{Stake, StakeAccount};
use anchor_spl::token::{mint_to, spl_token, Mint, MintTo, Token};
use anchor_spl::token::{mint_to, Mint, MintTo, Token};

use crate::error::MarinadeError;
use crate::state::stake_system::{StakeRecord, StakeSystemHelpers};
use crate::{checks::check_address, state::StateHelpers, State};
use crate::{state::StateHelpers, State};

#[derive(Accounts)]
pub struct UpdateCommon<'info> {
Expand Down Expand Up @@ -109,8 +109,6 @@ impl<'info> UpdateCommon<'info> {
.check_treasury_msol_account(&self.treasury_msol_account)?;
self.state
.check_stake_withdraw_authority(self.stake_withdraw_authority.key)?;
check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;
check_address(self.token_program.key, &spl_token::ID, "token_program")?;

let virtual_reserve_balance = self
.state
Expand Down Expand Up @@ -339,11 +337,6 @@ impl<'info> UpdateDeactivated<'info> {
is_treasury_msol_ready_for_transfer,
} = self.begin(stake_index)?;

check_address(
self.system_program.to_account_info().key,
&system_program::ID,
"system_program",
)?;
self.state
.check_operational_sol_account(self.operational_sol_account.key)?;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke_signed, system_instruction, system_program};
use anchor_lang::solana_program::{program::invoke_signed, system_instruction};

use crate::state::delayed_unstake_ticket::TicketAccountData;
use crate::State;
use crate::{checks::check_address, state::StateHelpers, MarinadeError};
use crate::{state::StateHelpers, MarinadeError};

///How many epochs to wats for ticket. e.g.: Ticket created on epoch 14, ticket is due on epoch 15
const WAIT_EPOCHS: u64 = 1;
Expand Down Expand Up @@ -79,11 +79,6 @@ impl<'info> Claim<'info> {

pub fn process(&mut self) -> Result<()> {
// fn claim()
check_address(
self.system_program.to_account_info().key,
&system_program::ID,
"system_program",
)?;
self.check_ticket_account()?;

let lamports = self.ticket_account.lamports_amount;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anchor_lang::prelude::*;
use anchor_spl::token::{burn, spl_token, Burn, Mint, Token, TokenAccount};
use anchor_spl::token::{burn, Burn, Mint, Token, TokenAccount};

use crate::{
checks::{check_address, check_min_amount},
checks::check_min_amount,
state::delayed_unstake_ticket::TicketAccountData,
State,
};
Expand Down Expand Up @@ -72,7 +72,6 @@ impl<'info> OrderUnstake<'info> {
// fn order_unstake() // create delayed-unstake Ticket-account
pub fn process(&mut self, msol_amount: u64) -> Result<()> {
// fn order_unstake()
check_address(self.token_program.key, &spl_token::ID, "token_program")?;
self.check_burn_msol_from(msol_amount)?;
let ticket_beneficiary = self.burn_msol_from.owner;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::State;
use crate::{calc::shares_from_value, checks::*};
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke, system_instruction, system_program};
use anchor_spl::token::{mint_to, spl_token, Mint, MintTo, Token, TokenAccount};
use anchor_spl::token::{mint_to, Mint, MintTo, Token, TokenAccount};

#[derive(Accounts)]
pub struct AddLiquidity<'info> {
Expand Down Expand Up @@ -66,12 +66,6 @@ impl<'info> AddLiquidity<'info> {
self.state
.liq_pool
.check_liquidity_cap(lamports, self.liq_pool_sol_leg_pda.lamports())?;
check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;
check_address(self.token_program.key, &spl_token::ID, "token_program")?;

msg!("add-liq after check");
// Update virtual lp_supply by real one
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke_signed, system_instruction, system_program};
use anchor_spl::token::{spl_token, transfer, Mint, Token, TokenAccount, Transfer};
use anchor_lang::solana_program::{program::invoke_signed, system_instruction};
use anchor_spl::token::{transfer, Mint, Token, TokenAccount, Transfer};

use crate::checks::check_min_amount;
use crate::state::liq_pool::{LiqPool, LiqPoolHelpers};
use crate::State;
use crate::{checks::check_address, MarinadeError};
use crate::MarinadeError;

#[derive(Accounts)]
pub struct LiquidUnstake<'info> {
Expand Down Expand Up @@ -83,12 +83,6 @@ impl<'info> LiquidUnstake<'info> {
let is_treasury_msol_ready_for_transfer = self
.state
.check_treasury_msol_account(&self.treasury_msol_account)?;
check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;
check_address(self.token_program.key, &spl_token::ID, "token_program")?;

let max_lamports = self
.liq_pool_sol_leg_pda
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
calc::proportional,
checks::{check_address, check_min_amount},
checks::check_min_amount,
state::liq_pool::{LiqPool, LiqPoolHelpers},
State,
};
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke_signed, system_instruction, system_program};
use anchor_spl::token::{burn, spl_token, transfer, Burn, Mint, Token, TokenAccount, Transfer};
use anchor_lang::solana_program::{program::invoke_signed, system_instruction};
use anchor_spl::token::{burn, transfer, Burn, Mint, Token, TokenAccount, Transfer};

#[derive(Accounts)]
pub struct RemoveLiquidity<'info> {
Expand Down Expand Up @@ -86,12 +86,6 @@ impl<'info> RemoveLiquidity<'info> {
.check_liq_pool_msol_leg(self.liq_pool_msol_leg.to_account_info().key)?;
self.state
.check_liq_pool_msol_leg_authority(self.liq_pool_msol_leg_authority.key)?;
check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;
check_address(self.token_program.key, &spl_token::ID, "token_program")?;

// Update virtual lp_supply by real one
if self.lp_mint.supply > self.state.liq_pool.lp_supply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke_signed, system_instruction, system_program};

use crate::State;
use crate::{checks::check_address, ID};
use crate::ID;

#[derive(Accounts)]
pub struct AddValidator<'info> {
Expand Down Expand Up @@ -43,11 +43,6 @@ impl<'info> AddValidator<'info> {
);
return Err(Error::from(ProgramError::InsufficientFunds).with_source(source!()));
}
check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;

msg!("Add validator {}", self.validator_vote.key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke_signed, stake};
use anchor_spl::stake::{Stake, StakeAccount};

use crate::checks::check_address;

#[derive(Accounts)]
pub struct EmergencyUnstake<'info> {
#[account(mut)]
Expand Down Expand Up @@ -43,7 +41,6 @@ impl<'info> EmergencyUnstake<'info> {
.check_stake_deposit_authority(self.stake_deposit_authority.key)?;
self.state
.check_stake_deposit_authority(self.stake_deposit_authority.key)?;
check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;

let mut stake = self.state.stake_system.get_checked(
&self.stake_list.data.as_ref().borrow(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use anchor_lang::solana_program::{
};
use anchor_spl::stake::{Stake, StakeAccount};

use crate::checks::check_address;

#[derive(Accounts)]
pub struct PartialUnstake<'info> {
#[account(mut)]
Expand Down Expand Up @@ -70,7 +68,6 @@ impl<'info> PartialUnstake<'info> {
.check_stake_deposit_authority(self.stake_deposit_authority.key)?;
self.state
.check_stake_deposit_authority(self.stake_deposit_authority.key)?;
check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;

let mut validator = self
.state
Expand Down
14 changes: 2 additions & 12 deletions programs/marinade-finance/src/instructions/user/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::{program::invoke, system_instruction, system_program};
use anchor_spl::token::{
mint_to, spl_token, transfer, Mint, MintTo, Token, TokenAccount, Transfer,
mint_to, transfer, Mint, MintTo, Token, TokenAccount, Transfer,
};

use crate::state::liq_pool::{LiqPool, LiqPoolHelpers};
use crate::State;
use crate::{
checks::{check_address, check_min_amount},
checks::{check_min_amount},
state::StateHelpers,
};

Expand Down Expand Up @@ -62,16 +62,6 @@ impl<'info> Deposit<'info> {
self.check_transfer_from(lamports)?;
self.state
.check_msol_mint_authority(self.msol_mint_authority.key)?;
check_address(
self.system_program.to_account_info().key,
&system_program::ID,
"system_program",
)?;
check_address(
self.token_program.to_account_info().key,
&spl_token::ID,
"token_program",
)?;

// impossible to happen check outside bug (msol mint auth is a PDA)
if self.msol_mint.supply > self.state.msol_supply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use anchor_lang::solana_program::{
system_instruction, system_program,
};
use anchor_spl::stake::{Stake, StakeAccount};
use anchor_spl::token::{mint_to, spl_token, Mint, MintTo, Token, TokenAccount};
use anchor_spl::token::{mint_to, Mint, MintTo, Token, TokenAccount};

use crate::checks::check_owner_program;
use crate::error::MarinadeError;
use crate::state::stake_system::StakeSystemHelpers;
use crate::State;
use crate::{checks::check_address, state::StateHelpers, ID};
use crate::{state::StateHelpers, ID};

#[derive(Accounts)]
pub struct DepositStakeAccount<'info> {
Expand Down Expand Up @@ -65,18 +65,6 @@ impl<'info> DepositStakeAccount<'info> {
self.state
.check_msol_mint_authority(self.msol_mint_authority.key)?;

check_address(
self.system_program.key,
&system_program::ID,
"system_program",
)?;
check_address(
self.token_program.to_account_info().key,
&spl_token::ID,
"token_program",
)?;
check_address(self.stake_program.key, &stake::program::ID, "stake_program")?;

// impossible to happen check (msol mint auth is a PDA)
if self.msol_mint.supply > self.state.msol_supply {
msg!(
Expand Down

0 comments on commit 873d3e4

Please sign in to comment.