@offckb/cli 0.3.0-rc3 → 0.3.0-rc5

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.
Files changed (52) hide show
  1. package/README.md +27 -20
  2. package/dist/cfg/setting.d.ts +11 -2
  3. package/dist/cli.js +16 -6
  4. package/dist/cmd/balance.d.ts +1 -1
  5. package/dist/cmd/balance.js +6 -8
  6. package/dist/cmd/debug.d.ts +1 -1
  7. package/dist/cmd/debug.js +5 -5
  8. package/dist/cmd/deploy.d.ts +2 -1
  9. package/dist/cmd/deploy.js +15 -116
  10. package/dist/cmd/deposit.d.ts +2 -2
  11. package/dist/cmd/deposit.js +29 -31
  12. package/dist/cmd/list-hashes.d.ts +1 -1
  13. package/dist/cmd/my-scripts.d.ts +1 -1
  14. package/dist/cmd/my-scripts.js +2 -2
  15. package/dist/cmd/node.js +2 -2
  16. package/dist/cmd/proxy-rpc.d.ts +1 -1
  17. package/dist/cmd/proxy-rpc.js +4 -4
  18. package/dist/cmd/system-scripts.d.ts +70 -2
  19. package/dist/cmd/system-scripts.js +4 -0
  20. package/dist/cmd/transfer-all.d.ts +5 -0
  21. package/dist/cmd/transfer-all.js +37 -0
  22. package/dist/cmd/transfer.d.ts +2 -2
  23. package/dist/cmd/transfer.js +8 -11
  24. package/dist/deploy/index.d.ts +18 -0
  25. package/dist/deploy/index.js +126 -0
  26. package/dist/deploy/migration.d.ts +17 -12
  27. package/dist/deploy/migration.js +52 -15
  28. package/dist/deploy/toml.d.ts +8 -4
  29. package/dist/deploy/toml.js +2 -2
  30. package/dist/deploy/util.d.ts +1 -1
  31. package/dist/deploy/util.js +4 -4
  32. package/dist/node/install.js +3 -4
  33. package/dist/scripts/gen.js +4 -4
  34. package/dist/scripts/private.d.ts +4 -0
  35. package/dist/scripts/private.js +14 -0
  36. package/dist/scripts/util.d.ts +1 -1
  37. package/dist/scripts/util.js +1 -1
  38. package/dist/sdk/ckb.d.ts +37 -0
  39. package/dist/sdk/ckb.js +254 -0
  40. package/dist/sdk/network.d.ts +17 -0
  41. package/dist/sdk/network.js +22 -0
  42. package/dist/tools/moleculec-es.js +3 -4
  43. package/dist/tools/rpc-proxy.d.ts +1 -1
  44. package/dist/tools/rpc-proxy.js +2 -2
  45. package/dist/{util/type.d.ts → type/base.d.ts} +2 -0
  46. package/dist/util/request.d.ts +6 -6
  47. package/dist/util/request.js +15 -11
  48. package/dist/util/validator.js +2 -2
  49. package/package.json +5 -4
  50. package/dist/util/ckb.d.ts +0 -141
  51. package/dist/util/ckb.js +0 -312
  52. /package/dist/{util/type.js → type/base.js} +0 -0
package/README.md CHANGED
@@ -60,28 +60,29 @@ Usage: offckb [options] [command]
60
60
  ckb development network for your first try
61
61
 
62
62
  Options:
63
- -V, --version output the version number
64
- -h, --help display help for command
63
+ -V, --version output the version number
64
+ -h, --help display help for command
65
65
 
66
66
  Commands:
