feat: refactor trading API and add multi-token type support (v1.2.0)

- Bump version to 1.2.0
- Remove TradeSwapParams to simplify API structure
- Add TradeTokenType enum supporting SOL/WSOL/USD1 tokens
- Standardize parameter naming: sol_amount → input_token_amount, token_amount → input_token_amount
- Refactor account management parameters: create_wsol_ata → create_input_token_ata etc.
- Update all example code to use new API
- Synchronize English and Chinese documentation

BREAKING CHANGE:
- Removed TradeSwapParams struct
- Modified field names in TradeBuyParams and TradeSellParams
- Existing code needs migration to use new parameter structure
This commit is contained in:
ysq
2025-09-22 23:18:53 +08:00
parent 020187a57a
commit ce5243702c
19 changed files with 274 additions and 435 deletions
+22 -62
View File
@@ -4,61 +4,11 @@ This document provides a comprehensive reference for all trading parameters used
## 📋 Table of Contents
- [TradeSwapParams](#tradeswapparams)
- [TradeBuyParams](#tradebuyparams)
- [TradeSellParams](#tradesellparams)
- [Parameter Categories](#parameter-categories)
- [Important Notes](#important-notes)
## TradeSwapParams
The `TradeSwapParams` struct contains all parameters required for executing swap orders across different DEX protocols. This is the most flexible trading method that supports swapping between any supported tokens.
### Basic Trading Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `dex_type` | `DexType` | ✅ | The trading protocol to use (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4) |
| `trade_type` | `TradeType` | ✅ | The type of trade to execute (Buy, Sell) |
| `input_mint` | `Pubkey` | ✅ | The public key of the token to spend (input token) |
| `output_mint` | `Pubkey` | ✅ | The public key of the token to receive (output token) |
| `input_token_program` | `Pubkey` | ✅ | The token program ID for the input token |
| `output_token_program` | `Pubkey` | ✅ | The token program ID for the output token |
| `input_amount` | `u64` | ✅ | Amount of input token to spend (in smallest token units) |
| `slippage_basis_points` | `Option<u64>` | ❌ | Slippage tolerance in basis points (e.g., 100 = 1%, 500 = 5%) |
| `recent_blockhash` | `Option<Hash>` | ❌ | Recent blockhash for transaction validity |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | Protocol-specific parameters (PumpFunParams, PumpSwapParams, etc.) |
### Advanced Configuration Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | Address lookup table key for transaction optimization |
| `wait_transaction_confirmed` | `bool` | ✅ | Whether to wait for transaction confirmation |
| `create_input_mint_ata` | `bool` | ✅ | Whether to create input token Associated Token Account |
| `close_input_mint_ata` | `bool` | ✅ | Whether to close input token ATA after transaction |
| `create_output_mint_ata` | `bool` | ✅ | Whether to create output token ATA |
| `close_output_mint_ata` | `bool` | ✅ | Whether to close output token ATA after transaction |
| `open_seed_optimize` | `bool` | ✅ | Whether to use seed optimization for reduced CU consumption |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
| `with_tip` | `bool` | ✅ | Whether to include tip for transaction priority |
### Supported Token Pairs
The SDK currently supports swap trading between the following base tokens and other tokens:
- **SOL** (Native Solana token)
- **WSOL** (Wrapped SOL)
- **USD1** (USD1 stablecoin - currently only supported on Bonk protocol)
**Important**: At least one token in the swap pair must be a supported base token (SOL, WSOL, or USD1).
### USD1 Token Support
USD1 token support has the following limitations:
- **Protocol Restriction**: USD1 trading is currently only supported on the Bonk protocol
- **Pair Requirements**: USD1 can be swapped with other tokens, but the swap must occur on Bonk DEX
- **Token Address**: `USD1ttGY1N17NEEHLmELoaybftRBUSErhqYiQzvEmuB`
## TradeBuyParams
The `TradeBuyParams` struct contains all parameters required for executing buy orders across different DEX protocols.
@@ -68,8 +18,9 @@ The `TradeBuyParams` struct contains all parameters required for executing buy o
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `dex_type` | `DexType` | ✅ | The trading protocol to use (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4) |
| `input_token_type` | `TradeTokenType` | ✅ | The type of input token to use (SOL, WSOL, USD1) |
| `mint` | `Pubkey` | ✅ | The public key of the token mint to purchase |
| `sol_amount` | `u64` | ✅ | Amount of SOL to spend (in lamports) |
| `input_token_amount` | `u64` | ✅ | Amount of input token to spend (in smallest token units) |
| `slippage_basis_points` | `Option<u64>` | ❌ | Slippage tolerance in basis points (e.g., 100 = 1%, 500 = 5%) |
| `recent_blockhash` | `Option<Hash>` | ❌ | Recent blockhash for transaction validity |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | Protocol-specific parameters (PumpFunParams, PumpSwapParams, etc.) |
@@ -80,8 +31,8 @@ The `TradeBuyParams` struct contains all parameters required for executing buy o
|-----------|------|----------|-------------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | Address lookup table key for transaction optimization |
| `wait_transaction_confirmed` | `bool` | ✅ | Whether to wait for transaction confirmation |
| `create_wsol_ata` | `bool` | ✅ | Whether to create wSOL Associated Token Account |
| `close_wsol_ata` | `bool` | ✅ | Whether to close wSOL ATA after transaction |
| `create_input_token_ata` | `bool` | ✅ | Whether to create input token Associated Token Account |
| `close_input_token_ata` | `bool` | ✅ | Whether to close input token ATA after transaction |
| `create_mint_ata` | `bool` | ✅ | Whether to create token mint ATA |
| `open_seed_optimize` | `bool` | ✅ | Whether to use seed optimization for reduced CU consumption |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
@@ -96,8 +47,9 @@ The `TradeSellParams` struct contains all parameters required for executing sell
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `dex_type` | `DexType` | ✅ | The trading protocol to use (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4) |
| `output_token_type` | `TradeTokenType` | ✅ | The type of output token to receive (SOL, WSOL, USD1) |
| `mint` | `Pubkey` | ✅ | The public key of the token mint to sell |
| `token_amount` | `u64` | ✅ | Amount of tokens to sell (in smallest token units) |
| `input_token_amount` | `u64` | ✅ | Amount of tokens to sell (in smallest token units) |
| `slippage_basis_points` | `Option<u64>` | ❌ | Slippage tolerance in basis points (e.g., 100 = 1%, 500 = 5%) |
| `recent_blockhash` | `Option<Hash>` | ❌ | Recent blockhash for transaction validity |
| `with_tip` | `bool` | ✅ | Whether to include tip in the transaction |
@@ -109,8 +61,8 @@ The `TradeSellParams` struct contains all parameters required for executing sell
|-----------|------|----------|-------------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | Address lookup table key for transaction optimization |
| `wait_transaction_confirmed` | `bool` | ✅ | Whether to wait for transaction confirmation |
| `create_wsol_ata` | `bool` | ✅ | Whether to create wSOL Associated Token Account |
| `close_wsol_ata` | `bool` | ✅ | Whether to close wSOL ATA after transaction |
| `create_output_token_ata` | `bool` | ✅ | Whether to create output token Associated Token Account |
| `close_output_token_ata` | `bool` | ✅ | Whether to close output token ATA after transaction |
| `open_seed_optimize` | `bool` | ✅ | Whether to use seed optimization for reduced CU consumption |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | Durable nonce information containing nonce account and current nonce value |
@@ -122,8 +74,9 @@ The `TradeSellParams` struct contains all parameters required for executing sell
These parameters are essential for defining the basic trading operation:
- **dex_type**: Determines which protocol to use for trading
- **input_token_type** (buy) / **output_token_type** (sell): Specifies the base token type (SOL, WSOL, USD1)
- **mint**: Specifies the token to trade
- **sol_amount** (buy) / **token_amount** (sell): Defines the trade size
- **input_token_amount**: Defines the trade size (for both buy and sell operations)
- **recent_blockhash**: Ensures transaction validity
### ⚙️ Transaction Control Parameters
@@ -137,9 +90,9 @@ These parameters control how the transaction is processed:
These parameters control automatic account creation and management:
- **create_wsol_ata**: Automatically wrap SOL to wSOL when needed
- **close_wsol_ata**: Automatically unwrap wSOL to SOL after trading
- **create_mint_ata**: Automatically create token accounts
- **create_input_token_ata** (buy) / **create_output_token_ata** (sell): Automatically create token accounts for input/output tokens
- **close_input_token_ata** (buy) / **close_output_token_ata** (sell): Automatically close token accounts after trading
- **create_mint_ata**: Automatically create token accounts for the traded token
### 🚀 Optimization Parameters
@@ -148,6 +101,13 @@ These parameters enable advanced optimizations:
- **lookup_table_key**: Use address lookup tables for reduced transaction size
- **open_seed_optimize**: Use seed-based account creation for lower CU consumption
### 🔄 Token Type Parameters
The **TradeTokenType** enum supports the following base tokens:
- **SOL**: Native Solana token (typically used with PumpFun)
- **WSOL**: Wrapped SOL token (typically used with PumpSwap, Bonk, Raydium protocols)
- **USD1**: USD1 stablecoin (currently only supported on Bonk protocol)
### 🔄 Optional Parameters
When you need to use durable nonce, you need to fill in this parameter:
@@ -162,9 +122,9 @@ When `open_seed_optimize: true`:
- ⚠️ **Warning**: Official platform selling methods may fail
- 📝 **Note**: Use `get_associated_token_address_with_program_id_fast_use_seed` to get ATA addresses
### 💰 wSOL Account Management
### 💰 Token Account Management
The `create_wsol_ata` and `close_wsol_ata` parameters provide granular control:
The account management parameters provide granular control:
- **Independent Control**: Create and close operations can be controlled separately
- **Batch Operations**: Create once, trade multiple times, then close
+22 -62
View File
@@ -4,61 +4,11 @@
## 📋 目录
- [TradeSwapParams](#tradeswapparams)
- [TradeBuyParams](#tradebuyparams)
- [TradeSellParams](#tradesellparams)
- [参数分类](#参数分类)
- [重要说明](#重要说明)
## TradeSwapParams
`TradeSwapParams` 结构体包含在不同 DEX 协议上执行代币交换订单所需的所有参数。这是最灵活的交易方法,支持在任何支持的代币之间进行交换。
### 基础交易参数
| 参数 | 类型 | 必需 | 描述 |
|------|------|------|------|
| `dex_type` | `DexType` | ✅ | 要使用的交易协议 (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4) |
| `trade_type` | `TradeType` | ✅ | 要执行的交易类型 (Buy, Sell) |
| `input_mint` | `Pubkey` | ✅ | 要花费的代币公钥(输入代币) |
| `output_mint` | `Pubkey` | ✅ | 要接收的代币公钥(输出代币) |
| `input_token_program` | `Pubkey` | ✅ | 输入代币的代币程序 ID |
| `output_token_program` | `Pubkey` | ✅ | 输出代币的代币程序 ID |
| `input_amount` | `u64` | ✅ | 要花费的输入代币数量(最小代币单位) |
| `slippage_basis_points` | `Option<u64>` | ❌ | 滑点容忍度(基点单位,例如 100 = 1%, 500 = 5% |
| `recent_blockhash` | `Option<Hash>` | ❌ | 用于交易有效性的最新区块哈希 |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | 协议特定参数 (PumpFunParams, PumpSwapParams 等) |
### 高级配置参数
| 参数 | 类型 | 必需 | 描述 |
|------|------|------|------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | 用于交易优化的地址查找表键 |
| `wait_transaction_confirmed` | `bool` | ✅ | 是否等待交易确认 |
| `create_input_mint_ata` | `bool` | ✅ | 是否创建输入代币关联代币账户 |
| `close_input_mint_ata` | `bool` | ✅ | 交易后是否关闭输入代币 ATA |
| `create_output_mint_ata` | `bool` | ✅ | 是否创建输出代币 ATA |
| `close_output_mint_ata` | `bool` | ✅ | 交易后是否关闭输出代币 ATA |
| `open_seed_optimize` | `bool` | ✅ | 是否使用 seed 优化以减少 CU 消耗 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
| `with_tip` | `bool` | ✅ | 是否包含小费以提高交易优先级 |
### 支持的代币交易对
SDK 目前支持以下基础代币与其他代币之间的交换交易:
- **SOL**Solana 原生代币)
- **WSOL**(包装 SOL
- **USD1**USD1 稳定币 - 目前仅在 Bonk 协议上支持)
**重要提示**:交换对中至少有一个代币必须是支持的基础代币(SOL、WSOL 或 USD1)。
### USD1 代币支持
USD1 代币支持有以下限制:
- **协议限制**:USD1 交易目前仅在 Bonk 协议上支持
- **交易对要求**:USD1 可以与其他代币交换,但必须在 Bonk DEX 上进行
- **代币地址**`USD1ttGY1N17NEEHLmELoaybftRBUSErhqYiQzvEmuB`
## TradeBuyParams
`TradeBuyParams` 结构体包含在不同 DEX 协议上执行买入订单所需的所有参数。
@@ -68,8 +18,9 @@ USD1 代币支持有以下限制:
| 参数 | 类型 | 必需 | 描述 |
|------|------|------|------|
| `dex_type` | `DexType` | ✅ | 要使用的交易协议 (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4) |
| `input_token_type` | `TradeTokenType` | ✅ | 要使用的输入代币类型 (SOL, WSOL, USD1) |
| `mint` | `Pubkey` | ✅ | 要购买的代币 mint 公钥 |
| `sol_amount` | `u64` | ✅ | 要花费的 SOL 数量(以 lamports 为单位) |
| `input_token_amount` | `u64` | ✅ | 要花费的输入代币数量(最小代币单位) |
| `slippage_basis_points` | `Option<u64>` | ❌ | 滑点容忍度(基点单位,例如 100 = 1%, 500 = 5% |
| `recent_blockhash` | `Option<Hash>` | ❌ | 用于交易有效性的最新区块哈希 |
| `extension_params` | `Box<dyn ProtocolParams>` | ✅ | 协议特定参数 (PumpFunParams, PumpSwapParams 等) |
@@ -80,8 +31,8 @@ USD1 代币支持有以下限制:
|------|------|------|------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | 用于交易优化的地址查找表键 |
| `wait_transaction_confirmed` | `bool` | ✅ | 是否等待交易确认 |
| `create_wsol_ata` | `bool` | ✅ | 是否创建 wSOL 关联代币账户 |
| `close_wsol_ata` | `bool` | ✅ | 交易后是否关闭 wSOL ATA |
| `create_input_token_ata` | `bool` | ✅ | 是否创建输入代币关联代币账户 |
| `close_input_token_ata` | `bool` | ✅ | 交易后是否关闭输入代币 ATA |
| `create_mint_ata` | `bool` | ✅ | 是否创建代币 mint ATA |
| `open_seed_optimize` | `bool` | ✅ | 是否使用 seed 优化以减少 CU 消耗 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
@@ -96,8 +47,9 @@ USD1 代币支持有以下限制:
| 参数 | 类型 | 必需 | 描述 |
|------|------|------|------|
| `dex_type` | `DexType` | ✅ | 要使用的交易协议 (PumpFun, PumpSwap, Bonk, RaydiumCpmm, RaydiumAmmV4) |
| `output_token_type` | `TradeTokenType` | ✅ | 要接收的输出代币类型 (SOL, WSOL, USD1) |
| `mint` | `Pubkey` | ✅ | 要出售的代币 mint 公钥 |
| `token_amount` | `u64` | ✅ | 要出售的代币数量(最小代币单位) |
| `input_token_amount` | `u64` | ✅ | 要出售的代币数量(最小代币单位) |
| `slippage_basis_points` | `Option<u64>` | ❌ | 滑点容忍度(基点单位,例如 100 = 1%, 500 = 5% |
| `recent_blockhash` | `Option<Hash>` | ❌ | 用于交易有效性的最新区块哈希 |
| `with_tip` | `bool` | ✅ | 交易中是否包含小费 |
@@ -109,8 +61,8 @@ USD1 代币支持有以下限制:
|------|------|------|------|
| `lookup_table_key` | `Option<Pubkey>` | ❌ | 用于交易优化的地址查找表键 |
| `wait_transaction_confirmed` | `bool` | ✅ | 是否等待交易确认 |
| `create_wsol_ata` | `bool` | ✅ | 是否创建 wSOL 关联代币账户 |
| `close_wsol_ata` | `bool` | ✅ | 交易后是否关闭 wSOL ATA |
| `create_output_token_ata` | `bool` | ✅ | 是否创建输出代币关联代币账户 |
| `close_output_token_ata` | `bool` | ✅ | 交易后是否关闭输出代币 ATA |
| `open_seed_optimize` | `bool` | ✅ | 是否使用 seed 优化以减少 CU 消耗 |
| `durable_nonce` | `Option<DurableNonceInfo>` | ❌ | 持久 nonce 信息,包含 nonce 账户和当前 nonce 值 |
@@ -122,8 +74,9 @@ USD1 代币支持有以下限制:
这些参数对于定义基本交易操作至关重要:
- **dex_type**: 确定用于交易的协议
- **input_token_type** (买入) / **output_token_type** (卖出): 指定基础代币类型 (SOL, WSOL, USD1)
- **mint**: 指定要交易的代币
- **sol_amount** (买入) / **token_amount** (卖出): 定义交易规模
- **input_token_amount**: 定义交易规模(买入和卖出操作都使用此参数)
- **recent_blockhash**: 确保交易有效性
### ⚙️ 交易控制参数
@@ -137,9 +90,9 @@ USD1 代币支持有以下限制:
这些参数控制自动账户创建和管理:
- **create_wsol_ata**: 需要时自动将 SOL 包装为 wSOL
- **close_wsol_ata**: 交易后自动将 wSOL 解包装为 SOL
- **create_mint_ata**: 自动创建代币账户
- **create_input_token_ata** (买入) / **create_output_token_ata** (卖出): 自动为输入/输出代币创建代币账户
- **close_input_token_ata** (买入) / **close_output_token_ata** (卖出): 交易后自动关闭代币账户
- **create_mint_ata**: 自动为交易代币创建代币账户
### 🚀 优化参数
@@ -148,6 +101,13 @@ USD1 代币支持有以下限制:
- **lookup_table_key**: 使用地址查找表减少交易大小
- **open_seed_optimize**: 使用基于 seed 的账户创建以降低 CU 消耗
### 🔄 代币类型参数
**TradeTokenType** 枚举支持以下基础代币:
- **SOL**: Solana 原生代币(通常与 PumpFun 协议一起使用)
- **WSOL**: 包装 SOL 代币(通常与 PumpSwap、Bonk、Raydium 协议一起使用)
- **USD1**: USD1 稳定币(目前仅在 Bonk 协议上支持)
### 🔄 非必填参数
当你需要使用 durable nonce 时,需要填入这个参数:
@@ -162,9 +122,9 @@ USD1 代币支持有以下限制:
- ⚠️ **警告**: 官方平台的出售方法可能会失败
- 📝 **注意**: 使用 `get_associated_token_address_with_program_id_fast_use_seed` 获取 ATA 地址
### 💰 wSOL 账户管理
### 💰 代币账户管理
`create_wsol_ata``close_wsol_ata` 参数提供精细控制:
账户管理参数提供精细控制:
- **独立控制**: 创建和关闭操作可以分别控制
- **批量操作**: 创建一次,多次交易,然后关闭