@0xobelisk/sui-cli 1.2.0-pre.10 → 1.2.0-pre.100

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 (38) hide show
  1. package/README.md +3 -3
  2. package/dist/dubhe.js +126 -49
  3. package/dist/dubhe.js.map +1 -1
  4. package/package.json +31 -19
  5. package/src/commands/build.ts +47 -16
  6. package/src/commands/call.ts +83 -83
  7. package/src/commands/checkBalance.ts +12 -5
  8. package/src/commands/configStore.ts +12 -4
  9. package/src/commands/convertJson.ts +70 -0
  10. package/src/commands/doctor.ts +1515 -0
  11. package/src/commands/faucet.ts +11 -7
  12. package/src/commands/generateKey.ts +3 -2
  13. package/src/commands/index.ts +16 -7
  14. package/src/commands/info.ts +55 -0
  15. package/src/commands/loadMetadata.ts +57 -0
  16. package/src/commands/localnode.ts +22 -6
  17. package/src/commands/publish.ts +21 -7
  18. package/src/commands/query.ts +101 -101
  19. package/src/commands/schemagen.ts +15 -4
  20. package/src/commands/shell.ts +198 -0
  21. package/src/commands/switchEnv.ts +26 -0
  22. package/src/commands/test.ts +54 -11
  23. package/src/commands/upgrade.ts +11 -4
  24. package/src/commands/wait.ts +333 -22
  25. package/src/commands/watch.ts +2 -1
  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/constants.ts +5 -0
  30. package/src/utils/generateAccount.ts +1 -1
  31. package/src/utils/index.ts +4 -3
  32. package/src/utils/metadataHandler.ts +16 -0
  33. package/src/utils/publishHandler.ts +288 -292
  34. package/src/utils/queryStorage.ts +141 -141
  35. package/src/utils/startNode.ts +115 -16
  36. package/src/utils/storeConfig.ts +6 -12
  37. package/src/utils/upgradeHandler.ts +147 -86
  38. package/src/utils/utils.ts +771 -55
@@ -3,19 +3,46 @@ import { execSync } from 'child_process';
3
3
  import chalk from 'chalk';
4
4
  import {
5
5
  saveContractData,
6
- updateDubheDependency,
6
+ updateMoveTomlAddress,
7
7
  switchEnv,
8
8
  delay,
9
- getDubheSchemaId,
10
- initializeDubhe
9
+ getDubheDappHub,
10
+ initializeDubhe,
11
+ saveMetadata,
12
+ getOriginalDubhePackageId,
13
+ updatePublishedToml,
14
+ updateEphemeralPubFile,
15
+ getEphemeralPubFilePath
11
16
  } from './utils';
12
17
  import { DubheConfig } from '@0xobelisk/sui-common';
13
18
  import * as fs from 'fs';
14
19
  import * as path from 'path';
15
20
 
16
- const MAX_RETRIES = 60; // 60s timeout
17
- const RETRY_INTERVAL = 1000; // 1s retry interval
18
- const SPINNER = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
21
+ /**
22
+ * Temporarily add localnet to Move.toml [environments] section before building.
23
+ * Sui CLI 1.40+ requires the active environment to be declared in Move.toml even
24
+ * when --build-env is specified. This patches the file and returns the original
25
+ * content so the caller can restore it in a finally block.
26
+ * Returns null if no changes were needed.
27
+ */
28
+ function patchMoveTomlWithLocalnetEnv(moveTomlPath: string, chainId: string): string | null {
29
+ if (!fs.existsSync(moveTomlPath)) return null;
30
+ const content = fs.readFileSync(moveTomlPath, 'utf-8');
31
+
32
+ if (content.includes('localnet')) {
33
+ return null;
34
+ }
35
+
36
+ let updatedContent: string;
37
+ if (content.includes('[environments]')) {
38
+ updatedContent = content.replace('[environments]', `[environments]\nlocalnet = "${chainId}"`);
39
+ } else {
40
+ updatedContent = content.trimEnd() + `\n\n[environments]\nlocalnet = "${chainId}"\n`;
41
+ }
42
+
43
+ fs.writeFileSync(moveTomlPath, updatedContent, 'utf-8');
44
+ return content;
45
+ }
19
46
 