67
- create [options] [your-project-name] Create a new dApp from bare templates
68
- node [options] [CKB-Version] Use the CKB to start devnet
69
- proxy-rpc [options] Start the rpc proxy server
70
- clean Clean the devnet data, need to stop running the chain first
71
- accounts Print account list info
72
- list-hashes [CKB-Version] Use the CKB to list blockchain scripts hashes
73
- inject-config Add offckb.config.ts to your frontend workspace
74
- sync-scripts Sync scripts json file in your frontend workspace
75
- deposit [options] [toAddress] [amountInShannon] Deposit CKB tokens to address, only devnet and testnet
76
- transfer [options] [toAddress] [amountInShannon] Transfer CKB tokens to address, only devnet and testnet
77
- balance [options] [toAddress] Check account balance, only devnet and testnet
78
- deploy [options] Deploy contracts to different networks, only supports devnet and testnet
79
- my-scripts [options] Show deployed contracts info on different networks, only supports devnet and testnet
80
- config <action> [item] [value] do a configuration action
81
- debug [options] CKB Debugger for development
82
- system-scripts [options] Output system scripts of the local devnet
83
- mol [options] Generate CKB Moleculec binding code for development
84
- help [command] display help for command
67
+ create [options] [your-project-name] Create a new dApp from bare templates
68
+ node [options] [CKB-Version] Use the CKB to start devnet
69
+ proxy-rpc [options] Start the rpc proxy server
70
+ clean Clean the devnet data, need to stop running the chain first
71
+ accounts Print account list info
72
+ list-hashes [CKB-Version] Use the CKB to list blockchain scripts hashes
73
+ inject-config Add offckb.config.ts to your frontend workspace
74
+ sync-scripts Sync scripts json files in your frontend workspace
75
+ deposit [options] [toAddress] [amountInCKB] Deposit CKB tokens to address, only devnet and testnet
76
+ transfer [options] [toAddress] [amountInCKB] Transfer CKB tokens to address, only devnet and testnet
77
+ transfer-all [options] [toAddress] Transfer All CKB tokens to address, only devnet and testnet
78
+ balance [options] [toAddress] Check account balance, only devnet and testnet
79
+ deploy [options] Deploy contracts to different networks, only supports devnet and testnet
80
+ my-scripts [options] Show deployed contracts info on different networks, only supports devnet and testnet
81
+ config <action> [item] [value] do a configuration action
82
+ debug [options] CKB Debugger for development
83
+ system-scripts [options] Output system scripts of the local devnet
84
+ mol [options] Generate CKB Moleculec binding code for development
85
+ help [command] display help for command
85
86
  ```
86
87
 
87
88
  _Use `offckb [command] -h` to learn more about a specific command._
@@ -212,6 +213,12 @@ To deploy the script, cd into the frontend folder and run:
212
213
  cd frontend && offckb deploy --network <devnet/testnet>
213
214
  ```
214
215
 
216
+ Pass `--type-id` option if you want Scripts to be upgradable
217
+
218
+ ```sh
219
+ cd frontend && offckb deploy --type-id --network <devnet/testnet>
220
+ ```
221
+
215
222
  Once the deployment is done, you can use the following command to check the deployed scripts:
216
223
 
