pump-trader 1.0.0 → 1.0.2
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/README.md +24 -2
- package/dist/index.d.ts +177 -0
- package/dist/index.js +954 -0
- package/index.js +83 -4
- package/index.ts +83 -4
- package/package.json +5 -19
- package/tsconfig.json +8 -14
package/index.js
CHANGED
|
@@ -371,14 +371,93 @@ export class PumpTrader {
|
|
|
371
371
|
|
|
372
372
|
/* ---------- 余额查询 ---------- */
|
|
373
373
|
|
|
374
|
-
|
|
375
|
-
|
|
374
|
+
/**
|
|
375
|
+
* 查询代币余额
|
|
376
|
+
* @param {string} tokenAddr - 代币地址(可选),如果不传则返回所有代币
|
|
377
|
+
* @returns {Promise<number|Array>} 如果传入地址则返回该代币的余额数字,否则返回所有代币的详细信息
|
|
378
|
+
*/
|
|
379
|
+
async tokenBalance(tokenAddr = null) {
|
|
380
|
+
if (tokenAddr) {
|
|
381
|
+
// 查询单个代币
|
|
382
|
+
const mint = new PublicKey(tokenAddr);
|
|
383
|
+
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
384
|
+
this.wallet.publicKey,
|
|
385
|
+
{ mint }
|
|
386
|
+
);
|
|
387
|
+
return tokenAccounts.value[0]?.account.data.parsed.info.tokenAmount.uiAmount || 0;
|
|
388
|
+
} else {
|
|
389
|
+
// 查询所有代币
|
|
390
|
+
return this.getAllTokenBalances();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* 获取账户所有代币余额(仅显示余额 > 0 的)
|
|
396
|
+
* @returns {Promise<Array>} 代币信息数组,包含mint地址、余额等信息
|
|
397
|
+
*/
|
|
398
|
+
async getAllTokenBalances() {
|
|
376
399
|
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
377
400
|
this.wallet.publicKey,
|
|
378
|
-
{
|
|
401
|
+
{ programId: TOKEN_PROGRAM_ID }
|
|
379
402
|
);
|
|
380
403
|
|
|
381
|
-
|
|
404
|
+
const balances = tokenAccounts.value
|
|
405
|
+
.map((account) => {
|
|
406
|
+
const parsed = account.account.data.parsed;
|
|
407
|
+
if (parsed.type !== 'account') return null;
|
|
408
|
+
|
|
409
|
+
const tokenAmount = parsed.info.tokenAmount;
|
|
410
|
+
if (Number(tokenAmount.amount) === 0) return null;
|
|
411
|
+
|
|
412
|
+
return {
|
|
413
|
+
mint: parsed.info.mint,
|
|
414
|
+
amount: BigInt(tokenAmount.amount),
|
|
415
|
+
decimals: tokenAmount.decimals,
|
|
416
|
+
uiAmount: tokenAmount.uiAmount || 0
|
|
417
|
+
};
|
|
418
|
+
})
|
|
419
|
+
.filter((item) => item !== null);
|
|
420
|
+
|
|
421
|
+
// 同时查询 TOKEN_2022_PROGRAM_ID
|
|
422
|
+
const token2022Accounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
423
|
+
this.wallet.publicKey,
|
|
424
|
+
{ programId: TOKEN_2022_PROGRAM_ID }
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
const token2022Balances = token2022Accounts.value
|
|
428
|
+
.map((account) => {
|
|
429
|
+
const parsed = account.account.data.parsed;
|
|
430
|
+
if (parsed.type !== 'account') return null;
|
|
431
|
+
|
|
432
|
+
const tokenAmount = parsed.info.tokenAmount;
|
|
433
|
+
if (Number(tokenAmount.amount) === 0) return null;
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
mint: parsed.info.mint,
|
|
437
|
+
amount: BigInt(tokenAmount.amount),
|
|
438
|
+
decimals: tokenAmount.decimals,
|
|
439
|
+
uiAmount: tokenAmount.uiAmount || 0
|
|
440
|
+
};
|
|
441
|
+
})
|
|
442
|
+
.filter((item) => item !== null);
|
|
443
|
+
|
|
444
|
+
// 合并并去重
|
|
445
|
+
const allBalances = [...balances, ...token2022Balances];
|
|
446
|
+
const seen = new Set();
|
|
447
|
+
const uniqueBalances = allBalances
|
|
448
|
+
.filter((b) => {
|
|
449
|
+
if (seen.has(b.mint)) return false;
|
|
450
|
+
seen.add(b.mint);
|
|
451
|
+
return true;
|
|
452
|
+
})
|
|
453
|
+
.map((b) => ({
|
|
454
|
+
mint: b.mint,
|
|
455
|
+
amount: Number(b.amount),
|
|
456
|
+
decimals: b.decimals,
|
|
457
|
+
uiAmount: b.uiAmount
|
|
458
|
+
}));
|
|
459
|
+
|
|
460
|
+
return uniqueBalances;
|
|
382
461
|
}
|
|
383
462
|
|
|
384
463
|
async solBalance() {
|
package/index.ts
CHANGED
|
@@ -469,14 +469,93 @@ export class PumpTrader {
|
|
|
469
469
|
|
|
470
470
|
/* ---------- 余额查询 ---------- */
|
|
471
471
|
|
|
472
|
-
|
|
473
|
-
|
|
472
|
+
/**
|
|
473
|
+
* 查询代币余额
|
|
474
|
+
* @param tokenAddr - 代币地址(可选),如果不传则返回所有代币
|
|
475
|
+
* @returns 如果传入地址则返回该代币的余额数字,否则返回所有代币的详细信息
|
|
476
|
+
*/
|
|
477
|
+
async tokenBalance(tokenAddr?: string): Promise<number | Array<{ mint: string; amount: number; decimals: number; uiAmount: number }>> {
|
|
478
|
+
if (tokenAddr) {
|
|
479
|
+
// 查询单个代币
|
|
480
|
+
const mint = new PublicKey(tokenAddr);
|
|
481
|
+
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
482
|
+
this.wallet.publicKey,
|
|
483
|
+
{ mint }
|
|
484
|
+
);
|
|
485
|
+
return tokenAccounts.value[0]?.account.data.parsed.info.tokenAmount.uiAmount || 0;
|
|
486
|
+
} else {
|
|
487
|
+
// 查询所有代币
|
|
488
|
+
return this.getAllTokenBalances();
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* 获取账户所有代币余额(仅显示余额 > 0 的)
|
|
494
|
+
* @returns 代币信息数组,包含mint地址、余额等信息
|
|
495
|
+
*/
|
|
496
|
+
async getAllTokenBalances(): Promise<Array<{ mint: string; amount: number; decimals: number; uiAmount: number }>> {
|
|
474
497
|
const tokenAccounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
475
498
|
this.wallet.publicKey,
|
|
476
|
-
{
|
|
499
|
+
{ programId: TOKEN_PROGRAM_ID }
|
|
477
500
|
);
|
|
478
501
|
|
|
479
|
-
|
|
502
|
+
const balances = tokenAccounts.value
|
|
503
|
+
.map((account) => {
|
|
504
|
+
const parsed = account.account.data.parsed;
|
|
505
|
+
if (parsed.type !== 'account') return null;
|
|
506
|
+
|
|
507
|
+
const tokenAmount = parsed.info.tokenAmount;
|
|
508
|
+
if (Number(tokenAmount.amount) === 0) return null; // 跳过余额为0的
|
|
509
|
+
|
|
510
|
+
return {
|
|
511
|
+
mint: parsed.info.mint,
|
|
512
|
+
amount: BigInt(tokenAmount.amount),
|
|
513
|
+
decimals: tokenAmount.decimals,
|
|
514
|
+
uiAmount: tokenAmount.uiAmount || 0
|
|
515
|
+
};
|
|
516
|
+
})
|
|
517
|
+
.filter((item) => item !== null) as Array<{ mint: string; amount: bigint; decimals: number; uiAmount: number }>;
|
|
518
|
+
|
|
519
|
+
// 同时查询 TOKEN_2022_PROGRAM_ID
|
|
520
|
+
const token2022Accounts = await this.connection.getParsedTokenAccountsByOwner(
|
|
521
|
+
this.wallet.publicKey,
|
|
522
|
+
{ programId: TOKEN_2022_PROGRAM_ID }
|
|
523
|
+
);
|
|
524
|
+
|
|
525
|
+
const token2022Balances = token2022Accounts.value
|
|
526
|
+
.map((account) => {
|
|
527
|
+
const parsed = account.account.data.parsed;
|
|
528
|
+
if (parsed.type !== 'account') return null;
|
|
529
|
+
|
|
530
|
+
const tokenAmount = parsed.info.tokenAmount;
|
|
531
|
+
if (Number(tokenAmount.amount) === 0) return null; // 跳过余额为0的
|
|
532
|
+
|
|
533
|
+
return {
|
|
534
|
+
mint: parsed.info.mint,
|
|
535
|
+
amount: BigInt(tokenAmount.amount),
|
|
536
|
+
decimals: tokenAmount.decimals,
|
|
537
|
+
uiAmount: tokenAmount.uiAmount || 0
|
|
538
|
+
};
|
|
539
|
+
})
|
|
540
|
+
.filter((item) => item !== null) as Array<{ mint: string; amount: bigint; decimals: number; uiAmount: number }>;
|
|
541
|
+
|
|
542
|
+
// 合并并去重
|
|
543
|
+
const allBalances = [...balances, ...token2022Balances];
|
|
544
|
+
const seen = new Set<string>();
|
|
545
|
+
const uniqueBalances = allBalances
|
|
546
|
+
.filter((b) => {
|
|
547
|
+
if (seen.has(b.mint)) return false;
|
|
548
|
+
seen.add(b.mint);
|
|
549
|
+
return true;
|
|
550
|
+
})
|
|
551
|
+
.map((b) => ({
|
|
552
|
+
mint: b.mint,
|
|
553
|
+
amount: Number(b.amount),
|
|
554
|
+
decimals: b.decimals,
|
|
555
|
+
uiAmount: b.uiAmount
|
|
556
|
+
}));
|
|
557
|
+
|
|
558
|
+
return uniqueBalances;
|
|
480
559
|
}
|
|
481
560
|
|
|
482
561
|
async solBalance(): Promise<number> {
|
package/package.json
CHANGED
|
@@ -1,31 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pump-trader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "PumpFun 交易库 - 自动判断 Token Program 和内盘/外盘",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"types": "./index.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"import": "./index.js",
|
|
11
|
-
"require": "./index.js",
|
|
12
|
-
"types": "./index.ts"
|
|
13
|
-
}
|
|
14
|
-
},
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
15
7
|
"scripts": {
|
|
16
|
-
"build": "tsc
|
|
8
|
+
"build": "tsc",
|
|
17
9
|
"format": "prettier --write ."
|
|
18
10
|
},
|
|
19
11
|
"keywords": [
|
|
20
12
|
"solana",
|
|
21
|
-
"pump",
|
|
22
13
|
"pumpfun",
|
|
23
|
-
"trading"
|
|
24
|
-
"token",
|
|
25
|
-
"bonding-curve",
|
|
26
|
-
"amm",
|
|
27
|
-
"token-program",
|
|
28
|
-
"token-2022"
|
|
14
|
+
"trading"
|
|
29
15
|
],
|
|
30
16
|
"author": "Sylor",
|
|
31
17
|
"license": "MIT",
|
package/tsconfig.json
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "
|
|
5
|
-
"lib": ["ES2020"],
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"declarationMap": true,
|
|
8
|
-
"sourceMap": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"strict": true,
|
|
3
|
+
"target": "ES2021",
|
|
4
|
+
"module": "CommonJS",
|
|
11
5
|
"esModuleInterop": true,
|
|
12
|
-
"skipLibCheck": true,
|
|
13
6
|
"forceConsistentCasingInFileNames": true,
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
7
|
+
"strict": true,
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"resolveJsonModule": true
|
|
18
12
|
},
|
|
19
13
|
"include": [
|
|
20
14
|
"index.ts"
|
|
@@ -22,4 +16,4 @@
|
|
|
22
16
|
"exclude": [
|
|
23
17
|
"node_modules"
|
|
24
18
|
]
|
|
25
|
-
}
|
|
19
|
+
}
|