From 92d00adc0b901267f9c97784faf72030b38d5a49 Mon Sep 17 00:00:00 2001 From: rvanasa Date: Thu, 21 Nov 2024 11:46:33 -0700 Subject: [PATCH 1/3] Add example to 'Inter-canister calls' docs --- doc/md/writing-motoko/intercanister-calls.md | 49 ++++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/doc/md/writing-motoko/intercanister-calls.md b/doc/md/writing-motoko/intercanister-calls.md index bbce113aaa8..e0091f848e1 100644 --- a/doc/md/writing-motoko/intercanister-calls.md +++ b/doc/md/writing-motoko/intercanister-calls.md @@ -16,24 +16,24 @@ This example will showcase a simple way to configure inter-canister calls that c ## Example -Consider the following code for `canister1`: +Consider the following code for `Canister1`: ```motoko no-repl import Canister2 "canister:canister2"; -actor { +actor Canister1 { public func main() : async Nat { return await Canister2.getValue(); }; }; ``` -Then, consider the following code for `canister2`: +Then, consider the following code for `Canister2`: ```motoko import Debug "mo:base/Debug"; -actor { +actor Canister2 { public func getValue() : async Nat { Debug.print("Hello from canister 2!"); return 10; @@ -72,4 +72,43 @@ Then, use the following call, replacing `canisterID` with the principal ID of a dfx canister call canister1 main "canisterID" ``` -Logo \ No newline at end of file +## Advanced usage + +If the method name or input types are unknown at compile time, it's possible to call arbitrary canister methods using the `ExperimentalInternetComputer` module. + +Modifying the above example to use this feature: + +```motoko +import IC "mo:base/ExperimentalInternetComputer"; +import Debug "mo:base/Debug"; + +actor AdvancedCanister1 { + public func main(canisterId : Principal) : async Nat { + // Define the method name and input args + let name = "getValueTimesTwo"; + let args = (123); + + // Call the method + let encodedArgs = to_candid (args); + let encodedValue = await IC.call(canisterId, name, encodedArgs); + + // Decode the return value + let ?value : ?Nat = from_candid encodedValue + else Debug.trap("Error while decoding return value"); + return value; + } +} +``` + +```motoko +import Debug "mo:base/Debug"; + +actor AdvancedCanister2 { + public func getValueTimesTwo(number: Nat) : async Nat { + Debug.print("Hello from canister 2!"); + return number * 2; + }; +}; +``` + +Logo From 47f87b74895926dc360ee71a37e81928925ebbe5 Mon Sep 17 00:00:00 2001 From: rvanasa Date: Thu, 21 Nov 2024 11:50:50 -0700 Subject: [PATCH 2/3] Adjust examples --- doc/md/writing-motoko/intercanister-calls.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/md/writing-motoko/intercanister-calls.md b/doc/md/writing-motoko/intercanister-calls.md index e0091f848e1..aad03f40d0f 100644 --- a/doc/md/writing-motoko/intercanister-calls.md +++ b/doc/md/writing-motoko/intercanister-calls.md @@ -76,7 +76,7 @@ dfx canister call canister1 main "canisterID" If the method name or input types are unknown at compile time, it's possible to call arbitrary canister methods using the `ExperimentalInternetComputer` module. -Modifying the above example to use this feature: +Here is an example which you can modify for your specific use case: ```motoko import IC "mo:base/ExperimentalInternetComputer"; @@ -85,7 +85,7 @@ import Debug "mo:base/Debug"; actor AdvancedCanister1 { public func main(canisterId : Principal) : async Nat { // Define the method name and input args - let name = "getValueTimesTwo"; + let name = "getValue"; let args = (123); // Call the method @@ -104,8 +104,8 @@ actor AdvancedCanister1 { import Debug "mo:base/Debug"; actor AdvancedCanister2 { - public func getValueTimesTwo(number: Nat) : async Nat { - Debug.print("Hello from canister 2!"); + public func getValue(number: Nat) : async Nat { + Debug.print("Hello from advanced canister 2!"); return number * 2; }; }; From a6b55b80e8d7115bfd6693010cf3be8ffcd92103 Mon Sep 17 00:00:00 2001 From: rvanasa Date: Thu, 21 Nov 2024 12:05:17 -0700 Subject: [PATCH 3/3] Adjust wording of 'Debug.trap' message --- doc/md/writing-motoko/intercanister-calls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/md/writing-motoko/intercanister-calls.md b/doc/md/writing-motoko/intercanister-calls.md index aad03f40d0f..f41bd00cb8b 100644 --- a/doc/md/writing-motoko/intercanister-calls.md +++ b/doc/md/writing-motoko/intercanister-calls.md @@ -94,7 +94,7 @@ actor AdvancedCanister1 { // Decode the return value let ?value : ?Nat = from_candid encodedValue - else Debug.trap("Error while decoding return value"); + else Debug.trap("Unexpected return value"); return value; } }