Skip to content

Commit

Permalink
Get rid of the convert ratio on the rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Nov 14, 2024
1 parent 0604cbc commit 0bd3f74
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 57 deletions.
17 changes: 6 additions & 11 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ use frame_support::{
ord_parameter_types, parameter_types,
traits::{
fungible, fungibles,
tokens::{
imbalance::ResolveAssetTo, nonfungibles_v2::Inspect, Fortitude::Polite,
Preservation::Expendable,
},
tokens::{imbalance::ResolveAssetTo, nonfungibles_v2::Inspect},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, InstanceFilter,
Nothing, TransformOrigin,
},
Expand All @@ -68,7 +65,7 @@ use parachains_common::{
NORMAL_DISPATCH_RATIO,
};
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160};
use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H160, U256};
use sp_runtime::{
generic, impl_opaque_keys,
traits::{AccountIdConversion, BlakeTwo256, Block as BlockT, Saturating, Verify},
Expand Down Expand Up @@ -127,7 +124,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: alloc::borrow::Cow::Borrowed("westmint"),
impl_name: alloc::borrow::Cow::Borrowed("westmint"),
authoring_version: 1,
spec_version: 1_016_005,
spec_version: 1_016_006,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 16,
Expand Down Expand Up @@ -2080,10 +2077,8 @@ impl_runtime_apis! {

impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber, EventRecord> for Runtime
{
fn balance(address: H160) -> Balance {
use frame_support::traits::fungible::Inspect;
let account = <Runtime as pallet_revive::Config>::AddressMapper::to_account_id(&address);
Balances::reducible_balance(&account, Expendable, Polite)
fn balance(address: H160) -> U256 {
Revive::evm_balance(&address)
}

fn nonce(address: H160) -> Nonce {
Expand All @@ -2093,7 +2088,7 @@ impl_runtime_apis! {
fn eth_transact(
from: H160,
dest: Option<H160>,
value: Balance,
value: U256,
input: Vec<u8>,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
Expand Down
11 changes: 5 additions & 6 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use frame_support::{
},
tokens::{
imbalance::ResolveAssetTo, nonfungibles_v2::Inspect, pay::PayAssetFromAccount,
Fortitude::Polite, GetSalary, PayFromAccount, Preservation::Preserve,
GetSalary, PayFromAccount,
},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, ConstU64, Contains,
Currency, EitherOfDiverse, EnsureOriginWithArg, EqualPrivilegeOnly, Imbalance, InsideBoth,
Expand Down Expand Up @@ -86,6 +86,7 @@ use pallet_nis::WithMaximumOf;
use pallet_nomination_pools::PoolId;
use pallet_revive::{evm::runtime::EthExtra, AddressMapper};
use pallet_session::historical as pallet_session_historical;
use sp_core::U256;
// Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles
// <https://github.com/paritytech/polkadot-sdk/issues/226>
use pallet_broker::TaskId;
Expand Down Expand Up @@ -3205,10 +3206,8 @@ impl_runtime_apis! {

impl pallet_revive::ReviveApi<Block, AccountId, Balance, Nonce, BlockNumber, EventRecord> for Runtime
{
fn balance(address: H160) -> Balance {
use frame_support::traits::fungible::Inspect;
let account = <Runtime as pallet_revive::Config>::AddressMapper::to_account_id(&address);
Balances::reducible_balance(&account, Preserve, Polite)
fn balance(address: H160) -> U256 {
Revive::evm_balance(&address)
}

fn nonce(address: H160) -> Nonce {
Expand All @@ -3219,7 +3218,7 @@ impl_runtime_apis! {
fn eth_transact(
from: H160,
dest: Option<H160>,
value: Balance,
value: U256,
input: Vec<u8>,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
Expand Down
Binary file modified substrate/frame/revive/rpc/revive_chain.metadata
Binary file not shown.
38 changes: 8 additions & 30 deletions substrate/frame/revive/rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ struct ClientInner {
cache: Shared<BlockCache<CACHE_SIZE>>,
chain_id: u64,
max_block_weight: Weight,
native_to_evm_ratio: U256,
}

impl ClientInner {
Expand All @@ -252,20 +251,10 @@ impl ClientInner {

let rpc = LegacyRpcMethods::<SrcChainConfig>::new(RpcClient::new(rpc_client.clone()));

let (native_to_evm_ratio, chain_id, max_block_weight) =
tokio::try_join!(native_to_evm_ratio(&api), chain_id(&api), max_block_weight(&api))?;
let (chain_id, max_block_weight) =
tokio::try_join!(chain_id(&api), max_block_weight(&api))?;

Ok(Self { api, rpc_client, rpc, cache, chain_id, max_block_weight, native_to_evm_ratio })
}

/// Convert a native balance to an EVM balance.
fn native_to_evm_decimals(&self, value: U256) -> U256 {
value.saturating_mul(self.native_to_evm_ratio)
}

/// Convert an evm balance to a native balance.
fn evm_to_native_decimals(&self, value: U256) -> U256 {
value / self.native_to_evm_ratio
Ok(Self { api, rpc_client, rpc, cache, chain_id, max_block_weight })
}

/// Get the receipt infos from the extrinsics in a block.
Expand Down Expand Up @@ -368,13 +357,6 @@ async fn max_block_weight(api: &OnlineClient<SrcChainConfig>) -> Result<Weight,
Ok(max_block.0)
}

/// Fetch the native to EVM ratio from the substrate chain.
async fn native_to_evm_ratio(api: &OnlineClient<SrcChainConfig>) -> Result<U256, ClientError> {
let query = subxt_client::constants().revive().native_to_eth_ratio();
let ratio = api.constants().at(&query)?;
Ok(U256::from(ratio))
}

/// Extract the block timestamp.
async fn extract_block_timestamp(block: &SubstrateBlock) -> Option<u64> {
let extrinsics = block.extrinsics().await.ok()?;
Expand Down Expand Up @@ -607,8 +589,9 @@ impl Client {

let runtime_api = self.runtime_api(at).await?;
let payload = subxt_client::apis().revive_api().balance(address);
let balance = runtime_api.call(payload).await?.into();
Ok(self.inner.native_to_evm_decimals(balance))
let balance = runtime_api.call(payload).await?;

Ok(*balance)
}

/// Get the contract storage for the given contract address and key.
Expand Down Expand Up @@ -659,20 +642,15 @@ impl Client {
) -> Result<EthContractResult<Balance, Vec<u8>>, ClientError> {
let runtime_api = self.runtime_api(&block).await?;

let value = self
.inner
.evm_to_native_decimals(tx.value.unwrap_or_default())
.try_into()
.map_err(|_| ClientError::ConversionFailed)?;

// TODO: remove once subxt is updated
let value = tx.value.unwrap_or_default();
let from = tx.from.map(|v| v.0.into());
let to = tx.to.map(|v| v.0.into());

let payload = subxt_client::apis().revive_api().eth_transact(
from.unwrap_or_default(),
to,
value,
subxt::utils::Static(value),
tx.input.clone().unwrap_or_default().0,
None,
None,
Expand Down
5 changes: 5 additions & 0 deletions substrate/frame/revive/rpc/src/subxt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use subxt::config::{signed_extensions, Config, PolkadotConfig};

#[subxt::subxt(
runtime_metadata_path = "revive_chain.metadata",
// TODO remove once subxt use the same U256 type
substitute_type(
path = "primitive_types::U256",
with = "::subxt::utils::Static<::sp_core::U256>"
),
substitute_type(
path = "pallet_revive::primitives::EthContractResult<A, B>",
with = "::subxt::utils::Static<::pallet_revive::EthContractResult<A, B>>"
Expand Down
39 changes: 29 additions & 10 deletions substrate/frame/revive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use frame_support::{
pallet_prelude::DispatchClass,
traits::{
fungible::{Inspect, Mutate, MutateHold},
tokens::{Fortitude::Polite, Preservation::Preserve},
ConstU32, ConstU64, Contains, EnsureOrigin, Get, IsType, OriginTrait, Time,
},
weights::{Weight, WeightMeter},
Expand Down Expand Up @@ -1216,7 +1217,7 @@ where
///
/// - `origin`: The origin of the call.
/// - `dest`: The destination address of the call.
/// - `value`: The value to transfer.
/// - `value`: The EVM value to transfer.
/// - `input`: The input data.
/// - `gas_limit`: The gas limit enforced during contract execution.
/// - `storage_deposit_limit`: The maximum balance that can be charged to the caller for storage
Expand All @@ -1228,7 +1229,7 @@ where
pub fn bare_eth_transact(
origin: T::AccountId,
dest: Option<H160>,
value: BalanceOf<T>,
value: U256,
input: Vec<u8>,
gas_limit: Weight,
storage_deposit_limit: BalanceOf<T>,
Expand All @@ -1252,6 +1253,18 @@ where
// Get the nonce to encode in the tx.
let nonce: T::Nonce = <System<T>>::account_nonce(&origin);

// Convert the value to the native balance type.
let native_value = match Self::convert_evm_to_native(value) {
Ok(v) => v,
Err(err) =>
return EthContractResult {
gas_required: Default::default(),
storage_deposit: Default::default(),
fee: Default::default(),
result: Err(err.into()),
},
};

// Dry run the call
let (mut result, dispatch_info) = match dest {
// A contract call.
Expand All @@ -1260,7 +1273,7 @@ where
let result = crate::Pallet::<T>::bare_call(
T::RuntimeOrigin::signed(origin),
dest,
value,
native_value,
gas_limit,
storage_deposit_limit,
input.clone(),
Expand All @@ -1277,7 +1290,7 @@ where
// Get the dispatch info of the call.
let dispatch_call: <T as Config>::RuntimeCall = crate::Call::<T>::call {
dest,
value,
value: native_value,
gas_limit: result.gas_required,
storage_deposit_limit: result.storage_deposit,
data: input.clone(),
Expand All @@ -1303,7 +1316,7 @@ where
// Dry run the call.
let result = crate::Pallet::<T>::bare_instantiate(
T::RuntimeOrigin::signed(origin),
value,
native_value,
gas_limit,
storage_deposit_limit,
Code::Upload(code.to_vec()),
Expand All @@ -1323,7 +1336,7 @@ where
// Get the dispatch info of the call.
let dispatch_call: <T as Config>::RuntimeCall =
crate::Call::<T>::instantiate_with_code {
value,
value: native_value,
gas_limit: result.gas_required,
storage_deposit_limit: result.storage_deposit,
code: code.to_vec(),
Expand All @@ -1336,7 +1349,7 @@ where
};

let mut tx = TransactionLegacyUnsigned {
value: Self::convert_native_to_evm(value),
value,
input: input.into(),
nonce: nonce.into(),
chain_id: Some(T::ChainId::get().into()),
Expand Down Expand Up @@ -1375,6 +1388,12 @@ where
result
}

/// Get the balance with EVM decimals of the given `address`.
pub fn evm_balance(address: &H160) -> U256 {
let account = T::AddressMapper::to_account_id(&address);
Self::convert_native_to_evm(T::Currency::reducible_balance(&account, Preserve, Polite))
}

/// A generalized version of [`Self::upload_code`].
///
/// It is identical to [`Self::upload_code`] and only differs in the information it returns.
Expand Down Expand Up @@ -1473,8 +1492,8 @@ sp_api::decl_runtime_apis! {
BlockNumber: Codec,
EventRecord: Codec,
{
/// Returns the free balance of the given `[H160]` address.
fn balance(address: H160) -> Balance;
/// Returns the free balance of the given `[H160]` address, using EVM decimals.
fn balance(address: H160) -> U256;

/// Returns the nonce of the given `[H160]` address.
fn nonce(address: H160) -> Nonce;
Expand Down Expand Up @@ -1511,7 +1530,7 @@ sp_api::decl_runtime_apis! {
fn eth_transact(
origin: H160,
dest: Option<H160>,
value: Balance,
value: U256,
input: Vec<u8>,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
Expand Down

0 comments on commit 0bd3f74

Please sign in to comment.