Skip to content

Commit

Permalink
Add account_call and account_create in EVM Runtime RPC (#2698)
Browse files Browse the repository at this point in the history
* Add account_call and account_create in EVM Runtime RPC

* use get_or_create_evm_address

* add EVMRuntimeApi

* add account_call and account_create without new version
  • Loading branch information
zjb0807 authored Jan 30, 2024
1 parent 97bfc23 commit 06e988b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
24 changes: 23 additions & 1 deletion modules/evm/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ use sp_std::vec::Vec;

sp_api::decl_runtime_apis! {
#[api_version(2)]
pub trait EVMRuntimeRPCApi<Balance> where
pub trait EVMRuntimeRPCApi<Balance, AccountId> where
Balance: Codec + MaybeDisplay + MaybeFromStr,
AccountId: Codec + MaybeDisplay + MaybeFromStr,
{
fn call(
from: H160,
Expand All @@ -56,5 +57,26 @@ sp_api::decl_runtime_apis! {
fn get_estimate_resources_request(data: Vec<u8>) -> Result<EstimateResourcesRequest, sp_runtime::DispatchError>;

fn block_limits() -> BlockLimits;

fn account_call(
from: AccountId,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CallInfo, sp_runtime::DispatchError>;

fn account_create(
from: AccountId,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CreateInfo, sp_runtime::DispatchError>;
}
}
35 changes: 33 additions & 2 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use module_currencies::BasicCurrencyAdapter;
use module_evm::{runner::RunnerExtended, CallInfo, CreateInfo, EvmChainId, EvmTask};
use module_evm_accounts::EvmAddressMapping;
use module_relaychain::RelayChainCallBuilder;
use module_support::{AssetIdMapping, DispatchableTask, PoolId};
use module_support::{AddressMapping, AssetIdMapping, DispatchableTask, PoolId};
use module_transaction_payment::TargetedFeeAdjustment;

use cumulus_pallet_parachain_system::RelaychainDataProvider;
Expand Down Expand Up @@ -2102,7 +2102,7 @@ sp_api::impl_runtime_apis! {
}
}

impl module_evm_rpc_runtime_api::EVMRuntimeRPCApi<Block, Balance> for Runtime {
impl module_evm_rpc_runtime_api::EVMRuntimeRPCApi<Block, Balance, AccountId> for Runtime {
fn block_limits() -> BlockLimits {
BlockLimits {
max_gas_limit: runtime_common::EvmLimits::<Runtime>::max_gas_limit(),
Expand Down Expand Up @@ -2187,6 +2187,37 @@ sp_api::impl_runtime_apis! {

request.ok_or(sp_runtime::DispatchError::Other("Invalid parameter extrinsic, not evm Call"))
}

// required by xtokens precompile
#[transactional]
fn account_call(
from: AccountId,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CallInfo, sp_runtime::DispatchError> {
let from = EvmAddressMapping::<Runtime>::get_or_create_evm_address(&from);

Self::call(from, to, data, value, gas_limit, storage_limit, access_list, estimate)
}

fn account_create(
from: AccountId,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CreateInfo, sp_runtime::DispatchError> {
let from = EvmAddressMapping::<Runtime>::get_or_create_evm_address(&from);

Self::create(from, data, value, gas_limit, storage_limit, access_list, estimate)
}
}

impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
Expand Down
35 changes: 33 additions & 2 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use module_currencies::BasicCurrencyAdapter;
use module_evm::{runner::RunnerExtended, CallInfo, CreateInfo, EvmChainId, EvmTask};
use module_evm_accounts::EvmAddressMapping;
use module_relaychain::RelayChainCallBuilder;
use module_support::{AssetIdMapping, DispatchableTask, ExchangeRateProvider, FractionalRate, PoolId};
use module_support::{AddressMapping, AssetIdMapping, DispatchableTask, ExchangeRateProvider, FractionalRate, PoolId};
use module_transaction_payment::TargetedFeeAdjustment;

use cumulus_pallet_parachain_system::RelaychainDataProvider;
Expand Down Expand Up @@ -2104,7 +2104,7 @@ impl_runtime_apis! {
}
}

impl module_evm_rpc_runtime_api::EVMRuntimeRPCApi<Block, Balance> for Runtime {
impl module_evm_rpc_runtime_api::EVMRuntimeRPCApi<Block, Balance, AccountId> for Runtime {
fn block_limits() -> BlockLimits {
BlockLimits {
max_gas_limit: runtime_common::EvmLimits::<Runtime>::max_gas_limit(),
Expand Down Expand Up @@ -2189,6 +2189,37 @@ impl_runtime_apis! {

request.ok_or(sp_runtime::DispatchError::Other("Invalid parameter extrinsic, not evm Call"))
}

// required by xtokens precompile
#[transactional]
fn account_call(
from: AccountId,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CallInfo, sp_runtime::DispatchError> {
let from = EvmAddressMapping::<Runtime>::get_or_create_evm_address(&from);

Self::call(from, to, data, value, gas_limit, storage_limit, access_list, estimate)
}

fn account_create(
from: AccountId,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CreateInfo, sp_runtime::DispatchError> {
let from = EvmAddressMapping::<Runtime>::get_or_create_evm_address(&from);

Self::create(from, data, value, gas_limit, storage_limit, access_list, estimate)
}
}

impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
Expand Down
36 changes: 33 additions & 3 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use module_currencies::{BasicCurrencyAdapter, Currency};
use module_evm::{runner::RunnerExtended, CallInfo, CreateInfo, EvmChainId, EvmTask};
use module_evm_accounts::EvmAddressMapping;
use module_relaychain::RelayChainCallBuilder;
use module_support::{AssetIdMapping, DispatchableTask, ExchangeRateProvider, FractionalRate, PoolId};
use module_support::{AddressMapping, AssetIdMapping, DispatchableTask, ExchangeRateProvider, FractionalRate, PoolId};
use module_transaction_payment::TargetedFeeAdjustment;
use parity_scale_codec::{Decode, DecodeLimit, Encode};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -2309,7 +2309,7 @@ impl_runtime_apis! {
}
}

impl module_evm_rpc_runtime_api::EVMRuntimeRPCApi<Block, Balance> for Runtime {
impl module_evm_rpc_runtime_api::EVMRuntimeRPCApi<Block, Balance, AccountId> for Runtime {
fn block_limits() -> BlockLimits {
BlockLimits {
max_gas_limit: runtime_common::EvmLimits::<Runtime>::max_gas_limit(),
Expand Down Expand Up @@ -2394,6 +2394,37 @@ impl_runtime_apis! {

request.ok_or(sp_runtime::DispatchError::Other("Invalid parameter extrinsic, not evm Call"))
}

// required by xtokens precompile
#[transactional]
fn account_call(
from: AccountId,
to: H160,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CallInfo, sp_runtime::DispatchError> {
let from = EvmAddressMapping::<Runtime>::get_or_create_evm_address(&from);

Self::call(from, to, data, value, gas_limit, storage_limit, access_list, estimate)
}

fn account_create(
from: AccountId,
data: Vec<u8>,
value: Balance,
gas_limit: u64,
storage_limit: u32,
access_list: Option<Vec<AccessListItem>>,
estimate: bool,
) -> Result<CreateInfo, sp_runtime::DispatchError> {
let from = EvmAddressMapping::<Runtime>::get_or_create_evm_address(&from);

Self::create(from, data, value, gas_limit, storage_limit, access_list, estimate)
}
}

impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
Expand Down Expand Up @@ -2511,7 +2542,6 @@ mod tests {
use super::*;
use frame_support::{dispatch::DispatchInfo, traits::WhitelistedStorageKeys};
use frame_system::offchain::CreateSignedTransaction;
use module_support::AddressMapping;
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::traits::SignedExtension;
use std::collections::HashSet;
Expand Down

0 comments on commit 06e988b

Please sign in to comment.