pump-trader 1.1.8 → 1.2.1
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/dist/index.d.ts +56 -9
- package/dist/index.js +747 -138
- package/index.js +64 -50
- package/index.ts +118 -96
- package/package.json +1 -1
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,26 @@ function parsePoolKeys(data) {
|
|
|
179
178
|
/* ================= PumpTrader 类 ================= */
|
|
180
179
|
|
|
181
180
|
export class PumpTrader {
|
|
182
|
-
constructor(rpc,
|
|
181
|
+
constructor(rpc, wallet) {
|
|
183
182
|
this.connection = new Connection(rpc, "confirmed");
|
|
184
|
-
this.
|
|
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();
|
|
190
|
+
this.tokenProgramCache = new Map();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async signTx(tx) {
|
|
194
|
+
if (this._wallet instanceof Keypair) {
|
|
195
|
+
tx.sign(this._wallet);
|
|
196
|
+
} else {
|
|
197
|
+
const signed = await this._wallet.signTransaction(tx);
|
|
198
|
+
// Copy signatures back to the original tx (adapter returns a new tx)
|
|
199
|
+
tx.signatures = signed.signatures;
|
|
200
|
+
}
|
|
191
201
|
}
|
|
192
202
|
|
|
193
203
|
/* ---------- Token Program 检测 ---------- */
|
|
@@ -539,7 +549,7 @@ export class PumpTrader {
|
|
|
539
549
|
// 查询单个代币
|
|
540
550
|
const mint = new PublicKey(tokenAddr);
|
|
541
551
|
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
542
|
-
this.
|
|
552
|
+
this.publicKey,
|
|
543
553
|
{ mint },
|
|
544
554
|
);
|
|
545
555
|
return (
|
|
@@ -558,7 +568,7 @@ export class PumpTrader {
|
|
|
558
568
|
*/
|
|
559
569
|
async getAllTokenBalances() {
|
|
560
570
|
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
561
|
-
this.
|
|
571
|
+
this.publicKey,
|
|
562
572
|
{ programId: TOKEN_PROGRAM_ID },
|
|
563
573
|
);
|
|
564
574
|
|
|
@@ -582,7 +592,7 @@ export class PumpTrader {
|
|
|
582
592
|
// 同时查询 TOKEN_2022_PROGRAM_ID
|
|
583
593
|
const token2022Accounts =
|
|
584
594
|
await this.connection.getParsedTokenAccountsByOwner(
|
|
585
|
-
this.
|
|
595
|
+
this.publicKey,
|
|
586
596
|
{ programId: TOKEN_2022_PROGRAM_ID },
|
|
587
597
|
);
|
|
588
598
|
|
|
@@ -623,7 +633,7 @@ export class PumpTrader {
|
|
|
623
633
|
}
|
|
624
634
|
|
|
625
635
|
async solBalance() {
|
|
626
|
-
const balance = await this.connection.getBalance(this.
|
|
636
|
+
const balance = await this.connection.getBalance(this.publicKey);
|
|
627
637
|
return balance / 1e9;
|
|
628
638
|
}
|
|
629
639
|
|
|
@@ -633,7 +643,7 @@ export class PumpTrader {
|
|
|
633
643
|
const program = tokenProgram || TOKEN_2022_PROGRAM_ID;
|
|
634
644
|
const ata = getAssociatedTokenAddressSync(
|
|
635
645
|
mint,
|
|
636
|
-
this.
|
|
646
|
+
this.publicKey,
|
|
637
647
|
false,
|
|
638
648
|
program,
|
|
639
649
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
@@ -643,9 +653,9 @@ export class PumpTrader {
|
|
|
643
653
|
if (!acc) {
|
|
644
654
|
tx.add(
|
|
645
655
|
createAssociatedTokenAccountInstruction(
|
|
646
|
-
this.
|
|
656
|
+
this.publicKey,
|
|
647
657
|
ata,
|
|
648
|
-
this.
|
|
658
|
+
this.publicKey,
|
|
649
659
|
mint,
|
|
650
660
|
program,
|
|
651
661
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
@@ -827,7 +837,7 @@ export class PumpTrader {
|
|
|
827
837
|
const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
|
|
828
838
|
[
|
|
829
839
|
Buffer.from("user_volume_accumulator"),
|
|
830
|
-
this.
|
|
840
|
+
this.publicKey.toBuffer(),
|
|
831
841
|
],
|
|
832
842
|
PROGRAM_IDS.PUMP,
|
|
833
843
|
);
|
|
@@ -867,7 +877,7 @@ export class PumpTrader {
|
|
|
867
877
|
bonding,
|
|
868
878
|
associatedBondingCurve,
|
|
869
879
|
userAta,
|
|
870
|
-
wallet: this.
|
|
880
|
+
wallet: this.publicKey,
|
|
871
881
|
creatorVault,
|
|
872
882
|
eventAuthority: PROGRAM_IDS.EVENT_AUTHORITY,
|
|
873
883
|
pumpProgram: PROGRAM_IDS.PUMP,
|
|
@@ -890,8 +900,8 @@ export class PumpTrader {
|
|
|
890
900
|
const { blockhash, lastValidBlockHeight } =
|
|
891
901
|
await this.connection.getLatestBlockhash("finalized");
|
|
892
902
|
tx.recentBlockhash = blockhash;
|
|
893
|
-
tx.feePayer = this.
|
|
894
|
-
|
|
903
|
+
tx.feePayer = this.publicKey;
|
|
904
|
+
await this.signTx(tx);
|
|
895
905
|
|
|
896
906
|
const signature = await this.connection.sendRawTransaction(
|
|
897
907
|
tx.serialize(),
|
|
@@ -932,11 +942,11 @@ export class PumpTrader {
|
|
|
932
942
|
totalSolOut <= tradeOpt.maxSolPerTx
|
|
933
943
|
? [totalTokenIn]
|
|
934
944
|
: this.splitIntoN(
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
945
|
+
totalTokenIn,
|
|
946
|
+
Number(
|
|
947
|
+
(totalSolOut + tradeOpt.maxSolPerTx - 1n) / tradeOpt.maxSolPerTx,
|
|
948
|
+
),
|
|
949
|
+
);
|
|
940
950
|
|
|
941
951
|
const pendingTransactions = []; // 待确认的交易
|
|
942
952
|
const failedTransactions = []; // 发送失败的交易
|
|
@@ -951,7 +961,7 @@ export class PumpTrader {
|
|
|
951
961
|
|
|
952
962
|
const userAta = getAssociatedTokenAddressSync(
|
|
953
963
|
mint,
|
|
954
|
-
this.
|
|
964
|
+
this.publicKey,
|
|
955
965
|
false,
|
|
956
966
|
tokenProgram.programId,
|
|
957
967
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
@@ -969,7 +979,7 @@ export class PumpTrader {
|
|
|
969
979
|
const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
|
|
970
980
|
[
|
|
971
981
|
Buffer.from("user_volume_accumulator"),
|
|
972
|
-
this.
|
|
982
|
+
this.publicKey.toBuffer(),
|
|
973
983
|
],
|
|
974
984
|
PROGRAM_IDS.PUMP,
|
|
975
985
|
);
|
|
@@ -1002,7 +1012,7 @@ export class PumpTrader {
|
|
|
1002
1012
|
bonding,
|
|
1003
1013
|
associatedBondingCurve,
|
|
1004
1014
|
userAta,
|
|
1005
|
-
wallet: this.
|
|
1015
|
+
wallet: this.publicKey,
|
|
1006
1016
|
creatorVault,
|
|
1007
1017
|
eventAuthority: PROGRAM_IDS.EVENT_AUTHORITY,
|
|
1008
1018
|
pumpProgram: PROGRAM_IDS.PUMP,
|
|
@@ -1025,8 +1035,8 @@ export class PumpTrader {
|
|
|
1025
1035
|
const { blockhash, lastValidBlockHeight } =
|
|
1026
1036
|
await this.connection.getLatestBlockhash("finalized");
|
|
1027
1037
|
tx.recentBlockhash = blockhash;
|
|
1028
|
-
tx.feePayer = this.
|
|
1029
|
-
|
|
1038
|
+
tx.feePayer = this.publicKey;
|
|
1039
|
+
await this.signTx(tx);
|
|
1030
1040
|
|
|
1031
1041
|
const signature = await this.connection.sendRawTransaction(
|
|
1032
1042
|
tx.serialize(),
|
|
@@ -1086,11 +1096,11 @@ export class PumpTrader {
|
|
|
1086
1096
|
);
|
|
1087
1097
|
const userQuoteAta = isSolQuote
|
|
1088
1098
|
? await this.ensureWSOLAta(
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1099
|
+
tx,
|
|
1100
|
+
this.publicKey,
|
|
1101
|
+
"buy",
|
|
1102
|
+
maxQuoteIn,
|
|
1103
|
+
)
|
|
1094
1104
|
: await this.ensureAta(tx, quoteMint, quoteTokenProgramId);
|
|
1095
1105
|
|
|
1096
1106
|
const buyIx = this.createAmmBuyInstruction(
|
|
@@ -1107,8 +1117,8 @@ export class PumpTrader {
|
|
|
1107
1117
|
tx.add(
|
|
1108
1118
|
createCloseAccountInstruction(
|
|
1109
1119
|
userQuoteAta,
|
|
1110
|
-
this.
|
|
1111
|
-
this.
|
|
1120
|
+
this.publicKey,
|
|
1121
|
+
this.publicKey,
|
|
1112
1122
|
),
|
|
1113
1123
|
);
|
|
1114
1124
|
}
|
|
@@ -1116,8 +1126,8 @@ export class PumpTrader {
|
|
|
1116
1126
|
const { blockhash, lastValidBlockHeight } =
|
|
1117
1127
|
await this.connection.getLatestBlockhash("finalized");
|
|
1118
1128
|
tx.recentBlockhash = blockhash;
|
|
1119
|
-
tx.feePayer = this.
|
|
1120
|
-
|
|
1129
|
+
tx.feePayer = this.publicKey;
|
|
1130
|
+
await this.signTx(tx);
|
|
1121
1131
|
|
|
1122
1132
|
const signature = await this.connection.sendRawTransaction(
|
|
1123
1133
|
tx.serialize(),
|
|
@@ -1158,11 +1168,11 @@ export class PumpTrader {
|
|
|
1158
1168
|
totalSolOut <= tradeOpt.maxSolPerTx
|
|
1159
1169
|
? [totalTokenIn]
|
|
1160
1170
|
: this.splitIntoN(
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1171
|
+
totalTokenIn,
|
|
1172
|
+
Number(
|
|
1173
|
+
(totalSolOut + tradeOpt.maxSolPerTx - 1n) / tradeOpt.maxSolPerTx,
|
|
1174
|
+
),
|
|
1175
|
+
);
|
|
1166
1176
|
|
|
1167
1177
|
const pendingTransactions = [];
|
|
1168
1178
|
const failedTransactions = [];
|
|
@@ -1190,7 +1200,7 @@ export class PumpTrader {
|
|
|
1190
1200
|
tokenProgram.programId,
|
|
1191
1201
|
);
|
|
1192
1202
|
const userQuoteAta = isSolQuote
|
|
1193
|
-
? await this.ensureWSOLAta(tx, this.
|
|
1203
|
+
? await this.ensureWSOLAta(tx, this.publicKey, "sell")
|
|
1194
1204
|
: await this.ensureAta(tx, quoteMint, quoteTokenProgramId);
|
|
1195
1205
|
|
|
1196
1206
|
const sellIx = this.createAmmSellInstruction(
|
|
@@ -1207,8 +1217,8 @@ export class PumpTrader {
|
|
|
1207
1217
|
tx.add(
|
|
1208
1218
|
createCloseAccountInstruction(
|
|
1209
1219
|
userQuoteAta,
|
|
1210
|
-
this.
|
|
1211
|
-
this.
|
|
1220
|
+
this.publicKey,
|
|
1221
|
+
this.publicKey,
|
|
1212
1222
|
),
|
|
1213
1223
|
);
|
|
1214
1224
|
}
|
|
@@ -1216,8 +1226,8 @@ export class PumpTrader {
|
|
|
1216
1226
|
const { blockhash, lastValidBlockHeight } =
|
|
1217
1227
|
await this.connection.getLatestBlockhash("finalized");
|
|
1218
1228
|
tx.recentBlockhash = blockhash;
|
|
1219
|
-
tx.feePayer = this.
|
|
1220
|
-
|
|
1229
|
+
tx.feePayer = this.publicKey;
|
|
1230
|
+
await this.signTx(tx);
|
|
1221
1231
|
|
|
1222
1232
|
const signature = await this.connection.sendRawTransaction(
|
|
1223
1233
|
tx.serialize(),
|
|
@@ -1365,7 +1375,7 @@ export class PumpTrader {
|
|
|
1365
1375
|
const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
|
|
1366
1376
|
[
|
|
1367
1377
|
Buffer.from("user_volume_accumulator"),
|
|
1368
|
-
this.
|
|
1378
|
+
this.publicKey.toBuffer(),
|
|
1369
1379
|
],
|
|
1370
1380
|
PROGRAM_IDS.PUMP_AMM,
|
|
1371
1381
|
);
|
|
@@ -1421,7 +1431,7 @@ export class PumpTrader {
|
|
|
1421
1431
|
programId: PROGRAM_IDS.PUMP_AMM,
|
|
1422
1432
|
keys: [
|
|
1423
1433
|
{ pubkey: pool, isSigner: false, isWritable: true },
|
|
1424
|
-
{ pubkey: this.
|
|
1434
|
+
{ pubkey: this.publicKey, isSigner: true, isWritable: true },
|
|
1425
1435
|
{ pubkey: globalConfig.address, isSigner: false, isWritable: false },
|
|
1426
1436
|
{ pubkey: poolKeys.baseMint, isSigner: false, isWritable: false },
|
|
1427
1437
|
{ pubkey: poolKeys.quoteMint, isSigner: false, isWritable: false },
|
|
@@ -1528,7 +1538,7 @@ export class PumpTrader {
|
|
|
1528
1538
|
const [userVolumeAccumulator] = PublicKey.findProgramAddressSync(
|
|
1529
1539
|
[
|
|
1530
1540
|
Buffer.from("user_volume_accumulator"),
|
|
1531
|
-
this.
|
|
1541
|
+
this.publicKey.toBuffer(),
|
|
1532
1542
|
],
|
|
1533
1543
|
PROGRAM_IDS.PUMP_AMM,
|
|
1534
1544
|
);
|
|
@@ -1566,7 +1576,7 @@ export class PumpTrader {
|
|
|
1566
1576
|
programId: PROGRAM_IDS.PUMP_AMM,
|
|
1567
1577
|
keys: [
|
|
1568
1578
|
{ pubkey: pool, isSigner: false, isWritable: true },
|
|
1569
|
-
{ pubkey: this.
|
|
1579
|
+
{ pubkey: this.publicKey, isSigner: true, isWritable: true },
|
|
1570
1580
|
{ pubkey: globalConfig.address, isSigner: false, isWritable: false },
|
|
1571
1581
|
{ pubkey: poolKeys.baseMint, isSigner: false, isWritable: false },
|
|
1572
1582
|
{ pubkey: poolKeys.quoteMint, isSigner: false, isWritable: false },
|
|
@@ -1759,10 +1769,14 @@ export class PumpTrader {
|
|
|
1759
1769
|
/* ---------- 辅助方法 ---------- */
|
|
1760
1770
|
|
|
1761
1771
|
/**
|
|
1762
|
-
*
|
|
1772
|
+
* 获取原始 wallet 对象(Keypair 或前端 WalletAdapter)
|
|
1763
1773
|
*/
|
|
1764
1774
|
getWallet() {
|
|
1765
|
-
return this.
|
|
1775
|
+
return this._wallet;
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
getPublicKey() {
|
|
1779
|
+
return this.publicKey;
|
|
1766
1780
|
}
|
|
1767
1781
|
|
|
1768
1782
|
/**
|