Add pallet-revive to Your Runtime¶
Introduction¶
pallet-revive adds smart contract support to a Polkadot SDK runtime, and the Ethereum JSON-RPC adapter (eth-rpc) lets Ethereum tools talk to a chain that runs it. For an overview of what the pallet provides, see Add Smart Contract Functionality. This guide walks through the changes you make to your runtime:
- Add the
pallet-revivedependency. - Configure transaction fees in the way the pallet requires.
- Define the transaction extension and extrinsic types that let the runtime accept Ethereum transactions.
- Implement the pallet's
Configtrait, including the chain ID and precompiles. - Add the pallet to the runtime and implement the runtime APIs that
eth-rpccalls.
The code in this guide comes from runtimes in the Polkadot SDK at release polkadot-stable2609-1:
- Revive dev runtime: A minimal solochain runtime used by the Revive dev node.
- Kitchensink runtime: The Substrate node runtime, which configures every
Configitem explicitly. - Asset Hub Westend runtime: A parachain runtime with asset, XCM, and asset conversion precompiles.
Note
The pallet-revive Config trait changes between Polkadot SDK releases. For example, the Polkadot SDK master branch adds a MaxOutsideFrameLogs item that polkadot-stable2609-1 does not have. Compare the code in this guide with the reference runtimes of the release your runtime uses.
Prerequisites¶
Before you begin, make sure you have:
- Rust and the Polkadot SDK dependencies installed.
- A FRAME runtime that already includes
frame_system,pallet_balances,pallet_timestamp, andpallet_transaction_payment.pallet-reviveuses these pallets for accounts, balances, timestamps, and fees. - Familiarity with adding a pallet to a runtime, as described in Add an Existing Pallet to the Runtime.
Add the Dependency¶
How you add the dependency depends on how your runtime imports Polkadot SDK crates.
If your runtime depends on the polkadot-sdk umbrella crate, add pallet-revive to its features in runtime/Cargo.toml. The Revive dev runtime's Cargo.toml uses this approach:
polkadot-sdk = { workspace = true, features = [
"pallet-balances",
"pallet-revive",
"pallet-sudo",
"pallet-timestamp",
"pallet-transaction-payment",
# ... other features
] }
The polkadot-sdk/std entry in your std feature list also enables the standard library features of pallet-revive.
If your runtime depends on individual crates, add pallet-revive and propagate its features. The Asset Hub Westend Cargo.toml uses this approach:
Configure Transaction Fees¶
pallet-revive maps Ethereum gas to Polkadot SDK weight and fees, and this mapping depends on the fee structure. The pallet supports only one WeightToFee implementation for pallet_transaction_payment: BlockRatioFee, which enforces a ratio between ref_time and proof_size fees that matches their distribution in the block limits.
Set WeightToFee in your pallet_transaction_payment::Config implementation. The kitchensink runtime uses:
impl pallet_transaction_payment::Config for Runtime {
// ... other configuration
type WeightToFee = pallet_revive::evm::fees::BlockRatioFee<1, 1, Self, Balance>;
}
The two numeric parameters define the rational number that maps ref_time to fees, and neither can be zero.
Accept Ethereum Transactions¶
The eth-rpc adapter submits Ethereum transactions to the chain through the eth_transact call of pallet-revive. The runtime needs three types to accept them: a transaction extension pipeline that includes SetOrigin, an EthExtra implementation, and the pallet-revive extrinsic type.
-
Add
pallet_revive::evm::tx_extension::SetOriginto your transaction extension tuple, after all extensions that rely on a signed origin. The Revive dev runtime uses the following pipeline:runtime/src/lib.rstype TxExtension = ( frame_system::CheckNonZeroSender<Runtime>, frame_system::CheckSpecVersion<Runtime>, frame_system::CheckTxVersion<Runtime>, frame_system::CheckGenesis<Runtime>, frame_system::CheckEra<Runtime>, frame_system::CheckNonce<Runtime>, frame_system::CheckWeight<Runtime>, pallet_transaction_payment::ChargeTransactionPayment<Runtime>, // Needs to be done after all extensions that rely on a signed origin. pallet_revive::evm::tx_extension::SetOrigin<Runtime>, // Must be last to catch the refund in previous transaction extensions. frame_system::WeightReclaim<Runtime>, ); -
Implement the
EthExtratrait. Itsget_eth_extensionfunction builds the extension values that the runtime applies to an Ethereum transaction, using the nonce and tip taken from that transaction. Return one value per entry in yourTxExtensiontuple, in the same order, and constructSetOriginwithnew_from_eth_transaction(). The Revive dev runtime's implementation is:runtime/src/lib.rsuse pallet_revive::evm::runtime::EthExtra; /// Default extensions applied to Ethereum transactions. #[derive(Clone, PartialEq, Eq, Debug)] pub struct EthExtraImpl; impl EthExtra for EthExtraImpl { type Config = Runtime; type ExtensionV0 = TxExtension; type ExtensionOtherVersions = sp_runtime::traits::InvalidVersion; fn get_eth_extension(nonce: u32, tip: Balance) -> Self::ExtensionV0 { ( frame_system::CheckNonZeroSender::<Runtime>::new(), frame_system::CheckSpecVersion::<Runtime>::new(), frame_system::CheckTxVersion::<Runtime>::new(), frame_system::CheckGenesis::<Runtime>::new(), frame_system::CheckMortality::from(sp_runtime::generic::Era::Immortal), frame_system::CheckNonce::<Runtime>::from(nonce), frame_system::CheckWeight::<Runtime>::new(), pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(tip), pallet_revive::evm::tx_extension::SetOrigin::<Runtime>::new_from_eth_transaction(), frame_system::WeightReclaim::<Runtime>::new(), ) } }The extension must include
frame_system::CheckNonceso that the runtime checks the nonce of the Ethereum transaction. -
Replace your runtime's
UncheckedExtrinsictype with thepallet-reviveUncheckedExtrinsic, which wraps the generic extrinsic type and adds support foreth_transact:
If your runtime already uses a wrapping extension, such as cumulus_pallet_weight_reclaim::StorageWeightReclaim in a parachain, keep it and add SetOrigin inside the wrapped tuple. The Asset Hub Westend runtime shows this pattern.
Implement the Config Trait¶
Implement pallet_revive::Config for your runtime. The following implementation combines the kitchensink runtime's deposit parameters and Config implementation, with its precompiles removed. deposit is a helper in the kitchensink runtime that prices storage items and bytes in the native token; use your runtime's equivalent.
parameter_types! {
pub const DepositPerItem: Balance = deposit(1, 0);
pub const DepositPerChildTrieItem: Balance = deposit(1, 0) / 100;
pub const DepositPerByte: Balance = deposit(0, 1);
pub CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(30);
pub const MaxEthExtrinsicWeight: FixedU128 = FixedU128::from_rational(9, 10);
}
impl pallet_revive::Config for Runtime {
type Time = Timestamp;
type Balance = Balance;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type RuntimeOrigin = RuntimeOrigin;
type DepositPerItem = DepositPerItem;
type DepositPerChildTrieItem = DepositPerChildTrieItem;
type DepositPerByte = DepositPerByte;
type WeightInfo = pallet_revive::weights::SubstrateWeight<Self>;
type Precompiles = ();
type AddressMapper = pallet_revive::AccountId32Mapper<Self>;
type RuntimeMemory = ConstU32<{ 128 * 1024 * 1024 }>;
type PVFMemory = ConstU32<{ 512 * 1024 * 1024 }>;
type UploadOrigin = EnsureSigned<Self::AccountId>;
type InstantiateOrigin = EnsureSigned<Self::AccountId>;
type RuntimeHoldReason = RuntimeHoldReason;
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type ChainId = ConstU64<420_420_420>;
type NativeToEthRatio = ConstU32<1_000_000>; // 10^(18 - 12) Eth is 10^18, Native is 10^12.
type FindAuthor = <Runtime as pallet_authorship::Config>::FindAuthor;
type AllowEVMBytecode = ConstBool<true>;
type FeeInfo = pallet_revive::evm::fees::Info<Address, Signature, EthExtraImpl>;
type MaxEthExtrinsicWeight = MaxEthExtrinsicWeight;
type DebugEnabled = ConstBool<false>;
type AutoMap = ConstBool<false>;
type GasScale = ConstU32<1000>;
type OnBurn = ();
type Deposit = ();
}
The Config items fall into the following groups. For the full description of each item, see the Config trait source.
- Runtime types:
RuntimeEvent,RuntimeCall,RuntimeOrigin, andRuntimeHoldReasonlink the pallet to the runtime's aggregated types. - Currency and time:
Currencyis the fungible token that pays fees and holds contract balances,Balanceis its balance type, andTimesupplies timestamps to contracts. - Storage deposits:
DepositPerByte,DepositPerItem, andDepositPerChildTrieItemset what callers pay for storage.CodeHashLockupDepositPercentsets the share of the code deposit held when a contract is instantiated from a code hash.Depositselects the payment backend for deposits;()always uses the native currency. - Ethereum compatibility:
ChainIdis the chain ID reported to Ethereum tools.NativeToEthRatiois the ratio between the decimal representation of the native token and ETH's 18 decimals.FeeInfomust bepallet_revive::evm::fees::Infoin a production runtime.AddressMappermaps between native accounts and Ethereum addresses, and must beAccountId32MapperorH160Mapper. - Gas and weight:
GasScalescales the gas price and gas estimates, so that gas costs are closer to standard EVM values, and cannot be0.MaxEthExtrinsicWeightcaps the weight ofeth_transactextrinsics as a fraction of the maximum extrinsic weight. - Code and origins:
AllowEVMBytecodeallows EVM bytecode to be uploaded and instantiated, in addition to PVM bytecode.UploadOriginandInstantiateOrigincontrol who can upload and instantiate contracts;EnsureSignedallows anyone. - Memory:
RuntimeMemoryandPVFMemoryare the memory available to the runtime and to the PoV validation function. The pallet's integrity test uses them to check that the runtime has enough memory. - Other:
Precompilesregisters additional precompiles,FindAuthorfinds the block author,WeightInfoprovides benchmarked weights,OnBurnreceives native currency burned through gas rounding,DebugEnabledenables debug settings such as unlimited contract size, andAutoMapenables automatic account mapping.
The Asset Hub Westend runtime differs from this configuration in several items. For example, it registers asset, XCM, asset conversion, and vesting precompiles, sets AutoMap to true, and uses a different chain ID. See its Config implementation.
Note
The Revive dev runtime uses #[derive_impl(pallet_revive::config_preludes::TestDefaultConfig)] to fill in most items. That prelude provides default configurations for testing environments, for example, WeightInfo = () and FeeInfo = (). Set every item explicitly in a production runtime, as the kitchensink and Asset Hub Westend runtimes do.
Set the Chain ID¶
ChainId is the EIP-155 chain ID, which identifies your chain to Ethereum wallets and tools and prevents transactions signed for one chain from being replayed on another. The eth-rpc adapter reads this value from the runtime and returns it for eth_chainId.
Choose a value that no other network uses. The reference runtimes use 420_420_420 (kitchensink and Revive dev runtime) and 420_420_421 (Asset Hub Westend), so do not reuse those values for your own network.
Enable Automatic Account Mapping¶
Native AccountId32 accounts must be mapped to Ethereum addresses before they can interact with contracts. By default, accounts map themselves with the map_account call. If you set AutoMap to true, the pallet maps accounts automatically when they are created and unmaps them when they are removed, and disables the map_account and unmap_account calls.
AutoMap requires you to set pallet_revive::AutoMapper as the account creation and removal hooks in frame_system::Config. The Revive dev runtime does this:
impl frame_system::Config for Runtime {
// ... other configuration
type OnNewAccount = pallet_revive::AutoMapper<Runtime>;
type OnKilledAccount = pallet_revive::AutoMapper<Runtime>;
}
Add the Pallet to the Runtime¶
Add pallet-revive to your runtime under the name Revive. The runtime API macro in the next section takes this name as a parameter, and the eth-rpc adapter's client looks up the pallet's constants by it.
The Revive dev runtime declares the pallet as follows. Use a pallet index that is unused in your runtime.
The Asset Hub Westend runtime declares the pallet as follows. Use a pallet index that is unused in your runtime.
Implement the Runtime APIs¶
The eth-rpc adapter calls the ReviveApi runtime API for balances, nonces, calls, gas estimation, gas price, block information, and tracing. pallet-revive provides the impl_runtime_apis_plus_revive_traits! macro, which wraps impl_runtime_apis!, implements ReviveApi, and implements the SetWeightLimit trait for the runtime call type.
Replace impl_runtime_apis! with this macro and pass four parameters before your existing runtime API implementations:
Runtime: The runtime type.Revive: The name under which the pallet is declared in the runtime.Executive: The runtime'sExecutivetype.EthExtraImpl: YourEthExtraimplementation.
The Revive dev runtime uses it as follows:
pallet_revive::impl_runtime_apis_plus_revive_traits!(
Runtime,
Revive,
Executive,
EthExtraImpl,
impl apis::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
VERSION
}
// ... other functions
}
// ... other runtime API implementations
);
Keep all the runtime API implementations you already have inside the macro call; do not implement ReviveApi yourself.
Register Precompiles¶
pallet-revive includes the standard Ethereum precompiles and its own system precompiles. To expose more runtime functionality to contracts, pass a tuple of types that implement the Precompile trait to Config::Precompiles. The type is usually provided by another pallet. To write your own, see Write a Custom Precompile.
For example, the kitchensink runtime registers ERC-20 precompiles for two instances of the Assets pallet and a vesting precompile:
use pallet_assets_precompiles::{InlineIdConfig, ERC20};
use pallet_vesting_precompiles::Vesting as VestingPrecompile;
impl pallet_revive::Config for Runtime {
// ... other configuration
type Precompiles = (
ERC20<Self, InlineIdConfig<0x1>, Instance1>,
ERC20<Self, InlineIdConfig<0x2>, Instance2>,
VestingPrecompile<Self>,
);
}
Each precompile crate is a separate dependency of your runtime, and some, such as the vesting precompile, also require their own pallet in the runtime. The Asset Hub Westend runtime additionally registers pallet_xcm_precompiles::XcmPrecompile and the asset conversion precompile. For the precompiles that contracts can call, see Precompiles.
Configure Genesis¶
The pallet-revive genesis configuration has a mapped_accounts field that lists native accounts to map at genesis, so they can interact with contracts from the first block. The Revive dev runtime's development preset maps its endowed accounts:
revive: ReviveConfig {
mapped_accounts: endowed_accounts
.iter()
.filter(|x| !<Runtime as pallet_revive::Config>::AddressMapper::is_mapped(x))
.cloned()
.collect(),
},
Verify the Integration¶
- Build your runtime and node, then start the node locally with its development configuration.
- Build and start the
eth-rpcadapter against your node, as described in Run the Ethereum JSON-RPC Adapter. -
Request the chain ID through the adapter:
curl -s -X POST http://127.0.0.1:8545 \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","method":"eth_chainId","params":[],"id":1}'The result is your
ChainIdvalue in hexadecimal. If the adapter fails to start, check its logs for missing runtime APIs or constants.
Where to Go Next¶
-
Guide Run the Ethereum JSON-RPC Adapter
Build, run, and configure the
eth-rpcadapter next to your node. -
Learn Smart Contracts Overview
Learn how
pallet-revive, its execution backends, and its tooling fit together.
| Created: November 17, 2026