Call EVM From WASM
This guide will walk you through the process of calling EVM contracts from the WASM code e.g., Rust.
Prerequisites
Before you begin, ensure you have the following:
Git: Used for code management and obtain examples.
Rust: Install it from rust-lang.org.
Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain.
Setup 1: Install WASM Toolchains With Cargo
Install the pharos wasm toolchain plugin using the Cargo tool:
cargo install --git https://github.com/PharosNetwork/pharos-cargo-stylus
Add the wasm32-unknown-unknown
build target to your Rust compiler:
rustup target add wasm32-unknown-unknown
You should now have it available as a Cargo subcommand:
cargo stylus --help
Cargo subcommand for developing Pharos Stylus projects
Note: Pharos built the chain excution client from scratch using C++, and we design, developed and open-sourced the Dora VM, which combines EVM and WASM in a deeply compiled VM arch with native interoperability. The Rust toolchain and Rust SDK are forked from Stylus toolchain and SDK to facilitate easier adoption, the change here is the removal of Arbitrum ink billing and the simplification of the activation and compilation process on Pharos.
Setup 2: Set Up the Project
Clone the example repo:
git clone https://github.com/PharosNetwork/examples
cd examples/interoperability/call-evm-from-wasm
Setup 3: Write the Interoperability Contract
Create a new file for your interoperability contract:
touch src/lib.rs
Write the interoperability Contract:
Open
src/lib.rs
in your favorite text editor and add the following code:
#![cfg_attr(not(feature = "export-abi"), no_main)]
extern crate alloc;
use stylus_sdk::{
abi::Bytes,
alloy_primitives::{Address, U256},
prelude::*,
stylus_core::calls::context::Call,
};
use alloy_sol_types::{sol, SolCall};
sol! {
interface IErc20 {
function mint(uint256 value) external;
}
}
#[storage]
#[entrypoint]
pub struct Interoperability;
#[public]
impl Interoperability {
/// Here we can call an EVM contract with the given target address and calldata.
pub fn execute(&self, target: Address, data: Bytes) -> Bytes {
let result = self.vm().call(&Call::default(), target, &data);
result.unwrap().into()
}
/// Here we can transfer ETH to the given target address.
pub fn transfer_eth(&self, to: Address, amount: U256) -> Result<(), Vec<u8>> {
self.vm().transfer_eth(to, amount)
}
/// Here we call an ERC20 token contract written in Solidity.
pub fn mint_erc20(&self, erc20: Address, value: U256) -> Bytes {
self.execute(erc20, Bytes(IErc20::mintCall {
value,
}.abi_encode()))
}
}
Compile the Smart Contract:
Use cargo to compile the contract:
cargo stylus check --endpoint=<PHAROS_RPC_URL>
Step 4: Deploy the Interoperability Contract
cargo stylus deploy --private-key=<YOUR_PRIVATE_KEY> --endpoint=<PHAROS_RPC_URL>
Troubleshooting
Contract Deployment Fails: Ensure you have enough testnet tokens to cover the deployment cost.
Interaction Issues: Verify that the contract address and ABI are correct.
Insufficient Balance: Ensure your wallet has enough tokens to transfer.
Last updated