|
| 1 | +use async_trait::async_trait; |
| 2 | +use candid::{utils::ArgumentEncoder, CandidType, Principal}; |
| 3 | +use ic_cdk::call::{Call, CallFailed, CandidDecodeFailed}; |
| 4 | +use ic_error_types::RejectCode; |
| 5 | +use serde::de::DeserializeOwned; |
| 6 | +use thiserror::Error; |
| 7 | + |
| 8 | +/// Abstract the canister runtime so that code making requests to canisters can be reused: |
| 9 | +/// * in production using [`ic_cdk`], |
| 10 | +/// * in unit tests by mocking this trait, |
| 11 | +/// * in integration tests by implementing this trait for `PocketIc`. |
| 12 | +#[async_trait] |
| 13 | +pub trait Runtime { |
| 14 | + /// Defines how asynchronous inter-canister update calls are made. |
| 15 | + async fn update_call<In, Out>( |
| 16 | + &self, |
| 17 | + id: Principal, |
| 18 | + method: &str, |
| 19 | + args: In, |
| 20 | + cycles: u128, |
| 21 | + ) -> Result<Out, IcError> |
| 22 | + where |
| 23 | + In: ArgumentEncoder + Send, |
| 24 | + Out: CandidType + DeserializeOwned; |
| 25 | + |
| 26 | + /// Defines how asynchronous inter-canister query calls are made. |
| 27 | + async fn query_call<In, Out>( |
| 28 | + &self, |
| 29 | + id: Principal, |
| 30 | + method: &str, |
| 31 | + args: In, |
| 32 | + ) -> Result<Out, IcError> |
| 33 | + where |
| 34 | + In: ArgumentEncoder + Send, |
| 35 | + Out: CandidType + DeserializeOwned; |
| 36 | +} |
| 37 | + |
| 38 | +/// Runtime when interacting with a canister running on the Internet Computer. |
| 39 | +#[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 40 | +pub struct IcRuntime; |
| 41 | + |
| 42 | +#[async_trait] |
| 43 | +impl Runtime for IcRuntime { |
| 44 | + async fn update_call<In, Out>( |
| 45 | + &self, |
| 46 | + id: Principal, |
| 47 | + method: &str, |
| 48 | + args: In, |
| 49 | + cycles: u128, |
| 50 | + ) -> Result<Out, IcError> |
| 51 | + where |
| 52 | + In: ArgumentEncoder + Send, |
| 53 | + Out: CandidType + DeserializeOwned, |
| 54 | + { |
| 55 | + Call::unbounded_wait(id, method) |
| 56 | + .with_args(&args) |
| 57 | + .with_cycles(cycles) |
| 58 | + .await |
| 59 | + .map_err(IcError::from) |
| 60 | + .and_then(|response| response.candid::<Out>().map_err(IcError::from)) |
| 61 | + } |
| 62 | + |
| 63 | + async fn query_call<In, Out>( |
| 64 | + &self, |
| 65 | + id: Principal, |
| 66 | + method: &str, |
| 67 | + args: In, |
| 68 | + ) -> Result<Out, IcError> |
| 69 | + where |
| 70 | + In: ArgumentEncoder + Send, |
| 71 | + Out: CandidType + DeserializeOwned, |
| 72 | + { |
| 73 | + Call::unbounded_wait(id, method) |
| 74 | + .with_args(&args) |
| 75 | + .await |
| 76 | + .map_err(IcError::from) |
| 77 | + .and_then(|response| response.candid::<Out>().map_err(IcError::from)) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// Error returned by the Internet Computer when making an inter-canister call. |
| 82 | +#[derive(Error, Clone, Debug, PartialEq, Eq)] |
| 83 | +pub enum IcError { |
| 84 | + /// The liquid cycle balance is insufficient to perform the call. |
| 85 | + #[error("Insufficient liquid cycles balance, available: {available}, required: {required}")] |
| 86 | + InsufficientLiquidCycleBalance { |
| 87 | + /// The liquid cycle balance available in the canister. |
| 88 | + available: u128, |
| 89 | + /// The required cycles to perform the call. |
| 90 | + required: u128, |
| 91 | + }, |
| 92 | + |
| 93 | + /// The `ic0.call_perform` operation failed when performing the inter-canister call. |
| 94 | + #[error("Inter-canister call perform failed")] |
| 95 | + CallPerformFailed, |
| 96 | + |
| 97 | + /// The inter-canister call is rejected. |
| 98 | + #[error("Inter-canister call rejected: {code:?} - {message})")] |
| 99 | + CallRejected { |
| 100 | + /// Rejection code as specified [here](https://internetcomputer.org/docs/current/references/ic-interface-spec#reject-codes) |
| 101 | + code: RejectCode, |
| 102 | + /// Associated helper message. |
| 103 | + message: String, |
| 104 | + }, |
| 105 | + |
| 106 | + /// The response from the inter-canister call could not be decoded as Candid. |
| 107 | + #[error("The inter-canister call response could not be decoded: {message}")] |
| 108 | + CandidDecodeFailed { |
| 109 | + /// The specific Candid error that occurred. |
| 110 | + message: String, |
| 111 | + }, |
| 112 | +} |
| 113 | + |
| 114 | +impl From<CallFailed> for IcError { |
| 115 | + fn from(err: CallFailed) -> Self { |
| 116 | + match err { |
| 117 | + CallFailed::CallPerformFailed(_) => IcError::CallPerformFailed, |
| 118 | + CallFailed::CallRejected(e) => { |
| 119 | + IcError::CallRejected { |
| 120 | + // `CallRejected::reject_code()` can only return an error result if there is a |
| 121 | + // new error code on ICP that the CDK is not aware of. We map it to `SysFatal` |
| 122 | + // since none of the other error codes apply. |
| 123 | + // In particular, note that `RejectCode::SysUnknown` is only applicable to |
| 124 | + // inter-canister calls that used `ic0.call_with_best_effort_response`. |
| 125 | + code: e.reject_code().unwrap_or(RejectCode::SysFatal), |
| 126 | + message: e.reject_message().to_string(), |
| 127 | + } |
| 128 | + } |
| 129 | + CallFailed::InsufficientLiquidCycleBalance(e) => { |
| 130 | + IcError::InsufficientLiquidCycleBalance { |
| 131 | + available: e.available, |
| 132 | + required: e.required, |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl From<CandidDecodeFailed> for IcError { |
| 140 | + fn from(err: CandidDecodeFailed) -> Self { |
| 141 | + IcError::CandidDecodeFailed { |
| 142 | + message: err.to_string(), |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments