@@ -10,6 +10,7 @@ use dfx_core::canister::install_canister_wasm;
1010use dfx_core:: config:: model:: dfinity:: { NetworksConfig , ReplicaSubnetType } ;
1111use dfx_core:: config:: model:: network_descriptor:: NetworkDescriptor ;
1212use dfx_core:: identity:: CallSender ;
13+ use dfx_extensions_utils:: dependencies:: download_wasms:: nns:: { CYCLES_LEDGER , NNS_CYCLES_MINTING } ;
1314use dfx_extensions_utils:: {
1415 call_extension_bundled_binary, download_nns_wasms, nns_wasm_dir, IcNnsInitCanister ,
1516 SnsCanisterInstallation , StandardCanister , ED25519_TEST_ACCOUNT , NNS_CORE , NNS_FRONTEND ,
@@ -19,14 +20,15 @@ use dfx_extensions_utils::{
1920use anyhow:: { anyhow, bail, Context } ;
2021use backoff:: backoff:: Backoff ;
2122use backoff:: ExponentialBackoff ;
22- use candid:: Encode ;
23+ use candid:: { CandidType , Encode } ;
2324use fn_error_context:: context;
2425use futures_util:: future:: try_join_all;
2526use ic_agent:: export:: Principal ;
2627use ic_agent:: Agent ;
2728use ic_utils:: interfaces:: management_canister:: builders:: InstallMode ;
2829use ic_utils:: interfaces:: ManagementCanister ;
2930use reqwest:: Url ;
31+ use sha2:: { Digest , Sha256 } ;
3032use slog:: Logger ;
3133use std:: ffi:: OsString ;
3234use std:: fs;
@@ -115,6 +117,7 @@ pub async fn install_nns(
115117 eprintln ! ( "Configuring the NNS..." ) ;
116118 set_xdr_rate ( 1234567 , & nns_url, dfx_cache_path) ?;
117119 set_cmc_authorized_subnets ( & nns_url, & subnet_id, dfx_cache_path) ?;
120+ set_cycles_ledger_canister_id_in_cmc ( & nns_url, dfx_cache_path) ?;
118121
119122 print_nns_details ( provider_url) ?;
120123 Ok ( ( ) )
@@ -394,7 +397,7 @@ pub async fn download(source: &Url, target: &Path) -> anyhow::Result<()> {
394397
395398/// Arguments for the ic-nns-init command line function.
396399pub struct IcNnsInitOpts {
397- /// An URL to accees one or more NNS subnet replicas.
400+ /// An URL to access one or more NNS subnet replicas.
398401 nns_url : String ,
399402 /// A directory that needs to be populated will all required wasms before calling ic-nns-init.
400403 wasm_dir : PathBuf ,
@@ -483,6 +486,60 @@ pub fn set_cmc_authorized_subnets(
483486 . map_err ( |e| anyhow ! ( "Call to propose to set authorized subnets failed: {e}" ) )
484487}
485488
489+ /// Sets the cycles ledger canister id in the CMC
490+ #[ context( "Failed to set the cycles ledger canister id in the CMC" ) ]
491+ pub fn set_cycles_ledger_canister_id_in_cmc (
492+ nns_url : & Url ,
493+ dfx_cache_path : & Path ,
494+ ) -> anyhow:: Result < ( ) > {
495+ #[ derive( CandidType , Clone , Debug , PartialEq , Eq ) ]
496+ struct CyclesCanisterInitPayload {
497+ pub cycles_ledger_canister_id : Option < Principal > ,
498+ }
499+
500+ let wasm_path = nns_wasm_dir ( dfx_cache_path) ;
501+ let cmc_wasm_path = wasm_path. join ( NNS_CYCLES_MINTING . wasm_name ) ;
502+ let cmc_wasm_bytes = dfx_core:: fs:: read ( & cmc_wasm_path) ?;
503+ let wasm_hash = Sha256 :: digest ( cmc_wasm_bytes) ;
504+ let mut upgrade_arg_file = tempfile:: NamedTempFile :: new ( ) ?;
505+ upgrade_arg_file
506+ . write_all (
507+ & Encode ! ( & ( Some ( CyclesCanisterInitPayload {
508+ cycles_ledger_canister_id: Some (
509+ Principal :: from_text( CYCLES_LEDGER . canister_id) . unwrap( )
510+ ) ,
511+ } ) , ) )
512+ . unwrap ( ) ,
513+ )
514+ . context ( "Failed to write to tempfile." ) ?;
515+
516+ let cmc_wasm_path_str = cmc_wasm_path. to_string_lossy ( ) ;
517+ let wasm_hash_str = hex:: encode ( wasm_hash) ;
518+ let upgrade_arg_file_str = upgrade_arg_file. path ( ) . to_string_lossy ( ) ;
519+ let args = vec ! [
520+ "--nns-url" ,
521+ nns_url. as_str( ) ,
522+ "propose-to-change-nns-canister" ,
523+ "--test-neuron-proposer" ,
524+ "--proposal-title" ,
525+ "Set cycles ledger canister id in Cycles Minting Canister" ,
526+ "--summary" ,
527+ "Set cycles ledger canister id in Cycles Minting Canister" ,
528+ "--mode" ,
529+ "upgrade" ,
530+ "--canister-id" ,
531+ NNS_CYCLES_MINTING . canister_id,
532+ "--wasm-module-path" ,
533+ & cmc_wasm_path_str,
534+ "--wasm-module-sha256" ,
535+ & wasm_hash_str,
536+ "--arg" ,
537+ & upgrade_arg_file_str,
538+ ] ;
539+ call_extension_bundled_binary ( "ic-admin" , args, dfx_cache_path)
540+ . map_err ( |e| anyhow ! ( "Call to set the cycles ledger canister id in the CMC: {e}" ) )
541+ }
542+
486543/// Uploads wasms to the nns-sns-wasm canister.
487544#[ context( "Failed to upload wasm files to the nns-sns-wasm canister; it may not be possible to create an SNS." ) ]
488545pub fn upload_nns_sns_wasms_canister_wasms ( dfx_cache_path : & Path ) -> anyhow:: Result < ( ) > {
0 commit comments