@0xobelisk/sui-cli 1.2.0-pre.2 → 1.2.0-pre.21

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,6 +8,7 @@ import chalk from 'chalk';
8
8
  import { spawn } from 'child_process';
9
9
  import { Dubhe, NetworkType, SuiMoveNormalizedModules } from '@0xobelisk/sui-client';
10
10
  import { DubheCliError } from './errors';
11
+ import packageJson from '../../package.json';
11
12
 
12
13
  export type DeploymentJsonType = {
13
14
  projectName: string;
@@ -90,13 +91,13 @@ export async function getDeploymentSchemaId(projectPath: string, network: string
90
91
 
91
92
  export async function getDubheSchemaId(network: string) {
92
93
  const path = process.cwd();
93
- const contractPath = `${path}/contracts/dubhe-framework`;
94
+ const contractPath = `${path}/src/dubhe`;
94
95
 
95
96
  switch (network) {
96
97
  case 'mainnet':
97
98
  return await getDeploymentSchemaId(contractPath, 'mainnet');
98
99
  case 'testnet':
99
- return '0xa565cbb3641fff8f7e8ef384b215808db5f1837aa72c1cca1803b5d973699aac';
100
+ return await getDeploymentSchemaId(contractPath, 'testnet');
100
101
  case 'devnet':
101
102
  return await getDeploymentSchemaId(contractPath, 'devnet');
102
103
  case 'localnet':
@@ -165,7 +166,7 @@ export function saveContractData(
165
166
  const storeDeploymentData = JSON.stringify(DeploymentData, null, 2);
166
167
  writeOutput(
167
168
  storeDeploymentData,
168
- `${path}/contracts/${projectName}/.history/sui_${network}/latest.json`,
169
+ `${path}/src/${projectName}/.history/sui_${network}/latest.json`,
169
170
  'Update deploy log'
170
171
  );
171
172
  }
@@ -186,11 +187,11 @@ export async function writeOutput(
186
187
  function getDubheDependency(network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'): string {
187
188
  switch (network) {
188
189
  case 'localnet':
189
- return 'Dubhe = { local = "../dubhe-framework" }';
190
+ return 'Dubhe = { local = "../dubhe" }';
190
191
  case 'testnet':
191
- return 'Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", subdir = "contracts/dubhe", rev = "develop" }';
192
+ return `Dubhe = { git = "https://github.com/0xobelisk/dubhe-wip.git", subdir = "packages/sui-framework/src/dubhe", rev = "v${packageJson.version}" }`;
192
193
  case 'mainnet':
193
- return 'Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", subdir = "contracts/dubhe", rev = "develop" }';
194
+ return `Dubhe = { git = "https://github.com/0xobelisk/dubhe-wip.git", subdir = "packages/sui-framework/src/dubhe", rev = "v${packageJson.version}" }`;
194
195
  default:
195
196
  throw new Error(`Unsupported network: ${network}`);
196
197
  }
@@ -206,32 +207,147 @@ export async function updateDubheDependency(
206
207
  fs.writeFileSync(filePath, updatedContent, 'utf-8');
207
208
  console.log(`Updated Dubhe dependency in ${filePath} for ${network}.`);
208
209
  }
210
+
211
+ async function checkRpcAvailability(rpcUrl: string): Promise<boolean> {
212
+ try {
213
+ const response = await fetch(rpcUrl, {
214
+ method: 'POST',
215
+ headers: {
216
+ 'Content-Type': 'application/json'
217
+ },
218
+ body: JSON.stringify({
219
+ jsonrpc: '2.0',
220
+ id: 1,
221
+ method: 'sui_getLatestCheckpointSequenceNumber',
222
+ params: []
223
+ })
224
+ });
225
+
226
+ if (!response.ok) {
227
+ return false;
228
+ }
229
+
230
+ const data = await response.json();
231
+ return !data.error;
232
+ } catch (error) {
233
+ return false;
234
+ }
235
+ }
236
+
237
+ export async function addEnv(
238
+ network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
239
+ ): Promise<void> {
240
+ const rpcMap = {
241
+ localnet: 'http://127.0.0.1:9000',
242
+ devnet: 'https://fullnode.devnet.sui.io:443/',
243
+ testnet: 'https://fullnode.testnet.sui.io:443/',
244
+ mainnet: 'https://fullnode.mainnet.sui.io:443/'
245
+ };
246
+
247
+ const rpcUrl = rpcMap[network];
248
+
249
+ // Check RPC availability first
250
+ const isRpcAvailable = await checkRpcAvailability(rpcUrl);
251
+ if (!isRpcAvailable) {
252
+ throw new Error(
253
+ `RPC endpoint ${rpcUrl} is not available. Please check your network connection or try again later.`
254
+ );
255
+ }
256
+
257
+ return new Promise<void>((resolve, reject) => {
258
+ let errorOutput = '';
259
+ let stdoutOutput = '';
260
+
261
+ const suiProcess = spawn(
262
+ 'sui',
263
+ ['client', 'new-env', '--alias', network, '--rpc', rpcMap[network]],
264
+ {
265
+ env: { ...process.env },
266
+ stdio: 'pipe'
267
+ }
268
+ );
269
+
270
+ // Capture standard output
271
+ suiProcess.stdout.on('data', (data) => {
272
+ stdoutOutput += data.toString();
273
+ });
274
+
275
+ // Capture error output
276
+ suiProcess.stderr.on('data', (data) => {
277
+ errorOutput += data.toString();
278
+ });
279
+
280
+ // Handle process errors (e.g., command not found)
281
+ suiProcess.on('error', (error) => {
282
+ console.error(chalk.red(`\n❌ Failed to execute sui command: ${error.message}`));
283
+ reject(new Error(`Failed to execute sui command: ${error.message}`));
284
+ });
285
+
286
+ // Handle process exit
287
+ suiProcess.on('exit', (code) => {
288
+ // Check if "already exists" message is present
289
+ if (errorOutput.includes('already exists') || stdoutOutput.includes('already exists')) {
290
+ console.log(chalk.yellow(`Environment ${network} already exists, proceeding...`));
291
+ resolve();
292
+ return;
293
+ }
294
+
295
+ if (code === 0) {
296
+ console.log(chalk.green(`Successfully added environment ${network}`));
297
+ resolve();
298
+ } else {
299
+ const finalError = errorOutput || stdoutOutput || `Process exited with code ${code}`;
300
+ console.error(chalk.red(`\n❌ Failed to add environment ${network}`));
301
+ console.error(chalk.red(` └─ ${finalError.trim()}`));
302
+ reject(new Error(finalError));
303
+ }
304
+ });
305
+ });
306
+ }
307
+
209
308
  export async function switchEnv(network: 'mainnet' | 'testnet' | 'devnet' | 'localnet') {
210
309
  try {
310
+ // First, try to add the environment
311
+ await addEnv(network);
312
+
313
+ // Then switch to the specified environment
211
314
  return new Promise<void>((resolve, reject) => {
315
+ let errorOutput = '';
316
+ let stdoutOutput = '';
317
+
212
318
  const suiProcess = spawn('sui', ['client', 'switch', '--env', network], {
213
319
  env: { ...process.env },
214
320
  stdio: 'pipe'
215
321
  });
216
322
 
323
+ suiProcess.stdout.on('data', (data) => {
324
+ stdoutOutput += data.toString();
325
+ });
326
+
327
+ suiProcess.stderr.on('data', (data) => {
328
+ errorOutput += data.toString();
329
+ });
330
+
217
331
  suiProcess.on('error', (error) => {
218
- console.error(chalk.red('\n❌ Failed to Switch Env'));
219
- console.error(chalk.red(` Error: ${error.message}`));
220
- reject(error); // Reject promise on error
332
+ console.error(chalk.red(`\n❌ Failed to execute sui command: ${error.message}`));
333
+ reject(new Error(`Failed to execute sui command: ${error.message}`));
221
334
  });
222
335
 
223
336
  suiProcess.on('exit', (code) => {
224
- if (code !== 0) {
225
- console.error(chalk.red(`\n❌ Process exited with code: ${code}`));
226
- reject(new Error(`Process exited with code: ${code}`));
337
+ if (code === 0) {
338
+ console.log(chalk.green(`Successfully switched to environment ${network}`));
339
+ resolve();
227
340
  } else {
228
- resolve(); // Resolve promise on successful exit
341
+ const finalError = errorOutput || stdoutOutput || `Process exited with code ${code}`;
342
+ console.error(chalk.red(`\n❌ Failed to switch to environment ${network}`));
343
+ console.error(chalk.red(` └─ ${finalError.trim()}`));
344
+ reject(new Error(finalError));
229
345
  }
230
346
  });
231
347
  });
232
348
  } catch (error) {
233
- console.error(chalk.red('\n❌ Failed to Switch Env'));
234
- console.error(chalk.red(` └─ Error: ${error}`));
349
+ // Re-throw the error for the caller to handle
350
+ throw error;
235
351
  }
236
352
  }
237
353