|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use anchor_spl::{ |
| 3 | + associated_token::AssociatedToken, |
| 4 | + token::{Mint, Token, TokenAccount}, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + constants::{AUTHORITY_SEED, LIQUIDITY_SEED}, |
| 9 | + errors::*, |
| 10 | + state::{Amm, Pool}, |
| 11 | +}; |
| 12 | + |
| 13 | +pub fn create_pool(ctx: Context<CreatePool>) -> Result<()> { |
| 14 | + let pool = &mut ctx.accounts.pool; |
| 15 | + pool.amm = ctx.accounts.amm.key(); |
| 16 | + pool.mint_a = ctx.accounts.mint_a.key(); |
| 17 | + pool.mint_b = ctx.accounts.mint_b.key(); |
| 18 | + |
| 19 | + Ok(()) |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Accounts)] |
| 23 | +pub struct CreatePool<'info> { |
| 24 | + #[account( |
| 25 | + seeds = [ |
| 26 | + amm.id.as_ref() |
| 27 | + ], |
| 28 | + bump, |
| 29 | + )] |
| 30 | + pub amm: Account<'info, Amm>, |
| 31 | + |
| 32 | + #[account( |
| 33 | + init, |
| 34 | + payer = payer, |
| 35 | + space = Pool::LEN, |
| 36 | + seeds = [ |
| 37 | + amm.key().as_ref(), |
| 38 | + mint_a.key().as_ref(), |
| 39 | + mint_b.key().as_ref(), |
| 40 | + ], |
| 41 | + bump, |
| 42 | + constraint = mint_a.key() < mint_b.key() @ TutorialError::InvalidMint |
| 43 | + )] |
| 44 | + pub pool: Account<'info, Pool>, |
| 45 | + |
| 46 | + /// CHECK: Read only authority |
| 47 | + #[account( |
| 48 | + seeds = [ |
| 49 | + amm.key().as_ref(), |
| 50 | + mint_a.key().as_ref(), |
| 51 | + mint_b.key().as_ref(), |
| 52 | + AUTHORITY_SEED.as_ref(), |
| 53 | + ], |
| 54 | + bump, |
| 55 | + )] |
| 56 | + pub pool_authority: AccountInfo<'info>, |
| 57 | + |
| 58 | + #[account( |
| 59 | + init, |
| 60 | + payer = payer, |
| 61 | + seeds = [ |
| 62 | + amm.key().as_ref(), |
| 63 | + mint_a.key().as_ref(), |
| 64 | + mint_b.key().as_ref(), |
| 65 | + LIQUIDITY_SEED.as_ref(), |
| 66 | + ], |
| 67 | + bump, |
| 68 | + mint::decimals = 6, |
| 69 | + mint::authority = pool_authority, |
| 70 | + )] |
| 71 | + pub mint_liquidity: Box<Account<'info, Mint>>, |
| 72 | + |
| 73 | + pub mint_a: Box<Account<'info, Mint>>, |
| 74 | + |
| 75 | + pub mint_b: Box<Account<'info, Mint>>, |
| 76 | + |
| 77 | + #[account( |
| 78 | + init, |
| 79 | + payer = payer, |
| 80 | + associated_token::mint = mint_a, |
| 81 | + associated_token::authority = pool_authority, |
| 82 | + )] |
| 83 | + pub pool_account_a: Box<Account<'info, TokenAccount>>, |
| 84 | + |
| 85 | + #[account( |
| 86 | + init, |
| 87 | + payer = payer, |
| 88 | + associated_token::mint = mint_b, |
| 89 | + associated_token::authority = pool_authority, |
| 90 | + )] |
| 91 | + pub pool_account_b: Box<Account<'info, TokenAccount>>, |
| 92 | + |
| 93 | + /// The account paying for all rents |
| 94 | + #[account(mut)] |
| 95 | + pub payer: Signer<'info>, |
| 96 | + |
| 97 | + /// Solana ecosystem accounts |
| 98 | + pub token_program: Program<'info, Token>, |
| 99 | + pub associated_token_program: Program<'info, AssociatedToken>, |
| 100 | + pub system_program: Program<'info, System>, |
| 101 | +} |
0 commit comments