use crate::error::ErrorCode;
use crate::states::*;
use anchor_lang::prelude::*;
use std::ops::DerefMut;
#[derive(Accounts)]
#[instruction(index: u64)]
pub struct CreateAmmConfig<'info> {
#[account
(mut,
address = crate::admin::id() @ ErrorCode::InvalidOwner)
]
pub owner: Signer<'info>,
#[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(())
}