Pharos Docs
  • Introduction
    • About Pharos Network
    • Vision & Mision
    • Why Pharos Network
    • Concepts
      • Degree of Parallelism (DP)
  • Architecture
    • Pharos Modular Stack
    • Node Architecture
      • About Pharos Nodes
  • Core Technologies
    • Pharos Consensus
    • Pharos Execution
      • Why A New Blockchain Compute Model
      • Pharos VM
    • Pharos Pipelining
    • Pharos Store
      • Why We Need a Blockchain-Native Store
    • Pharos SPNs
  • Network Overview
    • Pharos Networks
      • Pharos Testnet Information
    • Pharos Gas Model
    • FAQ
  • Node & Validator Guide
    • Validator Requirements
    • Validator Node Deployment
      • Using Docker (Devnet)
      • Using Docker (Testnet)
    • Node Management
    • Rapid Node Initialization
      • Rapid Node Initialization(Testnet)
      • Rapid Node Initialization(Devnet)
    • Pharos Network Snapshots
    • Node Debugging & Configuration
  • Pharos Node Monitoring
  • Developer Guide
    • Foundry
      • Write Your First dApp
      • Write Your First Token
      • Write Your First NFT
      • Write Your First Uniswap Contract
    • Hardhat
      • Write Your First dApp
      • Write Your First Token
      • Write Your First NFT
      • Write Your First Uniswap Contract
    • Rust
    • Interoperability
      • Call EVM From WASM
  • API & SDK
    • JSON-RPC API Methods
  • Resources
    • EVM
    • Solidity
Powered by GitBook
On this page
  • Prerequisites
  • Setup 1: Install WASM Toolchains With Cargo
  • Setup 2: Set Up the Project
  • Setup 3: Write the Interoperability Contract
  • Step 4: Deploy the Interoperability Contract
  • Troubleshooting
Export as PDF
  1. Developer Guide
  2. Interoperability

Call EVM From WASM

PreviousInteroperabilityNextJSON-RPC API Methods

Last updated 21 days ago

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 .

  • 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 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.

rust-lang.org
Dora