From 58d6b5dac671ba3426371a972e4594f7190993a8 Mon Sep 17 00:00:00 2001 From: ysq Date: Thu, 17 Jul 2025 17:13:17 +0800 Subject: [PATCH] refactor(instruction): remove create functionality and restructure module - Remove Create struct and create instruction from PumpFun module - Clean up mod.rs by removing duplicate instruction implementations - Maintain core buy/sell instruction functionality - Simplify instruction module architecture --- src/instruction/mod.rs | 220 +------------------------------------ src/instruction/pumpfun.rs | 64 ----------- 2 files changed, 1 insertion(+), 283 deletions(-) diff --git a/src/instruction/mod.rs b/src/instruction/mod.rs index ccc2b16..baab225 100755 --- a/src/instruction/mod.rs +++ b/src/instruction/mod.rs @@ -1,222 +1,4 @@ -//! Instructions for interacting with the Pump.fun program. -//! -//! This module contains instruction builders for creating Solana instructions to interact with the -//! Pump.fun program. Each function takes the required accounts and instruction data and returns a -//! properly formatted Solana instruction. -//! -//! # Instructions -//! -//! - `create`: Instruction to create a new token with an associated bonding curve. -//! - `buy`: Instruction to buy tokens from a bonding curve by providing SOL. -//! - `sell`: Instruction to sell tokens back to the bonding curve in exchange for SOL. -use crate::{ - constants, - trading::pumpfun::common::{ - get_bonding_curve_pda, get_global_pda, get_metadata_pda, get_mint_authority_pda - }, -}; -use spl_associated_token_account::get_associated_token_address; - -use solana_sdk::{ - instruction::{AccountMeta, Instruction}, - pubkey::Pubkey, - signature::Keypair, - signer::Signer, -}; - pub mod pumpfun; pub mod pumpswap; pub mod bonk; -pub mod raydium_cpmm; - -pub struct Create { - pub _name: String, - pub _symbol: String, - pub _uri: String, - pub _creator: Pubkey, -} - -impl Create { - pub fn data(&self) -> Vec { - let mut data = Vec::with_capacity(8 + 4 + self._name.len() + 4 + self._symbol.len() + 4 + self._uri.len() + 32); - - // 追加 discriminator - data.extend_from_slice(&[24, 30, 200, 40, 5, 28, 7, 119]); // discriminator - - // 添加 name 字符串长度和内容 - data.extend_from_slice(&(self._name.len() as u32).to_le_bytes()); // 添加 name 长度 - data.extend_from_slice(self._name.as_bytes()); // 添加 name 内容 - - // 添加 symbol 字符串长度和内容 - data.extend_from_slice(&(self._symbol.len() as u32).to_le_bytes()); // 添加 symbol 长度 - data.extend_from_slice(self._symbol.as_bytes()); // 添加 symbol 内容 - - // 添加 uri 字符串长度和内容 - data.extend_from_slice(&(self._uri.len() as u32).to_le_bytes()); // 添加 uri 长度 - data.extend_from_slice(self._uri.as_bytes()); // 添加 uri 内容 - - data.extend_from_slice(&self._creator.to_bytes()); - - data - } -} - -pub struct Buy { - pub _amount: u64, - pub _max_sol_cost: u64, -} - -impl Buy { - pub fn data(&self) -> Vec { - let mut data = Vec::with_capacity(8 + 8 + 8); - data.extend_from_slice(&[102, 6, 61, 18, 1, 218, 235, 234]); // discriminator - data.extend_from_slice(&self._amount.to_le_bytes()); - data.extend_from_slice(&self._max_sol_cost.to_le_bytes()); - data - } -} - -pub struct Sell { - pub _amount: u64, - pub _min_sol_output: u64, -} - -impl Sell { - pub fn data(&self) -> Vec { - let mut data = Vec::with_capacity(8 + 8 + 8); - data.extend_from_slice(&[51, 230, 133, 164, 1, 127, 131, 173]); // discriminator - data.extend_from_slice(&self._amount.to_le_bytes()); - data.extend_from_slice(&self._min_sol_output.to_le_bytes()); - data - } -} - - -/// Creates an instruction to create a new token with bonding curve -/// -/// Creates a new SPL token with an associated bonding curve that determines its price. -/// -/// # Arguments -/// -/// * `payer` - Keypair that will pay for account creation and transaction fees -/// * `mint` - Keypair for the new token mint account that will be created -/// * `args` - Create instruction data containing token name, symbol and metadata URI -/// -/// # Returns -/// -/// Returns a Solana instruction that when executed will create the token and its accounts -pub fn create(payer: &Keypair, mint: &Keypair, args: Create) -> Instruction { - let bonding_curve: Pubkey = get_bonding_curve_pda(&mint.pubkey()).unwrap(); - Instruction::new_with_bytes( - constants::pumpfun::accounts::PUMPFUN, - &args.data(), - vec![ - AccountMeta::new(mint.pubkey(), true), - AccountMeta::new(get_mint_authority_pda(), false), - AccountMeta::new(bonding_curve, false), - AccountMeta::new( - get_associated_token_address(&bonding_curve, &mint.pubkey()), - false, - ), - AccountMeta::new_readonly(get_global_pda(), false), - AccountMeta::new_readonly(constants::pumpfun::accounts::MPL_TOKEN_METADATA, false), - AccountMeta::new(get_metadata_pda(&mint.pubkey()), false), - AccountMeta::new(payer.pubkey(), true), - AccountMeta::new_readonly(constants::pumpfun::accounts::SYSTEM_PROGRAM, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::TOKEN_PROGRAM, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::ASSOCIATED_TOKEN_PROGRAM, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::RENT, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::EVENT_AUTHORITY, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::PUMPFUN, false), - ], - ) -} - -/// Creates an instruction to buy tokens from a bonding curve -/// -/// Buys tokens by providing SOL. The amount of tokens received is calculated based on -/// the bonding curve formula. A portion of the SOL is taken as a fee and sent to the -/// fee recipient account. -/// -/// # Arguments -/// -/// * `payer` - Keypair that will provide the SOL to buy tokens -/// * `mint` - Public key of the token mint to buy -/// * `fee_recipient` - Public key of the account that will receive the transaction fee -/// * `args` - Buy instruction data containing the SOL amount and maximum acceptable token price -/// -/// # Returns -/// -/// Returns a Solana instruction that when executed will buy tokens from the bonding curve -pub fn buy( - payer: &Keypair, - mint: &Pubkey, - bonding_curve_pda: &Pubkey, - creator_vault_pda: &Pubkey, - fee_recipient: &Pubkey, - args: Buy, -) -> Instruction { - Instruction::new_with_bytes( - constants::pumpfun::accounts::PUMPFUN, - &args.data(), - vec![ - AccountMeta::new_readonly(constants::pumpfun::global_constants::GLOBAL_ACCOUNT, false), - AccountMeta::new(*fee_recipient, false), - AccountMeta::new_readonly(*mint, false), - AccountMeta::new(*bonding_curve_pda, false), - AccountMeta::new(get_associated_token_address(bonding_curve_pda, mint), false), - AccountMeta::new(get_associated_token_address(&payer.pubkey(), mint), false), - AccountMeta::new(payer.pubkey(), true), - AccountMeta::new_readonly(constants::pumpfun::accounts::SYSTEM_PROGRAM, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::TOKEN_PROGRAM, false), - AccountMeta::new(*creator_vault_pda, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::EVENT_AUTHORITY, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::PUMPFUN, false), - ], - ) -} - -/// Creates an instruction to sell tokens back to a bonding curve -/// -/// Sells tokens back to the bonding curve in exchange for SOL. The amount of SOL received -/// is calculated based on the bonding curve formula. A portion of the SOL is taken as -/// a fee and sent to the fee recipient account. -/// -/// # Arguments -/// -/// * `payer` - Keypair that owns the tokens to sell -/// * `mint` - Public key of the token mint to sell -/// * `fee_recipient` - Public key of the account that will receive the transaction fee -/// * `args` - Sell instruction data containing token amount and minimum acceptable SOL output -/// -/// # Returns -/// -/// Returns a Solana instruction that when executed will sell tokens to the bonding curve -pub fn sell( - payer: &Keypair, - mint: &Pubkey, - creator_vault_pda: &Pubkey, - fee_recipient: &Pubkey, - args: Sell, -) -> Instruction { - let bonding_curve: Pubkey = get_bonding_curve_pda(mint).unwrap(); - Instruction::new_with_bytes( - constants::pumpfun::accounts::PUMPFUN, - &args.data(), - vec![ - AccountMeta::new_readonly(constants::pumpfun::global_constants::GLOBAL_ACCOUNT, false), - AccountMeta::new(*fee_recipient, false), - AccountMeta::new_readonly(*mint, false), - AccountMeta::new(bonding_curve, false), - AccountMeta::new(get_associated_token_address(&bonding_curve, mint), false), - AccountMeta::new(get_associated_token_address(&payer.pubkey(), mint), false), - AccountMeta::new(payer.pubkey(), true), - AccountMeta::new_readonly(constants::pumpfun::accounts::SYSTEM_PROGRAM, false), - AccountMeta::new(*creator_vault_pda, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::TOKEN_PROGRAM, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::EVENT_AUTHORITY, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::PUMPFUN, false), - ], - ) -} - +pub mod raydium_cpmm; \ No newline at end of file diff --git a/src/instruction/pumpfun.rs b/src/instruction/pumpfun.rs index eb9c5e0..6a19e00 100755 --- a/src/instruction/pumpfun.rs +++ b/src/instruction/pumpfun.rs @@ -144,40 +144,6 @@ impl InstructionBuilder for PumpFunInstructionBuilder { } } -pub struct Create { - pub _name: String, - pub _symbol: String, - pub _uri: String, - pub _creator: Pubkey, -} - -impl Create { - pub fn data(&self) -> Vec { - let mut data = Vec::with_capacity( - 8 + 4 + self._name.len() + 4 + self._symbol.len() + 4 + self._uri.len() + 32, - ); - - // 追加 discriminator - data.extend_from_slice(&[24, 30, 200, 40, 5, 28, 7, 119]); // discriminator - - // 添加 name 字符串长度和内容 - data.extend_from_slice(&(self._name.len() as u32).to_le_bytes()); // 添加 name 长度 - data.extend_from_slice(self._name.as_bytes()); // 添加 name 内容 - - // 添加 symbol 字符串长度和内容 - data.extend_from_slice(&(self._symbol.len() as u32).to_le_bytes()); // 添加 symbol 长度 - data.extend_from_slice(self._symbol.as_bytes()); // 添加 symbol 内容 - - // 添加 uri 字符串长度和内容 - data.extend_from_slice(&(self._uri.len() as u32).to_le_bytes()); // 添加 uri 长度 - data.extend_from_slice(self._uri.as_bytes()); // 添加 uri 内容 - - data.extend_from_slice(&self._creator.to_bytes()); - - data - } -} - pub struct Buy { pub _amount: u64, pub _max_sol_cost: u64, @@ -208,36 +174,6 @@ impl Sell { } } -pub fn create(payer: &Keypair, mint: &Keypair, args: Create) -> Instruction { - let bonding_curve: Pubkey = get_bonding_curve_pda(&mint.pubkey()).unwrap(); - Instruction::new_with_bytes( - constants::pumpfun::accounts::PUMPFUN, - &args.data(), - vec![ - AccountMeta::new(mint.pubkey(), true), - AccountMeta::new(get_mint_authority_pda(), false), - AccountMeta::new(bonding_curve, false), - AccountMeta::new( - get_associated_token_address(&bonding_curve, &mint.pubkey()), - false, - ), - AccountMeta::new_readonly(get_global_pda(), false), - AccountMeta::new_readonly(constants::pumpfun::accounts::MPL_TOKEN_METADATA, false), - AccountMeta::new(get_metadata_pda(&mint.pubkey()), false), - AccountMeta::new(payer.pubkey(), true), - AccountMeta::new_readonly(constants::pumpfun::accounts::SYSTEM_PROGRAM, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::TOKEN_PROGRAM, false), - AccountMeta::new_readonly( - constants::pumpfun::accounts::ASSOCIATED_TOKEN_PROGRAM, - false, - ), - AccountMeta::new_readonly(constants::pumpfun::accounts::RENT, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::EVENT_AUTHORITY, false), - AccountMeta::new_readonly(constants::pumpfun::accounts::PUMPFUN, false), - ], - ) -} - pub fn buy( payer: &Keypair, mint: &Pubkey,