@0xobelisk/sui-cli 1.2.0-pre.12 → 1.2.0-pre.120

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 (40) hide show
  1. package/README.md +7 -7
  2. package/dist/dubhe.js +152 -51
  3. package/dist/dubhe.js.map +1 -1
  4. package/package.json +31 -19
  5. package/src/commands/build.ts +61 -18
  6. package/src/commands/call.ts +83 -83
  7. package/src/commands/checkBalance.ts +27 -12
  8. package/src/commands/convertJson.ts +84 -0
  9. package/src/commands/doctor.ts +1515 -0
  10. package/src/commands/faucet.ts +20 -10
  11. package/src/commands/generate.ts +61 -0
  12. package/src/commands/generateKey.ts +3 -2
  13. package/src/commands/index.ts +20 -11
  14. package/src/commands/info.ts +61 -0
  15. package/src/commands/loadMetadata.ts +68 -0
  16. package/src/commands/localnode.ts +22 -6
  17. package/src/commands/publish.ts +55 -7
  18. package/src/commands/query.ts +101 -101
  19. package/src/commands/shell.ts +208 -0
  20. package/src/commands/{configStore.ts → storeConfig.ts} +13 -5
  21. package/src/commands/switchEnv.ts +33 -0
  22. package/src/commands/test.ts +143 -31
  23. package/src/commands/upgrade.ts +46 -6
  24. package/src/commands/wait.ts +333 -22
  25. package/src/commands/watch.ts +9 -8
  26. package/src/dubhe.ts +12 -4
  27. package/src/utils/axios-downloader.ts +116 -0
  28. package/src/utils/callHandler.ts +118 -118
  29. package/src/utils/checkBalance.ts +6 -2
  30. package/src/utils/constants.ts +9 -0
  31. package/src/utils/generateAccount.ts +1 -1
  32. package/src/utils/index.ts +4 -3
  33. package/src/utils/metadataHandler.ts +17 -0
  34. package/src/utils/publishHandler.ts +404 -289
  35. package/src/utils/queryStorage.ts +141 -141
  36. package/src/utils/startNode.ts +115 -16
  37. package/src/utils/storeConfig.ts +50 -10
  38. package/src/utils/upgradeHandler.ts +210 -86
  39. package/src/utils/utils.ts +1025 -63
  40. package/src/commands/schemagen.ts +0 -40
@@ -1,6 +1,7 @@
1
1
  import type { CommandModule } from 'yargs';
2
2
  import chokidar from 'chokidar';
3
3
  import { exec } from 'child_process';
4
+ import { handlerExit } from './shell';
4
5
 
