Skip to content

Commit

Permalink
support create token2022 nft mint with metadata extension when open p…
Browse files Browse the repository at this point in the history
…osition (#99)

* support create token2022 nft mint with metadata extension when open position

* remove unnecessary use of InterfaceAccount in V1 instruction
  • Loading branch information
0x777A authored Nov 21, 2024
1 parent 17582bf commit 119ed36
Show file tree
Hide file tree
Showing 35 changed files with 1,280 additions and 798 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ KeyPairs
client/target
**/*.rs.bk
node_modules
tests/dist
tests
test-ledger
.yarn*
yarn.lock
Makefile
sdk/node_modules
Expand Down
11 changes: 2 additions & 9 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@ anchor_version = "0.29"
[features]
seeds = false

[programs.mainnet]
[programs.localnet]
amm_v3 = "CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK"

[programs.devnet]
amm_v3 = "devi51mZmdwUJGU9hjN27vEz64Gps7uUefqxg27EAtH"

[registry]
url = "https://github.com/raydium-io/raydium-amm-v3"

[provider]
cluster = "localnet"
wallet = "~/.config/solana/id.json"

[[test.genesis]]
address = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
program = "./external-programs/spl_token_metadata.so"
wallet = "~/.config/solana/id.json"
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.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ Attention, check your configuration and confirm the environment you want to depl
An example of calling clmm can be found [here](https://github.com/raydium-io/raydium-cpi-example/tree/master/clmm-cpi)

# License
The source code is licensed under Apache 2.0.
The source code is [licensed](https://github.com/raydium-io/raydium-clmm/blob/master/LICENSE) under Apache 2.0.
4 changes: 2 additions & 2 deletions client/src/instructions/amm_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub fn open_position_instr(
tick_upper_index: i32,
tick_array_lower_start_index: i32,
tick_array_upper_start_index: i32,
with_matedata: bool,
with_metadata: bool,
) -> Result<Vec<Instruction>> {
let payer = read_keypair_file(&config.payer_path)?;
let url = Cluster::Custom(config.http_url.clone(), config.ws_url.clone());
Expand Down Expand Up @@ -293,7 +293,7 @@ pub fn open_position_instr(
tick_upper_index,
tick_array_lower_start_index,
tick_array_upper_start_index,
with_matedata,
with_metadata,
base_flag: None,
})
.instructions()?;
Expand Down
2 changes: 1 addition & 1 deletion client/src/instructions/events_instructions_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ pub fn handle_program_instruction(
amount_0_max: instr.amount_0_max,
amount_1_max: instr.amount_1_max,
base_flag: instr.base_flag,
with_metadata: instr.with_matedata,
with_metadata: instr.with_metadata,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions programs/amm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ solana-program = "<1.17.0"
spl-memo = "4.0.0"
uint = { git = "https://github.com/raydium-io/parity-common", package = "uint" }
mpl-token-metadata = { version = "^1.11.0", features = ["no-entrypoint"] }
spl-token-metadata-interface = { version = "=0.2.0" }
bytemuck = { version = "1.19.0", features = ["derive", "min_const_generics"] }
arrayref = { version = "0.3.6" }
solana-security-txt = "1.1.1"
Expand Down
4 changes: 2 additions & 2 deletions programs/amm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub enum ErrorCode {
InvaildTickIndex,
#[msg("The lower tick must be below the upper tick")]
TickInvaildOrder,
#[msg("The tick must be greater, or equal to the minimum tick(-221818)")]
#[msg("The tick must be greater, or equal to the minimum tick(-443636)")]
TickLowerOverflow,
#[msg("The tick must be lesser than, or equal to the maximum tick(221818)")]
#[msg("The tick must be lesser than, or equal to the maximum tick(443636)")]
TickUpperOverflow,
#[msg("tick % tick_spacing must be zero")]
TickAndSpacingNotMatch,
Expand Down
16 changes: 7 additions & 9 deletions programs/amm/src/instructions/admin/collect_fund_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use crate::states::*;
use crate::util::*;
use anchor_lang::prelude::*;
use anchor_spl::token::Token;
use anchor_spl::token_interface::Mint;
use anchor_spl::token_interface::Token2022;
use anchor_spl::token_interface::TokenAccount;
use anchor_spl::token_interface::{Mint, Token2022, TokenAccount};
#[derive(Accounts)]
pub struct CollectFundFee<'info> {
/// Only admin or fund_owner can collect fee now
Expand Down Expand Up @@ -79,8 +77,8 @@ pub fn collect_fund_fee(
}
transfer_from_pool_vault_to_user(
&ctx.accounts.pool_state,
&ctx.accounts.token_vault_0,
&ctx.accounts.recipient_token_account_0,
&ctx.accounts.token_vault_0.to_account_info(),
&ctx.accounts.recipient_token_account_0.to_account_info(),
Some(ctx.accounts.vault_0_mint.clone()),
&ctx.accounts.token_program,
Some(ctx.accounts.token_program_2022.to_account_info()),
Expand All @@ -89,8 +87,8 @@ pub fn collect_fund_fee(

transfer_from_pool_vault_to_user(
&ctx.accounts.pool_state,
&ctx.accounts.token_vault_1,
&ctx.accounts.recipient_token_account_1,
&ctx.accounts.token_vault_1.to_account_info(),
&ctx.accounts.recipient_token_account_1.to_account_info(),
Some(ctx.accounts.vault_1_mint.clone()),
&ctx.accounts.token_program,
Some(ctx.accounts.token_program_2022.to_account_info()),
Expand All @@ -99,8 +97,8 @@ pub fn collect_fund_fee(

check_unclaimed_fees_and_vault(
&ctx.accounts.pool_state,
&mut ctx.accounts.token_vault_0,
&mut ctx.accounts.token_vault_1,
&ctx.accounts.token_vault_0.to_account_info(),
&ctx.accounts.token_vault_1.to_account_info(),
)?;

emit!(CollectProtocolFeeEvent {
Expand Down
16 changes: 7 additions & 9 deletions programs/amm/src/instructions/admin/collect_protocol_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use crate::states::*;
use crate::util::*;
use anchor_lang::prelude::*;
use anchor_spl::token::Token;
use anchor_spl::token_interface::Mint;
use anchor_spl::token_interface::Token2022;
use anchor_spl::token_interface::TokenAccount;
use anchor_spl::token_interface::{Mint, Token2022, TokenAccount};

#[derive(Accounts)]
pub struct CollectProtocolFee<'info> {
Expand Down Expand Up @@ -89,8 +87,8 @@ pub fn collect_protocol_fee(
}
transfer_from_pool_vault_to_user(
&ctx.accounts.pool_state,
&ctx.accounts.token_vault_0,
&ctx.accounts.recipient_token_account_0,
&ctx.accounts.token_vault_0.to_account_info(),
&ctx.accounts.recipient_token_account_0.to_account_info(),
Some(ctx.accounts.vault_0_mint.clone()),
&ctx.accounts.token_program,
Some(ctx.accounts.token_program_2022.to_account_info()),
Expand All @@ -99,8 +97,8 @@ pub fn collect_protocol_fee(

transfer_from_pool_vault_to_user(
&ctx.accounts.pool_state,
&ctx.accounts.token_vault_1,
&ctx.accounts.recipient_token_account_1,
&ctx.accounts.token_vault_1.to_account_info(),
&ctx.accounts.recipient_token_account_1.to_account_info(),
Some(ctx.accounts.vault_1_mint.clone()),
&ctx.accounts.token_program,
Some(ctx.accounts.token_program_2022.to_account_info()),
Expand All @@ -109,8 +107,8 @@ pub fn collect_protocol_fee(

check_unclaimed_fees_and_vault(
&ctx.accounts.pool_state,
&mut ctx.accounts.token_vault_0,
&mut ctx.accounts.token_vault_1,
&ctx.accounts.token_vault_0.to_account_info(),
&ctx.accounts.token_vault_1.to_account_info(),
)?;

emit!(CollectProtocolFeeEvent {
Expand Down
53 changes: 29 additions & 24 deletions programs/amm/src/instructions/close_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,33 @@ use crate::error::ErrorCode;
use crate::states::*;
use crate::util::{burn, close_spl_account};
use anchor_lang::prelude::*;
use anchor_spl::token::Token;
use anchor_spl::token_interface::{Mint, TokenAccount};
use anchor_spl::token_2022::spl_token_2022;
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

#[derive(Accounts)]
pub struct ClosePosition<'info> {
/// The position nft owner
#[account(mut)]
pub nft_owner: Signer<'info>,

/// Unique token mint address
/// Mint address bound to the personal position.
#[account(
mut,
address = personal_position.nft_mint,
mint::token_program = token_program,
)]
pub position_nft_mint: Box<InterfaceAccount<'info, Mint>>,

/// Token account where position NFT will be minted
/// User token account where position NFT be minted to
#[account(
mut,
associated_token::mint = position_nft_mint,
associated_token::authority = nft_owner,
token::mint = position_nft_mint,
token::authority = nft_owner,
constraint = position_nft_account.amount == 1,
token::token_program = token_program,
)]
pub position_nft_account: Box<InterfaceAccount<'info, TokenAccount>>,

/// To store metaplex metadata
/// CHECK: Safety check performed inside function body
// #[account(mut)]
// pub metadata_account: UncheckedAccount<'info>,

/// Metadata for the tokenized position
#[account(
mut,
seeds = [POSITION_SEED.as_bytes(), position_nft_mint.key().as_ref()],
Expand All @@ -43,12 +37,11 @@ pub struct ClosePosition<'info> {
)]
pub personal_position: Box<Account<'info, PersonalPositionState>>,

/// Program to create the position manager state account
/// System program to close the position state account
pub system_program: Program<'info, System>,
/// Program to create mint account and mint tokens
pub token_program: Program<'info, Token>,
// /// Reserved for upgrade
// pub token_program_2022: Program<'info, Token2022>,

/// Token/Token2022 program to close token/mint account
pub token_program: Interface<'info, TokenInterface>,
}

pub fn close_position<'a, 'b, 'c, 'info>(
Expand Down Expand Up @@ -78,24 +71,36 @@ pub fn close_position<'a, 'b, 'c, 'info>(
}
}

let token_program = ctx.accounts.token_program.to_account_info();
let position_nft_mint = ctx.accounts.position_nft_mint.to_account_info();
let personal_nft_account = ctx.accounts.position_nft_account.to_account_info();
burn(
&ctx.accounts.nft_owner,
&ctx.accounts.position_nft_mint,
&ctx.accounts.position_nft_account,
&ctx.accounts.token_program,
// &ctx.accounts.token_program_2022,
&position_nft_mint,
&personal_nft_account,
&token_program,
&[],
1,
)?;

// close use nft token account
close_spl_account(
&ctx.accounts.nft_owner,
&ctx.accounts.nft_owner,
&ctx.accounts.position_nft_account,
&ctx.accounts.token_program,
// &ctx.accounts.token_program_2022,
&personal_nft_account,
&token_program,
&[],
)?;

if *position_nft_mint.owner == spl_token_2022::id() {
// close nft mint account
close_spl_account(
&ctx.accounts.personal_position.to_account_info(),
&ctx.accounts.nft_owner,
&position_nft_mint,
&token_program,
&[&ctx.accounts.personal_position.seeds()],
)?;
}
Ok(())
}
4 changes: 2 additions & 2 deletions programs/amm/src/instructions/collect_remaining_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub fn collect_remaining_rewards(

transfer_from_pool_vault_to_user(
&ctx.accounts.pool_state,
&ctx.accounts.reward_token_vault,
&ctx.accounts.funder_token_account,
&ctx.accounts.reward_token_vault.to_account_info(),
&ctx.accounts.funder_token_account.to_account_info(),
Some(ctx.accounts.reward_vault_mint.clone()),
&ctx.accounts.token_program,
Some(ctx.accounts.token_program_2022.to_account_info()),
Expand Down
2 changes: 1 addition & 1 deletion programs/amm/src/instructions/create_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct CreatePool<'info> {
)]
pub pool_state: AccountLoader<'info, PoolState>,

/// Token_0 mint, the key must grater then token_1 mint.
/// Token_0 mint, the key must be smaller then token_1 mint.
#[account(
constraint = token_mint_0.key() < token_mint_1.key(),
mint::token_program = token_program_0
Expand Down
Loading

0 comments on commit 119ed36

Please sign in to comment.