Skip to content

Commit

Permalink
Add example for calling an Ethereum smart contract from Motoko (#3885)
Browse files Browse the repository at this point in the history
* Add example for calling an Ethereum smart contract from Motoko

* Apply suggestions from code review

Co-authored-by: Jessie Mongeon <[email protected]>

---------

Co-authored-by: Jessie Mongeon <[email protected]>
  • Loading branch information
rvanasa and jessiemongeon1 authored Dec 13, 2024
1 parent 81d0ccf commit 734eac6
Showing 1 changed file with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,65 @@ dfx canister call evm_rpc eth_getTransactionReceipt "(variant {$RPC_SOURCE}, $RP
<AdornedTabs groupId="languages">
<TabItem value="motoko" label="Motoko" default>

Calling an Ethereum smart contract from Motoko is possible but not yet officially supported.
```motoko no-repl
import EvmRpc "canister:evm_rpc";
import Cycles "mo:base/ExperimentalCycles";
import Debug "mo:base/Debug";
actor {
public func call() : async ?Text {
// Configure RPC request
let services = #EthMainnet(null);
let config = null;
// Add cycles to next call
Cycles.add<system>(2000000000);
// Call an Ethereum smart contract. The smart contract's information in this example is hard coded.
let result = await EvmRpc.eth_call(services, config, {
block = null;
transaction = {
to = ?"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
input = ?"0x70a08231000000000000000000000000b25eA1D493B49a1DeD42aC5B1208cC618f9A9B80"; // ABI-encoded
accessList = null;
blobVersionedHashes = null;
blobs = null;
chainId = null;
from = null;
gas = null;
gasPrice = null;
maxFeePerBlobGas = null;
maxFeePerGas = null;
maxPriorityFeePerGas = null;
nonce = null;
type_ = null;
value = null
};
});
// Process results
switch result {
// Prints the response if each RPC provider returns a consistent, successful result
case (#Consistent(#Ok response)) {
Debug.print("Success: " # debug_show response);
?response // ABI-encoded
};
// Trap is an RPC provider returns an error message consistent with the response of the other providers
case (#Consistent(#Err error)) {
Debug.trap("Error: " # debug_show error);
null
};
// Trap if an RPC provider returns a response inconsistent with the other providers
case (#Inconsistent(_results)) {
Debug.trap("Inconsistent results");
null
};
};
};
};
```

</TabItem>
<TabItem value="rust" label="Rust">
Expand Down

0 comments on commit 734eac6

Please sign in to comment.