-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add CyclesWalletRuntime
#37
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| use crate::{IcError, Runtime}; | ||
| use async_trait::async_trait; | ||
| use candid::{decode_one, encode_args, utils::ArgumentEncoder, CandidType, Deserialize, Principal}; | ||
| use ic_cdk::management_canister::CanisterId; | ||
| use ic_error_types::RejectCode; | ||
| use regex_lite::Regex; | ||
| use serde::de::DeserializeOwned; | ||
|
|
||
| /// Runtime wrapping another [`Runtime`] instance, where update calls are routed through a | ||
| /// [cycles wallet](https://github.com/dfinity/cycles-wallet) to attach cycles to them. | ||
| pub struct CyclesWalletRuntime<R> { | ||
| runtime: R, | ||
| cycles_wallet_canister_id: Principal, | ||
| } | ||
|
|
||
| impl<R> CyclesWalletRuntime<R> { | ||
| /// Create a new [`CyclesWalletRuntime`] wrapping the given [`Runtime`] by routing update calls | ||
| /// through the given cycles wallet to attach cycles. | ||
| pub fn new(runtime: R, cycles_wallet_canister_id: Principal) -> Self { | ||
| CyclesWalletRuntime { | ||
| runtime, | ||
| cycles_wallet_canister_id, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<R: Runtime + Send + Sync> Runtime for CyclesWalletRuntime<R> { | ||
| async fn update_call<In, Out>( | ||
| &self, | ||
| id: Principal, | ||
| method: &str, | ||
| args: In, | ||
| cycles: u128, | ||
| ) -> Result<Out, IcError> | ||
| where | ||
| In: ArgumentEncoder + Send, | ||
| Out: CandidType + DeserializeOwned, | ||
| { | ||
| self.runtime | ||
| .update_call::<(WalletCall128Args,), Result<WalletCall128Result, String>>( | ||
| self.cycles_wallet_canister_id, | ||
| "wallet_call128", | ||
| (WalletCall128Args::new(id, method, args, cycles),), | ||
| 0, | ||
| ) | ||
| .await | ||
| .and_then(decode_cycles_wallet_response) | ||
| } | ||
|
|
||
| async fn query_call<In, Out>( | ||
| &self, | ||
| id: Principal, | ||
| method: &str, | ||
| args: In, | ||
| ) -> Result<Out, IcError> | ||
| where | ||
| In: ArgumentEncoder + Send, | ||
| Out: CandidType + DeserializeOwned, | ||
| { | ||
| self.runtime.query_call(id, method, args).await | ||
| } | ||
| } | ||
|
|
||
| // Argument to the cycles wallet canister `wallet_call128` method. | ||
| #[derive(CandidType, Deserialize)] | ||
| struct WalletCall128Args { | ||
| canister: Principal, | ||
| method_name: String, | ||
| #[serde(with = "serde_bytes")] | ||
| args: Vec<u8>, | ||
| cycles: u128, | ||
| } | ||
|
|
||
| impl WalletCall128Args { | ||
| pub fn new<In: ArgumentEncoder>( | ||
| canister_id: CanisterId, | ||
| method: impl ToString, | ||
| args: In, | ||
| cycles: u128, | ||
| ) -> Self { | ||
| Self { | ||
| canister: canister_id, | ||
| method_name: method.to_string(), | ||
| args: encode_args(args).unwrap_or_else(panic_when_encode_fails), | ||
| cycles, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Return type of the cycles wallet canister `wallet_call128` method. | ||
| #[derive(CandidType, Deserialize)] | ||
| struct WalletCall128Result { | ||
| #[serde(with = "serde_bytes", rename = "return")] | ||
| pub bytes: Vec<u8>, | ||
| } | ||
|
|
||
| // The cycles wallet canister formats the rejection code and error message from the target | ||
| // canister into a single string. Extract them back from the formatted string. | ||
| fn decode_cycles_wallet_response<Out>( | ||
| result: Result<WalletCall128Result, String>, | ||
| ) -> Result<Out, IcError> | ||
| where | ||
| Out: CandidType + DeserializeOwned, | ||
| { | ||
| match result { | ||
| Ok(WalletCall128Result { bytes }) => { | ||
| decode_one(&bytes).map_err(|e| IcError::CandidDecodeFailed { | ||
| message: format!( | ||
| "failed to decode canister response as {}: {}", | ||
| std::any::type_name::<Out>(), | ||
| e | ||
| ), | ||
| }) | ||
| } | ||
| Err(message) => { | ||
| match Regex::new(r"^An error happened during the call: (\d+): (.*)$") | ||
| .unwrap() | ||
| .captures(&message) | ||
| { | ||
| Some(captures) => { | ||
| let (_, [code, message]) = captures.extract(); | ||
| Err(IcError::CallRejected { | ||
| code: code.parse::<u64>().unwrap().try_into().unwrap(), | ||
| message: message.to_string(), | ||
| }) | ||
| } | ||
| None => Err(IcError::CallRejected { | ||
| code: RejectCode::SysFatal, | ||
| message: message.to_string(), | ||
| }), | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn panic_when_encode_fails(err: candid::error::Error) -> Vec<u8> { | ||
| panic!("failed to encode args: {err}") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.