Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mmm] T22 extension MMM support #87

Merged
merged 24 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/mmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ community-managed-token = { version = "0.3.1", features = ["no-entrypoint"] }
mpl-token-metadata = { version = "4.0.0" }
open_creator_protocol = { version = "0.4.2", features = ["cpi"] }
solana-program = "~1.17"
spl-token-group-interface = "0.1.0"
spl-token = { version = "4.0.0", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "2.2.0", features = [
"no-entrypoint",
Expand Down
2 changes: 2 additions & 0 deletions programs/mmm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ pub enum MMMErrorCode {
UnexpectedMetadataUri, // 0x178c
#[msg("Invalid remaining accounts")]
InvalidRemainingAccounts, // 0x178d
#[msg("Invalid token extensions")]
InValidTokenExtension, // 0x178e
}
41 changes: 41 additions & 0 deletions programs/mmm/src/ext_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use solana_program::{account_info::AccountInfo, pubkey::Pubkey};
use spl_token_2022::{
extension::{
group_member_pointer::GroupMemberPointer, BaseStateWithExtensions, StateWithExtensions,
},
state::Mint as Token22Mint,
};
use spl_token_group_interface::state::TokenGroupMember;

use crate::state::{Allowlist, ALLOWLIST_KIND_GROUP};

use {crate::errors::MMMErrorCode, anchor_lang::prelude::*};

pub fn check_group_ext_for_mint(token_mint: &AccountInfo, allowlists: &[Allowlist]) -> Result<()> {
solonk8 marked this conversation as resolved.
Show resolved Hide resolved
if 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 {
solonk8 marked this conversation as resolved.
Show resolved Hide resolved
return Err(MMMErrorCode::InValidTokenExtension.into());
}
} else {
return Err(MMMErrorCode::InValidTokenExtension.into());
}

Ok(())
}
131 changes: 131 additions & 0 deletions programs/mmm/src/instructions/ext_vanilla/ext_deposit_sell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use anchor_lang::{prelude::*, AnchorDeserialize, AnchorSerialize};
use anchor_spl::{
associated_token::AssociatedToken,
token_interface::{Mint, TokenAccount, TokenInterface},
};
use solana_program::program::invoke;
use spl_token_2022::onchain::invoke_transfer_checked;

use crate::{
constants::*,
errors::MMMErrorCode,
ext_util::check_group_ext_for_mint,
state::{Pool, SellState},
util::log_pool,
};

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct ExtDepositeSellArgs {
solonk8 marked this conversation as resolved.
Show resolved Hide resolved
pub asset_amount: u64,
}

#[derive(Accounts)]
#[instruction(args: ExtDepositeSellArgs)]
pub struct ExtDepositeSell<'info> {
#[account(mut)]
pub owner: Signer<'info>,
pub cosigner: Signer<'info>,
#[account(
mut,
seeds = [POOL_PREFIX.as_bytes(), owner.key().as_ref(), pool.uuid.as_ref()],
has_one = owner @ MMMErrorCode::InvalidOwner,
has_one = cosigner @ MMMErrorCode::InvalidCosigner,
bump
)]
pub pool: Box<Account<'info, Pool>>,
pub asset_mint: InterfaceAccount<'info, Mint>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets enforce token program here

