Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions rs/rosetta-api/icp/test_utils/sender_canister/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![allow(deprecated)]
use candid::{CandidType, Principal};
use ic_cdk::api::call::RejectionCode;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
Expand All @@ -11,5 +9,37 @@ pub struct SendArg {
pub payment: u128,
}

/// The reject code returned by `send`.
///
/// Mirrors the variants exposed by the deprecated
/// `ic_cdk::api::call::RejectionCode` so the public Candid interface of this
/// test canister stays stable after the ic-cdk 0.18 upgrade.
#[derive(Clone, Debug, CandidType, Deserialize, Serialize)]
pub enum RejectionCode {
NoError,
SysFatal,
SysTransient,
DestinationInvalid,
CanisterReject,
CanisterError,
Unknown,
}

impl RejectionCode {
/// Translates the raw u32 reject code returned by `ic_cdk::call::CallRejected`
/// into the variant the test canister exposes over Candid.
pub fn from_raw(raw: u32) -> Self {
match raw {
0 => Self::NoError,
1 => Self::SysFatal,
2 => Self::SysTransient,
3 => Self::DestinationInvalid,
4 => Self::CanisterReject,
5 => Self::CanisterError,
_ => Self::Unknown,
}
}
}

pub type SendError = (RejectionCode, String);
pub type SendResult = Result<Vec<u8>, SendError>;
24 changes: 21 additions & 3 deletions rs/rosetta-api/icp/test_utils/sender_canister/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(deprecated)]
use ic_cdk::call::{Call, CallFailed};
use ic_cdk::update;
use ic_sender_canister_lib::{SendArg, SendResult};
use ic_sender_canister_lib::{RejectionCode, SendArg, SendResult};

#[update]
async fn send(calls: Vec<SendArg>) -> Vec<SendResult> {
Expand All @@ -12,12 +12,30 @@ async fn send(calls: Vec<SendArg>) -> Vec<SendResult> {
payment,
} in calls
{
futures.push(ic_cdk::api::call::call_raw128(to, &method, arg, payment));
futures.push(async move {
Call::unbounded_wait(to, &method)
.take_raw_args(arg)
.with_cycles(payment)
.await
.map(|response| response.into_bytes())
.map_err(map_call_error)
});
}

futures::future::join_all(futures).await
}

fn map_call_error(err: CallFailed) -> (RejectionCode, String) {
match err {
CallFailed::CallRejected(rejected) => (
RejectionCode::from_raw(rejected.raw_reject_code()),
rejected.reject_message().to_string(),
),
CallFailed::InsufficientLiquidCycleBalance(e) => (RejectionCode::Unknown, e.to_string()),
CallFailed::CallPerformFailed(e) => (RejectionCode::Unknown, e.to_string()),
}
}

fn main() {}

candid::export_service!();
Expand Down
Loading