-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.ts
More file actions
186 lines (166 loc) · 7.98 KB
/
cli.ts
File metadata and controls
186 lines (166 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
import { Command } from 'commander';
import { startNode } from './cmd/node';
import { accounts } from './cmd/accounts';
import { clean } from './cmd/clean';
import { setUTF8EncodingForWindows } from './util/encoding';
import { DepositOptions, deposit } from './cmd/deposit';
import { DeployOptions, deploy } from './cmd/deploy';
import { TransferOptions, transfer } from './cmd/transfer';
import { BalanceOption, balanceOf } from './cmd/balance';
import { createScriptProject, CreateScriptProjectOptions } from './cmd/create';
import { Config, ConfigItem } from './cmd/config';
import { devnetConfig } from './cmd/devnet-config';
import { debugSingleScript, debugTransaction, parseSingleScriptOption } from './cmd/debug';
import { printSystemScripts } from './cmd/system-scripts';
import { transferAll } from './cmd/transfer-all';
import { genSystemScriptsJsonFile } from './scripts/gen';
import { CKBDebugger } from './tools/ckb-debugger';
import { logger } from './util/logger';
import { Network } from './type/base';
const version = require('../package.json').version;
const description = require('../package.json').description;
// fix windows terminal encoding of simplified chinese text
setUTF8EncodingForWindows();
const program = new Command();
program.name('offckb').description(description).version(version).enablePositionalOptions();
program
.command('node [CKB-Version]')
.description('Use the CKB to start devnet')
.option('--network <network>', 'Specify the network to deploy to', 'devnet')
.option(
'-b, --binary-path <binaryPath>',
'Specify the CKB binary path to use, only for devnet, when set, will ignore version and network',
)
.action(async (version: string, options: { network: Network; binaryPath?: string }) => {
return startNode({ version, network: options.network, binaryPath: options.binaryPath });
});
program
.command('create [project-name]')
.description('Create a new CKB Smart Contract project in JavaScript.')
.option('-m, --manager <manager>', 'Specify the package manager to use (npm, yarn, pnpm)')
.option('-l, --language <language>', 'Specify the language to use (typescript, javascript)')
.option('-c, --contract-name <name>', 'Specify the name for the first contract (default: hello-world)')
.option('--no-interactive', 'Disable interactive prompts')
.option('--no-install', 'Skip dependency installation')
.option('--no-git', 'Skip git repository initialization')
.action(async (projectName: string, options: CreateScriptProjectOptions) => {
return await createScriptProject(projectName, options);
});
program
.command('deploy')
.description('Deploy contracts to different networks, only supports devnet and testnet')
.option('--network <network>', 'Specify the network to deploy to', 'devnet')
.option('--target <target>', 'Specify the script binaries file/folder path to deploy', './')
.option('-o, --output <output>', 'Specify the output folder path for the deployment record files', './deployment')
.option('-t, --type-id', 'Specify if use upgradable type id to deploy the script')
.option('--privkey <privkey>', 'Specify the private key to deploy scripts')
.option('-y, --yes', 'Skip confirmation prompt and deploy immediately')
.action((options: DeployOptions) => deploy(options));
program
.command('debug')
.option('--tx-hash <txHash>', 'Specify the transaction hash to debug with')
.option('--single-script <singleScript>', 'Specify the cell script to debug with')
.option('--bin <bin>', 'Specify a binary to replace the script to debug with')
.option('--network <network>', 'Specify the network to debug', 'devnet')
.description('Quickly debug transaction with tx-hash')
.action(async (option) => {
// For debugging, tx-hash is required
if (!option.txHash) {
logger.error('Error: --tx-hash is required for debugging operations');
process.exit(1);
}
const txHash = option.txHash;
if (option.singleScript) {
const { cellType, cellIndex, scriptType } = parseSingleScriptOption(option.singleScript);
return await debugSingleScript(txHash, cellIndex, cellType, scriptType, option.network, option.bin);
}
return await debugTransaction(txHash, option.network);
});
program
.command('system-scripts')
.option('--export-style <exportStyle>', 'Specify the export format, possible values are system, lumos and ccc.')
.option('--network <network>', 'Specify the CKB blockchain network', 'devnet')
.option(
'-o, --output <output>',
'Specify the output json file path for the system scripts, export-style and network will be ignored if output is specified',
)
.description('Print/Output system scripts of the CKB blockchain')
.action(async (option) => {
const network = option.network;
const exportStyle = option.exportStyle;
if (option.output) {
await genSystemScriptsJsonFile(option.output);
logger.success(`File ${option.output} generated successfully.`);
return;
}
return printSystemScripts({ style: exportStyle, network });
});
program
.command('clean')
.description('Clean the devnet data, need to stop running the chain first')
.option('-d, --data', 'Only remove chain data, keep devnet config files')
.action((options: { data?: boolean }) => clean(options));
program.command('accounts').description('Print account list info').action(accounts);
program
.command('deposit [toAddress] [amountInCKB]')
.description('Deposit CKB tokens to address, only devnet and testnet')
.option('--network <network>', 'Specify the network to deposit to', 'devnet')
.option('-r, --proxy-rpc', 'Use Proxy RPC to connect to blockchain')
.action(async (toAddress: string, amountInCKB: string, options: DepositOptions) => {
return deposit(toAddress, amountInCKB, options);
});
program
.command('transfer [toAddress] [amountInCKB]')
.description('Transfer CKB tokens to address, only devnet and testnet')
.option('--network <network>', 'Specify the network to transfer to', 'devnet')
.option('--privkey <privkey>', 'Specify the private key to transfer CKB')
.option('-r, --proxy-rpc', 'Use Proxy RPC to connect to blockchain')
.action(async (toAddress: string, amountInCKB: string, options: TransferOptions) => {
return transfer(toAddress, amountInCKB, options);
});
program
.command('transfer-all [toAddress]')
.description('Transfer All CKB tokens to address, only devnet and testnet')
.option('--network <network>', 'Specify the network to transfer to', 'devnet')
.option('--privkey <privkey>', 'Specify the private key to deploy scripts')
.option('-r, --proxy-rpc', 'Use Proxy RPC to connect to blockchain')
.action(async (toAddress: string, options: TransferOptions) => {
return transferAll(toAddress, options);
});
program
.command('balance [toAddress]')
.description('Check account balance, only devnet and testnet')
.option('--network <network>', 'Specify the network to check', 'devnet')
.action(async (toAddress: string, options: BalanceOption) => {
return balanceOf(toAddress, options);
});
program
.command('debugger')
.description('Port of the raw CKB Standalone Debugger')
.passThroughOptions()
.allowUnknownOption()
.helpOption(false) // Disable the default help option
.action(async () => {
return CKBDebugger.runWithArgs(process.argv.slice(2));
});
program
.command('config <action> [item] [value]')
.description('do a configuration action')
.action((action, item, value) => Config(action, item as ConfigItem, value));
const devnetCommand = program.command('devnet').description('Devnet utility commands');
devnetCommand
.command('config')
.description('Open a full-screen editor to tweak devnet config files')
.option(
'-s, --set <key=value>',
'Set a devnet config field non-interactively (repeatable), e.g. --set ckb.logger.filter=info',
(value: string, previous: string[] = []) => [...previous, value],
[],
)
.action(devnetConfig);
program.parse(process.argv);
// If no command is specified, display help
if (!process.argv.slice(2).length) {
program.outputHelp();
}