11// Some of the imports will only be used in later examples; we list them here for simplicity
22use candid:: { Nat , Principal } ;
33use ic_cdk:: api:: time;
4- use ic_cdk:: call:: { Call , CallErrorExt , RejectCode } ;
5- use ic_cdk:: management_canister:: { EcdsaCurve , EcdsaKeyId , SignWithEcdsaArgs , SignWithEcdsaResult , cost_sign_with_ecdsa } ;
4+ use ic_cdk:: call:: { Call , CallErrorExt } ;
5+ use ic_cdk:: management_canister:: { DepositCyclesArgs , CanisterId } ;
66use ic_cdk_macros:: update;
7- use sha2:: { Digest , Sha256 } ;
87
98// When calling other canisters:
10- // 1. The simplest is to mark your function as `update`. Then you can always call any public
11- // endpoint on any other canister.
12- // 2. Mark the function as `async`. Then you can use the `Call` API to call other canisters.
9+ //
10+ // 1. The simplest is to mark your function as `update`. Then you can always call any public
11+ // endpoint on any other canister.
12+ // 2. Mark the function as `async`. Then you can use the `Call` API to call other canisters.
13+ //
1314// This particular example requires the caller to provide the principal (i.e., ID) of the counter canister.
1415#[ update]
1516pub async fn call_get_and_set ( counter : Principal , new_value : Nat ) -> Nat {
@@ -136,52 +137,17 @@ pub async fn stubborn_set(counter: Principal, new_value: Nat) -> Result<(), Stri
136137}
137138
138139#[ update]
139- pub async fn sign_message ( message : String ) -> Result < String , String > {
140- let message_hash = Sha256 :: digest ( & message) . to_vec ( ) ;
141-
142- let request = SignWithEcdsaArgs {
143- message_hash,
144- // This example does not use the fancier signing features
145- derivation_path : vec ! [ ] ,
146- key_id : EcdsaKeyId {
147- curve : EcdsaCurve :: Secp256k1 ,
148- // This is the key name used for local testing; different
149- // key names are needed for the mainnet
150- name : "dfx_test_key" . to_string ( ) ,
151- } ,
140+ pub async fn send_cycles ( target : CanisterId , amount : u64 ) -> Result < ( ) , String > {
141+ let request = DepositCyclesArgs {
142+ canister_id : target,
152143 } ;
153144
154- let cycles_cost = cost_sign_with_ecdsa ( & request) . map_err ( |e| {
155- format ! (
156- "Failed to compute cycles cost for signing with ECDSA: {:?}" ,
157- e
158- )
159- } ) ?;
160-
161- // Use bounded-wait calls in this example, since the amount attached is
162- // fairly low, and losing the attached cycles isn't catastrophic.
163- match Call :: bounded_wait ( Principal :: management_canister ( ) , "sign_with_ecdsa" )
145+ match Call :: bounded_wait ( Principal :: management_canister ( ) , "deposit_cycles" )
164146 . with_arg ( & request)
165- // Signing with a test key requires 30 billion cycles
166- . with_cycles ( cycles_cost)
147+ . with_cycles ( amount as u128 )
167148 . await
168149 {
169- Ok ( resp) => match resp. candid :: < SignWithEcdsaResult > ( ) {
170- Ok ( signature) => Ok ( hex:: encode ( signature. signature ) ) ,
171- Err ( e) => Err ( format ! ( "Error decoding response: {:?}" , e) ) ,
172- } ,
173- // A SysUnknown reject code only occurs due to a bounded-wait call timing out.
174- // It means that no cycles will be refunded, even
175- // if the call didn't make it to the callee. Here, this is fine since
176- // only a small amount is used.
177- Err ( ic_cdk:: call:: CallFailed :: CallRejected ( e) )
178- if e. reject_code ( ) == Ok ( RejectCode :: SysUnknown ) =>
179- {
180- Err ( format ! (
181- "Got a SysUnknown error while signing message: {:?}; cycles are not refunded" ,
182- e
183- ) )
184- }
185- Err ( e) => Err ( format ! ( "Error signing message: {:?}" , e) ) ,
150+ Ok ( _) => Ok ( ( ) ) ,
151+ Err ( e) => Err ( format ! ( "Error attaching {} cycles: {:?}" , amount, e) ) ,
186152 }
187- }
153+ }
0 commit comments