@0xobelisk/sui-cli 1.1.5 → 1.1.7

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.
@@ -8,221 +8,195 @@ import chalk from 'chalk';
8
8
  import { spawn } from 'child_process';
9
9
 
10
10
  export type DeploymentJsonType = {
11
- projectName: string;
12
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
13
- packageId: string;
14
- schemaId: string;
15
- upgradeCap: string;
16
- version: number;
17
- schemas: Record<string, string>;
11
+ projectName: string;
12
+ network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
13
+ packageId: string;
14
+ schemaId: string;
15
+ upgradeCap: string;
16
+ version: number;
17
+ schemas: Record<string, string>;
18
18
  };
19
19
 
20
20
  export function validatePrivateKey(privateKey: string): false | string {
21
- if (privateKey.startsWith(SUI_PRIVATE_KEY_PREFIX)) {
22
- if (privateKey.length === 70) {
23
- return privateKey;
24
- } else {
25
- return false;
26
- }
27
- } else if (privateKey.startsWith('0x')) {
28
- const strippedPrivateKey = privateKey.slice(2);
29
- if (strippedPrivateKey.length === 64) {
30
- return strippedPrivateKey;
31
- } else {
32
- return false;
33
- }
34
- } else {
35
- if (privateKey.length === 64) {
36
- return privateKey;
37
- } else {
38
- return false;
39
- }
40
- }
21
+ if (privateKey.startsWith(SUI_PRIVATE_KEY_PREFIX)) {
22
+ if (privateKey.length === 70) {
23
+ return privateKey;
24
+ } else {
25
+ return false;
26
+ }
27
+ } else if (privateKey.startsWith('0x')) {
28
+ const strippedPrivateKey = privateKey.slice(2);
29
+ if (strippedPrivateKey.length === 64) {
30
+ return strippedPrivateKey;
31
+ } else {
32
+ return false;
33
+ }
34
+ } else {
35
+ if (privateKey.length === 64) {
36
+ return privateKey;
37
+ } else {
38
+ return false;
39
+ }
40
+ }
41
41
  }
42
42
 
