Skip to content

Commit

Permalink
refactor: use evm_rpc_types in Rust E2E canister (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanasa authored Oct 3, 2024
1 parent 93d3e9f commit 5f0221b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ic-cdk = { workspace = true }
ic-cdk-macros = { workspace = true }
serde = { workspace = true }
serde_bytes = { workspace = true }
evm_rpc_types = { path = "../../evm_rpc_types" }

[build-dependencies]
ic-cdk-bindgen = { workspace = true }
16 changes: 0 additions & 16 deletions e2e/rust/build.rs

This file was deleted.

48 changes: 30 additions & 18 deletions e2e/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
use candid::candid_method;
use ic_cdk_macros::update;
use std::str::FromStr;

mod declarations;
use candid::{candid_method, Principal};
use ic_cdk_macros::update;

use declarations::EVM_RPC_STAGING::{
BlockTag, EthMainnetService, GetBlockByNumberResult, MultiGetBlockByNumberResult,
ProviderError, RpcError, RpcService, RpcServices, EVM_RPC_STAGING as evm_rpc,
use evm_rpc_types::{
Block, BlockTag, EthMainnetService, Hex32, MultiRpcResult, ProviderError, RpcError, RpcService,
RpcServices,
};

fn main() {}

#[cfg(target_arch = "wasm32")]
const CANISTER_ID: Option<&str> = Some(std::env!(
"CANISTER_ID_EVM_RPC_STAGING",
"Unspecified canister ID environment variable"
));
#[cfg(not(target_arch = "wasm32"))]
const CANISTER_ID: Option<&str> = None;

#[update]
#[candid_method(update)]
pub async fn test() {
assert!(ic_cdk::api::is_controller(&ic_cdk::caller()));

let canister_id = Principal::from_str(CANISTER_ID.unwrap())
.expect("Error parsing canister ID environment variable");

// Define request parameters
let params = (
RpcService::EthMainnet(EthMainnetService::PublicNode), // Ethereum mainnet
Expand All @@ -24,15 +35,15 @@ pub async fn test() {

// Get cycles cost
let (cycles_result,): (Result<u128, RpcError>,) =
ic_cdk::api::call::call(evm_rpc.0, "requestCost", params.clone())
ic_cdk::api::call::call(canister_id, "requestCost", params.clone())
.await
.unwrap();
let cycles = cycles_result
.unwrap_or_else(|e| ic_cdk::trap(&format!("error in `request_cost`: {:?}", e)));

// Call without sending cycles
let (result_without_cycles,): (Result<String, RpcError>,) =
ic_cdk::api::call::call(evm_rpc.0, "request", params.clone())
ic_cdk::api::call::call(canister_id, "request", params.clone())
.await
.unwrap();
match result_without_cycles {
Expand All @@ -45,7 +56,7 @@ pub async fn test() {

// Call with expected number of cycles
let (result,): (Result<String, RpcError>,) =
ic_cdk::api::call::call_with_payment128(evm_rpc.0, "request", params, cycles)
ic_cdk::api::call::call_with_payment128(canister_id, "request", params, cycles)
.await
.unwrap();
match result {
Expand All @@ -61,8 +72,8 @@ pub async fn test() {
}

// Call a Candid-RPC method
let (results,): (MultiGetBlockByNumberResult,) = ic_cdk::api::call::call_with_payment128(
evm_rpc.0,
let (results,): (MultiRpcResult<Block>,) = ic_cdk::api::call::call_with_payment128(
canister_id,
"eth_getBlockByNumber",
(
RpcServices::EthMainnet(Some(vec![
Expand All @@ -79,18 +90,19 @@ pub async fn test() {
.await
.unwrap();
match results {
MultiGetBlockByNumberResult::Consistent(result) => match result {
GetBlockByNumberResult::Ok(block) => {
MultiRpcResult::Consistent(result) => match result {
Ok(block) => {
assert_eq!(
block.hash,
"0x114755458f57fe1a81e7b03e038ad00f9a675681c8b94cf102c30a84c5545c76"
Hex32::from_str(
"0x114755458f57fe1a81e7b03e038ad00f9a675681c8b94cf102c30a84c5545c76"
)
.unwrap()
);
}
GetBlockByNumberResult::Err(err) => {
ic_cdk::trap(&format!("error in `eth_getBlockByNumber`: {:?}", err))
}
Err(err) => ic_cdk::trap(&format!("error in `eth_getBlockByNumber`: {:?}", err)),
},
MultiGetBlockByNumberResult::Inconsistent(results) => ic_cdk::trap(&format!(
MultiRpcResult::Inconsistent(results) => ic_cdk::trap(&format!(
"inconsistent results in `eth_getBlockByNumber`: {:?}",
results
)),
Expand Down

0 comments on commit 5f0221b

Please sign in to comment.