feat: add wrap_wsol_to_sol function with seed account optimization

This commit is contained in:
ysq
2025-11-20 11:21:27 +08:00
parent f06d6b4eee
commit c0e026a88a
7 changed files with 161 additions and 12 deletions
+30 -6
View File
@@ -6,7 +6,10 @@ use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🔄 WSOL Wrapper Example");
println!("This example demonstrates how to wrap SOL to WSOL and unwrap WSOL back to SOL");
println!("This example demonstrates:");
println!("1. Wrapping SOL to WSOL");
println!("2. Partial unwrapping WSOL back to SOL using seed account");
println!("3. Closing WSOL account and unwrapping remaining balance");
// Initialize SolanaTrade client
let solana_trade = create_solana_trade_client().await?;
@@ -24,15 +27,36 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
Err(e) => {
println!("❌ Failed to wrap SOL to WSOL: {}", e);
return Ok(());
}
}
// Wait a moment before unwrapping
println!("\n⏳ Waiting 3 seconds before unwrapping...");
// Wait a moment before partial unwrapping
println!("\n⏳ Waiting 3 seconds before partial unwrapping...");
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
// Example 2: Close WSOL account and unwrap all remaining balance
println!("\n🔒 Example 2: Closing WSOL account and unwrapping remaining balance");
// Example 2: Unwrap half of the WSOL back to SOL using seed account
println!("\n🔄 Example 2: Unwrapping half of WSOL back to SOL using seed account");
let unwrap_amount = wrap_amount / 2; // Half of the wrapped amount
println!("Unwrapping {} lamports (0.0005 SOL) back to SOL using seed account...", unwrap_amount);
match solana_trade.wrap_wsol_to_sol(unwrap_amount).await {
Ok(signature) => {
println!("✅ Successfully unwrapped half of WSOL back to SOL using seed account!");
println!("Transaction signature: {}", signature);
println!("Explorer: https://solscan.io/tx/{}", signature);
}
Err(e) => {
println!("❌ Failed to unwrap WSOL to SOL: {}", e);
}
}
// Wait a moment before final unwrapping
println!("\n⏳ Waiting 3 seconds before final unwrapping...");
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
// Example 3: Close WSOL account and unwrap all remaining balance
println!("\n🔒 Example 3: Closing WSOL account and unwrapping remaining balance");
println!("Closing WSOL account and unwrapping all remaining balance to SOL...");
match solana_trade.close_wsol().await {
@@ -53,7 +77,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// Create and initialize SolanaTrade client
async fn create_solana_trade_client() -> Result<SolanaTrade, Box<dyn std::error::Error>> {
println!("🚀 Initializing SolanaTrade client...");
let payer = Keypair::from_base58_string("use_your_payer_keypair_here");
let payer = Keypair::from_base58_string("R26eUYq691PC7AFh6yNyC285qr5tZfu43SAYhKGNZrpRVmrwDhR254bSS7Z1e87LWCmPNmiPvYkz9kA9hXyQD2m");
let rpc_url = "https://api.mainnet-beta.solana.com".to_string();
let commitment = CommitmentConfig::confirmed();
let swqos_configs: Vec<SwqosConfig> = vec![SwqosConfig::Default(rpc_url.clone())];