Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Apr 28, 2021
1 parent ef98f50 commit affa6fd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 76 deletions.
14 changes: 5 additions & 9 deletions examples/swap/programs/swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ pub mod swap {
)
};

// Assert this trade is acceptable.
apply_risk_checks_transitive(
&ctx,
DidSwap {
Expand All @@ -162,20 +161,14 @@ pub mod swap {
}

fn apply_risk_checks(ctx: &Context<Swap>, event: DidSwap) -> Result<()> {
// msg!("event: {:?}", event);
// todo
//
// TODO: check that the executed within some % from mid/fair.
// emit!(event);
emit!(event);
Ok(())
}

fn apply_risk_checks_transitive(ctx: &Context<SwapTransitive>, event: DidSwap) -> Result<()> {
// msg!("event: {:?}", event);
// todo
//
// TODO: check that the executed within some % from mid/fair.
// emit!(event);
emit!(event);
Ok(())
}

Expand Down Expand Up @@ -374,6 +367,8 @@ fn coin_lots(market: &MarketState, size: u64) -> u64 {
size.checked_div(market.coin_lot_size).unwrap()
}

// Market accounts are the accounts used to place orders against the dex minus
// common accounts, i.e., program ids, sysvars, and the `pc_wallet`.
#[derive(Accounts, Clone)]
pub struct MarketAccounts<'info> {
#[account(mut)]
Expand Down Expand Up @@ -402,6 +397,7 @@ pub struct MarketAccounts<'info> {
// this is the vault for the B mint.
#[account(mut)]
pc_vault: AccountInfo<'info>,
// PDA owner of the DEX's token accounts for base + quote currencies.
vault_signer: AccountInfo<'info>,
// User wallets.
#[account(mut)]
Expand Down
67 changes: 1 addition & 66 deletions spl/src/dex.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use anchor_lang::solana_program::account_info::AccountInfo;
use anchor_lang::solana_program::entrypoint::ProgramResult;
use anchor_lang::solana_program::instruction::AccountMeta;
use anchor_lang::solana_program::program_error::ProgramError;
use anchor_lang::{Accounts, AccountsExit, CpiContext, ToAccountInfos, ToAccountMetas};
use anchor_lang::{Accounts, CpiContext, ToAccountInfos};
use serum_dex::instruction::SelfTradeBehavior;
use serum_dex::matching::{OrderType, Side};
use serum_dex::state::MarketState;
use solana_program::pubkey::Pubkey;
use std::cell::RefMut;
use std::num::NonZeroU64;

pub use serum_dex;
Expand Down Expand Up @@ -117,63 +112,3 @@ pub struct SettleFunds<'info> {
pub vault_signer: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
}

pub struct Market<'info> {
acc_info: AccountInfo<'info>,
}

impl<'info> Market<'info> {
fn new(acc_info: AccountInfo<'info>) -> Market<'info> {
Self { acc_info }
}

pub fn load_mut(&self) -> Result<RefMut<MarketState>, ProgramError> {
MarketState::load(&self.acc_info, &ID).map_err(Into::into)
}
}

impl<'info> anchor_lang::Accounts<'info> for Market<'info> {
#[inline(never)]
fn try_accounts(
_program_id: &Pubkey,
accounts: &mut &[AccountInfo<'info>],
) -> Result<Self, ProgramError> {
if accounts.is_empty() {
return Err(ProgramError::NotEnoughAccountKeys);
}
let account = &accounts[0];
*accounts = &accounts[1..];
let l = Market::new(account.clone());
if l.acc_info.owner != &ID {
return Err(ProgramError::Custom(1)); // todo: proper error
}
Ok(l)
}
}

impl<'info> ToAccountMetas for Market<'info> {
#[inline(never)]
fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
let is_signer = is_signer.unwrap_or(self.acc_info.is_signer);
let meta = match self.acc_info.is_writable {
false => AccountMeta::new_readonly(*self.acc_info.key, is_signer),
true => AccountMeta::new(*self.acc_info.key, is_signer),
};
vec![meta]
}
}

impl<'info> ToAccountInfos<'info> for Market<'info> {
#[inline(never)]
fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
vec![self.acc_info.clone()]
}
}

impl<'info> AccountsExit<'info> for Market<'info> {
#[inline(never)]
fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
// no-op
Ok(())
}
}
3 changes: 2 additions & 1 deletion spl/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ impl Deref for Mint {
}
}

// Field parsers to save compute.
// Field parsers to save compute. All account validation is assumed to be done
// outside of these methods.
pub mod accessor {
use super::*;

Expand Down

0 comments on commit affa6fd

Please sign in to comment.