feat: add examples and enhance MEV protection services
This commit adds comprehensive examples for various trading operations including: - PumpFun copy and sniper trading - Bonk copy and sniper trading - PumpSwap trading - Raydium CPMM and AMM V4 trading - Middleware system demonstration - Event subscription Enhanced MEV protection services with FlashBlock and Node1 integration. Updated instruction modules for bonk, pumpswap, and raydium_cpmm. Improved SWQOS modules for better transaction handling.
This commit is contained in:
+31
-11
@@ -117,7 +117,7 @@ impl BonkInstructionBuilder {
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
&protocol_params.mint_token_program,
|
||||
));
|
||||
|
||||
// Create buy instruction
|
||||
@@ -125,7 +125,10 @@ impl BonkInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::AUTHORITY, false), // Authority (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::GLOBAL_CONFIG, false), // Global Config (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(protocol_params.platform_onfig, false), // Platform Config (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
protocol_params.platform_onfig,
|
||||
false,
|
||||
), // Platform Config (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(pool_state, false), // Pool State
|
||||
solana_sdk::instruction::AccountMeta::new(user_base_token_account, false), // User Base Token
|
||||
solana_sdk::instruction::AccountMeta::new(user_quote_token_account, false), // User Quote Token
|
||||
@@ -141,8 +144,14 @@ impl BonkInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::EVENT_AUTHORITY, false), // Event Authority (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::BONK, false), // Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(protocol_params.platform_associated_account, false), // Platform Associated Account
|
||||
solana_sdk::instruction::AccountMeta::new(protocol_params.creator_associated_account, false), // Creator Associated Account
|
||||
solana_sdk::instruction::AccountMeta::new(
|
||||
protocol_params.platform_associated_account,
|
||||
false,
|
||||
), // Platform Associated Account
|
||||
solana_sdk::instruction::AccountMeta::new(
|
||||
protocol_params.creator_associated_account,
|
||||
false,
|
||||
), // Creator Associated Account
|
||||
];
|
||||
// Create instruction data
|
||||
let mut data = vec![];
|
||||
@@ -218,10 +227,12 @@ impl BonkInstructionBuilder {
|
||||
);
|
||||
|
||||
// Create user token accounts
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
);
|
||||
let user_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&protocol_params.mint_token_program,
|
||||
);
|
||||
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
@@ -252,7 +263,10 @@ impl BonkInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::AUTHORITY, false), // Authority (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::GLOBAL_CONFIG, false), // Global Config (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(protocol_params.platform_onfig, false), // Platform Config (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
protocol_params.platform_onfig,
|
||||
false,
|
||||
), // Platform Config (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(pool_state, false), // Pool State
|
||||
solana_sdk::instruction::AccountMeta::new(user_base_token_account, false), // User Base Token
|
||||
solana_sdk::instruction::AccountMeta::new(user_quote_token_account, false), // User Quote Token
|
||||
@@ -268,8 +282,14 @@ impl BonkInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::EVENT_AUTHORITY, false), // Event Authority (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::BONK, false), // Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(protocol_params.platform_associated_account, false), // Platform Associated Account
|
||||
solana_sdk::instruction::AccountMeta::new(protocol_params.creator_associated_account, false), // Creator Associated Account
|
||||
solana_sdk::instruction::AccountMeta::new(
|
||||
protocol_params.platform_associated_account,
|
||||
false,
|
||||
), // Platform Associated Account
|
||||
solana_sdk::instruction::AccountMeta::new(
|
||||
protocol_params.creator_associated_account,
|
||||
false,
|
||||
), // Creator Associated Account
|
||||
];
|
||||
|
||||
// Create instruction data
|
||||
|
||||
+48
-26
@@ -57,6 +57,8 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
coin_creator_vault_ata,
|
||||
coin_creator_vault_authority,
|
||||
protocol_params.auto_handle_wsol,
|
||||
protocol_params.base_token_program,
|
||||
protocol_params.quote_token_program,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -86,6 +88,8 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
||||
coin_creator_vault_ata,
|
||||
coin_creator_vault_authority,
|
||||
protocol_params.auto_handle_wsol,
|
||||
protocol_params.base_token_program,
|
||||
protocol_params.quote_token_program,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -104,7 +108,12 @@ impl PumpSwapInstructionBuilder {
|
||||
params_coin_creator_vault_ata: Pubkey,
|
||||
params_coin_creator_vault_authority: Pubkey,
|
||||
auto_handle_wsol: bool,
|
||||
base_token_program: Pubkey,
|
||||
quote_token_program: Pubkey,
|
||||
) -> Result<Vec<Instruction>> {
|
||||
if base_mint != accounts::WSOL_TOKEN_ACCOUNT && quote_mint != accounts::WSOL_TOKEN_ACCOUNT {
|
||||
return Err(anyhow!("Invalid base mint and quote mint"));
|
||||
}
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
@@ -147,28 +156,32 @@ impl PumpSwapInstructionBuilder {
|
||||
}
|
||||
|
||||
// Create user token accounts
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
&base_mint,
|
||||
);
|
||||
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
"e_mint,
|
||||
);
|
||||
let user_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
&base_mint,
|
||||
&base_token_program,
|
||||
);
|
||||
let user_quote_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
"e_mint,
|
||||
"e_token_program,
|
||||
);
|
||||
|
||||
// Get pool token accounts
|
||||
let pool_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
&base_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
&base_token_program,
|
||||
);
|
||||
|
||||
let pool_quote_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
"e_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
"e_token_program,
|
||||
);
|
||||
|
||||
let mut instructions = vec![];
|
||||
@@ -216,7 +229,7 @@ impl PumpSwapInstructionBuilder {
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
if quote_mint_is_wsol { &base_mint } else { "e_mint },
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
if quote_mint_is_wsol { &base_token_program } else { "e_token_program },
|
||||
));
|
||||
|
||||
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
||||
@@ -234,8 +247,8 @@ impl PumpSwapInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(pool_quote_token_account, false), // pool_quote_token_account
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::FEE_RECIPIENT, false), // fee_recipient (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(fee_recipient_ata, false), // fee_recipient_ata
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly, duplicated as in JS)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(base_token_program, false), // TOKEN_PROGRAM_ID (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(quote_token_program, false), // TOKEN_PROGRAM_ID (readonly, duplicated as in JS)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
accounts::ASSOCIATED_TOKEN_PROGRAM,
|
||||
@@ -309,7 +322,12 @@ impl PumpSwapInstructionBuilder {
|
||||
params_coin_creator_vault_ata: Pubkey,
|
||||
params_coin_creator_vault_authority: Pubkey,
|
||||
auto_handle_wsol: bool,
|
||||
base_token_program: Pubkey,
|
||||
quote_token_program: Pubkey,
|
||||
) -> Result<Vec<Instruction>> {
|
||||
if base_mint != accounts::WSOL_TOKEN_ACCOUNT && quote_mint != accounts::WSOL_TOKEN_ACCOUNT {
|
||||
return Err(anyhow!("Invalid base mint and quote mint"));
|
||||
}
|
||||
if params.rpc.is_none() {
|
||||
return Err(anyhow!("RPC is not set"));
|
||||
}
|
||||
@@ -358,25 +376,29 @@ impl PumpSwapInstructionBuilder {
|
||||
|
||||
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
||||
|
||||
let user_base_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
&base_mint,
|
||||
);
|
||||
let user_quote_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
"e_mint,
|
||||
);
|
||||
let user_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
&base_mint,
|
||||
&base_token_program,
|
||||
);
|
||||
let user_quote_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
"e_mint,
|
||||
"e_token_program,
|
||||
);
|
||||
let pool_base_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
&base_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
&base_token_program,
|
||||
);
|
||||
let pool_quote_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
&pool,
|
||||
"e_mint,
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
"e_token_program,
|
||||
);
|
||||
|
||||
let mut instructions = vec![];
|
||||
@@ -397,7 +419,7 @@ impl PumpSwapInstructionBuilder {
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
if quote_mint_is_wsol { &base_mint } else { "e_mint },
|
||||
&accounts::TOKEN_PROGRAM,
|
||||
if quote_mint_is_wsol { &base_token_program } else { "e_token_program },
|
||||
));
|
||||
|
||||
// Create sell instruction
|
||||
@@ -413,8 +435,8 @@ impl PumpSwapInstructionBuilder {
|
||||
solana_sdk::instruction::AccountMeta::new(pool_quote_token_account, false), // pool_quote_token_account
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::FEE_RECIPIENT, false), // fee_recipient (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new(fee_recipient_ata, false), // fee_recipient_ata
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::TOKEN_PROGRAM, false), // TOKEN_PROGRAM_ID (readonly, duplicated as in JS)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(base_token_program, false), // TOKEN_PROGRAM_ID (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(quote_token_program, false), // TOKEN_PROGRAM_ID (readonly, duplicated as in JS)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(accounts::SYSTEM_PROGRAM, false), // System Program (readonly)
|
||||
solana_sdk::instruction::AccountMeta::new_readonly(
|
||||
accounts::ASSOCIATED_TOKEN_PROGRAM,
|
||||
|
||||
@@ -55,21 +55,29 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let is_base_in = protocol_params.base_mint == accounts::WSOL_TOKEN_ACCOUNT;
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.quote_token_program
|
||||
} else {
|
||||
protocol_params.base_token_program
|
||||
};
|
||||
|
||||
let wsol_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
);
|
||||
let mint_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
);
|
||||
let mint_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&mint_token_program,
|
||||
);
|
||||
|
||||
// Get pool token accounts
|
||||
let wsol_vault_account = get_vault_pda(&pool_state, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
let mint_vault_account = get_vault_pda(&pool_state, ¶ms.mint).unwrap();
|
||||
|
||||
let observation_state_account = get_observation_state_pda(&pool_state).unwrap();
|
||||
let is_base_in = protocol_params.base_mint == accounts::WSOL_TOKEN_ACCOUNT;
|
||||
|
||||
let amount_in: u64 = params.sol_amount;
|
||||
let result = compute_swap_amount(
|
||||
@@ -106,12 +114,6 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.quote_token_program
|
||||
} else {
|
||||
protocol_params.base_token_program
|
||||
};
|
||||
|
||||
instructions.push(create_associated_token_account_idempotent(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.payer.pubkey(),
|
||||
@@ -176,6 +178,11 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
}
|
||||
|
||||
let is_base_in = protocol_params.base_mint == params.mint;
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.base_token_program
|
||||
} else {
|
||||
protocol_params.quote_token_program
|
||||
};
|
||||
|
||||
let minimum_amount_out: u64 = compute_swap_amount(
|
||||
protocol_params.base_reserve,
|
||||
@@ -197,10 +204,12 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
¶ms.payer.pubkey(),
|
||||
&accounts::WSOL_TOKEN_ACCOUNT,
|
||||
);
|
||||
let mint_token_account = spl_associated_token_account::get_associated_token_address(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
);
|
||||
let mint_token_account =
|
||||
spl_associated_token_account::get_associated_token_address_with_program_id(
|
||||
¶ms.payer.pubkey(),
|
||||
¶ms.mint,
|
||||
&mint_token_program,
|
||||
);
|
||||
|
||||
// Get pool token accounts
|
||||
let wsol_vault_account = get_vault_pda(&pool_state, &accounts::WSOL_TOKEN_ACCOUNT).unwrap();
|
||||
@@ -221,12 +230,6 @@ impl RaydiumCpmmInstructionBuilder {
|
||||
),
|
||||
);
|
||||
|
||||
let mint_token_program = if is_base_in {
|
||||
protocol_params.base_token_program
|
||||
} else {
|
||||
protocol_params.quote_token_program
|
||||
};
|
||||
|
||||
// Create sell instruction
|
||||
let accounts = vec![
|
||||
solana_sdk::instruction::AccountMeta::new(params.payer.pubkey(), true), // Payer (signer)
|
||||
|
||||
Reference in New Issue
Block a user