Skip to content

Commit dd4c052

Browse files
authored
chore: use new ic-cdk naming (dfinity#10886)
This starts the migration to ic-cdk 0.20, which removes some APIs marked as deprecated in 0.18 (version currently in use: 0.19). The first step is to use the new module naming. The changes here only change the naming used in imports. https://github.com/dfinity/cdk-rs/blob/main/ic-cdk/CHANGELOG.md
1 parent 9ad211c commit dd4c052

15 files changed

Lines changed: 82 additions & 65 deletions

File tree

packages/pocket-ic/test_canister/src/canister.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(deprecated)]
22
use candid::{CandidType, Nat, Principal, define_function};
33
use ic_cdk::api::call::{RejectionCode, accept_message, arg_data_raw, reject};
4-
use ic_cdk::api::instruction_counter;
54
use ic_cdk::api::management_canister::ecdsa::{
65
EcdsaCurve, EcdsaKeyId, EcdsaPublicKeyArgument, EcdsaPublicKeyResponse, SignWithEcdsaArgument,
76
SignWithEcdsaResponse, ecdsa_public_key as ic_cdk_ecdsa_public_key,
@@ -11,6 +10,7 @@ use ic_cdk::api::management_canister::http_request::{
1110
TransformFunc, http_request as canister_http_outcall,
1211
};
1312
use ic_cdk::api::stable::{stable_grow, stable_size as raw_stable_size, stable_write};
13+
use ic_cdk::api::{canister_self, debug_print, instruction_counter};
1414
use ic_cdk::{inspect_message, query, trap, update};
1515
use icrc_ledger_types::icrc1::account::Account;
1616
use icrc_ledger_types::icrc1::transfer::Memo;
@@ -374,7 +374,7 @@ async fn canister_http_with_transform(http_server_addr: String) -> HttpResponse
374374
transform: Some(TransformContext {
375375
function: TransformFunc(candid::Func {
376376
method: "transform".to_string(),
377-
principal: ic_cdk::id(),
377+
principal: canister_self(),
378378
}),
379379
context,
380380
}),
@@ -387,7 +387,7 @@ async fn canister_http_with_transform(http_server_addr: String) -> HttpResponse
387387

388388
#[update]
389389
async fn whoami() -> String {
390-
ic_cdk::id().to_string()
390+
canister_self().to_string()
391391
}
392392

393393
#[update]
@@ -464,7 +464,7 @@ async fn execute_many_instructions(n: u64) {
464464

465465
#[update]
466466
async fn canister_log(msg: String) {
467-
ic_cdk::print(msg);
467+
debug_print(msg);
468468
}
469469

470470
// time

rs/boundary_node/rate_limits/canister/canister.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::state::{CanisterApi, init_version_and_config, with_canister_state};
1616
use candid::Principal;
1717
use ic_canister_log::{export as export_logs, log};
1818
use ic_cdk::api::call::call;
19-
use ic_cdk::{init, inspect_message, post_upgrade, query, update};
19+
use ic_cdk::{api::msg_caller, init, inspect_message, post_upgrade, query, update};
2020
use ic_http_types::{HttpRequest, HttpResponse, HttpResponseBuilder};
2121
use ic_nns_constants::REGISTRY_CANISTER_ID;
2222
use rate_limits_api::{
@@ -34,7 +34,7 @@ const REPLICATED_QUERY_METHOD: &str = "get_config";
3434
#[inspect_message]
3535
fn inspect_message() {
3636
// In order for this hook to succeed, accept_message() must be invoked.
37-
let caller_id: Principal = ic_cdk::api::caller();
37+
let caller_id: Principal = msg_caller();
3838
let called_method = ic_cdk::api::call::method_name();
3939

4040
let (has_full_access, has_full_read_access) = with_canister_state(|state| {
@@ -109,7 +109,7 @@ fn post_upgrade(init_arg: InitArg) {
109109
/// The response includes the config containing all rate-limit rules and the JSON schema version needed for decoding the rules.
110110
#[query]
111111
fn get_config(version: Option<Version>) -> GetConfigResponse {
112-
let caller_id = ic_cdk::api::caller();
112+
let caller_id = msg_caller();
113113
let response = with_canister_state(|state| {
114114
let access_resolver = AccessLevelResolver::new(caller_id, state.clone());
115115
let formatter = ConfigConfidentialityFormatter;
@@ -122,7 +122,7 @@ fn get_config(version: Option<Version>) -> GetConfigResponse {
122122
/// Retrieves a specific rate-limit rule by its ID, applying confidentiality formatting, based on caller's access level and rule's confidentiality status
123123
#[query]
124124
fn get_rule_by_id(rule_id: RuleId) -> GetRuleByIdResponse {
125-
let caller_id = ic_cdk::api::caller();
125+
let caller_id = msg_caller();
126126
let response = with_canister_state(|state| {
127127
let access_resolver = AccessLevelResolver::new(caller_id, state.clone());
128128
let formatter = RuleConfidentialityFormatter;
@@ -135,7 +135,7 @@ fn get_rule_by_id(rule_id: RuleId) -> GetRuleByIdResponse {
135135
/// Retrieves all rate-limit rules associated with a specific incident ID, applying confidentiality formatting, based on caller's access level and rule's confidentiality status
136136
#[query]
137137
fn get_rules_by_incident_id(incident_id: IncidentId) -> GetRulesByIncidentIdResponse {
138-
let caller_id = ic_cdk::api::caller();
138+
let caller_id = msg_caller();
139139
let response = with_canister_state(|state| {
140140
let access_resolver = AccessLevelResolver::new(caller_id, state.clone());
141141
let formatter = RuleConfidentialityFormatter;
@@ -151,7 +151,7 @@ fn get_rules_by_incident_id(incident_id: IncidentId) -> GetRulesByIncidentIdResp
151151
/// This update method includes authorization check and metrics collection.
152152
#[update]
153153
fn add_config(config: InputConfig) -> AddConfigResponse {
154-
let caller_id = ic_cdk::api::caller();
154+
let caller_id = msg_caller();
155155
let current_time = ic_cdk::api::time();
156156
with_canister_state(|state| {
157157
let access_resolver = AccessLevelResolver::new(caller_id, state.clone());
@@ -169,7 +169,7 @@ fn add_config(config: InputConfig) -> AddConfigResponse {
169169
/// making them viewable by the public. It includes authorization check and metrics collection.
170170
#[update]
171171
fn disclose_rules(args: DiscloseRulesArg) -> DiscloseRulesResponse {
172-
let caller_id = ic_cdk::api::caller();
172+
let caller_id = msg_caller();
173173
let disclose_time = ic_cdk::api::time();
174174
with_canister_state(|state| {
175175
let access_resolver = AccessLevelResolver::new(caller_id, state.clone());

rs/boundary_node/salt_sharing/canister/canister.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::logs::export_logs_as_http_response;
66
use crate::metrics::{METRICS, export_metrics_as_http_response};
77
use crate::storage::SALT;
88
use ic_cdk::api::call::{accept_message, method_name};
9-
use ic_cdk::api::time;
10-
use ic_cdk::{caller, trap};
9+
use ic_cdk::api::{msg_caller, time};
10+
use ic_cdk::trap;
1111
use ic_cdk::{init, inspect_message, post_upgrade, query};
1212
use ic_cdk_timers::set_timer;
1313
use ic_http_types::{HttpRequest, HttpResponse, HttpResponseBuilder};
@@ -19,7 +19,7 @@ const REPLICATED_QUERY_METHOD: &str = "get_salt";
1919
// Inspect the ingress messages in the pre-consensus phase and reject early, if the conditions are not met
2020
#[inspect_message]
2121
fn inspect_message() {
22-
let caller_id = caller();
22+
let caller_id = msg_caller();
2323
let called_method = method_name();
2424

2525
if called_method == REPLICATED_QUERY_METHOD && is_api_boundary_node_principal(&caller_id) {
@@ -51,7 +51,7 @@ fn post_upgrade(init_arg: InitArg) {
5151

5252
#[query]
5353
fn get_salt() -> GetSaltResponse {
54-
let caller_id = caller();
54+
let caller_id = msg_caller();
5555
if is_api_boundary_node_principal(&caller_id) {
5656
let stored_salt = SALT
5757
.with(|cell| cell.borrow().get(&()))

rs/execution_environment/benches/management_canister/test_canister/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ic_cdk::api::management_canister::main::{
1616
stop_canister as ic_cdk_stop_canister, update_settings as ic_cdk_update_settings,
1717
};
1818
use ic_cdk::call::Call;
19-
use ic_cdk::update;
19+
use ic_cdk::{api::canister_self, update};
2020
use serde::{Deserialize, Serialize};
2121

2222
#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
@@ -37,7 +37,7 @@ async fn create_canisters(args: CreateCanistersArgs) -> Vec<Principal> {
3737
ic_cdk_create_canister(
3838
CreateCanisterArgument {
3939
settings: Some(CanisterSettings {
40-
controllers: Some(vec![ic_cdk::api::id()]),
40+
controllers: Some(vec![canister_self()]),
4141
..CanisterSettings::default()
4242
}),
4343
},
@@ -159,7 +159,7 @@ pub struct UpdateSettingsArgs {
159159

160160
#[update]
161161
async fn update_settings(args: UpdateSettingsArgs) {
162-
let controllers = vec![ic_cdk::api::id(); args.controllers_number as usize];
162+
let controllers = vec![canister_self(); args.controllers_number as usize];
163163
let futures: Vec<_> = args
164164
.canister_ids
165165
.into_iter()

rs/nervous_system/long_message/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(deprecated)]
22
#[cfg(target_arch = "wasm32")]
3-
use ic_cdk::api::{call_context_instruction_counter, instruction_counter};
3+
use ic_cdk::api::{call_context_instruction_counter, canister_self, instruction_counter};
44
use ic_cdk::query;
55

66
#[cfg(not(target_arch = "wasm32"))]
@@ -51,7 +51,7 @@ async fn make_noop_call() {}
5151
/// Makes a call to a no-op function defined in this library.
5252
#[cfg(target_arch = "wasm32")]
5353
async fn make_noop_call() {
54-
() = ic_cdk::call(ic_cdk::id(), "__long_message_noop", ())
54+
() = ic_cdk::call(canister_self(), "__long_message_noop", ())
5555
.await
5656
.unwrap();
5757
}

rs/nervous_system/timer_task/tests/test_canisters/timer_task_canister.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(deprecated)]
22
use async_trait::async_trait;
3-
use ic_cdk::{init, query};
3+
use ic_cdk::{api::canister_self, init, query};
44
use ic_metrics_encoder::MetricsEncoder;
55
use ic_nervous_system_timer_task::{
66
PeriodicAsyncTask, PeriodicSyncTask, RecurringAsyncTask, RecurringSyncTask,
@@ -86,7 +86,7 @@ fn get_metrics() -> String {
8686
fn __self_call() {}
8787

8888
async fn invoke_self_call() {
89-
let () = ic_cdk::call(ic_cdk::api::id(), "__self_call", ())
89+
let () = ic_cdk::call(canister_self(), "__self_call", ())
9090
.await
9191
.unwrap();
9292
}

rs/nns/cmc/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
pub use ic_management_canister_types::CanisterSettings;
33

44
use candid::{CandidType, Nat};
5-
use ic_cdk::api::call::{CallResult, RejectionCode};
5+
use ic_cdk::api::{
6+
call::{CallResult, RejectionCode},
7+
msg_caller,
8+
};
69
use std::time::{Duration, SystemTime};
710

811
use dfn_protobuf::{ProtoBuf, ToProto};
@@ -60,7 +63,7 @@ pub fn ic0_mint_cycles128(amount: Cycles) -> Cycles {
6063

6164
/// caller that returns principalId instead of Principal
6265
pub fn caller() -> PrincipalId {
63-
PrincipalId::from(ic_cdk::caller())
66+
PrincipalId::from(msg_caller())
6467
}
6568

6669
// Duplicating some functionality that is no longer available

rs/nns/cmc/src/main.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use cycles_minting_canister::*;
55
use environment::Environment;
66
use exchange_rate_canister::{UpdateExchangeRateError, UpdateExchangeRateState};
77
use ic_cdk::{
8-
api::call::{CallResult, ManualReply},
8+
api::{
9+
call::{CallResult, ManualReply},
10+
canister_cycle_balance, canister_self, certified_data_set,
11+
},
912
heartbeat, init, post_upgrade, pre_upgrade, println, query, update,
1013
};
1114
use ic_crypto_tree_hash::{
@@ -130,7 +133,7 @@ impl Environment for CanisterEnvironment {
130133
}
131134

132135
fn set_certified_data(&self, data: &[u8]) {
133-
ic_cdk::api::set_certified_data(data)
136+
certified_data_set(data)
134137
}
135138
}
136139

@@ -425,7 +428,7 @@ where
425428
yansi::Paint<S>: std::string::ToString,
426429
{
427430
#[cfg(target_arch = "wasm32")]
428-
ic_cdk::api::print(yansi::Paint::yellow(s).to_string());
431+
ic_cdk::api::debug_print(yansi::Paint::yellow(s).to_string());
429432

430433
#[cfg(not(target_arch = "wasm32"))]
431434
println!("{}", yansi::Paint::yellow(s).to_string());
@@ -1612,7 +1615,7 @@ async fn fetch_transaction(
16121615
};
16131616

16141617
let expected_to = AccountIdentifier::new(
1615-
PrincipalId::from(ic_cdk::api::id()),
1618+
PrincipalId::from(canister_self()),
16161619
Some(expected_to_subaccount),
16171620
);
16181621
if to != expected_to {
@@ -1780,7 +1783,7 @@ async fn issue_automatic_refund_if_memo_not_offerred(
17801783
spender: _,
17811784
} => {
17821785
let incoming_to_account_identifier = AccountIdentifier::new(
1783-
PrincipalId::from(ic_cdk::api::id()),
1786+
PrincipalId::from(canister_self()),
17841787
Some(incoming_to_subaccount),
17851788
);
17861789
if to != &incoming_to_account_identifier {
@@ -2306,7 +2309,7 @@ fn ensure_balance(
23062309
) -> Result<(), String> {
23072310
let now = now_system_time();
23082311

2309-
let current_balance = Cycles::from(ic_cdk::api::canister_balance128());
2312+
let current_balance = Cycles::from(canister_cycle_balance());
23102313
let cycles_to_mint = cycles - current_balance;
23112314

23122315
with_state_mut(|state| {
@@ -2317,7 +2320,7 @@ fn ensure_balance(
23172320

23182321
// unused because of check above
23192322
let _minted_cycles = ic0_mint_cycles128(cycles_to_mint);
2320-
assert!(ic_cdk::api::canister_balance128() >= cycles.get());
2323+
assert!(canister_cycle_balance() >= cycles.get());
23212324
Ok(())
23222325
}
23232326

rs/nns/sns-wasm/canister/canister.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ use ic_types::CanisterId;
3737
use ic_types_cycles::Cycles;
3838
use std::{cell::RefCell, collections::HashMap, convert::TryInto};
3939

40-
use ic_cdk::{init, post_upgrade, pre_upgrade, println, query, update};
40+
use ic_cdk::{
41+
api::{canister_cycle_balance, canister_self, msg_caller},
42+
init, post_upgrade, pre_upgrade, println, query, update,
43+
};
4144
use ic_nervous_system_common::serve_metrics;
4245

4346
pub const LOG_PREFIX: &str = "[SNS-WASM] ";
@@ -58,7 +61,7 @@ struct CanisterApiImpl {}
5861
impl CanisterApi for CanisterApiImpl {
5962
/// See CanisterApi::local_canister_id
6063
fn local_canister_id(&self) -> CanisterId {
61-
CanisterId::unchecked_from_principal(PrincipalId::from(ic_cdk::api::id()))
64+
CanisterId::unchecked_from_principal(PrincipalId::from(canister_self()))
6265
}
6366

6467
/// See CanisterApi::create_canister
@@ -154,7 +157,7 @@ impl CanisterApi for CanisterApiImpl {
154157
}
155158

156159
fn this_canister_has_enough_cycles(&self, required_cycles: u64) -> Result<u64, String> {
157-
let available = ic_cdk::api::canister_balance();
160+
let available = canister_cycle_balance() as u64;
158161

159162
if available < required_cycles {
160163
return Err(format!(
@@ -253,7 +256,7 @@ impl CanisterApiImpl {
253256
}
254257

255258
fn caller() -> PrincipalId {
256-
PrincipalId::from(ic_cdk::caller())
259+
PrincipalId::from(msg_caller())
257260
}
258261

259262
/// In contrast to canister_init(), this method does not do deserialization.

rs/rust_canisters/call_tree_test/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(deprecated)]
22
//! This module contains a canister used for XNet integration test.
33
use candid::{CandidType, Principal};
4-
use ic_cdk::api::{call::call_raw, id};
4+
use ic_cdk::api::{call::call_raw, canister_self};
55
use ic_cdk::query;
66
use serde::{Deserialize, Serialize};
77
use std::cell::RefCell;
@@ -82,7 +82,7 @@ async fn start(arguments: Arguments) -> Vec<Message> {
8282
let calltrees = arguments.calltrees;
8383

8484
let mut messages = vec![];
85-
let this_cid = id().to_string();
85+
let this_cid = canister_self().to_string();
8686

8787
touch_memory(arguments.num_pages);
8888

0 commit comments

Comments
 (0)