refactor: optimize trading execution architecture and add wSOL management
- Simplify TradeExecutor interface by integrating middleware and swqos clients into params - Refactor parallel execution module with dedicated buy/sell execute functions - Add SOL wrapping/unwrapping functionality for wSOL management - Remove redundant parameter passing in sell operations - Delete example main.rs file - Optimize transaction building with integrated parameter structures BREAKING CHANGE: TradeExecutor interface simplified, middleware and swqos_clients now passed through params
This commit is contained in:
@@ -29,6 +29,7 @@ members = [
|
|||||||
"examples/address_lookup",
|
"examples/address_lookup",
|
||||||
"examples/nonce_cache",
|
"examples/nonce_cache",
|
||||||
"examples/pumpswap_direct_trading",
|
"examples/pumpswap_direct_trading",
|
||||||
|
"examples/wsol_wrapper",
|
||||||
]
|
]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ Please ensure that the parameters your trading logic depends on are available in
|
|||||||
| Middleware System | `middleware_system` | Custom instruction middleware example | `cargo run --package middleware_system` | [examples/middleware_system](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/middleware_system/src/main.rs) |
|
| Middleware System | `middleware_system` | Custom instruction middleware example | `cargo run --package middleware_system` | [examples/middleware_system](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/middleware_system/src/main.rs) |
|
||||||
| Address Lookup | `address_lookup` | Address lookup table example | `cargo run --package address_lookup` | [examples/address_lookup](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/address_lookup/src/main.rs) |
|
| Address Lookup | `address_lookup` | Address lookup table example | `cargo run --package address_lookup` | [examples/address_lookup](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/address_lookup/src/main.rs) |
|
||||||
| Nonce | `nonce_cache` | Nonce example | `cargo run --package nonce_cache` | [examples/nonce_cache](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/nonce_cache/src/main.rs) |
|
| Nonce | `nonce_cache` | Nonce example | `cargo run --package nonce_cache` | [examples/nonce_cache](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/nonce_cache/src/main.rs) |
|
||||||
|
| WSOL Wrapper | `wsol_wrapper` | Wrap/unwrap SOL to/from WSOL example | `cargo run --package wsol_wrapper` | [examples/wsol_wrapper](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/wsol_wrapper/src/main.rs) |
|
||||||
|
|
||||||
### SWQOS Service Configuration
|
### SWQOS Service Configuration
|
||||||
|
|
||||||
@@ -181,7 +182,6 @@ let trade_config = TradeConfig {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee, // Use custom priority fee
|
priority_fee, // Use custom priority fee
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -106,6 +106,7 @@ sol-trade-sdk = "0.6.1"
|
|||||||
| 中间件系统 | `middleware_system` | 自定义指令中间件示例 | `cargo run --package middleware_system` | [examples/middleware_system](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/middleware_system/src/main.rs) |
|
| 中间件系统 | `middleware_system` | 自定义指令中间件示例 | `cargo run --package middleware_system` | [examples/middleware_system](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/middleware_system/src/main.rs) |
|
||||||
| 地址查找表 | `address_lookup` | 地址查找表示例 | `cargo run --package address_lookup` | [examples/address_lookup](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/address_lookup/src/main.rs) |
|
| 地址查找表 | `address_lookup` | 地址查找表示例 | `cargo run --package address_lookup` | [examples/address_lookup](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/address_lookup/src/main.rs) |
|
||||||
| Nonce | `nonce_cache` | Nonce示例 | `cargo run --package nonce_cache` | [examples/nonce_cache](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/nonce_cache/src/main.rs) |
|
| Nonce | `nonce_cache` | Nonce示例 | `cargo run --package nonce_cache` | [examples/nonce_cache](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/nonce_cache/src/main.rs) |
|
||||||
|
| WSOL 包装器 | `wsol_wrapper` | SOL与WSOL相互转换示例 | `cargo run --package wsol_wrapper` | [examples/wsol_wrapper](https://github.com/0xfnzero/sol-trade-sdk/tree/main/examples/wsol_wrapper/src/main.rs) |
|
||||||
|
|
||||||
### SWQOS 服务配置说明
|
### SWQOS 服务配置说明
|
||||||
|
|
||||||
@@ -181,7 +182,6 @@ let trade_config = TradeConfig {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee, // 使用自定义优先费用
|
priority_fee, // 使用自定义优先费用
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -133,7 +133,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: Some(Pubkey::from_str("use_your_lookup_table_key_here").unwrap()),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -152,9 +151,9 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
|||||||
let slippage_basis_points = Some(100);
|
let slippage_basis_points = Some(100);
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
||||||
|
|
||||||
|
let lookup_table_key = Pubkey::from_str("use_your_lookup_table_key_here").unwrap();
|
||||||
// Setup lookup table cache
|
// Setup lookup table cache
|
||||||
setup_lookup_table_cache(client.rpc.clone(), client.trade_config.lookup_table_key.unwrap())
|
setup_lookup_table_cache(client.rpc.clone(), lookup_table_key).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Buy tokens
|
// Buy tokens
|
||||||
println!("Buying tokens from PumpFun...");
|
println!("Buying tokens from PumpFun...");
|
||||||
@@ -168,8 +167,9 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
|||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
None,
|
None,
|
||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||||
None, // You can also pass a new address lookup table account here, but you still need to update the AddressLookupTableCache
|
Some(lookup_table_key), // you still need to update the AddressLookupTableCache
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -119,7 +119,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -152,6 +151,7 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
|
|||||||
Box::new(BonkParams::from_trade(trade_info.clone())),
|
Box::new(BonkParams::from_trade(trade_info.clone())),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -178,6 +178,7 @@ async fn bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()>
|
|||||||
Box::new(BonkParams::from_trade(trade_info.clone())),
|
Box::new(BonkParams::from_trade(trade_info.clone())),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -88,7 +88,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -121,6 +120,7 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<
|
|||||||
Box::new(BonkParams::from_dev_trade(trade_info.clone())),
|
Box::new(BonkParams::from_dev_trade(trade_info.clone())),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -152,6 +152,7 @@ async fn bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<
|
|||||||
)),
|
)),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -72,7 +72,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: PriorityFee::default(),
|
priority_fee: PriorityFee::default(),
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -103,6 +102,7 @@ async fn test_middleware() -> AnyResult<()> {
|
|||||||
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?),
|
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
println!("tip: This transaction will not succeed because we're using a test account. You can modify the code to initialize the payer with your own private key");
|
println!("tip: This transaction will not succeed because we're using a test account. You can modify the code to initialize the payer with your own private key");
|
||||||
|
|||||||
@@ -114,7 +114,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -153,6 +152,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
|||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -146,6 +145,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
|||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -172,6 +172,7 @@ async fn pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResul
|
|||||||
Box::new(PumpFunParams::from_trade(&trade_info, Some(true))),
|
Box::new(PumpFunParams::from_trade(&trade_info, Some(true))),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -114,6 +113,7 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR
|
|||||||
Box::new(PumpFunParams::from_dev_trade(&trade_info, None)),
|
Box::new(PumpFunParams::from_dev_trade(&trade_info, None)),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -140,6 +140,7 @@ async fn pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyR
|
|||||||
Box::new(PumpFunParams::immediate_sell(trade_info.creator_vault, true)),
|
Box::new(PumpFunParams::immediate_sell(trade_info.creator_vault, true)),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?),
|
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -57,6 +58,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?),
|
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool).await?),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -84,7 +86,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
|
|||||||
@@ -136,7 +136,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -188,6 +187,7 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
|
|||||||
Box::new(params.clone()),
|
Box::new(params.clone()),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -216,6 +216,7 @@ async fn pumpswap_trade_with_grpc(mint_pubkey: Pubkey, params: PumpSwapParams) -
|
|||||||
Box::new(params.clone()),
|
Box::new(params.clone()),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -159,6 +158,7 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
|||||||
Box::new(params),
|
Box::new(params),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -186,6 +186,7 @@ async fn raydium_amm_v4_copy_trade_with_grpc(trade_info: RaydiumAmmV4SwapEvent)
|
|||||||
Box::new(params),
|
Box::new(params),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -123,7 +123,6 @@ async fn create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: priority_fee,
|
priority_fee: priority_fee,
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
@@ -163,6 +162,7 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
|
|||||||
Box::new(buy_params),
|
Box::new(buy_params),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -192,6 +192,7 @@ async fn raydium_cpmm_copy_trade_with_grpc(trade_info: RaydiumCpmmSwapEvent) ->
|
|||||||
Box::new(sell_params),
|
Box::new(sell_params),
|
||||||
None,
|
None,
|
||||||
true,
|
true,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,5 @@ fn create_trade_config(rpc_url: String, swqos_configs: Vec<SwqosConfig>) -> Trad
|
|||||||
commitment: CommitmentConfig::confirmed(),
|
commitment: CommitmentConfig::confirmed(),
|
||||||
priority_fee: PriorityFee::default(),
|
priority_fee: PriorityFee::default(),
|
||||||
swqos_configs,
|
swqos_configs,
|
||||||
lookup_table_key: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "wsol_wrapper"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
sol-trade-sdk = { path = "../.." }
|
||||||
|
solana-sdk = "2.3.0"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
anyhow = "1.0"
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
use sol_trade_sdk::{
|
||||||
|
common::{PriorityFee, TradeConfig},
|
||||||
|
SolanaTrade,
|
||||||
|
};
|
||||||
|
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
||||||
|
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");
|
||||||
|
|
||||||
|
// Initialize SolanaTrade client
|
||||||
|
let solana_trade = create_solana_trade_client().await?;
|
||||||
|
|
||||||
|
// Example 1: Wrap SOL to WSOL
|
||||||
|
println!("\n📦 Example 1: Wrapping SOL to WSOL");
|
||||||
|
let wrap_amount = 1_000_000; // 0.001 SOL in lamports
|
||||||
|
println!("Wrapping {} lamports (0.001 SOL) to WSOL...", wrap_amount);
|
||||||
|
|
||||||
|
match solana_trade.wrap_sol_to_wsol(wrap_amount).await {
|
||||||
|
Ok(signature) => {
|
||||||
|
println!("✅ Successfully wrapped SOL to WSOL!");
|
||||||
|
println!("Transaction signature: {}", signature);
|
||||||
|
println!("Explorer: https://solscan.io/tx/{}", signature);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("❌ Failed to wrap SOL to WSOL: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait a moment before unwrapping
|
||||||
|
println!("\n⏳ Waiting 3 seconds before 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");
|
||||||
|
println!("Closing WSOL account and unwrapping all remaining balance to SOL...");
|
||||||
|
|
||||||
|
match solana_trade.close_wsol().await {
|
||||||
|
Ok(signature) => {
|
||||||
|
println!("✅ Successfully closed WSOL account and unwrapped remaining balance!");
|
||||||
|
println!("Transaction signature: {}", signature);
|
||||||
|
println!("Explorer: https://solscan.io/tx/{}", signature);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("❌ Failed to close WSOL account: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\n🎉 WSOL Wrapper example completed!");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 rpc_url = "https://api.mainnet-beta.solana.com".to_string();
|
||||||
|
let trade_config = TradeConfig {
|
||||||
|
rpc_url,
|
||||||
|
commitment: CommitmentConfig::confirmed(),
|
||||||
|
priority_fee: PriorityFee::default(),
|
||||||
|
swqos_configs: vec![],
|
||||||
|
};
|
||||||
|
let solana_trade = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
||||||
|
println!("✅ SolanaTrade client initialized successfully!");
|
||||||
|
Ok(solana_trade)
|
||||||
|
}
|
||||||
+113
-33
@@ -25,21 +25,22 @@ pub enum InstructionCacheKey {
|
|||||||
owner: Pubkey,
|
owner: Pubkey,
|
||||||
mint: Pubkey,
|
mint: Pubkey,
|
||||||
token_program: Pubkey,
|
token_program: Pubkey,
|
||||||
|
use_seed: bool,
|
||||||
},
|
},
|
||||||
/// Close wSOL Account
|
/// Close wSOL Account
|
||||||
CloseWsolAccount { payer: Pubkey, wsol_token_account: Pubkey },
|
CloseWsolAccount { payer: Pubkey, wsol_token_account: Pubkey },
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Global instruction cache for storing common instructions
|
/// Global instruction cache for storing common instructions
|
||||||
static INSTRUCTION_CACHE: Lazy<RwLock<CLruCache<InstructionCacheKey, Instruction>>> =
|
static INSTRUCTION_CACHE: Lazy<RwLock<CLruCache<InstructionCacheKey, Vec<Instruction>>>> =
|
||||||
Lazy::new(|| {
|
Lazy::new(|| {
|
||||||
RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_INSTRUCTION_CACHE_SIZE).unwrap()))
|
RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_INSTRUCTION_CACHE_SIZE).unwrap()))
|
||||||
});
|
});
|
||||||
|
|
||||||
/// Get cached instruction, compute and cache if not exists
|
/// Get cached instruction, compute and cache if not exists
|
||||||
pub fn get_cached_instruction<F>(cache_key: InstructionCacheKey, compute_fn: F) -> Instruction
|
pub fn get_cached_instructions<F>(cache_key: InstructionCacheKey, compute_fn: F) -> Vec<Instruction>
|
||||||
where
|
where
|
||||||
F: FnOnce() -> Instruction,
|
F: FnOnce() -> Vec<Instruction>,
|
||||||
{
|
{
|
||||||
// Try to get from cache (using read lock)
|
// Try to get from cache (using read lock)
|
||||||
{
|
{
|
||||||
@@ -63,41 +64,75 @@ where
|
|||||||
|
|
||||||
// --------------------- Associated Token Account ---------------------
|
// --------------------- Associated Token Account ---------------------
|
||||||
|
|
||||||
|
pub fn create_associated_token_account_idempotent_fast_use_seed(
|
||||||
|
payer: &Pubkey,
|
||||||
|
owner: &Pubkey,
|
||||||
|
mint: &Pubkey,
|
||||||
|
token_program: &Pubkey,
|
||||||
|
use_seed: bool,
|
||||||
|
) -> Vec<Instruction> {
|
||||||
|
_create_associated_token_account_idempotent_fast(payer, owner, mint, token_program, use_seed)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_associated_token_account_idempotent_fast(
|
pub fn create_associated_token_account_idempotent_fast(
|
||||||
payer: &Pubkey,
|
payer: &Pubkey,
|
||||||
owner: &Pubkey,
|
owner: &Pubkey,
|
||||||
mint: &Pubkey,
|
mint: &Pubkey,
|
||||||
token_program: &Pubkey,
|
token_program: &Pubkey,
|
||||||
) -> Instruction {
|
) -> Vec<Instruction> {
|
||||||
|
_create_associated_token_account_idempotent_fast(payer, owner, mint, token_program, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _create_associated_token_account_idempotent_fast(
|
||||||
|
payer: &Pubkey,
|
||||||
|
owner: &Pubkey,
|
||||||
|
mint: &Pubkey,
|
||||||
|
token_program: &Pubkey,
|
||||||
|
use_seed: bool,
|
||||||
|
) -> Vec<Instruction> {
|
||||||
// Create cache key
|
// Create cache key
|
||||||
let cache_key = InstructionCacheKey::CreateAssociatedTokenAccount {
|
let cache_key = InstructionCacheKey::CreateAssociatedTokenAccount {
|
||||||
payer: *payer,
|
payer: *payer,
|
||||||
owner: *owner,
|
owner: *owner,
|
||||||
mint: *mint,
|
mint: *mint,
|
||||||
token_program: *token_program,
|
token_program: *token_program,
|
||||||
|
use_seed,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Use cache to get instruction
|
// Only use seed if the mint address is not wSOL or SOL
|
||||||
get_cached_instruction(cache_key, || {
|
// token 2022 测试不成功(TODO)
|
||||||
// Get Associated Token Address using cache
|
if use_seed
|
||||||
let associated_token_address =
|
&& !mint.eq(&crate::constants::WSOL_TOKEN_ACCOUNT)
|
||||||
get_associated_token_address_with_program_id_fast(owner, mint, token_program);
|
&& !mint.eq(&crate::constants::SOL_TOKEN_ACCOUNT)
|
||||||
|
&& token_program.eq(&spl_token::ID)
|
||||||
// Create Associated Token Account instruction
|
{
|
||||||
// Reference implementation of spl_associated_token_account::instruction::create_associated_token_account
|
// Use cache to get instruction
|
||||||
Instruction {
|
get_cached_instructions(cache_key, || {
|
||||||
program_id: ASSOCIATED_TOKEN_PROGRAM_ID,
|
super::seed::create_associated_token_account_use_seed(payer, owner, mint, token_program)
|
||||||
accounts: vec![
|
.unwrap()
|
||||||
AccountMeta::new(*payer, true), // Payer (signer, writable)
|
})
|
||||||
AccountMeta::new(associated_token_address, false), // ATA address (writable, non-signer)
|
} else {
|
||||||
AccountMeta::new_readonly(*owner, false), // Token account owner (readonly, non-signer)
|
// Use cache to get instruction
|
||||||
AccountMeta::new_readonly(*mint, false), // Token mint address (readonly, non-signer)
|
get_cached_instructions(cache_key, || {
|
||||||
crate::constants::SYSTEM_PROGRAM_META,
|
// Get Associated Token Address using cache
|
||||||
AccountMeta::new_readonly(*token_program, false), // Token program (readonly, non-signer)
|
let associated_token_address =
|
||||||
],
|
get_associated_token_address_with_program_id_fast(owner, mint, token_program);
|
||||||
data: vec![1],
|
// Create Associated Token Account instruction
|
||||||
}
|
// Reference implementation of spl_associated_token_account::instruction::create_associated_token_account
|
||||||
})
|
vec![Instruction {
|
||||||
|
program_id: ASSOCIATED_TOKEN_PROGRAM_ID,
|
||||||
|
accounts: vec![
|
||||||
|
AccountMeta::new(*payer, true), // Payer (signer, writable)
|
||||||
|
AccountMeta::new(associated_token_address, false), // ATA address (writable, non-signer)
|
||||||
|
AccountMeta::new_readonly(*owner, false), // Token account owner (readonly, non-signer)
|
||||||
|
AccountMeta::new_readonly(*mint, false), // Token mint address (readonly, non-signer)
|
||||||
|
crate::constants::SYSTEM_PROGRAM_META,
|
||||||
|
AccountMeta::new_readonly(*token_program, false), // Token program (readonly, non-signer)
|
||||||
|
],
|
||||||
|
data: vec![1],
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------- PDA ---------------------
|
// --------------------- PDA ---------------------
|
||||||
@@ -150,22 +185,52 @@ struct AtaCacheKey {
|
|||||||
wallet_address: Pubkey,
|
wallet_address: Pubkey,
|
||||||
token_mint_address: Pubkey,
|
token_mint_address: Pubkey,
|
||||||
token_program_id: Pubkey,
|
token_program_id: Pubkey,
|
||||||
|
use_seed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Global ATA cache for storing Associated Token Address computation results
|
/// Global ATA cache for storing Associated Token Address computation results
|
||||||
static ATA_CACHE: Lazy<RwLock<CLruCache<AtaCacheKey, Pubkey>>> =
|
static ATA_CACHE: Lazy<RwLock<CLruCache<AtaCacheKey, Pubkey>>> =
|
||||||
Lazy::new(|| RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_ATA_CACHE_SIZE).unwrap())));
|
Lazy::new(|| RwLock::new(CLruCache::new(NonZeroUsize::new(MAX_ATA_CACHE_SIZE).unwrap())));
|
||||||
|
|
||||||
|
pub fn get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
|
wallet_address: &Pubkey,
|
||||||
|
token_mint_address: &Pubkey,
|
||||||
|
token_program_id: &Pubkey,
|
||||||
|
use_seed: bool,
|
||||||
|
) -> Pubkey {
|
||||||
|
_get_associated_token_address_with_program_id_fast(
|
||||||
|
wallet_address,
|
||||||
|
token_mint_address,
|
||||||
|
token_program_id,
|
||||||
|
use_seed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get cached Associated Token Address, compute and cache if not exists
|
/// Get cached Associated Token Address, compute and cache if not exists
|
||||||
pub fn get_associated_token_address_with_program_id_fast(
|
pub fn get_associated_token_address_with_program_id_fast(
|
||||||
wallet_address: &Pubkey,
|
wallet_address: &Pubkey,
|
||||||
token_mint_address: &Pubkey,
|
token_mint_address: &Pubkey,
|
||||||
token_program_id: &Pubkey,
|
token_program_id: &Pubkey,
|
||||||
|
) -> Pubkey {
|
||||||
|
_get_associated_token_address_with_program_id_fast(
|
||||||
|
wallet_address,
|
||||||
|
token_mint_address,
|
||||||
|
token_program_id,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _get_associated_token_address_with_program_id_fast(
|
||||||
|
wallet_address: &Pubkey,
|
||||||
|
token_mint_address: &Pubkey,
|
||||||
|
token_program_id: &Pubkey,
|
||||||
|
use_seed: bool,
|
||||||
) -> Pubkey {
|
) -> Pubkey {
|
||||||
let cache_key = AtaCacheKey {
|
let cache_key = AtaCacheKey {
|
||||||
wallet_address: *wallet_address,
|
wallet_address: *wallet_address,
|
||||||
token_mint_address: *token_mint_address,
|
token_mint_address: *token_mint_address,
|
||||||
token_program_id: *token_program_id,
|
token_program_id: *token_program_id,
|
||||||
|
use_seed,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to get from cache (using read lock)
|
// Try to get from cache (using read lock)
|
||||||
@@ -177,11 +242,26 @@ pub fn get_associated_token_address_with_program_id_fast(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cache miss, compute new ATA
|
// Cache miss, compute new ATA
|
||||||
let ata = get_associated_token_address_with_program_id(
|
// Only use seed if the token mint address is not wSOL or SOL
|
||||||
wallet_address,
|
// token 2022 测试不成功(TODO)
|
||||||
token_mint_address,
|
let ata = if use_seed
|
||||||
token_program_id,
|
&& !token_mint_address.eq(&crate::constants::WSOL_TOKEN_ACCOUNT)
|
||||||
);
|
&& !token_mint_address.eq(&crate::constants::SOL_TOKEN_ACCOUNT)
|
||||||
|
&& token_program_id.eq(&spl_token::ID)
|
||||||
|
{
|
||||||
|
super::seed::get_associated_token_address_with_program_id_use_seed(
|
||||||
|
wallet_address,
|
||||||
|
token_mint_address,
|
||||||
|
token_program_id,
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
} else {
|
||||||
|
get_associated_token_address_with_program_id(
|
||||||
|
wallet_address,
|
||||||
|
token_mint_address,
|
||||||
|
token_program_id,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
// Store computation result in cache (using write lock)
|
// Store computation result in cache (using write lock)
|
||||||
{
|
{
|
||||||
@@ -206,20 +286,20 @@ pub fn fast_init(payer: &Pubkey) {
|
|||||||
&crate::constants::TOKEN_PROGRAM,
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
);
|
);
|
||||||
// Get Close wSOL Account instruction
|
// Get Close wSOL Account instruction
|
||||||
get_cached_instruction(
|
get_cached_instructions(
|
||||||
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
|
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
|
||||||
payer: *payer,
|
payer: *payer,
|
||||||
wsol_token_account,
|
wsol_token_account,
|
||||||
},
|
},
|
||||||
|| {
|
|| {
|
||||||
spl_token::instruction::close_account(
|
vec![spl_token::instruction::close_account(
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
&wsol_token_account,
|
&wsol_token_account,
|
||||||
&payer,
|
&payer,
|
||||||
&payer,
|
&payer,
|
||||||
&[],
|
&[],
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()]
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ pub mod bonding_curve;
|
|||||||
pub mod fast_fn;
|
pub mod fast_fn;
|
||||||
pub mod global;
|
pub mod global;
|
||||||
pub mod nonce_cache;
|
pub mod nonce_cache;
|
||||||
|
pub mod seed;
|
||||||
pub mod subscription_handle;
|
pub mod subscription_handle;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,114 @@
|
|||||||
|
use crate::common::SolanaRpcClient;
|
||||||
|
use anyhow::anyhow;
|
||||||
|
use fnv::FnvHasher;
|
||||||
|
use solana_sdk::{instruction::Instruction, program_pack::Pack, pubkey::Pubkey};
|
||||||
|
use solana_system_interface::instruction::create_account_with_seed;
|
||||||
|
use std::hash::Hasher;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::time::{sleep, Duration};
|
||||||
|
|
||||||
|
// Global rent values for token accounts
|
||||||
|
pub static mut SPL_TOKEN_RENT: Option<u64> = None;
|
||||||
|
pub static mut SPL_TOKEN_2022_RENT: Option<u64> = None;
|
||||||
|
|
||||||
|
pub async fn update_rents(client: &SolanaRpcClient) -> Result<(), anyhow::Error> {
|
||||||
|
let rent = fetch_rent_for_token_account(client, false).await?;
|
||||||
|
unsafe {
|
||||||
|
SPL_TOKEN_RENT = Some(rent);
|
||||||
|
}
|
||||||
|
let rent = fetch_rent_for_token_account(client, true).await?;
|
||||||
|
unsafe {
|
||||||
|
SPL_TOKEN_2022_RENT = Some(rent);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_rent_updater(client: Arc<SolanaRpcClient>) {
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
if let Err(_e) = update_rents(&client).await {}
|
||||||
|
sleep(Duration::from_secs(60 * 60)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn fetch_rent_for_token_account(
|
||||||
|
client: &SolanaRpcClient,
|
||||||
|
is_2022_token: bool,
|
||||||
|
) -> Result<u64, anyhow::Error> {
|
||||||
|
Ok(client
|
||||||
|
.get_minimum_balance_for_rent_exemption(if is_2022_token {
|
||||||
|
spl_token_2022::state::Account::LEN as usize
|
||||||
|
} else {
|
||||||
|
spl_token::state::Account::LEN as usize
|
||||||
|
})
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_associated_token_account_use_seed(
|
||||||
|
payer: &Pubkey,
|
||||||
|
owner: &Pubkey,
|
||||||
|
mint: &Pubkey,
|
||||||
|
token_program: &Pubkey,
|
||||||
|
) -> Result<Vec<Instruction>, anyhow::Error> {
|
||||||
|
let is_2022_token = token_program == &spl_token_2022::id();
|
||||||
|
let rent =
|
||||||
|
if is_2022_token { unsafe { SPL_TOKEN_2022_RENT } } else { unsafe { SPL_TOKEN_RENT } };
|
||||||
|
if rent.is_none() {
|
||||||
|
return Err(anyhow!("Rent is required when using seed"));
|
||||||
|
}
|
||||||
|
let mut buf = [0u8; 8];
|
||||||
|
let mut hasher = FnvHasher::default();
|
||||||
|
hasher.write(mint.as_ref());
|
||||||
|
let hash = hasher.finish();
|
||||||
|
let v = (hash & 0xFFFF_FFFF) as u32;
|
||||||
|
for i in 0..8 {
|
||||||
|
let nibble = ((v >> (28 - i * 4)) & 0xF) as u8;
|
||||||
|
buf[i] = match nibble {
|
||||||
|
0..=9 => b'0' + nibble,
|
||||||
|
_ => b'a' + (nibble - 10),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let seed = unsafe { std::str::from_utf8_unchecked(&buf) };
|
||||||
|
let ata_like = Pubkey::create_with_seed(payer, seed, token_program)?;
|
||||||
|
|
||||||
|
let len = if is_2022_token {
|
||||||
|
spl_token_2022::state::Account::LEN as u64
|
||||||
|
} else {
|
||||||
|
spl_token::state::Account::LEN as u64
|
||||||
|
};
|
||||||
|
let create_acc =
|
||||||
|
create_account_with_seed(payer, &ata_like, owner, seed, rent.unwrap(), len, token_program);
|
||||||
|
|
||||||
|
let init_acc = if is_2022_token {
|
||||||
|
spl_token_2022::instruction::initialize_account3(&token_program, &ata_like, mint, owner)?
|
||||||
|
} else {
|
||||||
|
spl_token::instruction::initialize_account3(&token_program, &ata_like, mint, owner)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(vec![create_acc, init_acc])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_associated_token_address_with_program_id_use_seed(
|
||||||
|
wallet_address: &Pubkey,
|
||||||
|
token_mint_address: &Pubkey,
|
||||||
|
token_program_id: &Pubkey,
|
||||||
|
) -> Result<Pubkey, anyhow::Error> {
|
||||||
|
let mut buf = [0u8; 8];
|
||||||
|
let mut hasher = FnvHasher::default();
|
||||||
|
hasher.write(token_mint_address.as_ref());
|
||||||
|
let hash = hasher.finish();
|
||||||
|
let v = (hash & 0xFFFF_FFFF) as u32;
|
||||||
|
for i in 0..8 {
|
||||||
|
let nibble = ((v >> (28 - i * 4)) & 0xF) as u8;
|
||||||
|
buf[i] = match nibble {
|
||||||
|
0..=9 => b'0' + nibble,
|
||||||
|
_ => b'a' + (nibble - 10),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let is_2022_token = token_program_id == &spl_token_2022::id();
|
||||||
|
let seed = unsafe { std::str::from_utf8_unchecked(&buf) };
|
||||||
|
let token_program = if is_2022_token { &spl_token_2022::id() } else { &spl_token::id() };
|
||||||
|
let ata_like = Pubkey::create_with_seed(wallet_address, seed, token_program)?;
|
||||||
|
Ok(ata_like)
|
||||||
|
}
|
||||||
+2
-4
@@ -9,7 +9,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use solana_client::rpc_client::RpcClient;
|
use solana_client::rpc_client::RpcClient;
|
||||||
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey, signature::Keypair};
|
use solana_sdk::{commitment_config::CommitmentConfig, signature::Keypair};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TradeConfig {
|
pub struct TradeConfig {
|
||||||
@@ -17,7 +17,6 @@ pub struct TradeConfig {
|
|||||||
pub swqos_configs: Vec<SwqosConfig>,
|
pub swqos_configs: Vec<SwqosConfig>,
|
||||||
pub priority_fee: PriorityFee,
|
pub priority_fee: PriorityFee,
|
||||||
pub commitment: CommitmentConfig,
|
pub commitment: CommitmentConfig,
|
||||||
pub lookup_table_key: Option<Pubkey>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TradeConfig {
|
impl TradeConfig {
|
||||||
@@ -26,9 +25,8 @@ impl TradeConfig {
|
|||||||
swqos_configs: Vec<SwqosConfig>,
|
swqos_configs: Vec<SwqosConfig>,
|
||||||
priority_fee: PriorityFee,
|
priority_fee: PriorityFee,
|
||||||
commitment: CommitmentConfig,
|
commitment: CommitmentConfig,
|
||||||
lookup_table_key: Option<Pubkey>,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self { rpc_url, swqos_configs, priority_fee, commitment, lookup_table_key }
|
Self { rpc_url, swqos_configs, priority_fee, commitment }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ pub const TOKEN_PROGRAM_2022_META: solana_sdk::instruction::AccountMeta =
|
|||||||
is_writable: false,
|
is_writable: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const SOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
||||||
|
|
||||||
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
pub const WSOL_TOKEN_ACCOUNT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
|
||||||
pub const WSOL_TOKEN_ACCOUNT_META: solana_sdk::instruction::AccountMeta =
|
pub const WSOL_TOKEN_ACCOUNT_META: solana_sdk::instruction::AccountMeta =
|
||||||
solana_sdk::instruction::AccountMeta {
|
solana_sdk::instruction::AccountMeta {
|
||||||
|
|||||||
+11
-9
@@ -94,12 +94,14 @@ impl InstructionBuilder for BonkInstructionBuilder {
|
|||||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||||
}
|
}
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.mint,
|
¶ms.payer.pubkey(),
|
||||||
&protocol_params.mint_token_program,
|
¶ms.mint,
|
||||||
));
|
&protocol_params.mint_token_program,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
let mut data = [0u8; 32];
|
let mut data = [0u8; 32];
|
||||||
data[..8].copy_from_slice(&BUY_EXECT_IN_DISCRIMINATOR);
|
data[..8].copy_from_slice(&BUY_EXECT_IN_DISCRIMINATOR);
|
||||||
@@ -131,7 +133,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
|
|||||||
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
|
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
|
||||||
|
|
||||||
if protocol_params.auto_handle_wsol {
|
if protocol_params.auto_handle_wsol {
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
@@ -213,7 +215,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
|
|||||||
// ========================================
|
// ========================================
|
||||||
let mut instructions = Vec::with_capacity(3);
|
let mut instructions = Vec::with_capacity(3);
|
||||||
|
|
||||||
instructions.push(crate::trading::common::create_wsol_ata(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::create_wsol_ata(¶ms.payer.pubkey()));
|
||||||
|
|
||||||
let mut data = [0u8; 32];
|
let mut data = [0u8; 32];
|
||||||
data[..8].copy_from_slice(&SELL_EXECT_IN_DISCRIMINATOR);
|
data[..8].copy_from_slice(&SELL_EXECT_IN_DISCRIMINATOR);
|
||||||
@@ -245,7 +247,7 @@ impl InstructionBuilder for BonkInstructionBuilder {
|
|||||||
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
|
instructions.push(Instruction::new_with_bytes(accounts::BONK, &data, accounts.to_vec()));
|
||||||
|
|
||||||
if protocol_params.auto_handle_wsol {
|
if protocol_params.auto_handle_wsol {
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
|
|||||||
+25
-17
@@ -76,13 +76,16 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
|||||||
protocol_params.associated_bonding_curve
|
protocol_params.associated_bonding_curve
|
||||||
};
|
};
|
||||||
|
|
||||||
let user_token_account = crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
let user_token_account =
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
¶ms.mint,
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
¶ms.mint,
|
||||||
);
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
params.open_seed_optimize,
|
||||||
|
);
|
||||||
|
|
||||||
let user_volume_accumulator = get_user_volume_accumulator_pda(¶ms.payer.pubkey()).unwrap();
|
let user_volume_accumulator =
|
||||||
|
get_user_volume_accumulator_pda(¶ms.payer.pubkey()).unwrap();
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
// Build instructions
|
// Build instructions
|
||||||
@@ -90,12 +93,15 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
|||||||
let mut instructions = Vec::with_capacity(2);
|
let mut instructions = Vec::with_capacity(2);
|
||||||
|
|
||||||
// Create associated token account
|
// Create associated token account
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.mint,
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
¶ms.mint,
|
||||||
));
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
params.open_seed_optimize,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
let mut buy_data = [0u8; 24];
|
let mut buy_data = [0u8; 24];
|
||||||
buy_data[..8].copy_from_slice(&[102, 6, 61, 18, 1, 218, 235, 234]); // Method ID
|
buy_data[..8].copy_from_slice(&[102, 6, 61, 18, 1, 218, 235, 234]); // Method ID
|
||||||
@@ -185,11 +191,13 @@ impl InstructionBuilder for PumpFunInstructionBuilder {
|
|||||||
protocol_params.associated_bonding_curve
|
protocol_params.associated_bonding_curve
|
||||||
};
|
};
|
||||||
|
|
||||||
let user_token_account = crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
let user_token_account =
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
¶ms.mint,
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
¶ms.mint,
|
||||||
);
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
params.open_seed_optimize,
|
||||||
|
);
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
// Build instructions
|
// Build instructions
|
||||||
|
|||||||
+28
-21
@@ -4,9 +4,12 @@ use crate::{
|
|||||||
accounts, fee_recipient_ata, get_user_volume_accumulator_pda, BUY_DISCRIMINATOR,
|
accounts, fee_recipient_ata, get_user_volume_accumulator_pda, BUY_DISCRIMINATOR,
|
||||||
SELL_DISCRIMINATOR,
|
SELL_DISCRIMINATOR,
|
||||||
},
|
},
|
||||||
trading::core::{
|
trading::{
|
||||||
params::{BuyParams, PumpSwapParams, SellParams},
|
common::wsol_manager,
|
||||||
traits::InstructionBuilder,
|
core::{
|
||||||
|
params::{BuyParams, PumpSwapParams, SellParams},
|
||||||
|
traits::InstructionBuilder,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
utils::calc::pumpswap::{buy_quote_input_internal, sell_base_input_internal},
|
utils::calc::pumpswap::{buy_quote_input_internal, sell_base_input_internal},
|
||||||
};
|
};
|
||||||
@@ -95,16 +98,18 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let user_base_token_account =
|
let user_base_token_account =
|
||||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
&base_mint,
|
&base_mint,
|
||||||
&base_token_program,
|
&base_token_program,
|
||||||
|
params.open_seed_optimize,
|
||||||
);
|
);
|
||||||
let user_quote_token_account =
|
let user_quote_token_account =
|
||||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
"e_mint,
|
"e_mint,
|
||||||
"e_token_program,
|
"e_token_program,
|
||||||
|
params.open_seed_optimize,
|
||||||
);
|
);
|
||||||
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
||||||
|
|
||||||
@@ -118,12 +123,15 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
|||||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), sol_amount));
|
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), sol_amount));
|
||||||
}
|
}
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast_use_seed(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
if quote_mint_is_wsol { &base_mint } else { "e_mint },
|
¶ms.payer.pubkey(),
|
||||||
if quote_mint_is_wsol { &base_token_program } else { "e_token_program },
|
if quote_mint_is_wsol { &base_mint } else { "e_mint },
|
||||||
));
|
if quote_mint_is_wsol { &base_token_program } else { "e_token_program },
|
||||||
|
params.open_seed_optimize,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// Create buy instruction
|
// Create buy instruction
|
||||||
let mut accounts = Vec::with_capacity(23);
|
let mut accounts = Vec::with_capacity(23);
|
||||||
@@ -181,7 +189,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
|||||||
});
|
});
|
||||||
if auto_handle_wsol {
|
if auto_handle_wsol {
|
||||||
// Close wSOL ATA account, reclaim rent
|
// Close wSOL ATA account, reclaim rent
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
}
|
}
|
||||||
@@ -259,16 +267,18 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
|||||||
|
|
||||||
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
let fee_recipient_ata = fee_recipient_ata(accounts::FEE_RECIPIENT, quote_mint);
|
||||||
let user_base_token_account =
|
let user_base_token_account =
|
||||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
&base_mint,
|
&base_mint,
|
||||||
&base_token_program,
|
&base_token_program,
|
||||||
|
params.open_seed_optimize,
|
||||||
);
|
);
|
||||||
let user_quote_token_account =
|
let user_quote_token_account =
|
||||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast_use_seed(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
"e_mint,
|
"e_mint,
|
||||||
"e_token_program,
|
"e_token_program,
|
||||||
|
params.open_seed_optimize,
|
||||||
);
|
);
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
@@ -276,12 +286,9 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
|||||||
// ========================================
|
// ========================================
|
||||||
let mut instructions = Vec::with_capacity(3);
|
let mut instructions = Vec::with_capacity(3);
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
if auto_handle_wsol {
|
||||||
¶ms.payer.pubkey(),
|
instructions.extend(wsol_manager::create_wsol_ata(¶ms.payer.pubkey()));
|
||||||
¶ms.payer.pubkey(),
|
}
|
||||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
|
||||||
));
|
|
||||||
|
|
||||||
// Create sell instruction
|
// Create sell instruction
|
||||||
let mut accounts = Vec::with_capacity(23);
|
let mut accounts = Vec::with_capacity(23);
|
||||||
@@ -340,7 +347,7 @@ impl InstructionBuilder for PumpSwapInstructionBuilder {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if auto_handle_wsol {
|
if auto_handle_wsol {
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,12 +68,14 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
|||||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||||
}
|
}
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.mint,
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
¶ms.mint,
|
||||||
));
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// Create buy instruction
|
// Create buy instruction
|
||||||
let accounts: [AccountMeta; 17] = [
|
let accounts: [AccountMeta; 17] = [
|
||||||
@@ -109,7 +111,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
|||||||
|
|
||||||
if protocol_params.auto_handle_wsol {
|
if protocol_params.auto_handle_wsol {
|
||||||
// Close wSOL ATA account, reclaim rent
|
// Close wSOL ATA account, reclaim rent
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
@@ -160,12 +162,14 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
|||||||
// ========================================
|
// ========================================
|
||||||
let mut instructions = Vec::with_capacity(3);
|
let mut instructions = Vec::with_capacity(3);
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||||
));
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// Create buy instruction
|
// Create buy instruction
|
||||||
let accounts: [AccountMeta; 17] = [
|
let accounts: [AccountMeta; 17] = [
|
||||||
@@ -200,7 +204,7 @@ impl InstructionBuilder for RaydiumAmmV4InstructionBuilder {
|
|||||||
));
|
));
|
||||||
|
|
||||||
if protocol_params.auto_handle_wsol {
|
if protocol_params.auto_handle_wsol {
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
|
|||||||
@@ -103,12 +103,14 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
|||||||
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
.extend(crate::trading::common::handle_wsol(¶ms.payer.pubkey(), amount_in));
|
||||||
}
|
}
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
¶ms.mint,
|
¶ms.payer.pubkey(),
|
||||||
&mint_token_program,
|
¶ms.mint,
|
||||||
));
|
&mint_token_program,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// Create buy instruction
|
// Create buy instruction
|
||||||
let accounts: [AccountMeta; 13] = [
|
let accounts: [AccountMeta; 13] = [
|
||||||
@@ -140,7 +142,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
|||||||
|
|
||||||
if protocol_params.auto_handle_wsol {
|
if protocol_params.auto_handle_wsol {
|
||||||
// Close wSOL ATA account, reclaim rent
|
// Close wSOL ATA account, reclaim rent
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
@@ -221,12 +223,14 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
|||||||
// ========================================
|
// ========================================
|
||||||
let mut instructions = Vec::with_capacity(3);
|
let mut instructions = Vec::with_capacity(3);
|
||||||
|
|
||||||
instructions.push(crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
instructions.extend(
|
||||||
¶ms.payer.pubkey(),
|
crate::common::fast_fn::create_associated_token_account_idempotent_fast(
|
||||||
¶ms.payer.pubkey(),
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
¶ms.payer.pubkey(),
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||||
));
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// Create sell instruction
|
// Create sell instruction
|
||||||
let accounts: [AccountMeta; 13] = [
|
let accounts: [AccountMeta; 13] = [
|
||||||
@@ -258,7 +262,7 @@ impl InstructionBuilder for RaydiumCpmmInstructionBuilder {
|
|||||||
|
|
||||||
if protocol_params.auto_handle_wsol {
|
if protocol_params.auto_handle_wsol {
|
||||||
// Close wSOL ATA account, reclaim rent
|
// Close wSOL ATA account, reclaim rent
|
||||||
instructions.push(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
instructions.extend(crate::trading::common::close_wsol(¶ms.payer.pubkey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(instructions)
|
Ok(instructions)
|
||||||
|
|||||||
+72
-21
@@ -22,10 +22,10 @@ use crate::trading::MiddlewareManager;
|
|||||||
use crate::trading::SellParams;
|
use crate::trading::SellParams;
|
||||||
use crate::trading::TradeFactory;
|
use crate::trading::TradeFactory;
|
||||||
use common::{PriorityFee, SolanaRpcClient, TradeConfig};
|
use common::{PriorityFee, SolanaRpcClient, TradeConfig};
|
||||||
|
use parking_lot::Mutex;
|
||||||
use rustls::crypto::{ring::default_provider, CryptoProvider};
|
use rustls::crypto::{ring::default_provider, CryptoProvider};
|
||||||
use solana_sdk::hash::Hash;
|
use solana_sdk::hash::Hash;
|
||||||
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
||||||
use parking_lot::Mutex;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use swqos::SwqosClient;
|
use swqos::SwqosClient;
|
||||||
|
|
||||||
@@ -34,8 +34,7 @@ pub struct SolanaTrade {
|
|||||||
pub rpc: Arc<SolanaRpcClient>,
|
pub rpc: Arc<SolanaRpcClient>,
|
||||||
pub rpc_client: Vec<Arc<SwqosClient>>,
|
pub rpc_client: Vec<Arc<SwqosClient>>,
|
||||||
pub swqos_clients: Vec<Arc<SwqosClient>>,
|
pub swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
pub priority_fee: PriorityFee,
|
pub priority_fee: Arc<PriorityFee>,
|
||||||
pub trade_config: TradeConfig,
|
|
||||||
pub middleware_manager: Option<Arc<MiddlewareManager>>,
|
pub middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +48,6 @@ impl Clone for SolanaTrade {
|
|||||||
rpc_client: self.rpc_client.clone(),
|
rpc_client: self.rpc_client.clone(),
|
||||||
swqos_clients: self.swqos_clients.clone(),
|
swqos_clients: self.swqos_clients.clone(),
|
||||||
priority_fee: self.priority_fee.clone(),
|
priority_fee: self.priority_fee.clone(),
|
||||||
trade_config: self.trade_config.clone(),
|
|
||||||
middleware_manager: self.middleware_manager.clone(),
|
middleware_manager: self.middleware_manager.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,7 +66,7 @@ impl SolanaTrade {
|
|||||||
|
|
||||||
let rpc_url = trade_config.rpc_url.clone();
|
let rpc_url = trade_config.rpc_url.clone();
|
||||||
let swqos_configs = trade_config.swqos_configs.clone();
|
let swqos_configs = trade_config.swqos_configs.clone();
|
||||||
let priority_fee = trade_config.priority_fee.clone();
|
let priority_fee = Arc::new(trade_config.priority_fee.clone());
|
||||||
let commitment = trade_config.commitment.clone();
|
let commitment = trade_config.commitment.clone();
|
||||||
let mut swqos_clients: Vec<Arc<SwqosClient>> = vec![];
|
let mut swqos_clients: Vec<Arc<SwqosClient>> = vec![];
|
||||||
|
|
||||||
@@ -79,6 +77,8 @@ impl SolanaTrade {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let rpc = Arc::new(SolanaRpcClient::new_with_commitment(rpc_url.clone(), commitment));
|
let rpc = Arc::new(SolanaRpcClient::new_with_commitment(rpc_url.clone(), commitment));
|
||||||
|
common::seed::update_rents(&rpc).await.unwrap();
|
||||||
|
common::seed::start_rent_updater(rpc.clone());
|
||||||
|
|
||||||
let rpc_client = SwqosConfig::get_swqos_client(
|
let rpc_client = SwqosConfig::get_swqos_client(
|
||||||
rpc_url.clone(),
|
rpc_url.clone(),
|
||||||
@@ -92,7 +92,6 @@ impl SolanaTrade {
|
|||||||
rpc_client: vec![rpc_client],
|
rpc_client: vec![rpc_client],
|
||||||
swqos_clients,
|
swqos_clients,
|
||||||
priority_fee,
|
priority_fee,
|
||||||
trade_config: trade_config.clone(),
|
|
||||||
middleware_manager: None,
|
middleware_manager: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -157,6 +156,7 @@ impl SolanaTrade {
|
|||||||
extension_params: Box<dyn ProtocolParams>,
|
extension_params: Box<dyn ProtocolParams>,
|
||||||
lookup_table_key: Option<Pubkey>,
|
lookup_table_key: Option<Pubkey>,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
|
open_seed_optimize: bool,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<(), anyhow::Error> {
|
||||||
if slippage_basis_points.is_none() {
|
if slippage_basis_points.is_none() {
|
||||||
println!(
|
println!(
|
||||||
@@ -167,23 +167,24 @@ impl SolanaTrade {
|
|||||||
let executor = TradeFactory::create_executor(dex_type.clone());
|
let executor = TradeFactory::create_executor(dex_type.clone());
|
||||||
let protocol_params = extension_params;
|
let protocol_params = extension_params;
|
||||||
|
|
||||||
let final_lookup_table_key = lookup_table_key.or(self.trade_config.lookup_table_key);
|
|
||||||
|
|
||||||
let mut buy_params = BuyParams {
|
let mut buy_params = BuyParams {
|
||||||
rpc: Some(self.rpc.clone()),
|
rpc: Some(self.rpc.clone()),
|
||||||
payer: self.payer.clone(),
|
payer: self.payer.clone(),
|
||||||
mint: mint,
|
mint: mint,
|
||||||
sol_amount: sol_amount,
|
sol_amount: sol_amount,
|
||||||
slippage_basis_points: slippage_basis_points,
|
slippage_basis_points: slippage_basis_points,
|
||||||
priority_fee: self.trade_config.priority_fee.clone(),
|
priority_fee: self.priority_fee.clone(),
|
||||||
lookup_table_key: final_lookup_table_key,
|
lookup_table_key,
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
data_size_limit: 0,
|
data_size_limit: 256 * 1024,
|
||||||
wait_transaction_confirmed: wait_transaction_confirmed,
|
wait_transaction_confirmed: wait_transaction_confirmed,
|
||||||
protocol_params: protocol_params.clone(),
|
protocol_params: protocol_params.clone(),
|
||||||
|
open_seed_optimize,
|
||||||
|
swqos_clients: self.swqos_clients.clone(),
|
||||||
|
middleware_manager: self.middleware_manager.clone(),
|
||||||
};
|
};
|
||||||
if custom_priority_fee.is_some() {
|
if custom_priority_fee.is_some() {
|
||||||
buy_params.priority_fee = custom_priority_fee.unwrap();
|
buy_params.priority_fee = Arc::new(custom_priority_fee.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate protocol params
|
// Validate protocol params
|
||||||
@@ -205,9 +206,7 @@ impl SolanaTrade {
|
|||||||
return Err(anyhow::anyhow!("Invalid protocol params for Trade"));
|
return Err(anyhow::anyhow!("Invalid protocol params for Trade"));
|
||||||
}
|
}
|
||||||
|
|
||||||
executor
|
executor.buy_with_tip(buy_params).await
|
||||||
.buy_with_tip(buy_params, self.swqos_clients.clone(), self.middleware_manager.clone())
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a sell order for a specified token
|
/// Execute a sell order for a specified token
|
||||||
@@ -249,6 +248,7 @@ impl SolanaTrade {
|
|||||||
extension_params: Box<dyn ProtocolParams>,
|
extension_params: Box<dyn ProtocolParams>,
|
||||||
lookup_table_key: Option<Pubkey>,
|
lookup_table_key: Option<Pubkey>,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
|
open_seed_optimize: bool,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<(), anyhow::Error> {
|
||||||
if slippage_basis_points.is_none() {
|
if slippage_basis_points.is_none() {
|
||||||
println!(
|
println!(
|
||||||
@@ -259,23 +259,28 @@ impl SolanaTrade {
|
|||||||
let executor = TradeFactory::create_executor(dex_type.clone());
|
let executor = TradeFactory::create_executor(dex_type.clone());
|
||||||
let protocol_params = extension_params;
|
let protocol_params = extension_params;
|
||||||
|
|
||||||
let final_lookup_table_key = lookup_table_key.or(self.trade_config.lookup_table_key);
|
|
||||||
|
|
||||||
let mut sell_params = SellParams {
|
let mut sell_params = SellParams {
|
||||||
rpc: Some(self.rpc.clone()),
|
rpc: Some(self.rpc.clone()),
|
||||||
payer: self.payer.clone(),
|
payer: self.payer.clone(),
|
||||||
mint: mint,
|
mint: mint,
|
||||||
token_amount: Some(token_amount),
|
token_amount: Some(token_amount),
|
||||||
slippage_basis_points: slippage_basis_points,
|
slippage_basis_points: slippage_basis_points,
|
||||||
priority_fee: self.trade_config.priority_fee.clone(),
|
priority_fee: self.priority_fee.clone(),
|
||||||
lookup_table_key: final_lookup_table_key,
|
lookup_table_key,
|
||||||
recent_blockhash,
|
recent_blockhash,
|
||||||
wait_transaction_confirmed: wait_transaction_confirmed,
|
wait_transaction_confirmed: wait_transaction_confirmed,
|
||||||
protocol_params: protocol_params.clone(),
|
protocol_params: protocol_params.clone(),
|
||||||
with_tip: with_tip,
|
with_tip: with_tip,
|
||||||
|
open_seed_optimize,
|
||||||
|
swqos_clients: if !with_tip {
|
||||||
|
self.rpc_client.clone()
|
||||||
|
} else {
|
||||||
|
self.swqos_clients.clone()
|
||||||
|
},
|
||||||
|
middleware_manager: self.middleware_manager.clone(),
|
||||||
};
|
};
|
||||||
if custom_priority_fee.is_some() {
|
if custom_priority_fee.is_some() {
|
||||||
sell_params.priority_fee = custom_priority_fee.unwrap();
|
sell_params.priority_fee = Arc::new(custom_priority_fee.unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate protocol params
|
// Validate protocol params
|
||||||
@@ -301,7 +306,7 @@ impl SolanaTrade {
|
|||||||
if !with_tip { self.rpc_client.clone() } else { self.swqos_clients.clone() };
|
if !with_tip { self.rpc_client.clone() } else { self.swqos_clients.clone() };
|
||||||
|
|
||||||
// Execute sell based on tip preference
|
// Execute sell based on tip preference
|
||||||
executor.sell_with_tip(sell_params, _swqos_clients, self.middleware_manager.clone()).await
|
executor.sell_with_tip(sell_params).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a sell order for a percentage of the specified token amount
|
/// Execute a sell order for a percentage of the specified token amount
|
||||||
@@ -349,6 +354,7 @@ impl SolanaTrade {
|
|||||||
extension_params: Box<dyn ProtocolParams>,
|
extension_params: Box<dyn ProtocolParams>,
|
||||||
lookup_table_key: Option<Pubkey>,
|
lookup_table_key: Option<Pubkey>,
|
||||||
wait_transaction_confirmed: bool,
|
wait_transaction_confirmed: bool,
|
||||||
|
open_seed_optimize: bool,
|
||||||
) -> Result<(), anyhow::Error> {
|
) -> Result<(), anyhow::Error> {
|
||||||
if percent == 0 || percent > 100 {
|
if percent == 0 || percent > 100 {
|
||||||
return Err(anyhow::anyhow!("Percentage must be between 1 and 100"));
|
return Err(anyhow::anyhow!("Percentage must be between 1 and 100"));
|
||||||
@@ -365,7 +371,52 @@ impl SolanaTrade {
|
|||||||
extension_params,
|
extension_params,
|
||||||
lookup_table_key,
|
lookup_table_key,
|
||||||
wait_transaction_confirmed,
|
wait_transaction_confirmed,
|
||||||
|
open_seed_optimize,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wraps SOL into wSOL (Wrapped SOL)
|
||||||
|
///
|
||||||
|
/// This function creates a wSOL associated token account (if it doesn't exist),
|
||||||
|
/// transfers the specified amount of SOL to that account, and then syncs the native
|
||||||
|
/// token balance to make SOL usable as an SPL token.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// - `amount`: The amount of SOL to wrap (in lamports)
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// - `Ok(String)`: Transaction signature
|
||||||
|
/// - `Err(anyhow::Error)`: If the transaction fails
|
||||||
|
pub async fn wrap_sol_to_wsol(&self, amount: u64) -> Result<String, anyhow::Error> {
|
||||||
|
use crate::trading::common::wsol_manager::handle_wsol;
|
||||||
|
use solana_sdk::transaction::Transaction;
|
||||||
|
let recent_blockhash = self.rpc.get_latest_blockhash().await?;
|
||||||
|
let instructions = handle_wsol(&self.payer.pubkey(), amount);
|
||||||
|
let mut transaction =
|
||||||
|
Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
|
||||||
|
transaction.sign(&[&*self.payer], recent_blockhash);
|
||||||
|
let signature = self.rpc.send_and_confirm_transaction(&transaction).await?;
|
||||||
|
Ok(signature.to_string())
|
||||||
|
}
|
||||||
|
/// Closes the wSOL account and unwraps SOL back to native SOL
|
||||||
|
///
|
||||||
|
/// This function closes the wSOL associated token account, which automatically
|
||||||
|
/// transfers any remaining wSOL balance back to the account owner as native SOL.
|
||||||
|
/// This is useful for cleaning up wSOL accounts and recovering wrapped SOL.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
/// - `Ok(String)`: Transaction signature
|
||||||
|
/// - `Err(anyhow::Error)`: If the transaction fails
|
||||||
|
pub async fn close_wsol(&self) -> Result<String, anyhow::Error> {
|
||||||
|
use crate::trading::common::wsol_manager::close_wsol;
|
||||||
|
use solana_sdk::transaction::Transaction;
|
||||||
|
let recent_blockhash = self.rpc.get_latest_blockhash().await?;
|
||||||
|
let instructions = close_wsol(&self.payer.pubkey());
|
||||||
|
let mut transaction =
|
||||||
|
Transaction::new_with_payer(&instructions, Some(&self.payer.pubkey()));
|
||||||
|
transaction.sign(&[&*self.payer], recent_blockhash);
|
||||||
|
let signature = self.rpc.send_and_confirm_transaction(&transaction).await?;
|
||||||
|
Ok(signature.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-618
@@ -1,618 +0,0 @@
|
|||||||
use std::{str::FromStr, sync::Arc};
|
|
||||||
|
|
||||||
use sol_trade_sdk::{
|
|
||||||
common::{AnyResult, PriorityFee, TradeConfig},
|
|
||||||
swqos::{SwqosConfig, SwqosRegion},
|
|
||||||
trading::{
|
|
||||||
core::params::{BonkParams, PumpFunParams, PumpSwapParams, RaydiumCpmmParams},
|
|
||||||
factory::DexType,
|
|
||||||
middleware::builtin::LoggingMiddleware,
|
|
||||||
MiddlewareManager,
|
|
||||||
},
|
|
||||||
SolanaTrade,
|
|
||||||
};
|
|
||||||
use sol_trade_sdk::{
|
|
||||||
solana_streamer_sdk::{
|
|
||||||
match_event,
|
|
||||||
streaming::{
|
|
||||||
event_parser::{
|
|
||||||
protocols::{
|
|
||||||
bonk::{BonkPoolCreateEvent, BonkTradeEvent},
|
|
||||||
pumpfun::{PumpFunCreateTokenEvent, PumpFunTradeEvent},
|
|
||||||
pumpswap::{
|
|
||||||
PumpSwapBuyEvent, PumpSwapCreatePoolEvent, PumpSwapDepositEvent,
|
|
||||||
PumpSwapSellEvent, PumpSwapWithdrawEvent,
|
|
||||||
},
|
|
||||||
raydium_cpmm::RaydiumCpmmSwapEvent,
|
|
||||||
},
|
|
||||||
Protocol, UnifiedEvent,
|
|
||||||
},
|
|
||||||
ShredStreamGrpc, YellowstoneGrpc,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
trading::core::params::RaydiumAmmV4Params,
|
|
||||||
};
|
|
||||||
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey, signature::Keypair};
|
|
||||||
use solana_streamer_sdk::streaming::{
|
|
||||||
event_parser::protocols::{
|
|
||||||
bonk::parser::BONK_PROGRAM_ID, pumpfun::parser::PUMPFUN_PROGRAM_ID,
|
|
||||||
pumpswap::parser::PUMPSWAP_PROGRAM_ID, raydium_amm_v4::parser::RAYDIUM_AMM_V4_PROGRAM_ID,
|
|
||||||
raydium_clmm::parser::RAYDIUM_CLMM_PROGRAM_ID,
|
|
||||||
raydium_cpmm::parser::RAYDIUM_CPMM_PROGRAM_ID,
|
|
||||||
},
|
|
||||||
yellowstone_grpc::{AccountFilter, TransactionFilter},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
test_create_solana_trade_client().await?;
|
|
||||||
test_middleware().await?;
|
|
||||||
test_pumpswap().await?;
|
|
||||||
test_bonk().await?;
|
|
||||||
test_raydium_cpmm().await?;
|
|
||||||
test_raydium_amm_v4().await?;
|
|
||||||
test_grpc().await?;
|
|
||||||
test_shreds().await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create SolanaTrade client
|
|
||||||
/// Initializes a new SolanaTrade client with configuration
|
|
||||||
async fn test_create_solana_trade_client() -> AnyResult<SolanaTrade> {
|
|
||||||
println!("Creating SolanaTrade client...");
|
|
||||||
|
|
||||||
let payer = Keypair::new();
|
|
||||||
let rpc_url = "https://mainnet.helius-rpc.com/?api-key=xxxxxx".to_string();
|
|
||||||
|
|
||||||
let swqos_configs = create_swqos_configs(&rpc_url);
|
|
||||||
let trade_config = create_trade_config(rpc_url, swqos_configs);
|
|
||||||
|
|
||||||
let solana_trade_client = SolanaTrade::new(Arc::new(payer), trade_config).await;
|
|
||||||
println!("SolanaTrade client created successfully!");
|
|
||||||
|
|
||||||
Ok(solana_trade_client)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_swqos_configs(rpc_url: &str) -> Vec<SwqosConfig> {
|
|
||||||
vec![
|
|
||||||
SwqosConfig::Jito("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::NextBlock("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::Bloxroute("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::ZeroSlot("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::Temporal("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::Node1("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::FlashBlock("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::BlockRazor("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::Astralane("your api_token".to_string(), SwqosRegion::Frankfurt, None),
|
|
||||||
SwqosConfig::Default(rpc_url.to_string()),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_trade_config(rpc_url: String, swqos_configs: Vec<SwqosConfig>) -> TradeConfig {
|
|
||||||
TradeConfig {
|
|
||||||
rpc_url,
|
|
||||||
commitment: CommitmentConfig::confirmed(),
|
|
||||||
priority_fee: PriorityFee::default(),
|
|
||||||
swqos_configs,
|
|
||||||
lookup_table_key: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
async fn test_middleware() -> AnyResult<()> {
|
|
||||||
let mut client = test_create_solana_trade_client().await?;
|
|
||||||
// SDK example middleware that prints instruction information
|
|
||||||
// You can reference LoggingMiddleware to implement the InstructionMiddleware trait for your own middleware
|
|
||||||
let middleware_manager = MiddlewareManager::new().add_middleware(Box::new(LoggingMiddleware));
|
|
||||||
client = client.with_middleware_manager(middleware_manager);
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
let pool_address = Pubkey::from_str("xxxx")?;
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from PumpSwap...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::PumpSwap,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
// Through RPC call, adds latency. Can optimize by using from_buy_trade or manually initializing PumpSwapParams
|
|
||||||
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_pumpfun_copy_trade_with_grpc(trade_info: PumpFunTradeEvent) -> AnyResult<()> {
|
|
||||||
println!("Testing PumpFun trading...");
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from PumpFun...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::PumpFun,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from PumpFun...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::PumpFun,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_pumpfun_sniper_trade_with_shreds(trade_info: PumpFunTradeEvent) -> AnyResult<()> {
|
|
||||||
println!("Testing PumpFun trading...");
|
|
||||||
|
|
||||||
if !trade_info.is_dev_create_token_trade {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = trade_info.mint;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from PumpFun...");
|
|
||||||
let buy_sol_amount = 100_000;
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::PumpFun,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_amount,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from PumpFun...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::PumpFun,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
Box::new(PumpFunParams::from_trade(&trade_info, None)),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_pumpswap() -> AnyResult<()> {
|
|
||||||
println!("Testing PumpSwap trading...");
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("2zMMhcVQEXDtdE6vsFS7S7D5oUodfJHE8vd1gnBouauv")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
let pool_address = Pubkey::from_str("xxxxxxx")?;
|
|
||||||
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from PumpSwap...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::PumpSwap,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
// Through RPC call, adds latency. Can optimize by using from_buy_trade or manually initializing PumpSwapParams
|
|
||||||
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from PumpSwap...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::PumpSwap,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
// Through RPC call, adds latency. Can optimize by using from_sell_trade or manually initializing PumpSwapParams
|
|
||||||
Box::new(PumpSwapParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_bonk_copy_trade_with_grpc(trade_info: BonkTradeEvent) -> AnyResult<()> {
|
|
||||||
println!("Testing Bonk trading...");
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from letsbonk.fun...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::Bonk,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
Box::new(BonkParams::from_trade(trade_info.clone())),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from letsbonk.fun...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::Bonk,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
Box::new(BonkParams::from_trade(trade_info)),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_bonk_sniper_trade_with_shreds(trade_info: BonkTradeEvent) -> AnyResult<()> {
|
|
||||||
println!("Testing Bonk trading...");
|
|
||||||
|
|
||||||
if !trade_info.is_dev_create_token_trade {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from letsbonk.fun...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::Bonk,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
Box::new(BonkParams::from_dev_trade(trade_info.clone())),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from letsbonk.fun...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::Bonk,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
Box::new(BonkParams::from_dev_trade(trade_info)),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_bonk() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
println!("Testing Bonk trading...");
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from letsbonk.fun...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::Bonk,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
// Through RPC call, adds latency. Can optimize by using from_trade or manually initializing BonkParams
|
|
||||||
Box::new(BonkParams::from_mint_by_rpc(&client.rpc, &mint_pubkey).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from letsbonk.fun...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::Bonk,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
// Through RPC call, adds latency. Can optimize by using from_trade or manually initializing BonkParams
|
|
||||||
Box::new(BonkParams::from_mint_by_rpc(&client.rpc, &mint_pubkey).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_raydium_cpmm() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
println!("Testing Raydium Cpmm trading...");
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
let pool_address = Pubkey::from_str("xxxxxxx")?;
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from Raydium Cpmm...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::RaydiumCpmm,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
// Through RPC call, adds latency, or manually initialize RaydiumCpmmParams
|
|
||||||
Box::new(
|
|
||||||
RaydiumCpmmParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?,
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from Raydium Cpmm...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::RaydiumCpmm,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
// Through RPC call, adds latency, or manually initialize RaydiumCpmmParams
|
|
||||||
Box::new(
|
|
||||||
RaydiumCpmmParams::from_pool_address_by_rpc(&client.rpc, &pool_address).await?,
|
|
||||||
),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_raydium_amm_v4() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
println!("Testing Raydium Amm V4 trading...");
|
|
||||||
|
|
||||||
let client = test_create_solana_trade_client().await?;
|
|
||||||
let mint_pubkey = Pubkey::from_str("xxxxxxx")?;
|
|
||||||
let buy_sol_cost = 100_000;
|
|
||||||
let slippage_basis_points = Some(100);
|
|
||||||
let recent_blockhash = client.rpc.get_latest_blockhash().await?;
|
|
||||||
let amm_address = Pubkey::from_str("xxxxxx")?;
|
|
||||||
// Buy tokens
|
|
||||||
println!("Buying tokens from Raydium Amm V4...");
|
|
||||||
client
|
|
||||||
.buy(
|
|
||||||
DexType::RaydiumAmmV4,
|
|
||||||
mint_pubkey,
|
|
||||||
buy_sol_cost,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
// Through RPC call, adds latency, or from_amm_info_and_reserves or manually initialize RaydiumAmmV4Params
|
|
||||||
Box::new(RaydiumAmmV4Params::from_amm_address_by_rpc(&client.rpc, amm_address).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Sell tokens
|
|
||||||
println!("Selling tokens from Raydium Amm V4...");
|
|
||||||
let amount_token = 0;
|
|
||||||
client
|
|
||||||
.sell(
|
|
||||||
DexType::RaydiumAmmV4,
|
|
||||||
mint_pubkey,
|
|
||||||
amount_token,
|
|
||||||
slippage_basis_points,
|
|
||||||
recent_blockhash,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
// Through RPC call, adds latency, or from_amm_info_and_reserves or manually initialize RaydiumAmmV4Params
|
|
||||||
Box::new(RaydiumAmmV4Params::from_amm_address_by_rpc(&client.rpc, amm_address).await?),
|
|
||||||
None,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_grpc() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
println!("Subscribing to GRPC events...");
|
|
||||||
|
|
||||||
let grpc = YellowstoneGrpc::new(
|
|
||||||
"https://solana-yellowstone-grpc.publicnode.com:443".to_string(),
|
|
||||||
None,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let callback = create_event_callback();
|
|
||||||
let protocols =
|
|
||||||
vec![Protocol::PumpFun, Protocol::PumpSwap, Protocol::Bonk, Protocol::RaydiumCpmm];
|
|
||||||
// Filter accounts
|
|
||||||
let account_include = vec![
|
|
||||||
PUMPFUN_PROGRAM_ID.to_string(), // Listen to pumpfun program ID
|
|
||||||
PUMPSWAP_PROGRAM_ID.to_string(), // Listen to pumpswap program ID
|
|
||||||
BONK_PROGRAM_ID.to_string(), // Listen to bonk program ID
|
|
||||||
RAYDIUM_CPMM_PROGRAM_ID.to_string(), // Listen to raydium_cpmm program ID
|
|
||||||
RAYDIUM_CLMM_PROGRAM_ID.to_string(), // Listen to raydium_clmm program ID
|
|
||||||
RAYDIUM_AMM_V4_PROGRAM_ID.to_string(), // Listen to raydium_amm_v4 program ID
|
|
||||||
"xxxxxxxx".to_string(), // Listen to xxxxx account
|
|
||||||
];
|
|
||||||
let account_exclude = vec![];
|
|
||||||
let account_required = vec![];
|
|
||||||
|
|
||||||
// Listen to transaction data
|
|
||||||
let transaction_filter = TransactionFilter {
|
|
||||||
account_include: account_include.clone(),
|
|
||||||
account_exclude,
|
|
||||||
account_required,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Listen to account data belonging to owner programs -> account event monitoring
|
|
||||||
let account_filter = AccountFilter { account: vec![], owner: account_include.clone() };
|
|
||||||
|
|
||||||
println!("Starting to listen for events, press Ctrl+C to stop...");
|
|
||||||
grpc.subscribe_events_immediate(
|
|
||||||
protocols,
|
|
||||||
None,
|
|
||||||
transaction_filter,
|
|
||||||
account_filter,
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
callback,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn test_shreds() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
println!("Subscribing to ShredStream events...");
|
|
||||||
|
|
||||||
let shred_stream = ShredStreamGrpc::new("http://127.0.0.1:10800".to_string()).await?;
|
|
||||||
let callback = create_event_callback();
|
|
||||||
let protocols = vec![Protocol::PumpFun, Protocol::PumpSwap, Protocol::Bonk];
|
|
||||||
|
|
||||||
println!("Starting to listen for events, press Ctrl+C to stop...");
|
|
||||||
shred_stream.shredstream_subscribe(protocols, None, None, callback).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_event_callback() -> impl Fn(Box<dyn UnifiedEvent>) {
|
|
||||||
|event: Box<dyn UnifiedEvent>| {
|
|
||||||
match_event!(event, {
|
|
||||||
BonkPoolCreateEvent => |e: BonkPoolCreateEvent| {
|
|
||||||
println!("BonkPoolCreateEvent: {:?}", e.base_mint_param.symbol);
|
|
||||||
},
|
|
||||||
BonkTradeEvent => |e: BonkTradeEvent| {
|
|
||||||
println!("BonkTradeEvent: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpFunTradeEvent => |e: PumpFunTradeEvent| {
|
|
||||||
println!("PumpFunTradeEvent: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpFunCreateTokenEvent => |e: PumpFunCreateTokenEvent| {
|
|
||||||
println!("PumpFunCreateTokenEvent: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpSwapBuyEvent => |e: PumpSwapBuyEvent| {
|
|
||||||
println!("Buy event: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpSwapSellEvent => |e: PumpSwapSellEvent| {
|
|
||||||
println!("Sell event: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpSwapCreatePoolEvent => |e: PumpSwapCreatePoolEvent| {
|
|
||||||
println!("CreatePool event: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpSwapDepositEvent => |e: PumpSwapDepositEvent| {
|
|
||||||
println!("Deposit event: {:?}", e);
|
|
||||||
},
|
|
||||||
PumpSwapWithdrawEvent => |e: PumpSwapWithdrawEvent| {
|
|
||||||
println!("Withdraw event: {:?}", e);
|
|
||||||
},
|
|
||||||
RaydiumCpmmSwapEvent => |e: RaydiumCpmmSwapEvent| {
|
|
||||||
println!("RaydiumCpmmSwapEvent: {:?}", e);
|
|
||||||
},
|
|
||||||
// .....
|
|
||||||
// For more events and documentation, please refer to https://github.com/0xfnzero/solana-streamer
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,13 +14,13 @@ pub fn handle_wsol(payer: &Pubkey, amount_in: u64) -> SmallVec<[Instruction; 3]>
|
|||||||
);
|
);
|
||||||
|
|
||||||
let mut insts = SmallVec::<[Instruction; 3]>::new();
|
let mut insts = SmallVec::<[Instruction; 3]>::new();
|
||||||
|
insts.extend(create_associated_token_account_idempotent_fast(
|
||||||
|
&payer,
|
||||||
|
&payer,
|
||||||
|
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||||
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
|
));
|
||||||
insts.extend([
|
insts.extend([
|
||||||
create_associated_token_account_idempotent_fast(
|
|
||||||
&payer,
|
|
||||||
&payer,
|
|
||||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
|
||||||
),
|
|
||||||
transfer(&payer, &wsol_token_account, amount_in),
|
transfer(&payer, &wsol_token_account, amount_in),
|
||||||
spl_token::instruction::sync_native(&crate::constants::TOKEN_PROGRAM, &wsol_token_account)
|
spl_token::instruction::sync_native(&crate::constants::TOKEN_PROGRAM, &wsol_token_account)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
@@ -29,33 +29,33 @@ pub fn handle_wsol(payer: &Pubkey, amount_in: u64) -> SmallVec<[Instruction; 3]>
|
|||||||
insts
|
insts
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close_wsol(payer: &Pubkey) -> Instruction {
|
pub fn close_wsol(payer: &Pubkey) -> Vec<Instruction> {
|
||||||
let wsol_token_account =
|
let wsol_token_account =
|
||||||
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
crate::common::fast_fn::get_associated_token_address_with_program_id_fast(
|
||||||
&payer,
|
&payer,
|
||||||
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
&crate::constants::WSOL_TOKEN_ACCOUNT,
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
);
|
);
|
||||||
crate::common::fast_fn::get_cached_instruction(
|
crate::common::fast_fn::get_cached_instructions(
|
||||||
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
|
crate::common::fast_fn::InstructionCacheKey::CloseWsolAccount {
|
||||||
payer: *payer,
|
payer: *payer,
|
||||||
wsol_token_account,
|
wsol_token_account,
|
||||||
},
|
},
|
||||||
|| {
|
|| {
|
||||||
close_account(
|
vec![close_account(
|
||||||
&crate::constants::TOKEN_PROGRAM,
|
&crate::constants::TOKEN_PROGRAM,
|
||||||
&wsol_token_account,
|
&wsol_token_account,
|
||||||
&payer,
|
&payer,
|
||||||
&payer,
|
&payer,
|
||||||
&[],
|
&[],
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()]
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn create_wsol_ata(payer: &Pubkey) -> Instruction {
|
pub fn create_wsol_ata(payer: &Pubkey) -> Vec<Instruction> {
|
||||||
create_associated_token_account_idempotent_fast(
|
create_associated_token_account_idempotent_fast(
|
||||||
&payer,
|
&payer,
|
||||||
&payer,
|
&payer,
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::{sync::Arc, time::Instant};
|
use std::{sync::Arc, time::Instant};
|
||||||
|
|
||||||
|
use crate::trading::core::parallel::{buy_parallel_execute, sell_parallel_execute};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
parallel::parallel_execute_with_tips,
|
|
||||||
params::{BuyParams, SellParams},
|
params::{BuyParams, SellParams},
|
||||||
traits::{InstructionBuilder, TradeExecutor},
|
traits::{InstructionBuilder, TradeExecutor},
|
||||||
};
|
};
|
||||||
use crate::{swqos::SwqosClient, trading::middleware::MiddlewareManager};
|
|
||||||
|
|
||||||
const MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT: u32 = 256 * 1024;
|
|
||||||
|
|
||||||
/// Generic trade executor implementation
|
/// Generic trade executor implementation
|
||||||
pub struct GenericTradeExecutor {
|
pub struct GenericTradeExecutor {
|
||||||
@@ -27,22 +25,12 @@ impl GenericTradeExecutor {
|
|||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl TradeExecutor for GenericTradeExecutor {
|
impl TradeExecutor for GenericTradeExecutor {
|
||||||
async fn buy_with_tip(
|
async fn buy_with_tip(&self, params: BuyParams) -> Result<()> {
|
||||||
&self,
|
|
||||||
params: BuyParams,
|
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut data_size_limit = params.data_size_limit;
|
|
||||||
if data_size_limit == 0 {
|
|
||||||
data_size_limit = MAX_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
|
|
||||||
}
|
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
// Build instructions directly from params to avoid unnecessary cloning
|
// Build instructions directly from params to avoid unnecessary cloning
|
||||||
let instructions = self.instruction_builder.build_buy_instructions(¶ms).await?;
|
let instructions = self.instruction_builder.build_buy_instructions(¶ms).await?;
|
||||||
let final_instructions = match &middleware_manager {
|
let final_instructions = match ¶ms.middleware_manager {
|
||||||
Some(middleware_manager) => middleware_manager
|
Some(middleware_manager) => middleware_manager
|
||||||
.apply_middlewares_process_protocol_instructions(
|
.apply_middlewares_process_protocol_instructions(
|
||||||
instructions,
|
instructions,
|
||||||
@@ -55,36 +43,17 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
|
println!("Building buy transaction instructions time cost: {:?}", start.elapsed());
|
||||||
|
|
||||||
// Execute transactions in parallel
|
// Execute transactions in parallel
|
||||||
parallel_execute_with_tips(
|
buy_parallel_execute(params, final_instructions, self.protocol_name).await?;
|
||||||
swqos_clients,
|
|
||||||
params.payer,
|
|
||||||
final_instructions,
|
|
||||||
Arc::new(params.priority_fee),
|
|
||||||
params.lookup_table_key,
|
|
||||||
params.recent_blockhash,
|
|
||||||
data_size_limit,
|
|
||||||
middleware_manager,
|
|
||||||
self.protocol_name,
|
|
||||||
true,
|
|
||||||
params.wait_transaction_confirmed,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sell_with_tip(
|
async fn sell_with_tip(&self, params: SellParams) -> Result<()> {
|
||||||
&self,
|
|
||||||
params: SellParams,
|
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
// Build instructions directly from params to avoid unnecessary cloning
|
// Build instructions directly from params to avoid unnecessary cloning
|
||||||
let instructions = self.instruction_builder.build_sell_instructions(¶ms).await?;
|
let instructions = self.instruction_builder.build_sell_instructions(¶ms).await?;
|
||||||
let final_instructions = match &middleware_manager {
|
let final_instructions = match ¶ms.middleware_manager {
|
||||||
Some(middleware_manager) => middleware_manager
|
Some(middleware_manager) => middleware_manager
|
||||||
.apply_middlewares_process_protocol_instructions(
|
.apply_middlewares_process_protocol_instructions(
|
||||||
instructions,
|
instructions,
|
||||||
@@ -97,21 +66,7 @@ impl TradeExecutor for GenericTradeExecutor {
|
|||||||
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
|
println!("Building sell transaction instructions time cost: {:?}", start.elapsed());
|
||||||
|
|
||||||
// Execute transactions in parallel
|
// Execute transactions in parallel
|
||||||
parallel_execute_with_tips(
|
sell_parallel_execute(params, final_instructions, self.protocol_name).await?;
|
||||||
swqos_clients,
|
|
||||||
params.payer,
|
|
||||||
final_instructions,
|
|
||||||
Arc::new(params.priority_fee),
|
|
||||||
params.lookup_table_key,
|
|
||||||
params.recent_blockhash,
|
|
||||||
0,
|
|
||||||
middleware_manager,
|
|
||||||
self.protocol_name,
|
|
||||||
false,
|
|
||||||
params.wait_transaction_confirmed,
|
|
||||||
params.with_tip,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,55 @@ use tokio::task::JoinHandle;
|
|||||||
use crate::{
|
use crate::{
|
||||||
common::PriorityFee,
|
common::PriorityFee,
|
||||||
swqos::{SwqosClient, SwqosType, TradeType},
|
swqos::{SwqosClient, SwqosType, TradeType},
|
||||||
trading::{common::build_transaction, MiddlewareManager},
|
trading::{common::build_transaction, BuyParams, MiddlewareManager, SellParams},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub async fn buy_parallel_execute(
|
||||||
|
params: BuyParams,
|
||||||
|
instructions: Vec<Instruction>,
|
||||||
|
protocol_name: &'static str,
|
||||||
|
) -> Result<()> {
|
||||||
|
parallel_execute(
|
||||||
|
params.swqos_clients,
|
||||||
|
params.payer,
|
||||||
|
instructions,
|
||||||
|
params.priority_fee,
|
||||||
|
params.lookup_table_key,
|
||||||
|
params.recent_blockhash,
|
||||||
|
params.data_size_limit,
|
||||||
|
params.middleware_manager,
|
||||||
|
protocol_name,
|
||||||
|
true,
|
||||||
|
params.wait_transaction_confirmed,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn sell_parallel_execute(
|
||||||
|
params: SellParams,
|
||||||
|
instructions: Vec<Instruction>,
|
||||||
|
protocol_name: &'static str,
|
||||||
|
) -> Result<()> {
|
||||||
|
parallel_execute(
|
||||||
|
params.swqos_clients,
|
||||||
|
params.payer,
|
||||||
|
instructions,
|
||||||
|
params.priority_fee,
|
||||||
|
params.lookup_table_key,
|
||||||
|
params.recent_blockhash,
|
||||||
|
0,
|
||||||
|
params.middleware_manager,
|
||||||
|
protocol_name,
|
||||||
|
false,
|
||||||
|
params.wait_transaction_confirmed,
|
||||||
|
params.with_tip,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
/// Generic function for parallel transaction execution
|
/// Generic function for parallel transaction execution
|
||||||
pub async fn parallel_execute_with_tips(
|
async fn parallel_execute(
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
payer: Arc<Keypair>,
|
payer: Arc<Keypair>,
|
||||||
instructions: Vec<Instruction>,
|
instructions: Vec<Instruction>,
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ use crate::common::bonding_curve::BondingCurveAccount;
|
|||||||
use crate::common::{PriorityFee, SolanaRpcClient};
|
use crate::common::{PriorityFee, SolanaRpcClient};
|
||||||
use crate::solana_streamer_sdk::streaming::event_parser::common::EventType;
|
use crate::solana_streamer_sdk::streaming::event_parser::common::EventType;
|
||||||
use crate::solana_streamer_sdk::streaming::event_parser::protocols::bonk::BonkTradeEvent;
|
use crate::solana_streamer_sdk::streaming::event_parser::protocols::bonk::BonkTradeEvent;
|
||||||
|
use crate::swqos::SwqosClient;
|
||||||
use crate::trading::common::get_multi_token_balances;
|
use crate::trading::common::get_multi_token_balances;
|
||||||
|
use crate::trading::MiddlewareManager;
|
||||||
use solana_hash::Hash;
|
use solana_hash::Hash;
|
||||||
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
use solana_sdk::{pubkey::Pubkey, signature::Keypair};
|
||||||
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::PumpFunTradeEvent;
|
use solana_streamer_sdk::streaming::event_parser::protocols::pumpfun::PumpFunTradeEvent;
|
||||||
@@ -21,12 +23,15 @@ pub struct BuyParams {
|
|||||||
pub mint: Pubkey,
|
pub mint: Pubkey,
|
||||||
pub sol_amount: u64,
|
pub sol_amount: u64,
|
||||||
pub slippage_basis_points: Option<u64>,
|
pub slippage_basis_points: Option<u64>,
|
||||||
pub priority_fee: PriorityFee,
|
pub priority_fee: Arc<PriorityFee>,
|
||||||
pub lookup_table_key: Option<Pubkey>,
|
pub lookup_table_key: Option<Pubkey>,
|
||||||
pub recent_blockhash: Hash,
|
pub recent_blockhash: Hash,
|
||||||
pub data_size_limit: u32,
|
pub data_size_limit: u32,
|
||||||
pub wait_transaction_confirmed: bool,
|
pub wait_transaction_confirmed: bool,
|
||||||
pub protocol_params: Box<dyn ProtocolParams>,
|
pub protocol_params: Box<dyn ProtocolParams>,
|
||||||
|
pub open_seed_optimize: bool,
|
||||||
|
pub swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
|
pub middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sell parameters
|
/// Sell parameters
|
||||||
@@ -37,12 +42,15 @@ pub struct SellParams {
|
|||||||
pub mint: Pubkey,
|
pub mint: Pubkey,
|
||||||
pub token_amount: Option<u64>,
|
pub token_amount: Option<u64>,
|
||||||
pub slippage_basis_points: Option<u64>,
|
pub slippage_basis_points: Option<u64>,
|
||||||
pub priority_fee: PriorityFee,
|
pub priority_fee: Arc<PriorityFee>,
|
||||||
pub lookup_table_key: Option<Pubkey>,
|
pub lookup_table_key: Option<Pubkey>,
|
||||||
pub recent_blockhash: Hash,
|
pub recent_blockhash: Hash,
|
||||||
pub wait_transaction_confirmed: bool,
|
pub wait_transaction_confirmed: bool,
|
||||||
pub with_tip: bool,
|
pub with_tip: bool,
|
||||||
pub protocol_params: Box<dyn ProtocolParams>,
|
pub protocol_params: Box<dyn ProtocolParams>,
|
||||||
|
pub open_seed_optimize: bool,
|
||||||
|
pub swqos_clients: Vec<Arc<SwqosClient>>,
|
||||||
|
pub middleware_manager: Option<Arc<MiddlewareManager>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// PumpFun protocol specific parameters
|
/// PumpFun protocol specific parameters
|
||||||
|
|||||||
@@ -1,28 +1,14 @@
|
|||||||
use std::sync::Arc;
|
use super::params::{BuyParams, SellParams};
|
||||||
|
|
||||||
use crate::{swqos::SwqosClient, trading::MiddlewareManager};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use solana_sdk::instruction::Instruction;
|
use solana_sdk::instruction::Instruction;
|
||||||
|
|
||||||
use super::params::{BuyParams, SellParams};
|
|
||||||
|
|
||||||
/// 交易执行器trait - 定义了所有交易协议都需要实现的核心方法
|
/// 交易执行器trait - 定义了所有交易协议都需要实现的核心方法
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait TradeExecutor: Send + Sync {
|
pub trait TradeExecutor: Send + Sync {
|
||||||
/// 使用MEV服务执行买入交易
|
/// 使用MEV服务执行买入交易
|
||||||
async fn buy_with_tip(
|
async fn buy_with_tip(&self, params: BuyParams) -> Result<()>;
|
||||||
&self,
|
|
||||||
params: BuyParams,
|
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
||||||
) -> Result<()>;
|
|
||||||
/// 使用MEV服务执行卖出交易
|
/// 使用MEV服务执行卖出交易
|
||||||
async fn sell_with_tip(
|
async fn sell_with_tip(&self, params: SellParams) -> Result<()>;
|
||||||
&self,
|
|
||||||
params: SellParams,
|
|
||||||
swqos_clients: Vec<Arc<SwqosClient>>,
|
|
||||||
middleware_manager: Option<Arc<MiddlewareManager>>,
|
|
||||||
) -> Result<()>;
|
|
||||||
/// 获取协议名称
|
/// 获取协议名称
|
||||||
fn protocol_name(&self) -> &'static str;
|
fn protocol_name(&self) -> &'static str;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user