5
6
  const commandModule: CommandModule = {
6
7
  command: 'watch',
@@ -14,17 +15,17 @@ const commandModule: CommandModule = {
14
15
  async handler() {
15
16
  const configFilePath = 'dubhe.config.ts';
16
17
 
17
- const runSchemagen = () => {
18
- exec('pnpm dubhe schemagen', (error, stdout, stderr) => {
18
+ const runGenerate = () => {
19
+ exec('pnpm dubhe generate', (error, stdout, stderr) => {
19
20
  if (error) {
20
- console.error(`Error executing schemagen: ${error.message}`);
21
+ console.error(`Error executing generate: ${error.message}`);
21
22
  return;
22
23
  }
23
24
  if (stderr) {
24
- console.error(`schemagen stderr: ${stderr}`);
25
+ console.error(`generate stderr: ${stderr}`);
25
26
  return;
26
27
  }
27
- console.log(`schemagen stdout: ${stdout}`);
28
+ console.log(`generate stdout: ${stdout}`);
28
29
  });
29
30
  };
30
31
 
@@ -33,8 +34,8 @@ const commandModule: CommandModule = {
33
34
  });
34
35
 
35
36
  watcher.on('change', (path) => {
36
- console.log(`${path} has been changed. Running schemagen...`);
37
- runSchemagen();
37
+ console.log(`${path} has been changed. Running generate...`);
38
+ runGenerate();
38
39
  });
39
40
 
40
41
  console.log(`Watching for changes in ${configFilePath}...`);
@@ -42,7 +43,7 @@ const commandModule: CommandModule = {
42
43
  process.on('SIGINT', () => {
43
44
  watcher.close();
44
45
  console.log('\nWatch stopped.');
45
- process.exit();
46
+ handlerExit();
46
47
  });
47
48
  }
48
49
  };
package/src/dubhe.ts CHANGED
@@ -2,25 +2,30 @@
2
2
 
3
3
  import yargs from 'yargs';
4
4
  import { hideBin } from 'yargs/helpers';
5
+ import chalk from 'chalk';
5
6
  import { commands } from './commands';
6
7
  import { logError } from './utils/errors';
8
+ import packageJson from '../package.json';
7
9
 
8
10
  // Load .env file into process.env
9
11
  import * as dotenv from 'dotenv';
10
- import chalk from 'chalk';
11
12
  dotenv.config();
12
13
 
13
14
  yargs(hideBin(process.argv))
14
15
  // Explicit name to display in help (by default it's the entry file, which may not be "dubhe" for e.g. ts-node)
15
16
  .scriptName('dubhe')
16
17
  // Use the commands directory to scaffold
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- command array overload isn't typed, see https://github.com/yargs/yargs/blob/main/docs/advanced.md#esm-hierarchy
18
+
18
19
  .command(commands as any)
20
+ .version(packageJson.version)
21
+ .demandCommand(1, 'Please provide a command')
22
+ .recommendCommands()
19
23
  // Enable strict mode.
20
24
  .strict()
21
25
  // Custom error handler
22
26
  .fail((msg, err) => {
23
27
  console.error(chalk.red(msg));
28
+
24
29
  if (msg.includes('Missing required argument')) {
25
30
  console.log(
26
31
  chalk.yellow(
@@ -29,8 +34,11 @@ yargs(hideBin(process.argv))
29
34
  );
30
35
  }
31
36
  console.log('');
32
- logError(err);
33
- console.log('');
37
+ // Even though `.fail` type says we should get an `Error`, this can sometimes be undefined
38
+ if (err != null) {
39
+ logError(err);
40
+ console.log('');
41
+ }
34
42
 
35
43
  process.exit(1);
36
44
  })
@@ -0,0 +1,116 @@
1
+ // Better download implementation using axios
2
+ import chalk from 'chalk';
3
+ import * as cliProgress from 'cli-progress';
4
+ import * as fs from 'fs';
5
+ import axios from 'axios';
6
+
7
+ /**
8
+ * Download file using axios
9
+ */
10
+ export async function downloadWithAxios(url: string, outputPath: string): Promise<void> {
11
+ try {
12
+ const response = await axios.get(url, {
13
+ responseType: 'stream',
14
+ timeout: 30000,
15
+ headers: { 'User-Agent': 'dubhe-cli' },
16
+ maxRedirects: 5,
17
+ validateStatus: (status) => status < 400 // Accept all status codes < 400
18
+ });
19
+
20
+ await streamToFile(response, outputPath);
21
+ console.log(chalk.green(` ✓ Successfully downloaded`));
22
+ } catch (error: any) {
23
+ // Handle specific network error cases with more descriptive messages
24
+ if (error.code === 'ENOTFOUND') {
25
+ throw new Error(
26
+ `DNS resolution failed: ${error.message}. Please check your internet connection.`
27
+ );
28
+ } else if (error.code === 'ECONNRESET') {
29
+ throw new Error(`Connection reset: ${error.message}. Please check your network connection.`);
30
+ } else if (error.code === 'ETIMEDOUT') {
31
+ throw new Error(
32
+ `Connection timeout: ${error.message}. Please check your network connection.`
33
+ );
34
+ } else if (error.message.includes('protocol mismatch')) {
35
+ throw new Error(
36
+ `Protocol mismatch: ${error.message}. Please check your network configuration.`
37
+ );
38
+ } else if (error.response) {
39
+ throw new Error(`HTTP ${error.response.status}: ${error.response.statusText}`);
40
+ } else {
41
+ const errorMsg = error instanceof Error ? error.message : String(error);
42
+ throw new Error(`Download failed: ${errorMsg}`);
43
+ }
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Stream response data to file with progress bar
49
+ */
50
+ async function streamToFile(response: any, outputPath: string): Promise<void> {
51
+ const totalSize = parseInt(response.headers['content-length'] || '0');
52
+
53
+ // Create progress bar
54
+ const progressBar = new cliProgress.SingleBar({
55
+ format:
56
+ chalk.cyan('Download Progress') +
57
+ ' |{bar}| {percentage}% | {value}/{total} MB | Speed: {speed} MB/s | ETA: {eta}s',
58
+ barCompleteChar: '\u2588',
59
+ barIncompleteChar: '\u2591',
60
+ hideCursor: true,
61
+ barsize: 30,
62
+ forceRedraw: true
63
+ });
64
+
65
+ if (totalSize > 0) {
66
+ progressBar.start(Math.round((totalSize / 1024 / 1024) * 100) / 100, 0, {
67
+ speed: '0.00'
68
+ });
69
+ } else {
70
+ console.log(chalk.blue('📥 Downloading... (unable to get file size)'));
71
+ }
72
+
73
+ const writer = fs.createWriteStream(outputPath);
74
+ let downloadedBytes = 0;
75
+ const startTime = Date.now();
76
+
77
+ return new Promise((resolve, reject) => {
78
+ response.data.on('data', (chunk: Buffer) => {
79
+ downloadedBytes += chunk.length;
80
+
81
+ if (totalSize > 0) {
82
+ const downloadedMB = Math.round((downloadedBytes / 1024 / 1024) * 100) / 100;
83
+ const elapsedTime = (Date.now() - startTime) / 1000;
84
+ const speed = elapsedTime > 0 ? Math.round((downloadedMB / elapsedTime) * 100) / 100 : 0;
85
+
86
+ progressBar.update(downloadedMB, {
87
+ speed: speed.toFixed(2)
88
+ });
89
+ }
90
+ });
91
+
92
+ response.data.pipe(writer);
93
+
94
+ writer.on('finish', () => {
95
+ if (totalSize > 0) {
96
+ progressBar.stop();
97
+ }
98
+
99
+ const totalMB = Math.round((downloadedBytes / 1024 / 1024) * 100) / 100;
100
+ const elapsedTime = (Date.now() - startTime) / 1000;
101
+ const avgSpeed = elapsedTime > 0 ? Math.round((totalMB / elapsedTime) * 100) / 100 : 0;
102
+
103
+ console.log(
104
+ chalk.green(`✓ Download completed! ${totalMB} MB, average speed: ${avgSpeed} MB/s`)
105
+ );
106
+ resolve();
107
+ });
108
+
109
+ writer.on('error', (error) => {
110
+ if (totalSize > 0) {
111
+ progressBar.stop();
112
+ }
113
+ reject(error);
114
+ });
115
+ });
116
+ }
@@ -1,129 +1,129 @@
1
- import { loadMetadata, Transaction, TransactionResult } from '@0xobelisk/sui-client';
2
- import { DubheCliError } from './errors';
3
- import { getOldPackageId, initializeDubhe } from './utils';
4
- import { DubheConfig } from '@0xobelisk/sui-common';
5
- import { loadMetadataFromFile } from './queryStorage';
1
+ // import { loadMetadata, Transaction, TransactionResult } from '@0xobelisk/sui-client';
2
+ // import { DubheCliError } from './errors';
3
+ // import { getOldPackageId, initializeDubhe } from './utils';
4
+ // import { DubheConfig } from '@0xobelisk/sui-common';
5
+ // import { loadMetadataFromFile } from './queryStorage';
6
6
 
7
- const BaseTxType = [
8
- 'u8',
9
- 'u16',
10
- 'u32',
11
- 'u64',
12
- 'u128',
13
- 'u256',
14
- 'bool',
15
- 'id',
16
- 'string',
17
- 'address',
18
- 'object'
19
- ];
7
+ // const BaseTxType = [
8
+ // 'u8',
9
+ // 'u16',
10
+ // 'u32',
11
+ // 'u64',
12
+ // 'u128',
13
+ // 'u256',
14
+ // 'bool',
15
+ // 'id',
16
+ // 'string',
17
+ // 'address',
18
+ // 'object'
19
+ // ];
20
20
 
21
- function validateParams(params: any[]) {
22
- try {
23
- params.forEach((param) => {
24
- const [type, _] = param.split(':');
25
- if (!BaseTxType.includes(type)) {
26
- throw new Error(`Invalid param type: ${type}`);
27
- }
28
- });
29
- } catch (error) {
30
- throw new Error(`Invalid params: ${error}`);
31
- }
32
- }
21
+ // function validateParams(params: any[]) {
22
+ // try {
23
+ // params.forEach((param) => {
24
+ // const [type, _] = param.split(':');
25
+ // if (!BaseTxType.includes(type)) {
26
+ // throw new Error(`Invalid param type: ${type}`);
27
+ // }
28
+ // });
29
+ // } catch (error) {
30
+ // throw new Error(`Invalid params: ${error}`);
31
+ // }
32
+ // }
33
33
 
34
- // param:
35
- // u8:1
36
- // u16:1
37
- // u32:1
38
- // u64:1
39
- // u128:1
40
- // u256:1
41
- // object:0x1
42
- // address:0x1
43
- // bool:true
44
- // string:"hello"
45
- function formatBCS(tx: Transaction, param: string) {
46
- const [type, value] = param.split(':');
47
- switch (type) {
48
- case 'u8':
49
- return tx.pure.u8(parseInt(value));
50
- case 'u16':
51
- return tx.pure.u16(parseInt(value));
52
- case 'u32':
53
- return tx.pure.u32(parseInt(value));
54
- case 'u64':
55
- return tx.pure.u64(parseInt(value));
56
- case 'u128':
57
- return tx.pure.u128(parseInt(value));
58
- case 'u256':
59
- return tx.pure.u256(parseInt(value));
60
- case 'object':
61
- return tx.object(value);
62
- case 'address':
63
- return tx.pure.address(value);
64
- case 'bool':
65
- return tx.pure.bool(value === 'true');
66
- case 'string':
67
- return tx.pure.string(value);
68
- default:
69
- throw new Error(`Invalid param type: ${type}`);
70
- }
71
- }
34
+ // // param:
35
+ // // u8:1
36
+ // // u16:1
37
+ // // u32:1
38
+ // // u64:1
39
+ // // u128:1
40
+ // // u256:1
41
+ // // object:0x1
42
+ // // address:0x1
43
+ // // bool:true
44
+ // // string:"hello"
45
+ // function formatBCS(tx: Transaction, param: string) {
46
+ // const [type, value] = param.split(':');
47
+ // switch (type) {
48
+ // case 'u8':
49
+ // return tx.pure.u8(parseInt(value));
50
+ // case 'u16':
51
+ // return tx.pure.u16(parseInt(value));
52
+ // case 'u32':
53
+ // return tx.pure.u32(parseInt(value));
54
+ // case 'u64':
55
+ // return tx.pure.u64(parseInt(value));
56
+ // case 'u128':
57
+ // return tx.pure.u128(parseInt(value));
58
+ // case 'u256':
59
+ // return tx.pure.u256(parseInt(value));
60
+ // case 'object':
61
+ // return tx.object(value);
62
+ // case 'address':
63
+ // return tx.pure.address(value);
64
+ // case 'bool':
65
+ // return tx.pure.bool(value === 'true');
66
+ // case 'string':
67
+ // return tx.pure.string(value);
68
+ // default:
69
+ // throw new Error(`Invalid param type: ${type}`);
70
+ // }
71
+ // }
72
72
 
73
- function formatBCSParams(tx: Transaction, params: any[]) {
74
- return params.map((param) => formatBCS(tx, param));
75
- }
73
+ // function formatBCSParams(tx: Transaction, params: any[]) {
74
+ // return params.map((param) => formatBCS(tx, param));
75
+ // }
76
76
 
77
- export async function callHandler({
78
- dubheConfig,
79
- moduleName,
80
- funcName,
81
- params,
82
- network,
83
- packageId,
84
- metadataFilePath
85
- }: {
86
- dubheConfig: DubheConfig;
87
- moduleName: string;
88
- funcName: string;
89
- params?: any[];
90
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
91
- packageId?: string;
92
- metadataFilePath?: string;
93
- }) {
94
- const path = process.cwd();
95
- const projectPath = `${path}/src/${dubheConfig.name}`;
77
+ // export async function callHandler({
78
+ // dubheConfig,
79
+ // moduleName,
80
+ // funcName,
81
+ // params,
82
+ // network,
83
+ // packageId,
84
+ // metadataFilePath
85
+ // }: {
86
+ // dubheConfig: DubheConfig;
87
+ // moduleName: string;
88
+ // funcName: string;
89
+ // params?: any[];
90
+ // network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
91
+ // packageId?: string;
92
+ // metadataFilePath?: string;
93
+ // }) {
94
+ // const path = process.cwd();
95
+ // const projectPath = `${path}/src/${dubheConfig.name}`;
96
96
 
97
- packageId = packageId || (await getOldPackageId(projectPath, network));
97
+ // packageId = packageId || (await getOldPackageId(projectPath, network));
98
98
 
99
- // objectId = objectId || (await getObjectId(projectPath, network, schema));
99
+ // // objectId = objectId || (await getObjectId(projectPath, network, schema));
100
100
 
101
- let metadata;
102
- if (metadataFilePath) {
103
- metadata = await loadMetadataFromFile(metadataFilePath);
104
- } else {
105
- metadata = await loadMetadata(network, packageId);
106
- }
107
- if (!metadata) {
108
- throw new DubheCliError(
109
- `Metadata file not found. Please provide a metadata file path or set the packageId.`
110
- );
111
- }
101
+ // let metadata;
102
+ // if (metadataFilePath) {
103
+ // metadata = await loadMetadataFromFile(metadataFilePath);
104
+ // } else {
105
+ // metadata = await loadMetadata(network, packageId);
106
+ // }
107
+ // if (!metadata) {
108
+ // throw new DubheCliError(
109
+ // `Metadata file not found. Please provide a metadata file path or set the packageId.`
110
+ // );
111
+ // }
112
112
 
113
- const processedParams = params || [];
114
- validateParams(processedParams);
115
- const dubhe = initializeDubhe({
116
- network,
117
- packageId,
118
- metadata
119
- });
120
- const tx = new Transaction();
121
- const formattedParams = formatBCSParams(tx, processedParams);
113
+ // const processedParams = params || [];
114
+ // validateParams(processedParams);
115
+ // const dubhe = initializeDubhe({
116
+ // network,
117
+ // packageId,
118
+ // metadata
119
+ // });
120
+ // const tx = new Transaction();
121
+ // const formattedParams = formatBCSParams(tx, processedParams);
122
122
 
123
- const result = (await dubhe.tx[moduleName][funcName]({
124
- tx,
125
- params: formattedParams
126
- })) as TransactionResult;
123
+ // const result = (await dubhe.tx[moduleName][funcName]({
124
+ // tx,
125
+ // params: formattedParams
126
+ // })) as TransactionResult;
127
127
 
128
- console.log(JSON.stringify(result, null, 2));
129
- }
128
+ // console.log(JSON.stringify(result, null, 2));
129
+ // }
@@ -4,10 +4,14 @@ import { initializeDubhe } from './utils';
4
4
  import { DubheCliError } from './errors';
5
5
  dotenv.config();
6
6
 
7
- export async function checkBalanceHandler(network: 'mainnet' | 'testnet' | 'devnet' | 'localnet') {
7
+ export async function checkBalanceHandler(
8
+ network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
9
+ fullnodeUrls?: string[]
10
+ ) {
8
11
  try {
9
12
  const dubhe = initializeDubhe({
10
- network
13
+ network,
14
+ fullnodeUrls
11
15
  });
12
16
 
13
17
  const balance = await dubhe.getBalance();
@@ -0,0 +1,9 @@
1
+ export {
2
+ TESTNET_DUBHE_HUB_OBJECT_ID,
3
+ TESTNET_DUBHE_FRAMEWORK_PACKAGE_ID,
4
+ MAINNET_DUBHE_HUB_OBJECT_ID,
5
+ MAINNET_DUBHE_FRAMEWORK_PACKAGE_ID
6
+ } from '@0xobelisk/sui-client';
7
+
8
+ // Keep legacy alias for backwards compatibility within this package
9
+ export { TESTNET_DUBHE_FRAMEWORK_PACKAGE_ID as TESTNET_ORIGINAL_DUBHE_PACKAGE_ID } from '@0xobelisk/sui-client';
@@ -73,7 +73,7 @@ export async function generateAccountHandler(
73
73
  console.log(chalk.blue(`Using existing account: ${keypair.toSuiAddress()}`));
74
74
  return;
75
75
  }
76
- } catch (error) {
76
+ } catch (_error) {
77
77
  // .env file doesn't exist or failed to read, continue to generate new account
78
78
  }
79
79
 
@@ -1,7 +1,8 @@
1
1
  export * from './publishHandler';
2
- // export * from "./upgradeHandler";
2
+ export * from './upgradeHandler';
3
3
  export * from './errors';
4
4
  export * from './printDubhe';
5
5
  export * from './utils';
6
- export * from './queryStorage';
7
- export * from './callHandler';
6
+ export * from './constants';
7
+ // export * from './queryStorage';
8
+ // export * from './callHandler';
@@ -0,0 +1,17 @@
1
+ import { DubheConfig } from '@0xobelisk/sui-common';
2
+ import { getOldPackageId, saveMetadata } from './utils';
3
+
4
+ export async function loadMetadataHandler(
5
+ dubheConfig: DubheConfig,
6
+ network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
7
+ packageId?: string,
8
+ fullnodeUrls?: string[]
9
+ ) {
10
+ if (packageId) {
11
+ await saveMetadata(dubheConfig.name, network, packageId, fullnodeUrls);
12
+ } else {
13
+ const projectPath = `${process.cwd()}/src/${dubheConfig.name}`;
14
+ const packageId = await getOldPackageId(projectPath, network);
15
+ await saveMetadata(dubheConfig.name, network, packageId, fullnodeUrls);
16
+ }
17
+ }