1
1
#!/usr/bin/env node
2
2
3
3
import { IDL } from '@dfinity/candid' ;
4
- import { ICManagementCanister } from '@dfinity/ic-management' ;
5
- import { fromNullable , uint8ArrayToHexString } from '@dfinity/utils' ;
4
+ import { UpgradeCodeProgressStep , upgradeModule } from '@junobuild/admin' ;
6
5
import { fileExists } from '@junobuild/cli-tools' ;
7
6
import { createHash } from 'crypto' ;
8
7
import { readFile } from 'fs/promises' ;
9
8
import { join } from 'node:path' ;
10
9
import { icAgent , localAgent } from './actor.mjs' ;
10
+ import { getIdentity } from './console.config.utils.mjs' ;
11
11
import { targetMainnet } from './utils.mjs' ;
12
12
13
13
const INSTALL_MODE_UPGRADE = {
@@ -20,46 +20,72 @@ const loadGzippedWasm = async (destination) => {
20
20
const buffer = await readFile ( destination ) ;
21
21
22
22
return {
23
- wasm : [ ... new Uint8Array ( buffer ) ] ,
23
+ wasm : buffer ,
24
24
hash : createHash ( 'sha256' ) . update ( buffer ) . digest ( 'hex' )
25
25
} ;
26
26
} ;
27
27
28
- const fnAgent = targetMainnet ( ) ? icAgent : localAgent ;
28
+ const mainnet = targetMainnet ( ) ;
29
+
30
+ const fnAgent = mainnet ? icAgent : localAgent ;
29
31
const agent = await fnAgent ( ) ;
30
32
33
+ const identity = await getIdentity ( mainnet ) ;
34
+
35
+ const onProgress = ( { step, state } ) => {
36
+ switch ( step ) {
37
+ case UpgradeCodeProgressStep . AssertingExistingCode :
38
+ console . log ( `Validating: ${ state } ` ) ;
39
+ break ;
40
+ case UpgradeCodeProgressStep . StoppingCanister :
41
+ console . log ( `Stopping: ${ state } ` ) ;
42
+ break ;
43
+ case UpgradeCodeProgressStep . TakingSnapshot :
44
+ console . log ( `Creating a snapshot: ${ state } ` ) ;
45
+ break ;
46
+ case UpgradeCodeProgressStep . UpgradingCode :
47
+ console . log ( `Upgrading: ${ state } ` ) ;
48
+ break ;
49
+ case UpgradeCodeProgressStep . RestartingCanister :
50
+ console . log ( `Restarting: ${ state } ` ) ;
51
+ break ;
52
+ }
53
+ } ;
54
+
31
55
export const upgrade = async ( { sourceFilename, canisterId } ) => {
32
56
const source = join ( target , sourceFilename ) ;
33
57
34
- console . log ( `About to upgrade module ${ canisterId } with source ${ source } ...` ) ;
58
+ console . log ( `About to upgrade module ${ canisterId . toText ( ) } with source ${ source } ...` ) ;
35
59
36
60
if ( ! ( await fileExists ( source ) ) ) {
37
61
throw new Error ( `${ source } not found.` ) ;
38
62
}
39
63
40
- const EMPTY_ARG = IDL . encode ( [ ] , [ ] ) ;
41
-
42
- const { installCode, canisterStatus } = ICManagementCanister . create ( {
43
- agent
44
- } ) ;
64
+ console . log ( '' ) ;
45
65
46
66
const { wasm, hash } = await loadGzippedWasm ( source ) ;
47
67
48
- const { module_hash } = await canisterStatus ( canisterId ) ;
49
-
50
- const currentHash = fromNullable ( module_hash ) ;
68
+ const EMPTY_ARG = IDL . encode ( [ ] , [ ] ) ;
51
69
52
- if ( uint8ArrayToHexString ( currentHash ) === hash ) {
53
- console . log ( `Module hash ${ hash } already installed.` ) ;
54
- return ;
70
+ try {
71
+ await upgradeModule ( {
72
+ actor : {
73
+ agent,
74
+ identity
75
+ } ,
76
+ mode : INSTALL_MODE_UPGRADE ,
77
+ canisterId,
78
+ wasmModule : wasm ,
79
+ arg : new Uint8Array ( EMPTY_ARG ) ,
80
+ takeSnapshot : true ,
81
+ onProgress
82
+ } ) ;
83
+
84
+ console . log ( '' ) ;
85
+ console . log ( `Module upgraded to hash ${ hash } .` ) ;
86
+ } catch ( err ) {
87
+ console . log ( '' ) ;
88
+ console . error ( 'message' in err ? err . message : err ) ;
89
+ process . exit ( 1 ) ;
55
90
}
56
-
57
- await installCode ( {
58
- mode : INSTALL_MODE_UPGRADE ,
59
- canisterId,
60
- wasmModule : wasm ,
61
- arg : new Uint8Array ( EMPTY_ARG )
62
- } ) ;
63
-
64
- console . log ( `Module upgraded to hash ${ hash } .` ) ;
65
91
} ;
0 commit comments