-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mmm] T22 extension MMM support (#87)
* [mmm] initial commit * [mmm] deposit sell for T22 extension * fmt * add metadata check * remove mut * add entrypoints * merge check fn * add integration test * integration test fixes * add multiple unit tests * add fulfill sell + buy and integration tests * fmt * minor refactor * add ext_withdraw_sell * withdraw from m2 first * address comment * add test for shared escrow to trigger pool close/remain_open * fix test fn import * address comments * update test * add todo * change name * fix test * add comment for lp_fee_bp
- Loading branch information
Showing
26 changed files
with
4,123 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,8 @@ | |
"@metaplex-foundation/umi-bundle-tests": "^0.8.2", | ||
"@metaplex-foundation/umi-web3js-adapters": "^0.8.2", | ||
"@project-serum/anchor": "^0.26.0", | ||
"@solana/spl-token": "^0.3.5", | ||
"@solana/spl-token": "^0.4.1", | ||
"@solana/spl-token-group": "^0.0.1", | ||
"@solana/web3.js": "^1.65.0", | ||
"borsh": "^0.7.0", | ||
"old-mpl-token-metadata": "npm:@metaplex-foundation/[email protected]" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
programs/mmm/src/instructions/ext_vanilla/ext_deposit_sell.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use anchor_lang::{prelude::*, AnchorDeserialize}; | ||
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, | ||
state::{Pool, SellState}, | ||
util::{check_allowlists_for_mint_ext, log_pool}, | ||
DepositSellArgs, | ||
}; | ||
|
||
#[derive(Accounts)] | ||
#[instruction(args: DepositSellArgs)] | ||
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>>, | ||
#[account( | ||
mint::token_program = token_program, | ||
constraint = asset_mint.supply == 1 && asset_mint.decimals == 0 @ MMMErrorCode::InvalidTokenMint, | ||
)] | ||
pub asset_mint: Box<InterfaceAccount<'info, Mint>>, | ||
#[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 fn handler<'info>( | ||
ctx: Context<'_, '_, '_, 'info, ExtDepositeSell<'info>>, | ||
args: DepositSellArgs, | ||
) -> 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_allowlists_for_mint_ext( | ||
&pool.allowlists, | ||
&asset_mint.to_account_info(), | ||
args.allowlist_aux, | ||
)?; | ||
|
||
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_ext_deposit_sell", pool)?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.