raydium-amm-v3 中文注释: create_pool

以下是添加了中文解释的代码:usecrate::error::ErrorCode;usecrate::states::;usecrate::{libraries::tick_math,util};useanchor_lang::prelude::;useanchor_spl

以下是添加了中文解释的代码:

use crate::error::ErrorCode;
use crate::states::*;
use crate::{libraries::tick_math, util};
use anchor_lang::prelude::*;
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
// use solana_program::{program::invoke_signed, system_instruction};

#[derive(Accounts)]
pub struct CreatePool<'info> {
    /// 创建池子的付款地址。可以是任何人
    #[account(mut)]
    pub pool_creator: Signer<'info>,

    /// 池子所属的配置
    pub amm_config: Box<Account<'info, AmmConfig>>,

    /// 初始化一个账户来存储池子的状态
    #[account(
        init,
        seeds = [
            POOL_SEED.as_bytes(),
            amm_config.key().as_ref(),
            token_mint_0.key().as_ref(),
            token_mint_1.key().as_ref(),
        ],
        bump,
        payer = pool_creator,
        space = PoolState::LEN
    )]
    pub pool_state: AccountLoader<'info, PoolState>,

    /// Token_0 的 mint,key 必须大于 token_1 的 mint
    #[account(
        constraint = token_mint_0.key() < token_mint_1.key(),
        mint::token_program = token_program_0
    )]
    pub token_mint_0: Box<InterfaceAccount<'info, Mint>>,

    /// Token_1 的 mint
    #[account(
        mint::token_program = token_program_1
    )]
    pub token_mint_1: Box<InterfaceAccount<'info, Mint>>,

    /// 池子的 Token_0 存储库
    #[account(
        init,
        seeds =[
            POOL_VAULT_SEED.as_bytes(),
            pool_state.key().as_ref(),
            token_mint_0.key().as_ref(),
        ],
        bump,
        payer = pool_creator,
        token::mint = token_mint_0,
        token::authority = pool_state,
        token::token_program = token_program_0,
    )]
    pub token_vault_0: Box<InterfaceAccount<'info, TokenAccount>>,

    /// 池子的 Token_1 存储库
    #[account(
        init,
        seeds =[
            POOL_VAULT_SEED.as_bytes(),
            pool_state.key().as_ref(),
            token_mint_1.key().as_ref(),
        ],
        bump,
        payer = pool_creator,
        token::mint = token_mint_1,
        token::authority = pool_state,
        token::token_program = token_program_1,
    )]
    pub token_vault_1: Box<InterfaceAccount<'info, TokenAccount>>,

    /// 检查:初始化一个账户来存储 oracle 的观测,账户必须在链下创建,合约会初始化它
    #[account(mut)]
    pub observation_state: UncheckedAccount<'info>,

    /// 初始化一个账户来存储 tick 数组是否初始化的状态
    #[account(
        init,
        seeds = [
            POOL_TICK_ARRAY_BITMAP_SEED.as_bytes(),
            pool_state.key().as_ref(),
        ],
        bump,
        payer = pool_creator,
        space = TickArrayBitmapExtension::LEN
    )]
    pub tick_array_bitmap: AccountLoader<'info, TickArrayBitmapExtension>,

    /// Spl token 程序或 token 程序 2022
    pub token_program_0: Interface<'info, TokenInterface>,
    /// Spl token 程序或 token 程序 2022
    pub token_program_1: Interface<'info, TokenInterface>,
    /// 创建新程序账户
    pub system_program: Program<'info, System>,
    /// 程序账户的 Sysvar
    pub rent: Sysvar<'info, Rent>,
}

pub fn create_pool(ctx: Context<CreatePool>, sqrt_price_x64: u128, open_time: u64) -> Result<()> {
    // 检查 token_mint_0 和 token_mint_1 是否受支持
    if !(util::is_supported_mint(&ctx.accounts.token_mint_0).unwrap()
        && util::is_supported_mint(&ctx.accounts.token_mint_1).unwrap())
    {
        return err!(ErrorCode::NotSupportMint);
    }
    let pool_id = ctx.accounts.pool_state.key();
    let mut pool_state = ctx.accounts.pool_state.load_init()?;

    // 获取 sqrt_price_x64 对应的 tick
    let tick = tick_math::get_tick_at_sqrt_price(sqrt_price_x64)?;
    #[cfg(feature = "enable-log")]
    msg!(
        "create pool, init_price: {}, init_tick:{}",
        sqrt_price_x64,
        tick
    );
    // 初始化观测状态
    ObservationState::initialize(ctx.accounts.observation_state.as_ref(), pool_id)?;

    let bump = ctx.bumps.pool_state;
    pool_state.initialize(
        bump,
        sqrt_price_x64,
        open_time,
        tick,
        ctx.accounts.pool_creator.key(),
        ctx.accounts.token_vault_0.key(),
        ctx.accounts.token_vault_1.key(),
        ctx.accounts.amm_config.as_ref(),
        ctx.accounts.token_mint_0.as_ref(),
        ctx.accounts.token_mint_1.as_ref(),
        ctx.accounts.observation_state.key(),
    )?;

    // 初始化 tick 数组位图
    ctx.accounts
        .tick_array_bitmap
        .load_init()?
        .initialize(pool_id);

    // 触发 PoolCreatedEvent 事件
    emit!(PoolCreatedEvent {
        token_mint_0: ctx.accounts.token_mint_0.key(),
        token_mint_1: ctx.accounts.token_mint_1.key(),
        tick_spacing: ctx.accounts.amm_config.tick_spacing,
        pool_state: ctx.accounts.pool_state.key(),
        sqrt_price_x64,
        tick,
        token_vault_0: ctx.accounts.token_vault_0.key(),
        token_vault_1: ctx.accounts.token_vault_1.key(),
    });
    Ok(())
}
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
blockchain002
blockchain002
江湖只有他的大名,没有他的介绍。