I tried to deploy a XVM Wasm contract wrapping around a deployed EVM Solidity contract in my local node.
Steps
- Deploy my EVM Solidity contract via Remix connecting to my local node
//SPDX-License-Identifier:MIT
pragma solidity 0.8.21;
contract Storage {
uint256 number;
function store(uint256 num) public {
number = num;
}
function retrieve() public view returns (uint256) {
return number;
}
}
- Write XVM Wasm contract:
#![cfg_attr(not(feature = "std"), no_std, no_main)] //added "no_main"
/// EVM ID (from astar runtime)
const EVM_ID: u8 = 0x0F;
#[ink::contract(env = xvm_environment::XvmDefaultEnvironment)]
mod store_xvm {
use ethabi::{
ethereum_types::{
H160,
U256,
},
Token,
};
use hex_literal::hex;
use ink::prelude::vec::Vec;
const STORE_SELECTOR: [u8; 4] = hex!["6057361d"];
#[ink(storage)]
pub struct StoreXvm {
evm_address: [u8; 20],
}
impl StoreXvm {
#[ink(constructor)]
pub fn new(evm_address: [u8; 20]) -> Self {
Self { evm_address }
}
#[ink(message)]
pub fn store(&mut self, value: u128) -> bool {
let encoded_input = Self::store_encode( value.into());
self.env()
.extension()
.xvm_call(
super::EVM_ID,
Vec::from(self.evm_address.as_ref()),
encoded_input, 0
)
.is_ok()
}
fn store_encode(value: U256) -> Vec<u8> {
let mut encoded = STORE_SELECTOR.to_vec();
let input = [Token::Uint(value)];
encoded.extend(ðabi::encode(&input));
encoded
}
}
}
My Cargo.toml
[package]
name = "store_xvm"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
[dependencies]
ink = { version = "4.3.0", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true }
ethabi = { git = "https://github.com/akru/ethabi", default-features = false }
hex-literal = "0.4.1"
xvm-environment = { path = "../../lib/xvm-environment", default-features = false }
[lib]
path = "lib.rs"
[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
"xvm-environment/std",
]
ink-as-dependency = []
- Compile my XVM Wasm contract:
$ cargo contract build
cargo contract version is 3.2.0, good for my Wasm Ink! 4.3.0 version
Compilation successful with
- store_xvm.contract (code + metadata)
- store_xvm.wasm (the contract's code)
- store_xvm.json (the contract's metadata)
- Use
https://ui.use.ink/
to deploy the compiled wasm with Solidity contract address.
Then I got Contract Reverted! DispatchError: DecodingFailed Input passed to a contract API function failed to decode as expected type...
Code Repo: https://github.com/AuroraLantean/ink-xvm-sdk
Reference:
https://medium.com/astar-network/cross-virtual-machine-creating-a-portal-to-the-future-of-smart-contracts-a96c6d2f79b8
https://theastarbulletin.news/how-to-implement-a-contract-using-xvm-1c94d2072c30
https://docs.astar.network/docs/learn/interoperability/xvm/#interfaces
Code are copied from https://github.com/realtakahashi/ink-xvm-sdk
and https://github.com/AstarNetwork/ink-xvm-sdk