Access EVM Accounts
Illustrates how to integrate access to Ethereum-based accounts and contracts through a Substrate blockchain node.
Last updated
Illustrates how to integrate access to Ethereum-based accounts and contracts through a Substrate blockchain node.
Last updated
This tutorial illustrates how to use crates from the project to build an Ethereum-compatible blockchain that can access Ethereum-based accounts and execute Solidity-based smart contracts. The two main goals of the Frontier project are to enable you to do the following:
Run Ethereum decentralized apps unmodified using local Substrate nodes.
Import state from the Ethereum main network.
This tutorial uses a predefined node template to provide a working environment. The template was generated using the instructions in the .
If you want to generate a standalone template for yourself, you can use the template generation script. If you build your own node using from the repository or using the template generation script, be aware that Frontier uses its own versions of Substrate crates and you might need to update the dependencies in your Cargo
files to match the dependencies in your project.
You should have completed the following Substrate tutorials before attempting this tutorial:
From the tutorials, you should be familiar with how to perform the following tasks:
Launch a Substrate blockchain node.
Add, remove, and configure pallets in a runtime.
Submit transactions by connecting to a node using Polkadot-JS or another front-end.
Before starting this tutorial, you should also be familiar with the following:
Ethereum core concepts and terminology
Ethereum Virtual Machine (EVM) basics
Decentralized applications and smart contracts
Pallet design principles
To compile the Frontier node template:
Open a terminal shell on your computer.
Clone the node template repository by running the following command:
Change to the root of the node template directory by running the following command:
Compile the node template by running the following command:
After your node compiles, you must start the node to begin exploring the preconfigured EVM accounts.
To start the local Substrate node:
Open a terminal shell on your local computer, if needed.
Change to the root directory where you compiled the frontier-node-template
.
Start the node in development mode by running the following command:
The --dev
command-line option specifies that the node runs using the predefined development
chain specification that includes the predefined EVM account for alice
and other accounts for testing.
Verify your node is up and running successfully by reviewing the output displayed in the terminal.
The terminal should display output similar to this:
Click Settings, then click Developer.
Define the following account information to create an EVM Account
type and enable the account to send transactions and to inspect blocks.
To send transactions, you must define Address
and LookupSource
settings.
To inspect blocks, you must define Transaction
and Signature
settings.
Click Save.
After you have configured settings for the EVM account, you can use the Polkadot-JS application to view information about the EVM account for alice
.
Click Developer, then select RPC calls.
On the Submission tab, select eth as the endpoint to call.
Select getBalance(address, number) from the list of functions to call.
Specify the EVM account identifier for the alice
account for the address.
Click Submit RPC call.
The call should return output similar to the following:
Create the ERC-20 contract.
Click Developer, then select Extrinsics.
Select the ALICE development account as the account used to submit the transaction.
Select evm.
Select the create function.
Configure the parameters for the function.
source
0xd43593c715fdd31c61141abd04a99fd6822c8558
init
raw bytecode
hex value from MyToken.json
value
0
gasLimit
4294967295
maxFeePerGas
100000000
You can leave optional parameters empty. The value for the nonce
will increment the known nonce for the source account, starting from 0x0
. Depending on the function you selected, you might need to remove unused parameters.
Click Submit Transaction.
Click Sign and Submit to authorize the transaction.
Click Network, then select Explorer.
Click the evm.Created event to verify the address of the newly-created contract is 0x8a50db1e0f9452cfd91be8dc004ceb11cb08832f
.
You can also view details about the transaction using the Console in the Developer Tools for your browser.
Because EVM contract addresses are determined by the account identifier and nonce of the contract creator, the address where the contract is deployed is calculated using the well-known account identifier 0xd43593c715fdd31c61141abd04a99fd6822c8558
and nonce 0x0
for the alice
account.
Click Developer, then select Chain State.
Select evm as the state to query and accountCodes.
Specify the account identifier 0xd43593c715fdd31c61141abd04a99fd6822c8558
for the alice
account and notice that the account code is empty (0x
)
Specify the contract address 0x8a50db1e0f9452cfd91be8dc004ceb11cb08832f
for the contract you deployed using the alice
development account and notice that contract account code is the bytecode from the Solidity contract.
To query the account storage associated with the smart contract:
In Chain State with evm as the state to query, select accountStorages.
Specify the ERC-20 contract address 0x8a50db1e0f9452cfd91be8dc004ceb11cb08832f
as the first parameter.
Specify the storage slot to read as the second parameter 0x045c0350b9cf0df39c4b40400c965118df2dca5ce0fbcf0de4aafc099aea4a14
.
The value that is returned should be 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
.
If you check the balance for the alice
account after deploying the contract, you see that a fee was withdrawn from the account and the getBalance(address, number)
call returns a value similar to the following:
So far, you have worked exclusively with the alice
development account. The next step is to use the deployed contract to transfer tokens to another account.
Click Developer, then select Extrinsics.
Select the ALICE development account as the account used to submit the transaction.
Select evm.
Select call to invoke the transfer(address, uint256)
function on the ERC-20 contract.
Configure the parameters for the function.
source
0xd43593c715fdd31c61141abd04a99fd6822c8558
target
0x8a50db1e0f9452cfd91be8dc004ceb11cb08832f
input
0xa9059cbb0000000000000000000000008eaf04151687736326c9fea17e25fc528761369300000000000000000000000000000000000000000000000000000000000000dd
value
0
gasLimit
4294967295
maxFeePerGas
100000000
The source
represents the account holding the tokens. In this case, the source
is the EVM account for alice
, the contract creator. The target
is the contract address for the transfer of tokens from alice
to bob
.
The input
parameter is an EVM ABI-encoded function call that specifies the function call to perform a transfer (0xa9059cbb
) and the arguments the function requires. For this function, the arguments are the bob
EVM account identifier (0x8eaf04151687736326c9fea17e25fc5287613693
) and the number of tokens to be transferred (221 or 0xdd
in hex).
Click Submit Transaction.
Click Sign and Submit to authorize the transaction.
Click Network, then select Explorer.
Click the evm.Executed event to verify the address of the executed contract is 0x8a50db1e0f9452cfd91be8dc004ceb11cb08832f
.
Click Developer, then select Chain State.
Select evm as the state to query and accountStorages.
Check the storage for the contract address 0x8a50db1e0f9452cfd91be8dc004ceb11cb08832f
and storage slot 0x045c0350b9cf0df39c4b40400c965118df2dca5ce0fbcf0de4aafc099aea4a14
.
If you check the balance for the alice
account after deploying the contract, you see that a fee was withdrawn from the account and the getBalance(address, number)
call returns a value similar to the following:
The development in the frontier-node-template
defines a genesis block that is been pre-configured with an EVM account for the alice
account. When you start this node in development mode, the EVM account for alice
is funded with a default amount of Ether. You'll be using this account to view EVM account details and to call Ethereum smart contracts. After you start the node, you'll be able to use the to see the details of the EVM account for alice
.
The provides a working development environment so that you can start building on Substrate right away.
Connect to the local node using the .
Verify that your node is still running and the is connected to the node.
The predefined account address is 0xd43593c715fdd31c61141abd04a99fd6822c8558
. The address for the account was calculated from the public key for the alice
account using .
Now that you'e seen how to query the balance for an Ethereum address, you might want to explore how you can deploy and call Ethereum smart contracts and test the related functionality. This tutorial uses a sample contract that defines an . You can also create an ERC-20 token contract using Polkadot JS SDK and .
For convenience, you can use the compiled bytecode
from the token contract in to deploy the contract on the Substrate blockchain.
Verify that your node is still running and the is connected to the node.
After you submit the transaction, the contract is deployed on the network and you can view information about it using the .
Verify that your node is still running and the is connected to the node.
The ERC-20 contract you deployed is based on the . This contract includes a constructor that mints a maximum number of tokens and stores them in the account associated with the contract creator.
The storage slot for the address was calculated using based on slot 0 and the account identifier 0xd43593c715fdd31c61141abd04a99fd6822c8558
.
Verify that your node is still running and the is connected to the node.
The input value in this tutorial was calculated using the .
After you submit the transaction, you can verify the token transfer using the .
Verify that your node is still running and the is connected to the node.