@tronscanteam/cli 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 CHANGED
@@ -6,33 +6,39 @@ A CLI tool built on [TronScan API](https://docs.tronscan.org/), providing 80+ su
6
6
 
7
7
  Two ways to use: **`ts` in the terminal** (after global install) and **natural language in AI IDEs** ([Claude Code](https://claude.ai/code), [Cursor](https://cursor.com), VS Code Copilot, etc.).
8
8
 
9
- ## Install (global `ts` command)
9
+ ## Install
10
+
11
+ ### 1. Install from npm
10
12
 
11
13
  ```bash
12
- git clone https://github.com/sshnii/tronscan-cli.git
13
- cd tronscan-cli
14
- npm install
15
- cp .env.example .env
16
- # Edit .env and set TRONSCAN_API_KEY
14
+ npm install -g @tronscanteam/cli
17
15
  ```
18
16
 
17
+ This registers the global command **`ts`** for use in any directory. Run `ts setup` to configure your API Key, and `ts help` to see all commands.
18
+
19
+ **API Key**:
20
+
19
21
  Get your API Key: https://docs.tronscan.org/zh/api/api-keys
20
22
 
21
- Register **`ts`** on your machine (use either one):
23
+ ```bash
24
+ ts setup YOUR_API_KEY # replace with your actual API Key
25
+ ```
26
+
27
+ ### 2. Install from source (clone)
22
28
 
23
29
  ```bash
30
+ git clone https://github.com/sshnii/tronscan-cli.git
31
+ cd tronscan-cli
32
+ npm install
24
33
  npm link
25
- # or
26
- npm install -g .
34
+ ts setup YOUR_API_KEY # replace with your actual API Key
27
35
  ```
28
36
 
29
- The CLI loads `.env` from the **cloned project root** (same folder as `package.json`). Keep your API Key there; you can run `ts` from any working directory.
30
-
31
37
  ## Usage
32
38
 
33
39
  ### 1. In the terminal
34
40
 
35
- After installing `ts` globally, run commands directly (`ts help` lists all subcommands):
41
+ After installing `ts` globally, run commands directly:
36
42
 
37
43
  ```bash
38
44
  ts help # list all subcommands
@@ -48,7 +54,7 @@ ts security-account TXxx... # account risk check
48
54
 
49
55
  ### 2. In AI IDEs
50
56
 
51
- Open this repo as your working directory and describe what you need in natural language. The AI reads the rule files, infers intent, picks the right API, and interprets the response.
57
+ To use with AI IDEs, we recommend installing from source (clone). Cloning downloads the full set of rule files (`CLAUDE.md`, `.cursorrules`, `.github/copilot-instructions.md`), which improve AI response accuracy. Run `ts setup` on first use to quickly configure your API Key.
52
58
 
53
59
  Built-in rule files:
54
60
 
@@ -94,14 +100,12 @@ No need to memorize commands or API paths — the AI matches the best endpoint f
94
100
  | Account | 11 | Balance, holdings, resources, approvals, votes, analysis |
95
101
  | Super Representative | 4 | SR list, votes, chain parameters, proposals |
96
102
  | Transaction | 10 | Transaction details, transfers (TRX/TRC20/TRC721/TRC1155) |
97
- | Statistics | 19 | TRX supply, transaction trends, active accounts, energy, DeFi TVL |
98
- | Contract | 10 | Contract details, callers, energy consumption, daily analysis |
103
+ | Statistics | 17 | TRX supply, transaction trends, active accounts, energy, DeFi TVL |
104
+ | Contract | 9 | Contract details, callers, energy consumption, daily analysis |
99
105
  | Security | 6 | Account/token/URL/transaction risk detection, authorization check |
100
106
  | Deep Analysis | 4 | Related accounts, fund flow, large transactions |
101
107
  | Stablecoin | 11 | Holder distribution, top holders, mint/burn, liquidity pools |
102
108
 
103
- Run `ts help` for the full command list.
104
-
105
109
  ## API Reference
106
110
 
107
111
  See [api-reference.md](api-reference.md) for the complete API endpoint list.
package/README_CN.md CHANGED
@@ -6,33 +6,39 @@
6
6
 
7
7
  支持两种使用方式:**终端全局命令 `ts`** 和 **在 AI IDE 中用自然语言查询**(支持 [Claude Code](https://claude.ai/code)、[Cursor](https://cursor.com)、VS Code Copilot 等)。
8
8
 
9
- ## 安装(全局命令 `ts`)
9
+ ## 安装
10
+
11
+ ### 一、通过 npm 全局安装
10
12
 
11
13
  ```bash
12
- git clone https://github.com/sshnii/tronscan-cli.git
13
- cd tronscan-cli
14
- npm install
15
- cp .env.example .env
16
- # 编辑 .env,设置 TRONSCAN_API_KEY
14
+ npm install -g @tronscanteam/cli
17
15
  ```
18
16
 
17
+ 安装后即可在任意目录使用全局命令 **`ts`**,输入 `ts setup` 设置 API Key,输入 `ts help` 查看全部命令。
18
+
19
+ **配置 API Key**:
20
+
19
21
  API Key 申请:https://docs.tronscan.org/zh/api/api-keys
20
22
 
21
- 在本机注册全局命令 **`ts`**(任选其一):
23
+ ```bash
24
+ ts setup YOUR_API_KEY # 替换为你的真实 API Key
25
+ ```
26
+
27
+ ### 二、从源码克隆安装
22
28
 
23
29
  ```bash
30
+ git clone https://github.com/sshnii/tronscan-cli.git
31
+ cd tronscan-cli
32
+ npm install
24
33
  npm link
25
- #
26
- npm install -g .
34
+ ts setup YOUR_API_KEY # 替换为你的真实 API Key
27
35
  ```
28
36
 
29
- CLI 会从**你克隆下来的项目根目录**(与 `package.json` 同级)读取 `.env`。请在该目录配置好 API Key;之后在任意工作目录都可以直接执行 `ts`。
30
-
31
37
  ## 使用方式
32
38
 
33
39
  ### 一、在终端中使用
34
40
 
35
- 全局安装 `ts` 后,在终端直接输入命令(`ts help` 可查看全部子命令):
41
+ 全局安装 `ts` 后,在终端直接输入命令:
36
42
 
37
43
  ```bash
38
44
  ts help # 查看所有子命令
@@ -48,9 +54,7 @@ ts security-account TXxx... # 账户风险检测
48
54
 
49
55
  ### 二、在 AI IDE 中使用
50
56
 
51
- 将本项目作为工作目录打开,用自然语言描述需求即可;AI 会读取规则文件,识别意图、选择接口并解读返回结果。
52
-
53
- 已内置规则文件:
57
+ 若要在 AI IDE 中使用,建议优先通过从源码克隆的方式安装。克隆会下载完整的规则文件(`CLAUDE.md`、`.cursorrules`、`.github/copilot-instructions.md`),提升 AI 回复的准确性。首次使用运行 `ts setup` 即可快速完成 API Key 配置。
54
58
 
55
59
  | AI IDE | 规则文件 |
56
60
  |--------|----------|
@@ -94,14 +98,12 @@ AI:自动执行 ts tps 并返回结果
94
98
  | 账户 | 11 | 余额、持仓、资源、授权、投票、分析 |
95
99
  | 超级代表 | 4 | SR 列表、投票、链参数、提案 |
96
100
  | 交易 | 10 | 交易详情、转账记录(TRX/TRC20/TRC721/TRC1155) |
97
- | 统计 | 19 | TRX 供应、交易趋势、活跃账户、能量、DeFi TVL |
98
- | 合约 | 10 | 合约详情、调用者、能量消耗、日度分析 |
101
+ | 统计 | 17 | TRX 供应、交易趋势、活跃账户、能量、DeFi TVL |
102
+ | 合约 | 9 | 合约详情、调用者、能量消耗、日度分析 |
99
103
  | 安全 | 6 | 账户/代币/URL/交易风险检测、授权检查 |
100
104
  | 深度分析 | 4 | 关联账户、资金流向、大额交易 |
101
105
  | 稳定币 | 11 | 持仓分布、大户、增发/销毁、流动性池 |
102
106
 
103
- 输入 `ts help` 查看完整命令列表。
104
-
105
107
  ## API 参考
106
108
 
107
109
  完整 API 接口列表见 [api-reference.md](api-reference.md)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tronscanteam/cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "TronScan API CLI tool - 80+ commands for TRON blockchain data",
5
5
  "type": "module",
6
6
  "repository": {
package/src/api.js CHANGED
@@ -7,9 +7,9 @@ import { dirname } from 'path';
7
7
 
8
8
  const __filename = fileURLToPath(import.meta.url);
9
9
  const __dirname = dirname(__filename);
10
- const PROJECT_ROOT = resolve(__dirname, '..');
10
+ export const PROJECT_ROOT = resolve(__dirname, '..');
11
11
 
12
- export const VERSION = '1.0.0';
12
+ export const VERSION = '1.0.2';
13
13
  const BASE_URL = 'https://apilist.tronscanapi.com';
14
14
 
15
15
  let apiKey = process.env.TRONSCAN_API_KEY || '';
@@ -29,7 +29,8 @@ export function loadConfig() {
29
29
  }
30
30
  if (!apiKey) {
31
31
  err('未找到 API Key');
32
- warn('请设置 TRONSCAN_API_KEY 环境变量或创建 .env 文件');
32
+ warn('API Key 申请: https://tronscan.org/#/developer/api');
33
+ warn('运行 ts setup YOUR_API_KEY 快速配置');
33
34
  process.exit(2);
34
35
  }
35
36
  }
@@ -82,7 +83,12 @@ export async function get(path) {
82
83
  if (body) console.error(body);
83
84
  process.exit(4);
84
85
  }
85
- return res.json();
86
+ const text = await res.text();
87
+ try {
88
+ return JSON.parse(text);
89
+ } catch {
90
+ throw new Error(`API 返回非 JSON: ${text.slice(0, 200)}`);
91
+ }
86
92
  }
87
93
 
88
94
  export async function post(path, body) {
@@ -108,7 +114,12 @@ export async function post(path, body) {
108
114
  if (text) console.error(text);
109
115
  process.exit(4);
110
116
  }
111
- return res.json();
117
+ const text = await res.text();
118
+ try {
119
+ return JSON.parse(text);
120
+ } catch {
121
+ throw new Error(`API 返回非 JSON: ${text.slice(0, 200)}`);
122
+ }
112
123
  }
113
124
 
114
125
  // ============ Output ============
package/src/commands.js CHANGED
@@ -54,7 +54,7 @@ export const commands = {
54
54
  },
55
55
  'account-votes': {
56
56
  usage: 'ts account-votes <address>', desc: '投票记录',
57
- run: (a) => get(`/api/vote?address=${require(a, 0, 'address', 'ts account-votes <address>')}`)
57
+ run: (a) => get(`/api/vote?voter=${require(a, 0, 'address', 'ts account-votes <address>')}`)
58
58
  },
59
59
  'account-analysis': {
60
60
  usage: 'ts account-analysis <address>', desc: '日度分析',
@@ -360,8 +360,12 @@ export const commands = {
360
360
  run: () => get('/api/tokenTvc')
361
361
  },
362
362
  'token-analysis': {
363
- usage: 'ts token-analysis', desc: '代币交易分析',
364
- run: () => get('/api/token/analysis')
363
+ usage: 'ts token-analysis <contract|symbol>', desc: '代币交易分析',
364
+ run: async (a) => {
365
+ require(a, 0, 'contract|symbol', 'ts token-analysis <contract|symbol>');
366
+ const contract = await resolveAndLog(a[0], resolveContract);
367
+ return get(`/api/token/analysis?token=${contract}`);
368
+ }
365
369
  },
366
370
  'token-transfer-analysis': {
367
371
  usage: 'ts token-transfer-analysis', desc: '代币转账分析',
@@ -383,8 +387,12 @@ export const commands = {
383
387
  run: (a) => get(`/api/deep/account/token/bigAmount?address=${require(a, 0, 'address', 'ts deep-big-tx <address>')}`)
384
388
  },
385
389
  'deep-token-transfer': {
386
- usage: 'ts deep-token-transfer <address>', desc: '代币转账次数',
387
- run: (a) => get(`/api/deep/account/holderToken/basicInfo/trc20/transfer?address=${require(a, 0, 'address', 'ts deep-token-transfer <address>')}`)
390
+ usage: 'ts deep-token-transfer <address> <contract>', desc: '代币转账次数',
391
+ run: (a) => {
392
+ require(a, 0, 'address', 'ts deep-token-transfer <address> <contract>');
393
+ require(a, 1, 'contract', 'ts deep-token-transfer <address> <contract>');
394
+ return get(`/api/deep/account/holderToken/basicInfo/trc20/transfer?accountAddress=${a[0]}&tokenAddress=${a[1]}`);
395
+ }
388
396
  },
389
397
 
390
398
  // --- 稳定币 ---
@@ -402,32 +410,32 @@ export const commands = {
402
410
  run: () => get('/api/stableCoin/holder/top')
403
411
  },
404
412
  'stable-big-tx': {
405
- usage: 'ts stable-big-tx', desc: '大额交易',
406
- run: () => get('/api/deep/stableCoin/bigAmount')
413
+ usage: 'ts stable-big-tx [types]', desc: '大额交易 (types: 1=USDT 2=USDJ 3=TUSD 4=USDC)',
414
+ run: (a, o) => get(`/api/deep/stableCoin/bigAmount?types=${a[0] || '1'}&start=${o.start}&limit=${o.limit}`)
407
415
  },
408
416
  'stable-events': {
409
- usage: 'ts stable-events', desc: '增发/销毁/黑名单事件',
410
- run: () => get('/api/deep/stableCoin/totalSupply/keyEvents')
417
+ usage: 'ts stable-events [--sort 0|1] [--start N] [--limit N]', desc: '增发/销毁/黑名单事件 (sort: 0=asc 1=desc)',
418
+ run: (a, o) => get(`/api/deep/stableCoin/totalSupply/keyEvents?direction=1&sort=${o.sort || '1'}&start=${o.start}&limit=${o.limit}`)
411
419
  },
412
420
  'stable-dist': {
413
- usage: 'ts stable-dist', desc: '交易所/DeFi 分布',
414
- run: () => get('/api/stableCoin/distribution')
421
+ usage: 'ts stable-dist <contract>', desc: '交易所/DeFi 分布',
422
+ run: (a) => get(`/api/stableCoin/distribution?token=${require(a, 0, 'contract', 'ts stable-dist <contract> (如 USDT: TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t)')}`)
415
423
  },
416
424
  'stable-liquidity': {
417
425
  usage: 'ts stable-liquidity', desc: '流动性操作记录',
418
426
  run: () => get('/api/deep/stableCoin/liquidity/transaction')
419
427
  },
420
428
  'stable-pool': {
421
- usage: 'ts stable-pool', desc: '池子概览(TVL)',
422
- run: () => get('/api/stableCoin/pool/overview')
429
+ usage: 'ts stable-pool <pool_address>', desc: '池子概览(TVL)',
430
+ run: (a) => get(`/api/stableCoin/pool/overview?pool=${require(a, 0, 'pool_address', 'ts stable-pool <pool_address>')}`)
423
431
  },
424
432
  'stable-pool-trend': {
425
- usage: 'ts stable-pool-trend', desc: '池子趋势',
426
- run: () => get('/api/stableCoin/pool/trend')
433
+ usage: 'ts stable-pool-trend <pool_address>', desc: '池子趋势',
434
+ run: (a) => get(`/api/stableCoin/pool/trend?pool=${require(a, 0, 'pool_address', 'ts stable-pool-trend <pool_address>')}`)
427
435
  },
428
436
  'stable-pool-change': {
429
- usage: 'ts stable-pool-change', desc: '池子历史变化',
430
- run: () => get('/api/stableCoin/pool/change')
437
+ usage: 'ts stable-pool-change <pool_address>', desc: '池子历史变化',
438
+ run: (a) => get(`/api/stableCoin/pool/change?pool=${require(a, 0, 'pool_address', 'ts stable-pool-change <pool_address>')}`)
431
439
  },
432
440
  'stable-tvl': {
433
441
  usage: 'ts stable-tvl', desc: '稳定币 TVL 分布',
package/src/help.js CHANGED
@@ -17,6 +17,9 @@ ${B('全局选项:')}
17
17
  --help, -h 显示命令帮助
18
18
  --version, -V 显示版本号
19
19
 
20
+ 配置:
21
+ ts setup [api-key] 配置 API Key(首次使用必须)
22
+
20
23
  代币:
21
24
  ts token <contract|symbol> TRC20 代币详情(支持 USDT 等符号)
22
25
  ts token-trc10 <id|symbol> TRC10 代币详情
@@ -85,7 +88,7 @@ ${B('全局选项:')}
85
88
  ts bandwidth-daily 每日带宽消耗
86
89
  ts trigger-stats 合约调用分布
87
90
  ts token-tvc 代币链上价值(TVC)
88
- ts token-analysis 代币交易分析
91
+ ts token-analysis <contract|symbol> 代币交易分析
89
92
  ts token-transfer-analysis 代币转账分析
90
93
 
91
94
  合约:
@@ -112,19 +115,19 @@ ${B('全局选项:')}
112
115
  ts deep-related <addr> 关联账户
113
116
  ts deep-flow <addr> 资金流向
114
117
  ts deep-big-tx <addr> 大额交易
115
- ts deep-token-transfer <addr> 代币转账次数
118
+ ts deep-token-transfer <addr> <contract> 代币转账次数
116
119
 
117
120
  稳定币:
118
121
  ts stable-holders 持仓分布概览
119
122
  ts stable-change 持有者变化趋势
120
123
  ts stable-top 大户排行
121
- ts stable-big-tx 大额交易
124
+ ts stable-big-tx [types] 大额交易 (1=USDT 2=USDJ 3=TUSD 4=USDC)
122
125
  ts stable-events 增发/销毁/黑名单事件
123
- ts stable-dist 交易所/DeFi 分布
126
+ ts stable-dist <contract> 交易所/DeFi 分布
124
127
  ts stable-liquidity 流动性操作记录
125
- ts stable-pool 池子概览(TVL)
126
- ts stable-pool-trend 池子趋势
127
- ts stable-pool-change 池子历史变化
128
+ ts stable-pool <pool_address> 池子概览(TVL)
129
+ ts stable-pool-trend <pool_address> 池子趋势
130
+ ts stable-pool-change <pool_address> 池子历史变化
128
131
  ts stable-tvl 稳定币 TVL 分布
129
132
  `);
130
133
  }
package/src/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { loadConfig, output, err, warn, VERSION } from './api.js';
3
+ import { resolve } from 'path';
4
+ import { existsSync, readFileSync, writeFileSync, copyFileSync } from 'fs';
5
+ import { loadConfig, output, err, warn, VERSION, PROJECT_ROOT } from './api.js';
4
6
  import { commands } from './commands.js';
5
7
  import { printHelp, printCommandHelp } from './help.js';
6
8
 
@@ -44,6 +46,59 @@ while (i < rest.length) {
44
46
  }
45
47
  }
46
48
 
49
+ // ============ Setup (before loadConfig) ============
50
+
51
+ if (cmd === 'setup') {
52
+ const envPath = resolve(PROJECT_ROOT, '.env');
53
+ const examplePath = resolve(PROJECT_ROOT, '.env.example');
54
+ const isTTY = process.stderr.isTTY ?? false;
55
+ const green = (s) => (isTTY ? `\x1b[32m${s}\x1b[0m` : s);
56
+ const bold = (s) => (isTTY ? `\x1b[1m${s}\x1b[0m` : s);
57
+ const dim = (s) => (isTTY ? `\x1b[2m${s}\x1b[0m` : s);
58
+
59
+ const inputKey = positional[0];
60
+
61
+ if (inputKey) {
62
+ // 校验:过滤明显无效的输入
63
+ if (/[<>]/.test(inputKey) || inputKey === 'your-api-key' || inputKey === 'your_api_key_here') {
64
+ err('请替换为你的真实 API Key');
65
+ warn('API Key 申请: https://tronscan.org/#/developer/api');
66
+ process.exit(2);
67
+ }
68
+ // 直接写入 API Key
69
+ writeFileSync(envPath, `TRONSCAN_API_KEY=${inputKey}\n`);
70
+ console.log(green('✓ API Key 已写入 .env'));
71
+ console.log(dim(` 路径: ${envPath}`));
72
+ console.log(`\n 现在可以使用 ${bold('ts')} 命令了,试试: ${bold('ts tps')}`);
73
+ process.exit(0);
74
+ }
75
+
76
+ // 检查现有配置
77
+ if (existsSync(envPath)) {
78
+ const content = readFileSync(envPath, 'utf-8');
79
+ const match = content.match(/^\s*TRONSCAN_API_KEY\s*=\s*(.+?)\s*$/m);
80
+ const key = match?.[1]?.replace(/^["']|["']$/g, '');
81
+ if (key && key !== 'your_api_key_here') {
82
+ console.log(green('✓ API Key 已配置'));
83
+ console.log(dim(` 路径: ${envPath}`));
84
+ console.log(dim(` Key: ${key.slice(0, 8)}...${key.slice(-4)}`));
85
+ process.exit(0);
86
+ }
87
+ }
88
+
89
+ // 未配置,引导用户
90
+ if (!existsSync(envPath) && existsSync(examplePath)) {
91
+ copyFileSync(examplePath, envPath);
92
+ }
93
+
94
+ console.log(bold('TronScan CLI 配置向导\n'));
95
+ console.log(` 1. 申请 API Key: ${bold('https://tronscan.org/#/developer/api')}`);
96
+ console.log(` 2. 运行: ${bold('ts setup YOUR_API_KEY')}`);
97
+ console.log(` 或手动编辑: ${dim(envPath)}`);
98
+ console.log(`\n 也可设置环境变量: ${bold('export TRONSCAN_API_KEY=YOUR_API_KEY')}`);
99
+ process.exit(0);
100
+ }
101
+
47
102
  // ============ Dispatch ============
48
103
 
49
104
  const command = commands[cmd];