gobblerdev 0.2.1

Created with Anchor
Documentation
use crate::error::ErrorCode;
use crate::states::*;
use anchor_lang::prelude::*;
use std::ops::DerefMut;

#[derive(Accounts)]
#[instruction(index: u64)]
pub struct CreateAmmConfig<'info> {
    /// Address to be set as protocol owner.
    #[account
        (mut, 
        address = crate::admin::id() @ ErrorCode::InvalidOwner)
    ]
    pub owner: Signer<'info>,

    /// Initialize config state account to store protocol owner address and fee rates.
    #[account(
        init,
        seeds = [
            AMM_CONFIG_SEED.as_bytes(),
            &index.to_be_bytes()
        ],
        bump,
        payer = owner,
        space = AmmConfig::LEN
    )]
    pub amm_config: Account<'info, AmmConfig>,

    pub system_program: Program<'info, System>,
}

pub fn create_amm_config(
    ctx: Context<CreateAmmConfig>,
    index: u64,
    token_0_lp_rate: u64,
    token_1_lp_rate: u64,
    protocol_fee: u64,
    creator_fee: u64,
) -> Result<()> {
    let owner = ctx.accounts.owner.key();

    #[cfg(not(feature = "local-testing"))]
    if owner != crate::admin::id() {
        return Err(ErrorCode::InvalidOwner.into());
    }
    let amm_config = ctx.accounts.amm_config.deref_mut();
    amm_config.protocol_owner = crate::admin::id();
    amm_config.bump = ctx.bumps.amm_config;
    amm_config.disable_create_pool = false;
    amm_config.index = index;
    amm_config.token_0_lp_rate = token_0_lp_rate;
    amm_config.token_1_lp_rate = token_1_lp_rate;
    amm_config.protocol_fee = protocol_fee;
    amm_config.creator_fee = creator_fee;
    amm_config.fund_owner = ctx.accounts.owner.key();
    Ok(())
}