20
47
  async function removeEnvContent(
21
48
  filePath: string,
@@ -127,21 +154,53 @@ published-version = "${config.publishedVersion}"
127
154
  // return segments.length > 0 ? segments[segments.length - 1] : '';
128
155
  // }
129
156
 
130
- function buildContract(projectPath: string): string[][] {
157
+ /**
158
+ * Build a Move package and return [modules, dependencies] as base64 arrays.
159
+ *
160
+ * For localnet (ephemeral) networks:
161
+ * - Uses --build-env testnet so dependency addresses are resolved via testnet
162
+ * Published.toml (no need to add 'localnet' to Move.toml [environments]).
163
+ * - Optionally reads a Pub.localnet.toml pubfile for already-published local deps.
164
+ * - This matches the Sui docs approach:
165
+ * https://docs.sui.io/guides/developer/packages/move-package-management
166
+ *
167
+ * For persistent networks (testnet/mainnet/devnet): uses -e <network> as before.
168
+ */
169
+ function buildContract(
170
+ projectPath: string,
171
+ network?: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
172
+ pubfilePath?: string
173
+ ): string[][] {
131
174
  let modules: any, dependencies: any;
132
175
  try {
176
+ let buildEnvFlag: string;
177
+ if (network === 'localnet') {
178
+ // Ephemeral approach: resolve deps via testnet configuration.
179
+ // --pubfile-path supplies already-published local dep addresses (e.g. dubhe).
180
+ buildEnvFlag = ' --build-env testnet';
181
+ if (pubfilePath) {
182
+ buildEnvFlag += ` --pubfile-path ${pubfilePath}`;
183
+ }
184
+ } else {
185
+ buildEnvFlag = network ? ` -e ${network}` : '';
186
+ }
187
+
188
+ // --no-tree-shaking avoids on-chain RPC calls during build.
133
189
  const buildResult = JSON.parse(
134
- execSync(`sui move build --dump-bytecode-as-base64 --path ${projectPath}`, {
135
- encoding: 'utf-8',
136
- stdio: 'pipe'
137
- })
190
+ execSync(
191
+ `sui move build --dump-bytecode-as-base64 --no-tree-shaking${buildEnvFlag} --path ${projectPath}`,
192
+ {
193
+ encoding: 'utf-8',
194
+ stdio: 'pipe'
195
+ }
196
+ )
138
197
  );
139
198
  modules = buildResult.modules;
140
199
  dependencies = buildResult.dependencies;
141
200
  } catch (error: any) {
142
201
  console.error(chalk.red(' └─ Build failed'));
143
- console.error(error.stdout);
144
- process.exit(1);
202
+ console.error(error.stdout || error.stderr);
203
+ throw new Error(`Build failed: ${error.stdout || error.stderr || error.message}`);
145
204
  }
146
205
  return [modules, dependencies];
147
206
  }
@@ -154,132 +213,28 @@ interface ObjectChange {
154
213
  }
155
214
 
156
215
  async function waitForNode(dubhe: Dubhe): Promise<string> {
157
- let retryCount = 0;
158
- let spinnerIndex = 0;
159
- const startTime = Date.now();
160
- let isInterrupted = false;
161
- let chainId = '';
162
- let hasShownBalanceWarning = false;
163
-
164
- const handleInterrupt = () => {
165
- isInterrupted = true;
166
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
167
- console.log('\n └─ Operation cancelled by user');
168
- process.exit(0);
169
- };
170
- process.on('SIGINT', handleInterrupt);
171
-
172
- try {
173
- // 第一阶段:等待获取 chainId
174
- while (retryCount < MAX_RETRIES && !isInterrupted && !chainId) {
175
- try {
176
- chainId = await dubhe.suiInteractor.currentClient.getChainIdentifier();
177
- } catch (error) {
178
- // 忽略错误,继续重试
179
- }
180
-
181
- if (isInterrupted) break;
182
-
183
- if (!chainId) {
184
- retryCount++;
185
- if (retryCount === MAX_RETRIES) {
186
- console.log(chalk.red(` └─ Failed to connect to node after ${MAX_RETRIES} attempts`));
187
- console.log(chalk.red(' └─ Please check if the Sui node is running.'));
188
- process.exit(1);
189
- }
190
-
191
- const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
192
- const spinner = SPINNER[spinnerIndex % SPINNER.length];
193
- spinnerIndex++;
194
-
195
- process.stdout.write(`\r ├─ ${spinner} Waiting for node... (${elapsedTime}s)`);
196
- await new Promise((resolve) => setTimeout(resolve, RETRY_INTERVAL));
197
- }
198
- }
199
-
200
- // 显示 chainId
201
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
202
- console.log(` ├─ ChainId: ${chainId}`);
203
-
204
- // 第二阶段:检查部署账户余额
205
- retryCount = 0;
206
- while (retryCount < MAX_RETRIES && !isInterrupted) {
207
- try {
208
- const address = dubhe.getAddress();
209
- const coins = await dubhe.suiInteractor.currentClient.getCoins({
210
- owner: address,
211
- coinType: '0x2::sui::SUI'
212
- });
213
-
214
- if (coins.data.length > 0) {
215
- const balance = coins.data.reduce((sum, coin) => sum + Number(coin.balance), 0);
216
- if (balance > 0) {
217
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
218
- console.log(` ├─ Deployer balance: ${balance / 10 ** 9} SUI`);
219
- return chainId;
220
- } else if (!hasShownBalanceWarning) {
221
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
222
- console.log(
223
- chalk.yellow(
224
- ` ├─ Deployer balance: 0 SUI, please ensure your account has sufficient SUI balance`
225
- )
226
- );
227
- hasShownBalanceWarning = true;
228
- }
229
- } else if (!hasShownBalanceWarning) {
230
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
231
- console.log(
232
- chalk.yellow(
233
- ` ├─ No SUI coins found in deployer account, please ensure your account has sufficient SUI balance`
234
- )
235
- );
236
- hasShownBalanceWarning = true;
237
- }
238
-
239
- retryCount++;
240
- if (retryCount === MAX_RETRIES) {
241
- console.log(
242
- chalk.red(` └─ Deployer account has no SUI balance after ${MAX_RETRIES} attempts`)
243
- );
244
- console.log(chalk.red(' └─ Please ensure your account has sufficient SUI balance.'));
245
- process.exit(1);
246
- }
247
-
248
- const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
249
- const spinner = SPINNER[spinnerIndex % SPINNER.length];
250
- spinnerIndex++;
251
-
252
- process.stdout.write(`\r ├─ ${spinner} Checking deployer balance... (${elapsedTime}s)`);
253
- await new Promise((resolve) => setTimeout(resolve, RETRY_INTERVAL));
254
- } catch (error) {
255
- if (isInterrupted) break;
256
-
257
- retryCount++;
258
- if (retryCount === MAX_RETRIES) {
259
- console.log(
260
- chalk.red(` └─ Failed to check deployer balance after ${MAX_RETRIES} attempts`)
261
- );
262
- console.log(chalk.red(' └─ Please check your account and network connection.'));
263
- process.exit(1);
264
- }
265
-
266
- const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
267
- const spinner = SPINNER[spinnerIndex % SPINNER.length];
268
- spinnerIndex++;
216
+ const chainId = await dubhe.suiInteractor.currentClient.getChainIdentifier();
217
+ console.log(` ├─ ChainId: ${chainId}`);
218
+ const address = dubhe.getAddress();
219
+ const coins = await dubhe.suiInteractor.currentClient.getCoins({
220
+ owner: address,
221
+ coinType: '0x2::sui::SUI'
222
+ });
269
223
 
270
- process.stdout.write(`\r ├─ ${spinner} Checking deployer balance... (${elapsedTime}s)`);
271
- await new Promise((resolve) => setTimeout(resolve, RETRY_INTERVAL));
272
- }
224
+ if (coins.data.length > 0) {
225
+ const balance = coins.data.reduce((sum, coin) => sum + Number(coin.balance), 0);
226
+ if (balance > 0) {
227
+ console.log(` ├─ Deployer balance: ${balance / 10 ** 9} SUI`);
228
+ return chainId;
229
+ } else {
230
+ console.log(
231
+ chalk.yellow(
232
+ ` ├─ Deployer balance: 0 SUI, please ensure your account has sufficient SUI balance`
233
+ )
234
+ );
273
235
  }
274
- } finally {
275
- process.removeListener('SIGINT', handleInterrupt);
276
236
  }
277
-
278
- if (isInterrupted) {
279
- process.exit(0);
280
- }
281
-
282
- throw new Error('Failed to connect to node');
237
+ return chainId;
283
238
  }
284
239
 
285
240
  async function publishContract(
@@ -301,7 +256,61 @@ async function publishContract(
301
256
  console.log(` └─ Account: ${dubhe.getAddress()}`);
302
257
 
303
258
  console.log('\n📦 Building Contract...');
304
- const [modules, dependencies] = buildContract(projectPath);
259
+ // For localnet: pass the ephemeral pubfile so the build system can resolve
260
+ // the dubhe dependency that was just published in publishDubheFramework().
261
+ const pubfilePath =
262
+ network === 'localnet' ? getEphemeralPubFilePath(process.cwd(), network) : undefined;
263
+
264
+ // For localnet: temporarily remove Published.toml to prevent the testnet address
265
+ // (if it exists) from being picked up by --build-env testnet, which would cause
266
+ // PublishErrorNonZeroAddress. The pubfile supplies dubhe's localnet address instead.
267
+ const contractPublishedTomlPath = `${projectPath}/Published.toml`;
268
+ let savedContractPublishedToml: string | null = null;
269
+ if (network === 'localnet' && fs.existsSync(contractPublishedTomlPath)) {
270
+ savedContractPublishedToml = fs.readFileSync(contractPublishedTomlPath, 'utf-8');
271
+ fs.unlinkSync(contractPublishedTomlPath);
272
+ }
273
+
274
+ // For localnet: also temporarily remove dubhe's Published.toml when building the
275
+ // contract package. After publishDubheFramework restores dubhe/Published.toml, its
276
+ // testnet entry's original-id may be "0x0", which collides with the contract package's
277
+ // own address (also 0x0 before publish), triggering a spurious cyclic-dependency error.
278
+ const dubhePublishedTomlPath = path.join(path.dirname(projectPath), 'dubhe', 'Published.toml');
279
+ let savedDubhePublishedToml: string | null = null;
280
+ if (network === 'localnet' && fs.existsSync(dubhePublishedTomlPath)) {
281
+ savedDubhePublishedToml = fs.readFileSync(dubhePublishedTomlPath, 'utf-8');
282
+ fs.unlinkSync(dubhePublishedTomlPath);
283
+ }
284
+
285
+ // Sui CLI 1.40+ checks that the active environment is declared in Move.toml
286
+ // even when --build-env is specified. Temporarily inject localnet into [environments]
287
+ // for both the contract and its dubhe dependency.
288
+ const contractMoveTomlPath = `${projectPath}/Move.toml`;
289
+ const dubheMoveTomlPath = path.join(path.dirname(projectPath), 'dubhe', 'Move.toml');
290
+ let savedContractMoveToml: string | null = null;
291
+ let savedDubheMoveToml: string | null = null;
292
+ if (network === 'localnet') {
293
+ savedContractMoveToml = patchMoveTomlWithLocalnetEnv(contractMoveTomlPath, chainId);
294
+ savedDubheMoveToml = patchMoveTomlWithLocalnetEnv(dubheMoveTomlPath, chainId);
295
+ }
296
+
297
+ let modules: any, dependencies: any;
298
+ try {
299
+ [modules, dependencies] = buildContract(projectPath, network, pubfilePath);
300
+ } finally {
301
+ if (savedContractPublishedToml !== null) {
302
+ fs.writeFileSync(contractPublishedTomlPath, savedContractPublishedToml, 'utf-8');
303
+ }
304
+ if (savedDubhePublishedToml !== null) {
305
+ fs.writeFileSync(dubhePublishedTomlPath, savedDubhePublishedToml, 'utf-8');
306
+ }
307
+ if (savedContractMoveToml !== null) {
308
+ fs.writeFileSync(contractMoveTomlPath, savedContractMoveToml, 'utf-8');
309
+ }
310
+ if (savedDubheMoveToml !== null) {
311
+ fs.writeFileSync(dubheMoveTomlPath, savedDubheMoveToml, 'utf-8');
312
+ }
313
+ }
305
314
 
306
315
  console.log('\n🔄 Publishing Contract...');
307
316
  const tx = new Transaction();
@@ -311,63 +320,28 @@ async function publishContract(
311
320
  const [upgradeCap] = tx.publish({ modules, dependencies });
312
321
  tx.transferObjects([upgradeCap], dubhe.getAddress());
313
322
 
314
- let result: any = null;
315
- let retryCount = 0;
316
- let spinnerIndex = 0;
317
- const startTime = Date.now();
318
- let isInterrupted = false;
319
-
320
- const handleInterrupt = () => {
321
- isInterrupted = true;
322
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
323
- console.log('\n └─ Operation cancelled by user');
324
- process.exit(0);
325
- };
326
- process.on('SIGINT', handleInterrupt);
327
-
323
+ let result;
328
324
  try {
329
- while (retryCount < MAX_RETRIES && !result && !isInterrupted) {
330
- try {
331
- result = await dubhe.signAndSendTxn({ tx });
332
- } catch (error) {
333
- if (isInterrupted) break;
334
-
335
- retryCount++;
336
- if (retryCount === MAX_RETRIES) {
337
- console.log(chalk.red(` └─ Publication failed after ${MAX_RETRIES} attempts`));
338
- console.log(chalk.red(' └─ Please check your network connection and try again later.'));
339
- process.exit(1);
340
- }
341
-
342
- const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
343
- const spinner = SPINNER[spinnerIndex % SPINNER.length];
344
- spinnerIndex++;
345
-
346
- process.stdout.write(`\r ├─ ${spinner} Retrying... (${elapsedTime}s)`);
347
- await new Promise((resolve) => setTimeout(resolve, RETRY_INTERVAL));
348
- }
349
- }
350
- } finally {
351
- process.removeListener('SIGINT', handleInterrupt);
352
- }
353
-
354
- if (isInterrupted) {
355
- process.exit(0);
325
+ result = await dubhe.signAndSendTxn({ tx });
326
+ } catch (error: any) {
327
+ console.error(chalk.red(' └─ Publication failed'));
328
+ console.error(error.message);
329
+ throw new Error(`Contract publication failed: ${error.message}`);
356
330
  }
357
331
 
358
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
359
-
360
332
  if (!result || result.effects?.status.status === 'failure') {
361
- console.log(chalk.red(' └─ Publication failed'));
362
- process.exit(1);
333
+ throw new Error('Contract publication transaction failed');
363
334
  }
364
335
 
365
336
  console.log(' ├─ Processing publication results...');
366
337
  let version = 1;
367
338
  let packageId = '';
368
- let schemaId = '';
369
- let schemas = dubheConfig.schemas;
339
+ let dappHub = '';
340
+ let components = dubheConfig.components;
341
+ let resources = dubheConfig.resources;
342
+ let enums = dubheConfig.enums;
370
343
  let upgradeCapId = '';
344
+ let startCheckpoint = '';
371
345
 
372
346
  let printObjects: any[] = [];
373
347
 
@@ -384,6 +358,13 @@ async function publishContract(
384
358
  console.log(` ├─ Upgrade Cap: ${object.objectId}`);
385
359
  upgradeCapId = object.objectId || '';
386
360
  }
361
+ if (
362
+ object.type === 'created' &&
363
+ object.objectType &&
364
+ object.objectType.includes('dapp_service::DappHub')
365
+ ) {
366
+ dappHub = object.objectId || '';
367
+ }
387
368
  if (object.type === 'created') {
388
369
  printObjects.push(object);
389
370
  }
@@ -392,19 +373,20 @@ async function publishContract(
392
373
  console.log(` └─ Transaction: ${result.digest}`);
393
374
 
394
375
  updateEnvFile(`${projectPath}/Move.lock`, network, 'publish', chainId, packageId);
376
+ updatePublishedToml(projectPath, network, chainId, packageId, packageId, 1);
395
377
 
396
378
  console.log('\n⚡ Executing Deploy Hook...');
397
379
  await delay(5000);
398
380
 
381
+ startCheckpoint = await dubhe.suiInteractor.currentClient.getLatestCheckpointSequenceNumber();
382
+
399
383
  const deployHookTx = new Transaction();
400
384
  let args = [];
401
- if (dubheConfig.name !== 'dubhe') {
402
- let dubheSchemaId = await getDubheSchemaId(network);
403
- args.push(deployHookTx.object(dubheSchemaId));
404
- }
385
+ let dubheDappHub = dubheConfig.name === 'dubhe' ? dappHub : await getDubheDappHub(network);
386
+ args.push(deployHookTx.object(dubheDappHub));
405
387
  args.push(deployHookTx.object('0x6'));
406
388
  deployHookTx.moveCall({
407
- target: `${packageId}::${dubheConfig.name}_genesis::run`,
389
+ target: `${packageId}::genesis::run`,
408
390
  arguments: args
409
391
  });
410
392
 
@@ -414,7 +396,7 @@ async function publishContract(
414
396
  } catch (error: any) {
415
397
  console.error(chalk.red(' └─ Deploy hook execution failed'));
416
398
  console.error(error.message);
417
- process.exit(1);
399
+ throw new Error(`genesis::run failed: ${error.message}`);
418
400
  }
419
401
 
420
402
  if (deployHookResult.effects?.status.status === 'success') {
@@ -422,45 +404,38 @@ async function publishContract(
422
404
  console.log(` ├─ Transaction: ${deployHookResult.digest}`);
423
405
 
424
406
  console.log('\n📋 Created Objects:');
425
- deployHookResult.objectChanges?.map((object: ObjectChange) => {
426
- if (
427
- object.type === 'created' &&
428
- object.objectType &&
429
- object.objectType.includes('schema::Schema')
430
- ) {
431
- schemaId = object.objectId || '';
432
- }
433
- if (
434
- object.type === 'created' &&
435
- object.objectType &&
436
- object.objectType.includes('schema') &&
437
- !object.objectType.includes('dynamic_field')
438
- ) {
439
- printObjects.push(object);
440
- }
441
- });
442
-
443
407
  printObjects.map((object: ObjectChange) => {
444
- console.log(` ├─ Type: ${object.objectType}`);
445
- console.log(` └─ ID: ${object.objectId}`);
408
+ console.log(` ├─ ID: ${object.objectId}`);
409
+ console.log(` └─ Type: ${object.objectType}`);
446
410
  });
447
411
 
448
- saveContractData(
412
+ await saveContractData(
449
413
  dubheConfig.name,
450
414
  network,
415
+ startCheckpoint,
451
416
  packageId,
452
- schemaId,
417
+ dubheDappHub,
453
418
  upgradeCapId,
454
419
  version,
455
- schemas
420
+ components,
421
+ resources,
422
+ enums
456
423
  );
424
+
425
+ await saveMetadata(dubheConfig.name, network, packageId);
426
+
427
+ // Insert package id to dubhe config
428
+ let config = JSON.parse(fs.readFileSync(`${process.cwd()}/dubhe.config.json`, 'utf-8'));
429
+ config.original_package_id = packageId;
430
+ config.dubhe_object_id = dubheDappHub;
431
+ config.original_dubhe_package_id = await getOriginalDubhePackageId(network);
432
+ config.start_checkpoint = startCheckpoint;
433
+
434
+ fs.writeFileSync(`${process.cwd()}/dubhe.config.json`, JSON.stringify(config, null, 2));
435
+
457
436
  console.log('\n✅ Contract Publication Complete\n');
458
437
  } else {
459
- console.log(chalk.yellow(' └─ Deploy hook execution failed'));
460
- console.log(chalk.yellow(' Please republish or manually call deploy_hook::run'));
461
- console.log(chalk.yellow(' Please check the transaction digest:'));
462
- console.log(chalk.yellow(` ${deployHookResult.digest}`));
463
- process.exit(1);
438
+ throw new Error(`genesis::run transaction failed. Digest: ${deployHookResult.digest}`);
464
439
  }
465
440
  }
466
441
 
@@ -469,15 +444,13 @@ async function checkDubheFramework(projectPath: string): Promise<boolean> {
469
444
  console.log(chalk.yellow('\nℹ️ Dubhe Framework Files Not Found'));
470
445
  console.log(chalk.yellow(' ├─ Expected Path:'), projectPath);
471
446
  console.log(chalk.yellow(' ├─ To set up Dubhe Framework:'));
472
- console.log(chalk.yellow(' │ 1. Create directory: mkdir -p contracts/dubhe-framework'));
447
+ console.log(chalk.yellow(' │ 1. Create directory: mkdir -p contracts/dubhe'));
473
448
  console.log(
474
449
  chalk.yellow(
475
- ' │ 2. Clone repository: git clone https://github.com/0xobelisk/dubhe-framework contracts/dubhe-framework'
450
+ ' │ 2. Clone repository: git clone https://github.com/0xobelisk/dubhe contracts/dubhe'
476
451
  )
477
452
  );
478
- console.log(
479
- chalk.yellow(' │ 3. Or download from: https://github.com/0xobelisk/dubhe-framework')
480
- );
453
+ console.log(chalk.yellow(' │ 3. Or download from: https://github.com/0xobelisk/dubhe'));
481
454
  console.log(chalk.yellow(' └─ After setup, restart the local node'));
482
455
  return false;
483
456
  }
@@ -488,8 +461,8 @@ export async function publishDubheFramework(
488
461
  dubhe: Dubhe,
489
462
  network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
490
463
  ) {
491
- const path = process.cwd();
492
- const projectPath = `${path}/contracts/dubhe-framework`;
464
+ const cwd = process.cwd();
465
+ const projectPath = `${cwd}/src/dubhe`;
493
466
 
494
467
  if (!(await checkDubheFramework(projectPath))) {
495
468
  return;
@@ -501,66 +474,66 @@ export async function publishDubheFramework(
501
474
  const chainId = await waitForNode(dubhe);
502
475
 
503
476
  await removeEnvContent(`${projectPath}/Move.lock`, network);
504
- const [modules, dependencies] = buildContract(projectPath);
505
- const tx = new Transaction();
506
- const [upgradeCap] = tx.publish({ modules, dependencies });
507
- tx.transferObjects([upgradeCap], dubhe.getAddress());
477
+ await updateMoveTomlAddress(projectPath, '0x0');
478
+
479
+ const startCheckpoint =
480
+ await dubhe.suiInteractor.currentClient.getLatestCheckpointSequenceNumber();
481
+
482
+ // For localnet: --build-env testnet is used to resolve git dependencies, but the
483
+ // Move CLI will also read Published.toml and use any existing testnet address for
484
+ // dubhe — causing PublishErrorNonZeroAddress if a testnet entry already exists.
485
+ // Fix: temporarily remove Published.toml before the build, then restore it.
486
+ // This ensures the dubhe package compiles with address 0x0 (from Move.toml).
487
+ const publishedTomlPath = `${projectPath}/Published.toml`;
488
+ let savedPublishedTomlContent: string | null = null;
489
+ if (network === 'localnet' && fs.existsSync(publishedTomlPath)) {
490
+ savedPublishedTomlContent = fs.readFileSync(publishedTomlPath, 'utf-8');
491
+ fs.unlinkSync(publishedTomlPath);
492
+ }
508
493
 
509
- let result: any = null;
510
- let retryCount = 0;
511
- let spinnerIndex = 0;
512
- const startTime = Date.now();
513
- let isInterrupted = false;
514
-
515
- const handleInterrupt = () => {
516
- isInterrupted = true;
517
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
518
- console.log('\n └─ Operation cancelled by user');
519
- process.exit(0);
520
- };
521
- process.on('SIGINT', handleInterrupt);
494
+ // Sui CLI 1.40+ checks that the active environment is declared in Move.toml
495
+ // even when --build-env is specified. Temporarily inject localnet into [environments].
496
+ const moveTomlPath = `${projectPath}/Move.toml`;
497
+ let savedMoveTomlContent: string | null = null;
498
+ if (network === 'localnet') {
499
+ savedMoveTomlContent = patchMoveTomlWithLocalnetEnv(moveTomlPath, chainId);
500
+ }
522
501
 
502
+ let modules: any, dependencies: any;
523
503
  try {
524
- while (retryCount < MAX_RETRIES && !result && !isInterrupted) {
525
- try {
526
- result = await dubhe.signAndSendTxn({ tx });
527
- } catch (error) {
528
- if (isInterrupted) break;
529
-
530
- retryCount++;
531
- if (retryCount === MAX_RETRIES) {
532
- console.log(chalk.red(` └─ Publication failed after ${MAX_RETRIES} attempts`));
533
- console.log(chalk.red(' └─ Please check your network connection and try again later.'));
534
- process.exit(1);
535
- }
536
-
537
- const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
538
- const spinner = SPINNER[spinnerIndex % SPINNER.length];
539
- spinnerIndex++;
540
-
541
- process.stdout.write(`\r ├─ ${spinner} Retrying... (${elapsedTime}s)`);
542
- await new Promise((resolve) => setTimeout(resolve, RETRY_INTERVAL));
543
- }
544
- }
504
+ // For localnet: use --build-env testnet (no pubfile needed dubhe has no local deps).
505
+ // For testnet/mainnet: use -e <network> as usual.
506
+ [modules, dependencies] = buildContract(projectPath, network);
545
507
  } finally {
546
- process.removeListener('SIGINT', handleInterrupt);
508
+ // Always restore Published.toml and Move.toml (successful build or error)
509
+ if (savedPublishedTomlContent !== null) {
510
+ fs.writeFileSync(publishedTomlPath, savedPublishedTomlContent, 'utf-8');
511
+ }
512
+ if (savedMoveTomlContent !== null) {
513
+ fs.writeFileSync(moveTomlPath, savedMoveTomlContent, 'utf-8');
514
+ }
547
515
  }
548
516
 
549
- if (isInterrupted) {
550
- process.exit(0);
551
- }
517
+ const tx = new Transaction();
518
+ const [upgradeCap] = tx.publish({ modules, dependencies });
519
+ tx.transferObjects([upgradeCap], dubhe.getAddress());
552
520
 
553
- process.stdout.write('\r' + ' '.repeat(50) + '\r');
521
+ let result;
522
+ try {
523
+ result = await dubhe.signAndSendTxn({ tx });
524
+ } catch (error: any) {
525
+ console.error(chalk.red(' └─ Publication failed'));
526
+ console.error(error.message);
527
+ throw new Error(`Dubhe framework publication failed: ${error.message}`);
528
+ }
554
529
 
555
530
  if (!result || result.effects?.status.status === 'failure') {
556
- console.log(chalk.red(' └─ Publication failed'));
557
- process.exit(1);
531
+ throw new Error('Dubhe framework publication transaction failed');
558
532
  }
559
533
 
560
534
  let version = 1;
561
535
  let packageId = '';
562
- let schemaId = '';
563
- let schemas: Record<string, string> = {};
536
+ let dappHub = '';
564
537
  let upgradeCapId = '';
565
538
 
566
539
  result.objectChanges!.map((object: ObjectChange) => {
@@ -574,14 +547,20 @@ export async function publishDubheFramework(
574
547
  ) {
575
548
  upgradeCapId = object.objectId || '';
576
549
  }
550
+ if (
551
+ object.type === 'created' &&
552
+ object.objectType &&
553
+ object.objectType.includes('dapp_service::DappHub')
554
+ ) {
555
+ dappHub = object.objectId || '';
556
+ }
577
557
  });
578
558
 
579
559
  await delay(3000);
580
-
581
560
  const deployHookTx = new Transaction();
582
561
  deployHookTx.moveCall({
583
- target: `${packageId}::dubhe_genesis::run`,
584
- arguments: [deployHookTx.object('0x6')]
562
+ target: `${packageId}::genesis::run`,
563
+ arguments: [deployHookTx.object(dappHub), deployHookTx.object('0x6')]
585
564
  });
586
565
 
587
566
  let deployHookResult;
@@ -590,30 +569,47 @@ export async function publishDubheFramework(
590
569
  } catch (error: any) {
591
570
  console.error(chalk.red(' └─ Deploy hook execution failed'));
592
571
  console.error(error.message);
593
- process.exit(1);
572
+ throw new Error(`Dubhe genesis::run failed: ${error.message}`);
594
573
  }
595
574
 
596
- if (deployHookResult.effects?.status.status === 'success') {
597
- deployHookResult.objectChanges?.map((object: ObjectChange) => {
598
- if (
599
- object.type === 'created' &&
600
- object.objectType &&
601
- object.objectType.includes('dubhe_schema::Schema')
602
- ) {
603
- schemaId = object.objectId || '';
604
- }
605
- });
575
+ if (deployHookResult.effects?.status.status !== 'success') {
576
+ throw new Error('Deploy hook execution failed');
606
577
  }
607
578
 
608
- saveContractData('dubhe-framework', network, packageId, schemaId, upgradeCapId, version, schemas);
579
+ await updateMoveTomlAddress(projectPath, packageId);
580
+ await saveContractData(
581
+ 'dubhe',
582
+ network,
583
+ startCheckpoint,
584
+ packageId,
585
+ dappHub,
586
+ upgradeCapId,
587
+ version,
588
+ {},
589
+ {},
590
+ {}
591
+ );
609
592
 
610
593
  updateEnvFile(`${projectPath}/Move.lock`, network, 'publish', chainId, packageId);
611
- await delay(1000);
594
+ updatePublishedToml(projectPath, network, chainId, packageId, packageId, 1);
595
+
596
+ // For localnet: write dubhe's published address to Pub.localnet.toml so that
597
+ // the counter package build (next step) can resolve the dubhe dependency.
598
+ if (network === 'localnet') {
599
+ const pubfilePath = getEphemeralPubFilePath(cwd, network);
600
+ updateEphemeralPubFile(pubfilePath, chainId, 'testnet', {
601
+ source: projectPath,
602
+ publishedAt: packageId,
603
+ originalId: packageId,
604
+ upgradeCap: upgradeCapId
605
+ });
606
+ }
612
607
  }
613
608
 
614
609
  export async function publishHandler(
615
610
  dubheConfig: DubheConfig,
616
611
  network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
612
+ _force: boolean,
617
613
  gasBudget?: number
618
614
  ) {
619
615
  await switchEnv(network);
@@ -622,12 +618,12 @@ export async function publishHandler(
622
618
  network
623
619
  });
624
620
 
625
- if (network === 'localnet') {
621
+ const path = process.cwd();
622
+ const projectPath = `${path}/src/${dubheConfig.name}`;
623
+
624
+ if (network === 'localnet' && dubheConfig.name !== 'dubhe') {
626
625
  await publishDubheFramework(dubhe, network);
627
626
  }
628
627
 
629
- const path = process.cwd();
630
- const projectPath = `${path}/contracts/${dubheConfig.name}`;
631
- await updateDubheDependency(`${projectPath}/Move.toml`, network);
632
628
  await publishContract(dubhe, dubheConfig, network, projectPath, gasBudget);
633
629
  }