feat: add transaction simulation support and close_mint_token_ata parameter

- Add `simulate` parameter to TradeBuyParams and TradeSellParams for transaction simulation
- Add `close_mint_token_ata` parameter to TradeSellParams for closing mint token ATA
- Implement simulate_transaction function to validate transactions without blockchain submission
- Update all examples to include new parameters (simulate: false, close_mint_token_ata: false)
- Add comprehensive documentation for transaction simulation feature
- Show detailed simulation output including logs, compute units, and errors
- Print timing metrics in English for better international compatibility
This commit is contained in:
ysq
2025-10-30 22:11:55 +08:00
parent 11383a0492
commit d5834e056f
19 changed files with 307 additions and 27 deletions
+19
View File
@@ -38,6 +38,7 @@ The `TradeBuyParams` struct contains all parameters required for executing buy o
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
| `fixed_output_token_amount` | `Option<u64>` | ❌ | Optional fixed output token amount. If set, this value will be directly assigned to the output amount instead of being calculated (required for Meteora DAMM V2) |
| `gas_fee_strategy` | `GasFeeStrategy` | ✅ | Gas fee strategy instance for controlling transaction fees and priorities |
| `simulate` | `bool` | ✅ | Whether to simulate the transaction instead of executing it. When true, the transaction will be simulated via RPC to validate and show detailed logs, compute units consumed, and potential errors without actually submitting to the blockchain |
## TradeSellParams
@@ -69,6 +70,7 @@ The `TradeSellParams` struct contains all parameters required for executing sell
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
| `gas_fee_strategy` | `GasFeeStrategy` | ✅ | Gas fee strategy instance for controlling transaction fees and priorities |
| `fixed_output_token_amount` | `Option<u64>` | ❌ | Optional fixed output token amount. If set, this value will be directly assigned to the output amount instead of being calculated (required for Meteora DAMM V2) |
| `simulate` | `bool` | ✅ | Whether to simulate the transaction instead of executing it. When true, the transaction will be simulated via RPC to validate and show detailed logs, compute units consumed, and potential errors without actually submitting to the blockchain |
## Parameter Categories
@@ -158,3 +160,20 @@ Each DEX protocol requires specific `extension_params`:
- **Meteora DAMM V2**: `MeteoraDammV2Params`
Refer to the respective protocol documentation for detailed parameter specifications.
### 🔍 Transaction Simulation
When `simulate: true`:
- **No Blockchain Submission**: The transaction is not actually submitted to the blockchain
- **Validation**: Validates transaction construction and execution without consuming actual tokens
- **Detailed Output**: Shows comprehensive information including:
- Transaction logs with detailed execution steps
- Compute units consumed (useful for optimizing CU budget)
- Potential errors and failure reasons
- Inner instructions for debugging
- **Use Cases**:
- Testing transaction logic before real execution
- Debugging failed transactions
- Estimating compute unit consumption
- Validating transaction parameters
- 📝 **Note**: Simulation uses RPC's `simulateTransaction` method with processed commitment level
+19
View File
@@ -38,6 +38,7 @@
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
| `fixed_output_token_amount` | `Option<u64>` | ❌ | 可选的固定输出代币数量。如果设置,此值将直接分配给输出数量而不是通过计算得出(Meteora DAMM V2 必需) |
| `gas_fee_strategy` | `GasFeeStrategy` | ✅ | Gas fee 策略实例,用于控制交易费用和优先级 |
| `simulate` | `bool` | ✅ | 是否模拟交易而不实际执行。当为 true 时,将通过 RPC 模拟交易以验证并显示详细日志、计算单元消耗和潜在错误,而不会实际提交到区块链 |
## TradeSellParams
@@ -69,6 +70,7 @@
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
| `gas_fee_strategy` | `GasFeeStrategy` | ✅ | Gas fee 策略实例,用于控制交易费用和优先级 |
| `fixed_output_token_amount` | `Option<u64>` | ❌ | 可选的固定输出代币数量。如果设置,此值将直接分配给输出数量而不是通过计算得出(Meteora DAMM V2 必需) |
| `simulate` | `bool` | ✅ | 是否模拟交易而不实际执行。当为 true 时,将通过 RPC 模拟交易以验证并显示详细日志、计算单元消耗和潜在错误,而不会实际提交到区块链 |
## 参数分类
@@ -158,3 +160,20 @@
- **Meteora DAMM V2**: `MeteoraDammV2Params`
请参阅相应的协议文档了解详细的参数规格。
### 🔍 交易模拟
`simulate: true` 时:
- **不提交区块链**: 交易不会实际提交到区块链
- **验证功能**: 验证交易构建和执行,而不会消耗实际代币
- **详细输出**: 显示全面的信息,包括:
- 带有详细执行步骤的交易日志
- 计算单元消耗(用于优化 CU 预算)
- 潜在错误和失败原因
- 用于调试的内部指令
- **使用场景**:
- 在真实执行前测试交易逻辑
- 调试失败的交易
- 估算计算单元消耗
- 验证交易参数
- 📝 **注意**: 模拟使用 RPC 的 `simulateTransaction` 方法,采用 processed 承诺级别