pump-trader 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/QUICKSTART.md +390 -0
- package/README.md +488 -0
- package/index.js +1294 -0
- package/index.ts +1429 -0
- package/package.json +55 -0
- package/tsconfig.json +25 -0
package/QUICKSTART.md
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
# 快速开始指南
|
|
2
|
+
|
|
3
|
+
## 🎯 5 分钟快速上手
|
|
4
|
+
|
|
5
|
+
### 步骤 1: 安装依赖
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @solana/web3.js @solana/spl-token bn.js bs58
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### 步骤 2: 导入库
|
|
12
|
+
|
|
13
|
+
#### JavaScript
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { PumpTrader } from './index.js';
|
|
17
|
+
|
|
18
|
+
const RPC = "https://api.mainnet-beta.solana.com";
|
|
19
|
+
const PRIVATE_KEY = "your_private_key";
|
|
20
|
+
|
|
21
|
+
const trader = new PumpTrader(RPC, PRIVATE_KEY);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
#### TypeScript
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { PumpTrader, TradeOptions } from './index';
|
|
28
|
+
|
|
29
|
+
const trader = new PumpTrader(RPC, PRIVATE_KEY);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 步骤 3: 自动交易
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
// 定义交易参数
|
|
36
|
+
const tradeOpt = {
|
|
37
|
+
maxSolPerTx: BigInt(1_000_000_000), // 1 SOL
|
|
38
|
+
slippage: { base: 500 }, // 5% 滑点
|
|
39
|
+
priority: { base: 5000 } // 优先级
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// 自动买入(库自动判断内盘/外盘)
|
|
43
|
+
const result = await trader.autoBuy(
|
|
44
|
+
"token_address",
|
|
45
|
+
BigInt(100_000_000), // 0.1 SOL
|
|
46
|
+
tradeOpt
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
console.log("交易已发送:", result.pendingTransactions);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## 🚀 核心概念(3 分钟)
|
|
55
|
+
|
|
56
|
+
### 问题 1: 什么是 Token Program?
|
|
57
|
+
|
|
58
|
+
**简答:** 控制 Token 的程序版本
|
|
59
|
+
|
|
60
|
+
- **TOKEN_PROGRAM_ID**: 标准 Token (SPL)
|
|
61
|
+
- **TOKEN_2022_PROGRAM_ID**: 新版本 Token (SPL-2022)
|
|
62
|
+
|
|
63
|
+
**库的处理:**
|
|
64
|
+
```javascript
|
|
65
|
+
// ✅ 自动检测,无需手动指定
|
|
66
|
+
const tokenProgram = await trader.detectTokenProgram(tokenAddr);
|
|
67
|
+
console.log(tokenProgram.type); // 自动识别是哪个版本
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 问题 2: 什么是内盘/外盘?
|
|
71
|
+
|
|
72
|
+
**简答:** Token 的两个交易阶段
|
|
73
|
+
|
|
74
|
+
- **内盘 (Bonding Curve)**: 启动阶段,价格由绑定曲线决定
|
|
75
|
+
- **外盘 (AMM)**: 成熟阶段,进入 Raydium 等 AMM
|
|
76
|
+
|
|
77
|
+
**库的处理:**
|
|
78
|
+
```javascript
|
|
79
|
+
// ✅ 自动判断,无需手动判断
|
|
80
|
+
const mode = await trader.getTradeMode(tokenAddr);
|
|
81
|
+
// 返回 "bonding" 或 "amm"
|
|
82
|
+
|
|
83
|
+
// ✅ 统一接口,自动选择
|
|
84
|
+
const result = await trader.autoBuy(tokenAddr, amount, tradeOpt);
|
|
85
|
+
// 无论是内盘还是外盘,都能正确交易
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 问题 3: 为什么需要这个库?
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
// ❌ 手动方式(繁琐)
|
|
92
|
+
if (mode === "bonding") {
|
|
93
|
+
result = await trader.buy(tokenAddr, amount, tradeOpt);
|
|
94
|
+
} else {
|
|
95
|
+
result = await trader.ammBuy(tokenAddr, amount, tradeOpt);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ✅ 改进方式(简洁)
|
|
99
|
+
result = await trader.autoBuy(tokenAddr, amount, tradeOpt);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## 📚 常用代码片段
|
|
105
|
+
|
|
106
|
+
### 买入(自动判断)
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
const result = await trader.autoBuy(
|
|
110
|
+
"TokenAddress",
|
|
111
|
+
BigInt(100_000_000),
|
|
112
|
+
{ maxSolPerTx: BigInt(1e9), slippage: { base: 500 }, priority: { base: 5000 } }
|
|
113
|
+
);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 卖出(自动判断)
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
119
|
+
const result = await trader.autoSell(
|
|
120
|
+
"TokenAddress",
|
|
121
|
+
BigInt(1_000_000), // 要卖出的代币数量
|
|
122
|
+
{ maxSolPerTx: BigInt(1e9), slippage: { base: 500 }, priority: { base: 5000 } }
|
|
123
|
+
);
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 查看价格
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
const { price, completed } = await trader.getPriceAndStatus("TokenAddress");
|
|
130
|
+
console.log(`价格: ${price} SOL`);
|
|
131
|
+
console.log(`状态: ${completed ? "外盘" : "内盘"}`);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 查看余额
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
const balance = await trader.tokenBalance("TokenAddress");
|
|
138
|
+
console.log(`代币余额: ${balance}`);
|
|
139
|
+
|
|
140
|
+
const solBalance = await trader.solBalance();
|
|
141
|
+
console.log(`SOL 余额: ${solBalance}`);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 监听交易
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
const unsubscribe = trader.listenTrades((event) => {
|
|
148
|
+
console.log(`${event.isBuy ? "买" : "卖"}: ${Number(event.solAmount) / 1e9} SOL`);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// 停止监听
|
|
152
|
+
setTimeout(() => unsubscribe(), 60000);
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 🎨 完整示例
|
|
158
|
+
|
|
159
|
+
### 示例:自动买入 + 确认 + 卖出
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
import { PumpTrader } from './index.js';
|
|
163
|
+
|
|
164
|
+
async function quickTrade() {
|
|
165
|
+
const trader = new PumpTrader(
|
|
166
|
+
"https://api.mainnet-beta.solana.com",
|
|
167
|
+
"your_private_key"
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const tokenAddr = "token_address_here";
|
|
171
|
+
const tradeOpt = {
|
|
172
|
+
maxSolPerTx: BigInt(1_000_000_000),
|
|
173
|
+
slippage: { base: 500 },
|
|
174
|
+
priority: { base: 5000, enableRandom: true, randomRange: 5000 }
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
// 1️⃣ 买入
|
|
179
|
+
console.log("🛒 开始买入...");
|
|
180
|
+
const buyResult = await trader.autoBuy(
|
|
181
|
+
tokenAddr,
|
|
182
|
+
BigInt(100_000_000), // 0.1 SOL
|
|
183
|
+
tradeOpt
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
if (buyResult.pendingTransactions.length > 0) {
|
|
187
|
+
const tx = buyResult.pendingTransactions[0];
|
|
188
|
+
console.log(`✅ 买入交易: ${tx.signature}`);
|
|
189
|
+
|
|
190
|
+
// 2️⃣ 确认
|
|
191
|
+
console.log("⏳ 等待确认...");
|
|
192
|
+
await trader.confirmTransactionWithPolling(
|
|
193
|
+
tx.signature,
|
|
194
|
+
tx.lastValidBlockHeight,
|
|
195
|
+
5,
|
|
196
|
+
2000
|
|
197
|
+
);
|
|
198
|
+
console.log("✅ 交易已确认");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 3️⃣ 查看余额
|
|
202
|
+
const balance = await trader.tokenBalance(tokenAddr);
|
|
203
|
+
console.log(`💰 你现在拥有 ${balance} 个 Token`);
|
|
204
|
+
|
|
205
|
+
// 4️⃣ 卖出
|
|
206
|
+
console.log("📤 开始卖出...");
|
|
207
|
+
const sellResult = await trader.autoSell(
|
|
208
|
+
tokenAddr,
|
|
209
|
+
BigInt(Math.floor(balance * 1e6)),
|
|
210
|
+
tradeOpt
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
if (sellResult.pendingTransactions.length > 0) {
|
|
214
|
+
console.log(`✅ 卖出交易: ${sellResult.pendingTransactions[0].signature}`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error("❌ 错误:", error.message);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
quickTrade();
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## 🔧 参数配置说明
|
|
228
|
+
|
|
229
|
+
### TradeOptions 详解
|
|
230
|
+
|
|
231
|
+
```javascript
|
|
232
|
+
{
|
|
233
|
+
// 每笔交易的最大 SOL 数量
|
|
234
|
+
maxSolPerTx: BigInt(1_000_000_000), // 1 SOL
|
|
235
|
+
|
|
236
|
+
// 滑点设置
|
|
237
|
+
slippage: {
|
|
238
|
+
base: 500, // 基础滑点 5% (bps)
|
|
239
|
+
min: 300, // 最小 3%
|
|
240
|
+
max: 1000, // 最大 10%
|
|
241
|
+
impactFactor: 1 // 价格影响系数 (1 = 标准)
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
// 优先级费用
|
|
245
|
+
priority: {
|
|
246
|
+
base: 5000, // 基础费用 (microLamports)
|
|
247
|
+
enableRandom: true, // 启用随机
|
|
248
|
+
randomRange: 5000 // 随机范围 (0-5000)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
**常见配置:**
|
|
254
|
+
|
|
255
|
+
```javascript
|
|
256
|
+
// 保守配置(低风险)
|
|
257
|
+
{
|
|
258
|
+
maxSolPerTx: BigInt(1e9),
|
|
259
|
+
slippage: { base: 1000, min: 500, max: 2000 },
|
|
260
|
+
priority: { base: 10000 }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// 激进配置(高速度)
|
|
264
|
+
{
|
|
265
|
+
maxSolPerTx: BigInt(5e9),
|
|
266
|
+
slippage: { base: 300, min: 100, max: 500 },
|
|
267
|
+
priority: { base: 50000, enableRandom: true, randomRange: 50000 }
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// 平衡配置(推荐)
|
|
271
|
+
{
|
|
272
|
+
maxSolPerTx: BigInt(1e9),
|
|
273
|
+
slippage: { base: 500, min: 300, max: 1000 },
|
|
274
|
+
priority: { base: 5000, enableRandom: true, randomRange: 5000 }
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
---
|
|
279
|
+
|
|
280
|
+
## ❌ 常见错误 & 解决方案
|
|
281
|
+
|
|
282
|
+
### 错误 1: "Token Program 检测失败"
|
|
283
|
+
|
|
284
|
+
```
|
|
285
|
+
错误: Failed to detect token program for XXX
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
**原因:** Token 地址无效或 RPC 连接问题
|
|
289
|
+
|
|
290
|
+
**解决:**
|
|
291
|
+
```javascript
|
|
292
|
+
// 检查地址是否正确
|
|
293
|
+
console.log("Token 地址:", tokenAddr);
|
|
294
|
+
|
|
295
|
+
// 检查 RPC 连接
|
|
296
|
+
const connection = trader.getConnection();
|
|
297
|
+
const balance = await connection.getBalance(trader.getWallet().publicKey);
|
|
298
|
+
console.log("钱包余额:", balance);
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### 错误 2: "Bonding curve already completed"
|
|
302
|
+
|
|
303
|
+
```
|
|
304
|
+
错误: Bonding curve already completed
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
**原因:** Token 已进入外盘,不能再用 buy() 方法
|
|
308
|
+
|
|
309
|
+
**解决:**
|
|
310
|
+
```javascript
|
|
311
|
+
// 使用自动方法,会自动选择
|
|
312
|
+
const result = await trader.autoBuy(tokenAddr, amount, tradeOpt);
|
|
313
|
+
// 不用手动判断
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### 错误 3: "交易超时"
|
|
317
|
+
|
|
318
|
+
```
|
|
319
|
+
错误: 交易确认超时
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
**原因:** 网络拥堵,交易没有及时确认
|
|
323
|
+
|
|
324
|
+
**解决:**
|
|
325
|
+
```javascript
|
|
326
|
+
// 增加优先级费用
|
|
327
|
+
const tradeOpt = {
|
|
328
|
+
priority: { base: 50000 } // 增加到 50000 microLamports
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
// 或者增加重试次数
|
|
332
|
+
await trader.confirmTransactionWithPolling(
|
|
333
|
+
signature,
|
|
334
|
+
blockHeight,
|
|
335
|
+
10, // 增加重试次数
|
|
336
|
+
2000
|
|
337
|
+
);
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
## 📊 性能优化建议
|
|
343
|
+
|
|
344
|
+
### 1. 使用缓存
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
// 第一次:自动缓存
|
|
348
|
+
const prog1 = await trader.detectTokenProgram(tokenAddr);
|
|
349
|
+
|
|
350
|
+
// 第二次:使用缓存(更快)
|
|
351
|
+
const prog2 = await trader.detectTokenProgram(tokenAddr);
|
|
352
|
+
|
|
353
|
+
// 查看缓存
|
|
354
|
+
console.log(trader.getCachedTokenProgram(tokenAddr));
|
|
355
|
+
|
|
356
|
+
// 清除缓存(如果需要)
|
|
357
|
+
trader.clearTokenProgramCache(tokenAddr);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### 2. 批量操作
|
|
361
|
+
|
|
362
|
+
```javascript
|
|
363
|
+
// 好的做法:顺序处理,允许失败继续
|
|
364
|
+
for (const tokenAddr of tokens) {
|
|
365
|
+
try {
|
|
366
|
+
const result = await trader.autoBuy(tokenAddr, amount, tradeOpt);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
console.error(`${tokenAddr} 失败: ${error.message}`);
|
|
369
|
+
// 继续下一个
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### 3. 监听事件而不是轮询
|
|
375
|
+
|
|
376
|
+
```javascript
|
|
377
|
+
// ❌ 不推荐:不断轮询余额
|
|
378
|
+
setInterval(async () => {
|
|
379
|
+
const balance = await trader.tokenBalance(tokenAddr);
|
|
380
|
+
// 效率低,浪费 API 配额
|
|
381
|
+
}, 1000);
|
|
382
|
+
|
|
383
|
+
// ✅ 推荐:监听事件
|
|
384
|
+
trader.listenTrades((event) => {
|
|
385
|
+
if (event.isBuy && event.solAmount > BigInt(1e9)) {
|
|
386
|
+
// 大额买入
|
|
387
|
+
console.log("检测到大额买入");
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
```
|