EVM (Ethereum Virtual Machine): A computation engine that handles smart contract deployment and execution in Ethereum-compatible blockchains.
Commune is EVM compatible, we are currently in the process of testing this feature on our testnet
First, install the required Python packages:
pip install substrate-interface eth-account web3 python-dotenv communex
Create a .env
file in your project root and initialize it with the following:
ETH_PRIVATE_KEY=your_private_key_here SUBSTRATE_PRIVATE_KEY=your_sub_seed_here RPC_URL=https://testnet.api.communeai.net WS_URL=wss://testnet.api.communeai.net
Replace your_private_key_here
and your_sub_seed_here
with your actual keys.
To interact with smart contracts and use EVM features on Commune, you need to have funds in your EVM-compatible address. This section explains how to transfer funds from your native Commune address to your EVM address.
The EVM in Commune uses the secp256k1 key type, which differs from the native mainnet keys of type sr25519. To deploy your funds on the EVM, you'll need to use address mapping, converting your ss58 address to the corresponding h160 address.
import os import asyncio from dotenv import load_dotenv from substrateinterface import SubstrateInterface, Keypair from eth_account import Account from web3 import Web3 from eth_utils import to_bytes from scalecodec.utils.ss58 import ss58_encode import hashlib from communex.client import CommuneClient # Load environment variables from .env file load_dotenv() # Access environment variables ETH_PRIVATE_KEY = os.getenv('ETH_PRIVATE_KEY') SUBSTRATE_PRIVATE_KEY = os.getenv('SUBSTRATE_PRIVATE_KEY') RPC_URL = os.getenv('RPC_URL') WS_URL = os.getenv('WS_URL') def convert_h160_to_ss58(eth_address): # Ensure the eth_address starts with '0x' if not eth_address.startswith('0x'): eth_address = '0x' + eth_address # Convert the prefix to bytes prefix = b'evm:' # Convert the Ethereum address to bytes address_bytes = to_bytes(hexstr=eth_address) # Combine prefix and address combined = prefix + address_bytes # Hash the combined data using Blake2 256-bit blake2_hash = hashlib.blake2b(combined, digest_size=32).digest() # Convert the public key to SS58 format ss58_address = ss58_encode(blake2_hash, ss58_format=42) # Using 42 as the network ID, adjust as needed return ss58_address async def perform_transfer(): keypair = Keypair.create_from_uri(SUBSTRATE_PRIVATE_KEY) eth_account = Account.from_key(ETH_PRIVATE_KEY) recipient_ethereum_address = eth_account.address ss58_address = convert_h160_to_ss58(recipient_ethereum_address) print(f"Mirror: {ss58_address}") amount = 10_000_000_000 # 10 tokens client = CommuneClient(WS_URL) print(client.transfer(key=keypair, dest=ss58_address, amount=amount)) print(f"Transfer sent to {recipient_ethereum_address} (its ss58 mirror address is: {ss58_address})") if __name__ == "__main__": asyncio.run(perform_transfer())
You should now have funds in your EVM account.
Adapation of: Moonbeam article at https://docs.moonbeam.network/builders/ethereum/dev-env/remix/
Now that you have some funds in your EVM account, you can deploy a smart contract.
When you visit Remix, you'll notice it is divided into four main sections:
Plugin panel: This area displays icons for each preloaded plugin, the plugin manager, and settings menu. You'll see icons for File explorer, Search in files, Solidity compiler, and Deploy and run transactions. As you activate more plugins, their icons will appear here too.
Side panel: This shows the content of the currently active plugin. By default, you'll see the File explorer, which displays your workspace and files. Clicking other icons in the plugin panel will switch the content here.
Main panel: This is where you'll do most of your work. It opens with a "Home" tab full of helpful resources. You can always reopen this by clicking the blue Remix icon in the top left. As you open files, they'll appear as tabs here.
Terminal: This works like a standard terminal. You can run scripts, view logs, and even interact with Ethers and Web3 JavaScript libraries directly.
Let's create a simple ERC-20 token contract:
MyToken.sol
.// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract MyToken is ERC20 { constructor(uint256 initialSupply) ERC20("MyToken", "MYTOK") { _mint(msg.sender, initialSupply); } }
This contract creates a token called "MyToken" with the symbol "MYTOK". It mints the initial supply to the contract creator.
Before compiling, make sure you've selected the right file in the File explorer. Then:
ERC20.sol
.A green checkmark will appear next to the Solidity compiler icon if compilation is successful.
Now, let's deploy your contract to Commune:
MyToken.sol
is selected in the "CONTRACT" dropdown.Once deployed, you'll see transaction details in the Remix terminal and your contract will appear under "Deployed Contracts".
Under "Deployed Contracts", you'll see all the functions you can interact with:
To use a function:
For example, to use the approve
function:
To view your balance or transfer tokens, you'll need to add the token to your wallet. Check your wallet's documentation for instructions on adding custom tokens.
Remember, every interaction that changes the blockchain state will require a transaction, so keep an eye on your wallet for confirmation prompts.
To deploy Tokens on commune EVM, follow the guide here
Precompiles: Built-in contracts that can directly access and modify the blockchain's runtime storage, enabling powerful core-level operations.
We are actively working on completing the documentation for precompiles. In the meantime, you can refer to our Subspace Precompiles Section for preliminary information.