217
224
  ```sh
@@ -1,11 +1,20 @@
1
- import { AxiosProxyConfig } from 'axios';
2
1
  export declare const configPath: string;
3
2
  export declare const dataPath: string;
4
3
  export declare const cachePath: string;
5
4
  export declare const packageSrcPath: string;
6
5
  export declare const packageRootPath: string;
6
+ export interface ProxyBasicCredentials {
7
+ username: string;
8
+ password: string;
9
+ }
10
+ export interface ProxyConfig {
11
+ host: string;
12
+ port: number;
13
+ auth?: ProxyBasicCredentials;
14
+ protocol?: string;
15
+ }
7
16
  export interface Settings {
8
- proxy?: AxiosProxyConfig;
17
+ proxy?: ProxyConfig;
9
18
  rpc: {
10
19
  proxyPort: number;
11
20
  };
package/dist/cli.js CHANGED
@@ -53,6 +53,7 @@ const system_scripts_1 = require("./cmd/system-scripts");
53
53
  const proxy_rpc_1 = require("./cmd/proxy-rpc");
54
54
  const mol_1 = require("./cmd/mol");
55
55
  const fs = __importStar(require("fs"));
56
+ const transfer_all_1 = require("./cmd/transfer-all");
56
57
  const version = require('../package.json').version;
57
58
  const description = require('../package.json').description;
58
59
  // fix windows terminal encoding of simplified chinese text
@@ -97,19 +98,27 @@ program
97
98
  program.command('inject-config').description('Add offckb.config.ts to your frontend workspace').action(inject_config_1.injectConfig);
98
99
  program.command('sync-scripts').description('Sync scripts json files in your frontend workspace').action(sync_scripts_1.syncScripts);
99
100
  program
100
- .command('deposit [toAddress] [amountInShannon]')
101
+ .command('deposit [toAddress] [amountInCKB]')
101
102
  .description('Deposit CKB tokens to address, only devnet and testnet')
102
103
  .option('--network <network>', 'Specify the network to deposit to', 'devnet')
103
- .action((toAddress, amount, options) => __awaiter(void 0, void 0, void 0, function* () {
104
- return (0, deposit_1.deposit)(toAddress, amount, options);
104
+ .action((toAddress, amountInCKB, options) => __awaiter(void 0, void 0, void 0, function* () {
105
+ return (0, deposit_1.deposit)(toAddress, amountInCKB, options);
105
106
  }));
106
107
  program
107
- .command('transfer [toAddress] [amountInShannon]')
108
+ .command('transfer [toAddress] [amountInCKB]')
108
109
  .description('Transfer CKB tokens to address, only devnet and testnet')
109
110
  .option('--network <network>', 'Specify the network to transfer to', 'devnet')
110
111
  .option('--privkey <privkey>', 'Specify the private key to deploy scripts')
111
- .action((toAddress, amount, options) => __awaiter(void 0, void 0, void 0, function* () {
112
- return (0, transfer_1.transfer)(toAddress, amount, options);
112
+ .action((toAddress, amountInCKB, options) => __awaiter(void 0, void 0, void 0, function* () {
113
+ return (0, transfer_1.transfer)(toAddress, amountInCKB, options);
114
+ }));
115
+ program
116
+ .command('transfer-all [toAddress]')
117
+ .description('Transfer All CKB tokens to address, only devnet and testnet')
118
+ .option('--network <network>', 'Specify the network to transfer to', 'devnet')
119
+ .option('--privkey <privkey>', 'Specify the private key to deploy scripts')
120
+ .action((toAddress, options) => __awaiter(void 0, void 0, void 0, function* () {
121
+ return (0, transfer_all_1.transferAll)(toAddress, options);
113
122
  }));
114
123
  program
115
124
  .command('balance [toAddress]')
@@ -123,6 +132,7 @@ program
123
132
  .description('Deploy contracts to different networks, only supports devnet and testnet')
124
133
  .option('--network <network>', 'Specify the network to deploy to', 'devnet')
125
134
  .option('--target <target>', 'Specify the relative bin target folder to deploy to')
135
+ .option('-t, --type-id', 'Specify if use upgradable type id to deploy the script')
126
136
  .option('--privkey <privkey>', 'Specify the private key to deploy scripts')
127
137
  .action((options) => (0, deploy_1.deploy)(options));
128
138
  program
@@ -1,4 +1,4 @@
1
- import { NetworkOption } from '../util/type';
1
+ import { NetworkOption } from '../type/base';
2
2
  export interface BalanceOption extends NetworkOption {
3
3
  }
4
4
  export declare function balanceOf(address: string, opt?: BalanceOption): Promise<void>;
@@ -10,19 +10,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.balanceOf = void 0;
13
- const lumos_1 = require("@ckb-lumos/lumos");
14
- const ckb_1 = require("../util/ckb");
13
+ const ckb_1 = require("../sdk/ckb");
15
14
  const validator_1 = require("../util/validator");
16
- const type_1 = require("../util/type");
17
- function balanceOf(address, opt = { network: type_1.Network.devnet }) {
15
+ const base_1 = require("../type/base");
16
+ function balanceOf(address, opt = { network: base_1.Network.devnet }) {
18
17
  return __awaiter(this, void 0, void 0, function* () {
19
18
  const network = opt.network;
20
19
  (0, validator_1.validateNetworkOpt)(network);
21
- const ckb = new ckb_1.CKB(network);
22
- const lumosConfig = ckb.getLumosConfig();
23
- const balance = yield ckb.capacityOf(address, lumosConfig);
24
- const balanceInCKB = balance.div(lumos_1.BI.from('100000000'));
20
+ const ckb = new ckb_1.CKB({ network });
21
+ const balanceInCKB = yield ckb.balance(address);
25
22
  console.log(`Balance: ${balanceInCKB} CKB`);
23
+ process.exit(0);
26
24
  });
27
25
  }
28
26
  exports.balanceOf = balanceOf;
@@ -1,4 +1,4 @@
1
- import { Network } from '../util/type';
1
+ import { Network } from '../type/base';
2
2
  export declare function debugTransaction(txHash: string, network: Network): void;
3
3
  export declare function buildTransactionDebugOptions(txHash: string, network: Network): {
4
4
  name: string;
package/dist/cmd/debug.js CHANGED
@@ -10,7 +10,7 @@ const fs_1 = __importDefault(require("fs"));
10
10
  const ckb_tx_dumper_1 = require("../tools/ckb-tx-dumper");
11
11
  const path_1 = __importDefault(require("path"));
12
12
  const advanced_1 = require("@ckb-ccc/core/advanced");
13
- const type_1 = require("../util/type");
13
+ const base_1 = require("../type/base");
14
14
  function debugTransaction(txHash, network) {
15
15
  const txFile = buildTxFileOptionBy(txHash, network);
16
16
  const opts = buildTransactionDebugOptions(txHash, network);
@@ -91,10 +91,10 @@ function buildTxFileOptionBy(txHash, network) {
91
91
  exports.buildTxFileOptionBy = buildTxFileOptionBy;
92
92
  function buildTransactionJsonFilePath(network, txHash) {
93
93
  const settings = (0, setting_1.readSettings)();
94
- if (network === type_1.Network.devnet) {
94
+ if (network === base_1.Network.devnet) {
95
95
  return `${settings.devnet.transactionsPath}/${txHash}.json`;
96
96
  }
97
- if (network === type_1.Network.testnet) {
97
+ if (network === base_1.Network.testnet) {
98
98
  return `${settings.testnet.transactionsPath}/${txHash}.json`;
99
99
  }
100
100
  return `${settings.mainnet.transactionsPath}/${txHash}.json`;
@@ -102,10 +102,10 @@ function buildTransactionJsonFilePath(network, txHash) {
102
102
  exports.buildTransactionJsonFilePath = buildTransactionJsonFilePath;
103
103
  function buildDebugFullTransactionFilePath(network, txHash) {
104
104
  const settings = (0, setting_1.readSettings)();
105
- if (network === type_1.Network.devnet) {
105
+ if (network === base_1.Network.devnet) {
106
106
  return `${settings.devnet.debugFullTransactionsPath}/${txHash}.json`;
107
107
  }
108
- if (network === type_1.Network.testnet) {
108
+ if (network === base_1.Network.testnet) {
109
109
  return `${settings.testnet.debugFullTransactionsPath}/${txHash}.json`;
110
110
  }
111
111
  return `${settings.mainnet.debugFullTransactionsPath}/${txHash}.json`;
@@ -1,6 +1,7 @@
1
- import { NetworkOption } from '../util/type';
1
+ import { NetworkOption } from '../type/base';
2
2
  export interface DeployOptions extends NetworkOption {
3
3
  target: string | null | undefined;
4
4
  privkey?: string | null;
5
+ typeId?: boolean;
5
6
  }
6
7
  export declare function deploy(opt?: DeployOptions): Promise<void>;
@@ -13,36 +13,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.deploy = void 0;
16
- const lumos_1 = require("@ckb-lumos/lumos");
17
- const fs_1 = __importDefault(require("fs"));
18
- const type_1 = require("../util/type");
16
+ const base_1 = require("../type/base");
19
17
  const path_1 = __importDefault(require("path"));
20
- const ckb_1 = require("../util/ckb");
21
18
  const account_1 = require("../cfg/account");
22
- const fs_2 = require("../util/fs");
19
+ const fs_1 = require("../util/fs");
23
20
  const validator_1 = require("../util/validator");
24
- const toml_1 = require("../deploy/toml");
25
- const migration_1 = require("../deploy/migration");
26
- const utils_1 = require("@ckb-lumos/lumos/utils");
27
- const gen_1 = require("../scripts/gen");
28
- const offckb_config_1 = require("../template/offckb-config");
29
- function deploy(opt = { network: type_1.Network.devnet, target: null }) {
21
+ const deploy_1 = require("../deploy");
22
+ const ckb_1 = require("../sdk/ckb");
23
+ function deploy(opt = { network: base_1.Network.devnet, typeId: false, target: null }) {
24
+ var _a;
30
25
  return __awaiter(this, void 0, void 0, function* () {
31
26
  const network = opt.network;
32
27
  (0, validator_1.validateNetworkOpt)(network);
33
- const ckb = new ckb_1.CKB(network);
28
+ const ckb = new ckb_1.CKB({ network });
34
29
  // we use deployerAccount to deploy contract by default
35
30
  const privateKey = opt.privkey || account_1.deployerAccount.privkey;
36
- const lumosConfig = ckb.getLumosConfig();
37
- const from = ckb_1.CKB.generateAccountFromPrivateKey(privateKey, lumosConfig);
31
+ const enableTypeId = (_a = opt.typeId) !== null && _a !== void 0 ? _a : false;
38
32
  const targetFolder = opt.target;
39
33
  if (targetFolder) {
40
- const binFolder = (0, fs_2.isAbsolutePath)(targetFolder) ? targetFolder : path_1.default.resolve(process.cwd(), targetFolder);
41
- const bins = (0, fs_2.listBinaryFilesInFolder)(binFolder);
34
+ const binFolder = (0, fs_1.isAbsolutePath)(targetFolder) ? targetFolder : path_1.default.resolve(process.cwd(), targetFolder);
35
+ const bins = (0, fs_1.listBinaryFilesInFolder)(binFolder);
42
36
  const binPaths = bins.map((bin) => path_1.default.resolve(binFolder, bin));
43
- const results = yield deployBinaries(binPaths, from, ckb);
37
+ const results = yield (0, deploy_1.deployBinaries)(binPaths, privateKey, enableTypeId, ckb);
44
38
  // record the deployed contract infos
45
- recordDeployResult(results, network, false); // we don't update my-scripts.json since we don't know where the file is
39
+ (0, deploy_1.recordDeployResult)(results, network, false); // we don't update my-scripts.json since we don't know where the file is
46
40
  return;
47
41
  }
48
42
  // check if target workspace is valid
@@ -53,105 +47,10 @@ function deploy(opt = { network: type_1.Network.devnet, target: null }) {
53
47
  return console.debug('Not a valid offCKB dapp workspace.');
54
48
  }
55
49
  // read contract bin folder
56
- const bins = getToDeployBinsPath();
57
- const results = yield deployBinaries(bins, from, ckb);
50
+ const bins = (0, deploy_1.getToDeployBinsPath)();
51
+ const results = yield (0, deploy_1.deployBinaries)(bins, privateKey, enableTypeId, ckb);
58
52
  // record the deployed contract infos
59
- recordDeployResult(results, network);
53
+ (0, deploy_1.recordDeployResult)(results, network);
60
54
  });
61
55
  }
62
56
  exports.deploy = deploy;
63
- function getToDeployBinsPath() {
64
- const userOffCKBConfigPath = path_1.default.resolve(process.cwd(), 'offckb.config.ts');
65
- const fileContent = fs_1.default.readFileSync(userOffCKBConfigPath, 'utf-8');
66
- const match = fileContent.match(/contractBinFolder:\s*['"]([^'"]+)['"]/);
67
- if (match && match[1]) {
68
- const contractBinFolderValue = match[1];
69
- const binFolderPath = (0, fs_2.isAbsolutePath)(contractBinFolderValue)
70
- ? contractBinFolderValue
71
- : path_1.default.resolve(process.cwd(), contractBinFolderValue);
72
- const bins = (0, fs_2.listBinaryFilesInFolder)(binFolderPath);
73
- return bins.map((bin) => path_1.default.resolve(binFolderPath, bin));
74
- }
75
- else {
76
- console.log('contractBinFolder value not found in offckb.config.ts');
77
- return [];
78
- }
79
- }
80
- function recordDeployResult(results, network, updateMyScriptsJsonFile = true) {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- if (results.length === 0) {
83
- return;
84
- }
85
- for (const result of results) {
86
- (0, toml_1.generateDeploymentToml)(result.deploymentOptions, network);
87
- (0, migration_1.generateDeploymentRecipeJsonFile)(result.deploymentOptions.name, result.deploymentRecipe, network);
88
- }
89
- // update my-scripts.json
90
- if (updateMyScriptsJsonFile) {
91
- const userOffCKBConfigPath = path_1.default.resolve(process.cwd(), 'offckb.config.ts');
92
- const folder = offckb_config_1.OffCKBConfigFile.readContractInfoFolder(userOffCKBConfigPath);
93
- if (folder) {
94
- const myScriptsFilePath = path_1.default.resolve(folder, 'my-scripts.json');
95
- (0, gen_1.genMyScriptsJsonFile)(myScriptsFilePath);
96
- }
97
- }
98
- console.log('done.');
99
- });
100
- }
101
- function deployBinaries(binPaths, from, ckb) {
102
- return __awaiter(this, void 0, void 0, function* () {
103
- if (binPaths.length === 0) {
104
- console.log('No binary to deploy.');
105
- }
106
- const results = [];
107
- for (const bin of binPaths) {
108
- const result = yield deployBinary(bin, from, ckb);
109
- results.push(result);
110
- }
111
- return results;
112
- });
113
- }
114
- function deployBinary(binPath, from, ckb) {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- const bin = yield (0, fs_2.readFileToUint8Array)(binPath);
117
- const contractName = path_1.default.basename(binPath);
118
- const result = yield lumos_1.commons.deploy.generateDeployWithTypeIdTx({
119
- cellProvider: ckb.indexer,
120
- fromInfo: from.address,
121
- scriptBinary: bin,
122
- config: ckb.getLumosConfig(),
123
- });
124
- // send deploy tx
125
- let txSkeleton = result.txSkeleton;
126
- txSkeleton = lumos_1.commons.common.prepareSigningEntries(txSkeleton);
127
- const message = txSkeleton.get('signingEntries').get(0).message;
128
- const Sig = lumos_1.hd.key.signRecoverable(message, from.privKey);
129
- const tx = lumos_1.helpers.sealTransaction(txSkeleton, [Sig]);
130
- const res = yield ckb.rpc.sendTransaction(tx, 'passthrough');
131
- console.log(`contract ${contractName} deployed, tx hash:`, res);
132
- console.log('wait 4 blocks..');
133
- yield ckb.indexer.waitForSync(-4); // why negative 4? a bug in ckb-lumos
134
- // todo: handle multiple cell recipes?
135
- return {
136
- deploymentOptions: {
137
- name: contractName,
138
- binFilePath: binPath,
139
- enableTypeId: true,
140
- lockScript: tx.outputs[+result.scriptConfig.INDEX].lock,
141
- },
142
- deploymentRecipe: {
143
- cellRecipes: [
144
- {
145
- name: contractName,
146
- txHash: result.scriptConfig.TX_HASH,
147
- index: result.scriptConfig.INDEX,
148
- occupiedCapacity: '0x' + BigInt(tx.outputsData[+result.scriptConfig.INDEX].slice(2).length / 2).toString(16),
149
- dataHash: (0, utils_1.ckbHash)(tx.outputsData[+result.scriptConfig.INDEX]),
150
- typeId: (0, utils_1.computeScriptHash)(result.typeId),
151
- },
152
- ],
153
- depGroupRecipes: [],
154
- },
155
- };
156
- });
157
- }
@@ -1,4 +1,4 @@
1
- import { NetworkOption } from '../util/type';
1
+ import { NetworkOption } from '../type/base';
2
2
  export interface DepositOptions extends NetworkOption {
3
3
  }
4
- export declare function deposit(toAddress: string, amount: string, opt?: DepositOptions): Promise<void>;
4
+ export declare function deposit(toAddress: string, amountInCKB: string, opt?: DepositOptions): Promise<void>;
@@ -10,29 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.deposit = void 0;
13
- const ckb_1 = require("../util/ckb");
13
+ const ckb_1 = require("../sdk/ckb");
14
14
  const account_1 = require("../cfg/account");
15
- const type_1 = require("../util/type");
15
+ const base_1 = require("../type/base");
16
16
  const link_1 = require("../util/link");
17
17
  const validator_1 = require("../util/validator");
18
18
  const request_1 = require("../util/request");
19
- function deposit(toAddress, amount, opt = { network: type_1.Network.devnet }) {
19
+ function deposit(toAddress, amountInCKB, opt = { network: base_1.Network.devnet }) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
21
  const network = opt.network;
22
22
  (0, validator_1.validateNetworkOpt)(network);
23
- const ckb = new ckb_1.CKB(network);
24
- const lumosConfig = ckb.getLumosConfig();
23
+ const ckb = new ckb_1.CKB({ network });
25
24
  if (network === 'testnet') {
26
25
  return yield depositFromTestnetFaucet(toAddress, ckb);
27
26
  }
28
27
  // deposit from devnet miner
29
- const from = ckb_1.CKB.generateAccountFromPrivateKey(account_1.ckbDevnetMinerAccount.privkey, lumosConfig);
28
+ const privateKey = account_1.ckbDevnetMinerAccount.privkey;
30
29
  const txHash = yield ckb.transfer({
31
- from: from.address,
32
- to: toAddress,
33
- privKey: from.privKey,
34
- amount,
35
- }, lumosConfig);
30
+ toAddress,
31
+ privateKey,
32
+ amountInCKB,
33
+ });
36
34
  console.log('tx hash: ', txHash);
37
35
  });
38
36
  }
@@ -40,29 +38,33 @@ exports.deposit = deposit;
40
38
  function depositFromTestnetFaucet(ckbAddress, ckb) {
41
39
  return __awaiter(this, void 0, void 0, function* () {
42
40
  console.log('testnet faucet only supports fixed-amount claim: 10,000 CKB');
43
- const lumosConfig = ckb.getLumosConfig();
44
41
  const randomAccountPrivateKey = '0x' + generateHex(64);
45
- const randomAccount = ckb_1.CKB.generateAccountFromPrivateKey(randomAccountPrivateKey, lumosConfig);
46
- console.log(`use random account to claim from faucet: \n\nprivate key: ${randomAccountPrivateKey}\n\n address: ${randomAccount.address}`);
42
+ const randomAccountAddress = yield ckb.buildSecp256k1Address(randomAccountPrivateKey);
43
+ console.log(`use random account to claim from faucet: \n\nprivate key: ${randomAccountPrivateKey}\n\n address: ${randomAccountAddress}`);
47
44
  try {
48
- yield sendClaimRequest(randomAccount.address);
49
- console.log('Wait for 4 blocks to transfer all from random account to your account..');
45
+ const claimResponse = yield sendClaimRequest(randomAccountAddress);
46
+ console.log('Wait for claim transaction confirmed to transfer all from random account to your account..');
50
47
  console.log('You can transfer by yourself if it ends up fails..');
51
- yield ckb.indexer.waitForSync(-4);
52
- const txHash = yield ckb.transferAll(randomAccount.privKey, ckbAddress, lumosConfig);
53
- console.log(`Done, check ${(0, link_1.buildTestnetTxLink)(txHash)} for details.`);
48
+ if (claimResponse.txHash != null) {
49
+ yield ckb.waitForTxConfirm(claimResponse.txHash);
50
+ }
51
+ else {
52
+ yield ckb.waitForBlocksBy(4); // wait 4 blocks
53
+ }
54
54
  }
55
55
  catch (error) {
56
56
  console.log(error);
57
- throw new Error('request failed.');
57
+ throw new Error('claim request failed.');
58
58
  }
59
+ const txHash = yield ckb.transferAll({ privateKey: randomAccountPrivateKey, toAddress: ckbAddress });
60
+ console.log(`Done, check ${(0, link_1.buildTestnetTxLink)(txHash)} for details.`);
59
61
  });
60
62
  }
61
63
  function sendClaimRequest(toAddress) {
62
64
  return __awaiter(this, void 0, void 0, function* () {
63
65
  const url = 'https://faucet-api.nervos.org/claim_events'; // Replace 'YOUR_API_ENDPOINT' with the actual API endpoint
64
66
  const headers = {
65
- 'User-Agent': 'axios-requests/2.31.0',
67
+ 'User-Agent': 'node-fetch-requests/v2',
66
68
  'Accept-Encoding': 'gzip, deflate',
67
69
  Accept: '*/*',
68
70
  Connection: 'keep-alive',
@@ -71,22 +73,18 @@ function sendClaimRequest(toAddress) {
71
73
  const body = JSON.stringify({
72
74
  claim_event: {
73
75
  address_hash: toAddress,
74
- amount: '10000',
76
+ amount: '10000', // unit: CKB
75
77
  },
76
78
  });
77
79
  const config = {
78
80
  method: 'post',
79
- url: url,
80
81
  headers: headers,
81
- data: body,
82
+ body,
82
83
  };
83
- try {
84
- const response = yield request_1.Request.send(config);
85
- console.log('send claim request, status: ', response.status); // Handle the response data here
86
- }
87
- catch (error) {
88
- console.error('Error:', error);
89
- }
84
+ const response = yield request_1.Request.send(url, config);
85
+ console.log('send claim request, status: ', response.status); // Handle the response data here
86
+ const jsonResponse = yield response.json();
87
+ return jsonResponse.data.attributes;
90
88
  });
91
89
  }
92
90
  function generateHex(length) {
@@ -1,4 +1,4 @@
1
- import { H256 } from '../util/type';
1
+ import { H256 } from '../type/base';
2
2
  export interface SystemCell {
3
3
  path: string;
4
4
  tx_hash: H256;
@@ -1,4 +1,4 @@
1
- import { NetworkOption } from '../util/type';
1
+ import { NetworkOption } from '../type/base';
2
2
  export interface DeployedScriptOption extends NetworkOption {
3
3
  }
4
4
  export declare function printMyScripts(option?: DeployedScriptOption): void;
@@ -4,9 +4,9 @@ exports.printMyScripts = void 0;
4
4
  const toml_1 = require("../deploy/toml");
5
5
  const util_1 = require("../deploy/util");
6
6
  const util_2 = require("../scripts/util");
7
- const type_1 = require("../util/type");
7
+ const base_1 = require("../type/base");
8
8
  const validator_1 = require("../util/validator");
9
- function printMyScripts(option = { network: type_1.Network.devnet }) {
9
+ function printMyScripts(option = { network: base_1.Network.devnet }) {
10
10
  const network = option.network;
11
11
  (0, validator_1.validateNetworkOpt)(network);
12
12
  const scriptsInfo = (0, util_2.readUserDeployedScriptsInfo)(network);
package/dist/cmd/node.js CHANGED
@@ -16,7 +16,7 @@ const install_1 = require("../node/install");
16
16
  const setting_1 = require("../cfg/setting");
17
17
  const encoding_1 = require("../util/encoding");
18
18
  const rpc_proxy_1 = require("../tools/rpc-proxy");
19
- const type_1 = require("../util/type");
19
+ const base_1 = require("../type/base");
20
20
  function node({ version, noProxyServer }) {
21
21
  var _a, _b;
22
22
  return __awaiter(this, void 0, void 0, function* () {
@@ -54,7 +54,7 @@ function node({ version, noProxyServer }) {
54
54
  if (!noProxyServer) {
55
55
  const ckbRpc = settings.devnet.rpcUrl;
56
56
  const port = settings.rpc.proxyPort;
57
- const proxy = (0, rpc_proxy_1.createRPCProxy)(type_1.Network.devnet, ckbRpc, port);
57
+ const proxy = (0, rpc_proxy_1.createRPCProxy)(base_1.Network.devnet, ckbRpc, port);
58
58
  proxy.start();
59
59
  }
60
60
  }
@@ -1,4 +1,4 @@
1
- import { Network } from '../util/type';
1
+ import { Network } from '../type/base';
2
2
  export interface ProxyRpcOptions {
3
3
  ckbRpc?: string;
4
4
  port?: number;
@@ -3,16 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.proxyRpc = void 0;
4
4
  const setting_1 = require("../cfg/setting");
5
5
  const rpc_proxy_1 = require("../tools/rpc-proxy");
6
- const type_1 = require("../util/type");
6
+ const base_1 = require("../type/base");
7
7
  function proxyRpc(options) {
8
8
  const settings = (0, setting_1.readSettings)();
9
- const network = options.network || type_1.Network.devnet;
9
+ const network = options.network || base_1.Network.devnet;
10
10
  let ckbRpc = options.ckbRpc;
11
11
  if (!ckbRpc) {
12
12
  ckbRpc =
13
- network === type_1.Network.devnet
13
+ network === base_1.Network.devnet
14
14
  ? settings.devnet.rpcUrl
15
- : network === type_1.Network.testnet
15
+ : network === base_1.Network.testnet
16
16
  ? settings.testnet.rpcUrl
17
17
  : settings.mainnet.rpcUrl;
18
18
  }