pump-trader 1.1.8 → 1.1.9

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/index.js CHANGED
@@ -21,7 +21,6 @@ import {
21
21
  } from "@solana/spl-token";
22
22
 
23
23
  import BN from "bn.js";
24
- import bs58 from "bs58";
25
24
 
26
25
  /* ================= 常量定义 ================= */
27
26
 
@@ -179,15 +178,24 @@ function parsePoolKeys(data) {
179
178
  /* ================= PumpTrader 类 ================= */
180
179
 
181
180
  export class PumpTrader {
182
- constructor(rpc, privateKey) {
181
+ constructor(rpc, wallet) {
183
182
  this.connection = new Connection(rpc, "confirmed");
184
- this.wallet = Keypair.fromSecretKey(bs58.decode(privateKey));
183
+ this._wallet = wallet;
184
+ this.publicKey = wallet.publicKey;
185
185
  this.global = PublicKey.findProgramAddressSync(
186
186
  [SEEDS.GLOBAL],
187
187
  PROGRAM_IDS.PUMP,
188
188
  )[0];
189
189
  this.globalState = null;
190
- this.tokenProgramCache = new Map(); // 缓存token program检测结果
190
+ this.tokenProgramCache = new Map();
191
+ }
192
+
193
+ async signTx(tx) {
194
+ if (this._wallet instanceof Keypair) {
195
+ tx.sign(this._wallet);
196
+ return tx;
197
+ }
198
+ return this._wallet.signTransaction(tx);
191
199
  }
192
200
 
193
201
  /* ---------- Token Program 检测 ---------- */
@@ -539,7 +547,7 @@ export class PumpTrader {
539
547
  // 查询单个代币
540
548
  const mint = new PublicKey(tokenAddr);
541
549
  const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
542
- this.wallet.publicKey,
550
+ this.publicKey,
543
551
  { mint },
544
552
  );
545
553
  return (
@@ -558,7 +566,7 @@ export class PumpTrader {
558
566
  */
559
567
  async getAllTokenBalances() {
560
568
  const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
561
- this.wallet.publicKey,
569
+ this.publicKey,
562
570
  { programId: TOKEN_PROGRAM_ID },
563
571
  );
564
572
 
@@ -582,7 +590,7 @@ export class PumpTrader {
582
590
  // 同时查询 TOKEN_2022_PROGRAM_ID
583
591
  const token2022Accounts =
584
592
  await this.connection.getParsedTokenAccountsByOwner(
585
- this.wallet.publicKey,
593
+ this.publicKey,
586
594
  { programId: TOKEN_2022_PROGRAM_ID },
587
595
  );
588
596
 
@@ -623,7 +631,7 @@ export class PumpTrader {
623
631
  }
624
632
 
625
633
  async solBalance() {
626
- const balance = await this.connection.getBalance(this.wallet.publicKey);
634
+ const balance = await this.connection.getBalance(this.publicKey);
627
635
  return balance / 1e9;
628
636
  }
629
637
 
@@ -633,7 +641,7 @@ export class PumpTrader {
633
641
  const program = tokenProgram || TOKEN_2022_PROGRAM_ID;
634
642
  const ata = getAssociatedTokenAddressSync(
635
643
  mint,
636
- this.wallet.publicKey,
644
+ this.publicKey,
637
645
  false,
638
646
  program,
639
647
  ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -643,9 +651,9 @@ export class PumpTrader {
643
651
  if (!acc) {
644
652
  tx.add(
645
653
  createAssociatedTokenAccountInstruction(
646
- this.wallet.publicKey,
654
+ this.publicKey,
647
655
  ata,
648
- this.wallet.publicKey,
656
+ this.publicKey,
649
657
  mint,
650
658
  program,
651
659
  ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -827,7 +835,7 @@ export class PumpTrader {
827
835
  const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
828
836
  [
829
837
  Buffer.from("user_volume_accumulator"),
830
- this.wallet.publicKey.toBuffer(),
838
+ this.publicKey.toBuffer(),
831
839
  ],
832
840
  PROGRAM_IDS.PUMP,
833
841
  );
@@ -867,7 +875,7 @@ export class PumpTrader {
867
875
  bonding,
868
876
  associatedBondingCurve,
869
877
  userAta,
870
- wallet: this.wallet.publicKey,
878
+ wallet: this.publicKey,
871
879
  creatorVault,
872
880
  eventAuthority: PROGRAM_IDS.EVENT_AUTHORITY,
873
881
  pumpProgram: PROGRAM_IDS.PUMP,
@@ -890,8 +898,8 @@ export class PumpTrader {
890
898
  const { blockhash, lastValidBlockHeight } =
891
899
  await this.connection.getLatestBlockhash("finalized");
892
900
  tx.recentBlockhash = blockhash;
893
- tx.feePayer = this.wallet.publicKey;
894
- tx.sign(this.wallet);
901
+ tx.feePayer = this.publicKey;
902
+ await this.signTx(tx);
895
903
 
896
904
  const signature = await this.connection.sendRawTransaction(
897
905
  tx.serialize(),
@@ -932,11 +940,11 @@ export class PumpTrader {
932
940
  totalSolOut <= tradeOpt.maxSolPerTx
933
941
  ? [totalTokenIn]
934
942
  : this.splitIntoN(
935
- totalTokenIn,
936
- Number(
937
- (totalSolOut + tradeOpt.maxSolPerTx - 1n) / tradeOpt.maxSolPerTx,
938
- ),
939
- );
943
+ totalTokenIn,
944
+ Number(
945
+ (totalSolOut + tradeOpt.maxSolPerTx - 1n) / tradeOpt.maxSolPerTx,
946
+ ),
947
+ );
940
948
 
941
949
  const pendingTransactions = []; // 待确认的交易
942
950
  const failedTransactions = []; // 发送失败的交易
@@ -951,7 +959,7 @@ export class PumpTrader {
951
959
 
952
960
  const userAta = getAssociatedTokenAddressSync(
953
961
  mint,
954
- this.wallet.publicKey,
962
+ this.publicKey,
955
963
  false,
956
964
  tokenProgram.programId,
957
965
  ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -969,7 +977,7 @@ export class PumpTrader {
969
977
  const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
970
978
  [
971
979
  Buffer.from("user_volume_accumulator"),
972
- this.wallet.publicKey.toBuffer(),
980
+ this.publicKey.toBuffer(),
973
981
  ],
974
982
  PROGRAM_IDS.PUMP,
975
983
  );
@@ -1002,7 +1010,7 @@ export class PumpTrader {
1002
1010
  bonding,
1003
1011
  associatedBondingCurve,
1004
1012
  userAta,
1005
- wallet: this.wallet.publicKey,
1013
+ wallet: this.publicKey,
1006
1014
  creatorVault,
1007
1015
  eventAuthority: PROGRAM_IDS.EVENT_AUTHORITY,
1008
1016
  pumpProgram: PROGRAM_IDS.PUMP,
@@ -1025,8 +1033,8 @@ export class PumpTrader {
1025
1033
  const { blockhash, lastValidBlockHeight } =
1026
1034
  await this.connection.getLatestBlockhash("finalized");
1027
1035
  tx.recentBlockhash = blockhash;
1028
- tx.feePayer = this.wallet.publicKey;
1029
- tx.sign(this.wallet);
1036
+ tx.feePayer = this.publicKey;
1037
+ await this.signTx(tx);
1030
1038
 
1031
1039
  const signature = await this.connection.sendRawTransaction(
1032
1040
  tx.serialize(),
@@ -1086,11 +1094,11 @@ export class PumpTrader {
1086
1094
  );
1087
1095
  const userQuoteAta = isSolQuote
1088
1096
  ? await this.ensureWSOLAta(
1089
- tx,
1090
- this.wallet.publicKey,
1091
- "buy",
1092
- maxQuoteIn,
1093
- )
1097
+ tx,
1098
+ this.publicKey,
1099
+ "buy",
1100
+ maxQuoteIn,
1101
+ )
1094
1102
  : await this.ensureAta(tx, quoteMint, quoteTokenProgramId);
1095
1103
 
1096
1104
  const buyIx = this.createAmmBuyInstruction(
@@ -1107,8 +1115,8 @@ export class PumpTrader {
1107
1115
  tx.add(
1108
1116
  createCloseAccountInstruction(
1109
1117
  userQuoteAta,
1110
- this.wallet.publicKey,
1111
- this.wallet.publicKey,
1118
+ this.publicKey,
1119
+ this.publicKey,
1112
1120
  ),
1113
1121
  );
1114
1122
  }
@@ -1116,8 +1124,8 @@ export class PumpTrader {
1116
1124
  const { blockhash, lastValidBlockHeight } =
1117
1125
  await this.connection.getLatestBlockhash("finalized");
1118
1126
  tx.recentBlockhash = blockhash;
1119
- tx.feePayer = this.wallet.publicKey;
1120
- tx.sign(this.wallet);
1127
+ tx.feePayer = this.publicKey;
1128
+ await this.signTx(tx);
1121
1129
 
1122
1130
  const signature = await this.connection.sendRawTransaction(
1123
1131
  tx.serialize(),
@@ -1158,11 +1166,11 @@ export class PumpTrader {
1158
1166
  totalSolOut <= tradeOpt.maxSolPerTx
1159
1167
  ? [totalTokenIn]
1160
1168
  : this.splitIntoN(
1161
- totalTokenIn,
1162
- Number(
1163
- (totalSolOut + tradeOpt.maxSolPerTx - 1n) / tradeOpt.maxSolPerTx,
1164
- ),
1165
- );
1169
+ totalTokenIn,
1170
+ Number(
1171
+ (totalSolOut + tradeOpt.maxSolPerTx - 1n) / tradeOpt.maxSolPerTx,
1172
+ ),
1173
+ );
1166
1174
 
1167
1175
  const pendingTransactions = [];
1168
1176
  const failedTransactions = [];
@@ -1190,7 +1198,7 @@ export class PumpTrader {
1190
1198
  tokenProgram.programId,
1191
1199
  );
1192
1200
  const userQuoteAta = isSolQuote
1193
- ? await this.ensureWSOLAta(tx, this.wallet.publicKey, "sell")
1201
+ ? await this.ensureWSOLAta(tx, this.publicKey, "sell")
1194
1202
  : await this.ensureAta(tx, quoteMint, quoteTokenProgramId);
1195
1203
 
1196
1204
  const sellIx = this.createAmmSellInstruction(
@@ -1207,8 +1215,8 @@ export class PumpTrader {
1207
1215
  tx.add(
1208
1216
  createCloseAccountInstruction(
1209
1217
  userQuoteAta,
1210
- this.wallet.publicKey,
1211
- this.wallet.publicKey,
1218
+ this.publicKey,
1219
+ this.publicKey,
1212
1220
  ),
1213
1221
  );
1214
1222
  }
@@ -1216,8 +1224,8 @@ export class PumpTrader {
1216
1224
  const { blockhash, lastValidBlockHeight } =
1217
1225
  await this.connection.getLatestBlockhash("finalized");
1218
1226
  tx.recentBlockhash = blockhash;
1219
- tx.feePayer = this.wallet.publicKey;
1220
- tx.sign(this.wallet);
1227
+ tx.feePayer = this.publicKey;
1228
+ await this.signTx(tx);
1221
1229
 
1222
1230
  const signature = await this.connection.sendRawTransaction(
1223
1231
  tx.serialize(),
@@ -1365,7 +1373,7 @@ export class PumpTrader {
1365
1373
  const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
1366
1374
  [
1367
1375
  Buffer.from("user_volume_accumulator"),
1368
- this.wallet.publicKey.toBuffer(),
1376
+ this.publicKey.toBuffer(),
1369
1377
  ],
1370
1378
  PROGRAM_IDS.PUMP_AMM,
1371
1379
  );
@@ -1421,7 +1429,7 @@ export class PumpTrader {
1421
1429
  programId: PROGRAM_IDS.PUMP_AMM,
1422
1430
  keys: [
1423
1431
  { pubkey: pool, isSigner: false, isWritable: true },
1424
- { pubkey: this.wallet.publicKey, isSigner: true, isWritable: true },
1432
+ { pubkey: this.publicKey, isSigner: true, isWritable: true },
1425
1433
  { pubkey: globalConfig.address, isSigner: false, isWritable: false },
1426
1434
  { pubkey: poolKeys.baseMint, isSigner: false, isWritable: false },
1427
1435
  { pubkey: poolKeys.quoteMint, isSigner: false, isWritable: false },
@@ -1528,7 +1536,7 @@ export class PumpTrader {
1528
1536
  const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
1529
1537
  [
1530
1538
  Buffer.from("user_volume_accumulator"),
1531
- this.wallet.publicKey.toBuffer(),
1539
+ this.publicKey.toBuffer(),
1532
1540
  ],
1533
1541
  PROGRAM_IDS.PUMP_AMM,
1534
1542
  );
@@ -1566,7 +1574,7 @@ export class PumpTrader {
1566
1574
  programId: PROGRAM_IDS.PUMP_AMM,
1567
1575
  keys: [
1568
1576
  { pubkey: pool, isSigner: false, isWritable: true },
1569
- { pubkey: this.wallet.publicKey, isSigner: true, isWritable: true },
1577
+ { pubkey: this.publicKey, isSigner: true, isWritable: true },
1570
1578
  { pubkey: globalConfig.address, isSigner: false, isWritable: false },
1571
1579
  { pubkey: poolKeys.baseMint, isSigner: false, isWritable: false },
1572
1580
  { pubkey: poolKeys.quoteMint, isSigner: false, isWritable: false },
@@ -1759,10 +1767,14 @@ export class PumpTrader {
1759
1767
  /* ---------- 辅助方法 ---------- */
1760
1768
 
1761
1769
  /**
1762
- * 获取钱包信息
1770
+ * 获取原始 wallet 对象(Keypair 或前端 WalletAdapter)
1763
1771
  */
1764
1772
  getWallet() {
1765
- return this.wallet;
1773
+ return this._wallet;
1774
+ }
1775
+
1776
+ getPublicKey() {
1777
+ return this.publicKey;
1766
1778
  }
1767
1779
 
1768
1780
  /**