Skip to content

Commit

Permalink
merge check fn
Browse files Browse the repository at this point in the history
  • Loading branch information
solonk8 committed Mar 7, 2024
1 parent 71951f5 commit 230df91
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
53 changes: 24 additions & 29 deletions programs/mmm/src/ext_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub fn check_allowlists_for_mint_ext(
if !mint_deserialized.base.is_initialized {
return Err(MMMErrorCode::InvalidTokenMint.into());
}

// verify metadata extension
if let Ok(metadata_ptr) = mint_deserialized.get_extension::<MetadataPointer>() {
if Option::<Pubkey>::from(metadata_ptr.metadata_address) != Some(*token_mint.key) {
return Err(MMMErrorCode::InValidTokenExtension.into());
Expand Down Expand Up @@ -52,6 +54,28 @@ pub fn check_allowlists_for_mint_ext(
}
}

// verify group member extension
if let Ok(group_member_ptr) = mint_deserialized.get_extension::<GroupMemberPointer>() {
if Some(*token_mint.key) != Option::<Pubkey>::from(group_member_ptr.member_address) {
return Err(MMMErrorCode::InValidTokenExtension.into());
}
}
if let Ok(group_member) = mint_deserialized.get_extension::<TokenGroupMember>() {
let group_allowlist = allowlists
.iter()
.find(|allowlist| allowlist.kind == ALLOWLIST_KIND_GROUP);
if Some(group_member.group) != group_allowlist.map(|allowlist| allowlist.value) {
return Err(MMMErrorCode::InValidTokenExtension.into());
}
// counter spoof check
if Some(group_member.mint) != Some(*token_mint.key) {
return Err(MMMErrorCode::InValidTokenExtension.into());
}

} else {
return Err(MMMErrorCode::InValidTokenExtension.into());
}

for allowlist_val in allowlists.iter() {
match allowlist_val.kind {
ALLOWLIST_KIND_EMPTY => {}
Expand Down Expand Up @@ -85,32 +109,3 @@ pub fn check_allowlists_for_mint_ext(
// at the end, we didn't find a match, thus return err
Err(MMMErrorCode::InvalidAllowLists.into())
}

pub fn check_group_ext_for_mint(token_mint: &AccountInfo, allowlists: &[Allowlist]) -> Result<()> {
if token_mint.owner != &spl_token_2022::ID || token_mint.data_is_empty() {
return Err(MMMErrorCode::InvalidTokenMint.into());
}
let borrowed_data = token_mint.data.borrow();
let mint_deserialized = StateWithExtensions::<Token22Mint>::unpack(&borrowed_data)?;
if !mint_deserialized.base.is_initialized {
return Err(MMMErrorCode::InvalidTokenMint.into());
}
if let Ok(group_member_ptr) = mint_deserialized.get_extension::<GroupMemberPointer>() {
if Some(*token_mint.key) != Option::<Pubkey>::from(group_member_ptr.member_address) {
return Err(MMMErrorCode::InValidTokenExtension.into());
}
}
if let Ok(group_member) = mint_deserialized.get_extension::<TokenGroupMember>() {
let group_address = allowlists
.iter()
.find(|allowlist| allowlist.kind == ALLOWLIST_KIND_GROUP)
.map(|allowlist| allowlist.value);
if Some(group_member.group) != group_address {
return Err(MMMErrorCode::InValidTokenExtension.into());
}
} else {
return Err(MMMErrorCode::InValidTokenExtension.into());
}

Ok(())
}
19 changes: 6 additions & 13 deletions programs/mmm/src/instructions/ext_vanilla/ext_deposit_sell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anchor_lang::{prelude::*, AnchorDeserialize, AnchorSerialize};
use anchor_lang::{prelude::*, AnchorDeserialize};
use anchor_spl::{
associated_token::AssociatedToken,
token_interface::{Mint, TokenAccount, TokenInterface},
Expand All @@ -9,19 +9,14 @@ use spl_token_2022::onchain::invoke_transfer_checked;
use crate::{
constants::*,
errors::MMMErrorCode,
ext_util::{check_allowlists_for_mint_ext, check_group_ext_for_mint},
ext_util::check_allowlists_for_mint_ext,
state::{Pool, SellState},
util::log_pool,
DepositSellArgs,
};

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct ExtDepositeSellArgs {
pub asset_amount: u64,
pub allowlist_aux: Option<String>,
}

#[derive(Accounts)]
#[instruction(args: ExtDepositeSellArgs)]
#[instruction(args: DepositSellArgs)]
pub struct ExtDepositeSell<'info> {
#[account(mut)]
pub owner: Signer<'info>,
Expand Down Expand Up @@ -68,12 +63,11 @@ pub struct ExtDepositeSell<'info> {
pub system_program: Program<'info, System>,
pub token_program: Interface<'info, TokenInterface>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub rent: Sysvar<'info, Rent>,
}

pub fn handler<'info>(
ctx: Context<'_, '_, '_, 'info, ExtDepositeSell<'info>>,
args: ExtDepositeSellArgs,
args: DepositSellArgs,
) -> Result<()> {
let owner = &ctx.accounts.owner;
let asset_token_account = &ctx.accounts.asset_token_account;
Expand All @@ -92,7 +86,6 @@ pub fn handler<'info>(
&asset_mint.to_account_info(),
args.allowlist_aux,
)?;
check_group_ext_for_mint(&asset_mint.to_account_info(), &pool.allowlists)?;

invoke_transfer_checked(
token_program.key,
Expand Down Expand Up @@ -135,7 +128,7 @@ pub fn handler<'info>(
.asset_amount
.checked_add(args.asset_amount)
.ok_or(MMMErrorCode::NumericOverflow)?;
log_pool("post_deposit_sell", pool)?;
log_pool("post_ext_deposit_sell", pool)?;

Ok(())
}
2 changes: 1 addition & 1 deletion programs/mmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub mod mmm {

pub fn ext_deposit_sell<'info>(
ctx: Context<'_, '_, '_, 'info, ExtDepositeSell<'info>>,
args: ExtDepositeSellArgs,
args: DepositSellArgs,
) -> Result<()> {
instructions::ext_deposit_sell::handler(ctx, args)
}
Expand Down

0 comments on commit 230df91

Please sign in to comment.