#[account(
mut,
associated_token::mint = asset_mint,
associated_token::authority = owner,
associated_token::token_program = token_program,
)]
pub asset_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
#[account(
init_if_needed,
payer = owner,
associated_token::mint = asset_mint,
associated_token::authority = pool,
associated_token::token_program = token_program,
)]
pub sellside_escrow_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
#[account(
init_if_needed,
payer = owner,
seeds = [
SELL_STATE_PREFIX.as_bytes(),
pool.key().as_ref(),
asset_mint.key().as_ref(),
],
space = SellState::LEN,
bump
)]
pub sell_state: Account<'info, SellState>,
pub system_program: Program<'info, System>,
pub token_program: Interface<'info, TokenInterface>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub rent: Sysvar<'info, Rent>,
solonk8 marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn handler<'info>(
ctx: Context<'_, '_, '_, 'info, ExtDepositeSell<'info>>,
args: ExtDepositeSellArgs,
) -> Result<()> {
let owner = &ctx.accounts.owner;
let asset_token_account = &ctx.accounts.asset_token_account;
let asset_mint = &ctx.accounts.asset_mint;
let sellside_escrow_token_account = &ctx.accounts.sellside_escrow_token_account;
let token_program = &ctx.accounts.token_program;
let pool = &mut ctx.accounts.pool;
let sell_state = &mut ctx.accounts.sell_state;

if pool.using_shared_escrow() {
return Err(MMMErrorCode::InvalidAccountState.into());
}

check_group_ext_for_mint(&asset_mint.to_account_info(), &pool.allowlists)?;
invoke_transfer_checked(
token_program.key,
asset_token_account.to_account_info(),
asset_mint.to_account_info(),
sellside_escrow_token_account.to_account_info(),
owner.to_account_info(),
ctx.remaining_accounts,
args.asset_amount,
0,
&[],
)?;

if asset_token_account.amount == args.asset_amount {
invoke(
&spl_token_2022::instruction::close_account(
token_program.key,
&asset_token_account.key(),
&owner.key(),
&owner.key(),
&[],
)?,
&[
asset_token_account.to_account_info(),
owner.to_account_info(),
],
)?;
}

pool.sellside_asset_amount = pool
.sellside_asset_amount
.checked_add(args.asset_amount)
.ok_or(MMMErrorCode::NumericOverflow)?;

sell_state.pool = pool.key();
sell_state.pool_owner = owner.key();
sell_state.asset_mint = asset_mint.key();
sell_state.cosigner_annotation = pool.cosigner_annotation;
sell_state.asset_amount = sell_state
.asset_amount
.checked_add(args.asset_amount)
.ok_or(MMMErrorCode::NumericOverflow)?;
log_pool("post_deposit_sell", pool)?;
solonk8 marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
3 changes: 3 additions & 0 deletions programs/mmm/src/instructions/ext_vanilla/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod ext_deposit_sell;

pub use ext_deposit_sell::*;
2 changes: 2 additions & 0 deletions programs/mmm/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![allow(missing_docs)]

pub mod admin;
pub mod ext_vanilla;
pub mod mip1;
pub mod ocp;
pub mod vanilla;

pub use admin::*;
pub use ext_vanilla::*;
pub use mip1::*;
pub use ocp::*;
pub use vanilla::*;
Expand Down
1 change: 1 addition & 0 deletions programs/mmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare_id!("mmm3XBJg5gk8XJxEKBvdgptZz6SgK4tXvn36sodowMc");
mod ata;
mod constants;
mod errors;
mod ext_util;
pub mod instructions;
pub mod state;
pub mod util;
Expand Down
6 changes: 4 additions & 2 deletions programs/mmm/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub const ALLOWLIST_KIND_FVCA: u8 = 1;
pub const ALLOWLIST_KIND_MINT: u8 = 2;
pub const ALLOWLIST_KIND_MCC: u8 = 3;
pub const ALLOWLIST_KIND_METADATA: u8 = 4;
pub const ALLOWLIST_KIND_GROUP: u8 = 5;
// ANY nft will pass the allowlist check, please make sure to use cosigner to check NFT validity
pub const ALLOWLIST_KIND_ANY: u8 = u8::MAX;

Expand All @@ -25,10 +26,11 @@ impl Allowlist {
// kind == 2: single mint, useful for SFT
// kind == 3: verified MCC
// kind == 4: metadata
// kind == 5,6,... will be supported in the future
// kind == 5: group extension
// kind == 6,7,... will be supported in the future
// kind == 255: any
pub fn valid(&self) -> bool {
if self.kind > ALLOWLIST_KIND_METADATA && self.kind != ALLOWLIST_KIND_ANY {
if self.kind > ALLOWLIST_KIND_GROUP && self.kind != ALLOWLIST_KIND_ANY {
return false;
}
if self.kind != 0 && self.kind != ALLOWLIST_KIND_ANY {
Expand Down
24 changes: 24 additions & 0 deletions sdk/src/idl/mmm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,18 @@ export type Mmm = {
]
}
},
{
"name": "ExtDepositeSellArgs",
"type": {
"kind": "struct",
"fields": [
{
"name": "assetAmount",
"type": "u64"
}
]
}
},
{
"name": "SolMip1FulfillSellArgs",
"type": {
Expand Down Expand Up @@ -4155,6 +4167,18 @@ export const IDL: Mmm = {
]
}
},
{
"name": "ExtDepositeSellArgs",
"type": {
"kind": "struct",
"fields": [
{
"name": "assetAmount",
"type": "u64"
}
]
}
},
{
"name": "SolMip1FulfillSellArgs",
"type": {
Expand Down
Loading