43
- export async function updateVersionInFile(
44
- projectPath: string,
45
- newVersion: string
46
- ) {
47
- try {
48
- const filePath = `${projectPath}/sources/script/migrate.move`;
49
- const data = await fsAsync.readFile(filePath, 'utf8');
50
-
51
- // update version data
52
- const updatedData = data.replace(
53
- /const VERSION: u64 = \d+;/,
54
- `const VERSION: u64 = ${newVersion};`
55
- );
56
-
57
- // write new version
58
- writeOutput(updatedData, filePath, 'Update package version');
59
- } catch {
60
- throw new FsIibError('Fs update version failed.');
61
- }
43
+ export async function updateVersionInFile(projectPath: string, newVersion: string) {
44
+ try {
45
+ const filePath = `${projectPath}/sources/script/migrate.move`;
46
+ const data = await fsAsync.readFile(filePath, 'utf8');
47
+
48
+ // update version data
49
+ const updatedData = data.replace(
50
+ /const VERSION: u64 = \d+;/,
51
+ `const VERSION: u64 = ${newVersion};`
52
+ );
53
+
54
+ // write new version
55
+ writeOutput(updatedData, filePath, 'Update package version');
56
+ } catch {
57
+ throw new FsIibError('Fs update version failed.');
58
+ }
62
59
  }
63
60
 
64
61
  async function getDeploymentJson(projectPath: string, network: string) {
65
- try {
66
- const data = await fsAsync.readFile(
67
- `${projectPath}/.history/sui_${network}/latest.json`,
68
- 'utf8'
69
- );
70
- return JSON.parse(data) as DeploymentJsonType;
71
- } catch {
72
- throw new FsIibError('Fs read deployment file failed.');
73
- }
62
+ try {
63
+ const data = await fsAsync.readFile(
64
+ `${projectPath}/.history/sui_${network}/latest.json`,
65
+ 'utf8'
66
+ );
67
+ return JSON.parse(data) as DeploymentJsonType;
68
+ } catch {
69
+ throw new FsIibError('Fs read deployment file failed.');
70
+ }
74
71
  }
75
72
 
76
73
  export async function getOnchainSchemas(
77
- projectPath: string,
78
- network: string
74
+ projectPath: string,
75
+ network: string
79
76
  ): Promise<Record<string, string>> {
80
- const deployment = await getDeploymentJson(projectPath, network);
81
- return deployment.schemas;
77
+ const deployment = await getDeploymentJson(projectPath, network);
78
+ return deployment.schemas;
82
79
  }
83
80
 
84
- export async function getVersion(
85
- projectPath: string,
86
- network: string
87
- ): Promise<number> {
88
- const deployment = await getDeploymentJson(projectPath, network);
89
- return deployment.version;
81
+ export async function getVersion(projectPath: string, network: string): Promise<number> {
82
+ const deployment = await getDeploymentJson(projectPath, network);
83
+ return deployment.version;
90
84
  }
91
85
 
92
86
  export async function getNetwork(
93
- projectPath: string,
94
- network: string
87
+ projectPath: string,
88
+ network: string
95
89
  ): Promise<'mainnet' | 'testnet' | 'devnet' | 'localnet'> {
96
- const deployment = await getDeploymentJson(projectPath, network);
97
- return deployment.network;
90
+ const deployment = await getDeploymentJson(projectPath, network);
91
+ return deployment.network;
98
92
  }
99
93
 
100
- export async function getOldPackageId(
101
- projectPath: string,
102
- network: string
103
- ): Promise<string> {
104
- const deployment = await getDeploymentJson(projectPath, network);
105
- return deployment.packageId;
94
+ export async function getOldPackageId(projectPath: string, network: string): Promise<string> {
95
+ const deployment = await getDeploymentJson(projectPath, network);
96
+ return deployment.packageId;
106
97
  }
107
98
 
108
- export async function getSchemaId(
109
- projectPath: string,
110
- network: string
111
- ): Promise<string> {
112
- const deployment = await getDeploymentJson(projectPath, network);
113
- return deployment.schemaId;
99
+ export async function getSchemaId(projectPath: string, network: string): Promise<string> {
100
+ const deployment = await getDeploymentJson(projectPath, network);
101
+ return deployment.schemaId;
114
102
  }
115
103
 
116
- export async function getUpgradeCap(
117
- projectPath: string,
118
- network: string
119
- ): Promise<string> {
120
- const deployment = await getDeploymentJson(projectPath, network);
121
- return deployment.upgradeCap;
104
+ export async function getUpgradeCap(projectPath: string, network: string): Promise<string> {
105
+ const deployment = await getDeploymentJson(projectPath, network);
106
+ return deployment.upgradeCap;
122
107
  }
123
108
 
124
109
  export function saveContractData(
125
- projectName: string,
126
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
127
- packageId: string,
128
- schemaId: string,
129
- upgradeCap: string,
130
- version: number,
131
- schemas: Record<string, string>
110
+ projectName: string,
111
+ network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
112
+ packageId: string,
113
+ schemaId: string,
114
+ upgradeCap: string,
115
+ version: number,
116
+ schemas: Record<string, string>
132
117
  ) {
133
- const DeploymentData: DeploymentJsonType = {
134
- projectName,
135
- network,
136
- packageId,
137
- schemaId,
138
- schemas,
139
- upgradeCap,
140
- version,
141
- };
142
-
143
- const path = process.cwd();
144
- const storeDeploymentData = JSON.stringify(DeploymentData, null, 2);
145
- writeOutput(
146
- storeDeploymentData,
147
- `${path}/contracts/${projectName}/.history/sui_${network}/latest.json`,
148
- 'Update deploy log'
149
- );
118
+ const DeploymentData: DeploymentJsonType = {
119
+ projectName,
120
+ network,
121
+ packageId,
122
+ schemaId,
123
+ schemas,
124
+ upgradeCap,
125
+ version
126
+ };
127
+
128
+ const path = process.cwd();
129
+ const storeDeploymentData = JSON.stringify(DeploymentData, null, 2);
130
+ writeOutput(
131
+ storeDeploymentData,
132
+ `${path}/contracts/${projectName}/.history/sui_${network}/latest.json`,
133
+ 'Update deploy log'
134
+ );
150
135
  }
151
136
 
152
137
  export async function writeOutput(
153
- output: string,
154
- fullOutputPath: string,
155
- logPrefix?: string
138
+ output: string,
139
+ fullOutputPath: string,
140
+ logPrefix?: string
156
141
  ): Promise<void> {
157
- mkdirSync(dirname(fullOutputPath), { recursive: true });
142
+ mkdirSync(dirname(fullOutputPath), { recursive: true });
158
143
 
159
- writeFileSync(fullOutputPath, output);
160
- if (logPrefix !== undefined) {
161
- console.log(`${logPrefix}: ${fullOutputPath}`);
162
- }
144
+ writeFileSync(fullOutputPath, output);
145
+ if (logPrefix !== undefined) {
146
+ console.log(`${logPrefix}: ${fullOutputPath}`);
147
+ }
163
148
  }
164
149
 
165
- function getDubheDependency(
166
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
167
- ): string {
168
- switch (network) {
169
- case 'localnet':
170
- return 'Dubhe = { local = "../dubhe-framework" }';
171
- case 'testnet':
172
- return 'Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "dubhe-testnet-v1.1.0" }';
173
- case 'mainnet':
174
- return 'Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "dubhe-mainnet-v1.1.0" }';
175
- default:
176
- throw new Error(`Unsupported network: ${network}`);
177
- }
150
+ function getDubheDependency(network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'): string {
151
+ switch (network) {
152
+ case 'localnet':
153
+ return 'Dubhe = { local = "../dubhe-framework" }';
154
+ case 'testnet':
155
+ return 'Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "dubhe-testnet-v1.1.0" }';
156
+ case 'mainnet':
157
+ return 'Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "dubhe-mainnet-v1.1.0" }';
158
+ default:
159
+ throw new Error(`Unsupported network: ${network}`);
160
+ }
178
161
  }
179
162
 
180
163
  export async function updateDubheDependency(
181
- filePath: string,
182
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
164
+ filePath: string,
165
+ network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
183
166
  ) {
184
- const fileContent = fs.readFileSync(filePath, 'utf-8');
185
- const newDependency = getDubheDependency(network);
186
- const updatedContent = fileContent.replace(/Dubhe = \{.*\}/, newDependency);
187
- fs.writeFileSync(filePath, updatedContent, 'utf-8');
188
- console.log(`Updated Dubhe dependency in ${filePath} for ${network}.`);
167
+ const fileContent = fs.readFileSync(filePath, 'utf-8');
168
+ const newDependency = getDubheDependency(network);
169
+ const updatedContent = fileContent.replace(/Dubhe = \{.*\}/, newDependency);
170
+ fs.writeFileSync(filePath, updatedContent, 'utf-8');
171
+ console.log(`Updated Dubhe dependency in ${filePath} for ${network}.`);
189
172
  }
190
- export async function switchEnv(
191
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
192
- ) {
193
- try {
194
- return new Promise<void>((resolve, reject) => {
195
- const suiProcess = spawn(
196
- 'sui',
197
- ['client', 'switch', '--env', network],
198
- {
199
- env: { ...process.env },
200
- stdio: 'pipe',
201
- }
202
- );
203
-
204
- suiProcess.on('error', error => {
205
- console.error(chalk.red('\n❌ Failed to Switch Env'));
206
- console.error(chalk.red(` Error: ${error.message}`));
207
- reject(error); // Reject promise on error
208
- });
209
-
210
- suiProcess.on('exit', code => {
211
- if (code !== 0) {
212
- console.error(
213
- chalk.red(`\n❌ Process exited with code: ${code}`)
214
- );
215
- reject(new Error(`Process exited with code: ${code}`));
216
- } else {
217
- resolve(); // Resolve promise on successful exit
218
- }
219
- });
220
- });
221
- } catch (error) {
222
- console.error(chalk.red('\n❌ Failed to Switch Env'));
223
- console.error(chalk.red(` └─ Error: ${error}`));
224
- }
173
+ export async function switchEnv(network: 'mainnet' | 'testnet' | 'devnet' | 'localnet') {
174
+ try {
175
+ return new Promise<void>((resolve, reject) => {
176
+ const suiProcess = spawn('sui', ['client', 'switch', '--env', network], {
177
+ env: { ...process.env },
178
+ stdio: 'pipe'
179
+ });
180
+
181
+ suiProcess.on('error', (error) => {
182
+ console.error(chalk.red('\n❌ Failed to Switch Env'));
183
+ console.error(chalk.red(` Error: ${error.message}`));
184
+ reject(error); // Reject promise on error
185
+ });
186
+
187
+ suiProcess.on('exit', (code) => {
188
+ if (code !== 0) {
189
+ console.error(chalk.red(`\n❌ Process exited with code: ${code}`));
190
+ reject(new Error(`Process exited with code: ${code}`));
191
+ } else {
192
+ resolve(); // Resolve promise on successful exit
193
+ }
194
+ });
195
+ });
196
+ } catch (error) {
197
+ console.error(chalk.red('\n❌ Failed to Switch Env'));
198
+ console.error(chalk.red(` └─ Error: ${error}`));
199
+ }
225
200
  }
226
201
 
227
- export const delay = (ms: number) =>
228
- new Promise(resolve => setTimeout(resolve, ms));
202
+ export const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));