Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: root call mod with new CallError #535

Merged
merged 10 commits into from
Dec 11, 2024
77 changes: 69 additions & 8 deletions e2e-tests/src/bin/call_struct.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,78 @@
use candid::Principal;
use ic_cdk::api::management_canister::main::{CanisterIdRecord, CreateCanisterArgument};
use ic_cdk::prelude::*;
use candid::Encode;
use ic_cdk::{call::DecoderConfig, prelude::*};

/// This endpoint is to be called by the following Call struct invocation.
#[update]
async fn create_canister_via_struct() -> Principal {
let res: (CanisterIdRecord,) = Call::new(Principal::management_canister(), "create_canister")
.with_args((CreateCanisterArgument::default(),))
.with_cycles(1_000_000_000_000)
async fn echo(arg: u32) -> u32 {
arg
}

#[update]
async fn call_struct() {
let num = 1u32;
let bytes: Vec<u8> = Encode!(&num).unwrap();

// 1. Various ways to call*
// 1.1 call()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.call()
.await
.unwrap();
assert_eq!(res.0, num);
// 1.2 call_raw()
let res = Call::new(id(), "echo")
.with_args((num,))
.call_raw()
.await
.unwrap();
assert_eq!(res, bytes);
// 1.3 call_with_decoder_config()
let config = DecoderConfig::default();
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.call_with_decoder_config(&config)
.await
.unwrap();
assert_eq!(res.0, num);
// 1.4 call_raw_with_decoder_config()
Call::new(id(), "echo")
.with_args((num,))
.call_and_forget()
.unwrap();

// 2. Various ways to config the call
// 2.1 with_raw_args()
let res: (u32,) = Call::new(id(), "echo")
.with_raw_args(&bytes)
.call()
.await
.unwrap();
assert_eq!(res.0, num);
// 2.2 with_guaranteed_response()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.with_guaranteed_response()
.call()
.await
.unwrap();
res.0.canister_id
assert_eq!(res.0, num);
// 2.3 change_timeout()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.change_timeout(5)
.call()
.await
.unwrap();
assert_eq!(res.0, num);
// 2.4 with_cycles()
let res: (u32,) = Call::new(id(), "echo")
.with_args((num,))
.with_cycles(100_000)
.call()
.await
.unwrap();
assert_eq!(res.0, num);
}

fn main() {}
14 changes: 8 additions & 6 deletions e2e-tests/tests/call_struct.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use candid::Principal;
use ic_cdk_e2e_tests::cargo_build_canister;
use pocket_ic::common::rest::RawEffectivePrincipal;
use pocket_ic::{call_candid, PocketIc};
use pocket_ic::{call_candid, PocketIcBuilder};

#[test]
fn call_struct() {
let pic = PocketIc::new();
let pic = PocketIcBuilder::new()
.with_application_subnet()
.with_nonmainnet_features(true)
.build();
let wasm = cargo_build_canister("call_struct");
let canister_id = pic.create_canister();
pic.add_cycles(canister_id, 100_000_000_000_000);
pic.install_canister(canister_id, wasm, vec![], None);
let _: (Principal,) = call_candid(
let _: () = call_candid(
&pic,
canister_id,
RawEffectivePrincipal::None,
"create_canister_via_struct",
"call_struct",
(),
)
.expect("Error calling create_canister_via_struct");
.expect("Error calling call_struct");
}
1 change: 0 additions & 1 deletion ic-cdk-timers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ extern "C" fn global_timer() {
| RejectionCode::DestinationInvalid
| RejectionCode::CanisterReject
| RejectionCode::CanisterError
| RejectionCode::SysUnknown
| RejectionCode::Unknown => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions ic-cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ic-cdk-macros = { path = "../ic-cdk-macros", version = "=0.18.0-alpha.1" }
serde.workspace = true
serde_bytes.workspace = true
slotmap = { workspace = true, optional = true }
thiserror = "2.0"

[dev-dependencies]
anyhow = "1"
Expand Down
Loading