Skip to content

Commit 2821151

Browse files
authored
Make NativeToEthRatio part of Environment (#2604)
* Make `NativeToEthRatio` part of `Environment` * Update changelog * Move `balance_to_evm_value` to sandbox top-level * Add `NATIVE_TO_ETH_RATIO` to missing doc comments * Fix syntax * Add `NATIVE_TO_ETH_RATIO` to missing doc comments * Add `NATIVE_TO_ETH_RATIO` to missing doc comments * Fix syntax * Add missing property * Update schema
1 parent 97837b9 commit 2821151

File tree

24 files changed

+92
-27
lines changed

24 files changed

+92
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Fixed
1313
- E2E: Fixes around correct handling of storage deposit limit ‒ [#2589](https://github.com/use-ink/ink/pull/2589)
14+
- Make `NativeToEthRatio` part of the `Environment`[#2604](https://github.com/use-ink/ink/pull/2604)
1415

1516
## Version 6.0.0-alpha.1
1617

crates/e2e/sandbox/src/api/revive_api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ where
179179
self.execute_with(|| {
180180
pallet_revive::Pallet::<Self::T>::bare_instantiate(
181181
origin,
182-
balance_to_evm_value::<BalanceOf<Self::T>>(value),
182+
balance_to_evm_value::<Self::T>(value),
183183
gas_limit,
184184
storage_deposit_limit,
185185
Code::Upload(contract_bytes),
@@ -204,7 +204,7 @@ where
204204
self.execute_with(|| {
205205
pallet_revive::Pallet::<Self::T>::bare_instantiate(
206206
origin,
207-
balance_to_evm_value::<BalanceOf<Self::T>>(value),
207+
balance_to_evm_value::<Self::T>(value),
208208
gas_limit,
209209
storage_deposit_limit,
210210
Code::Existing(code_hash),
@@ -244,7 +244,7 @@ where
244244
pallet_revive::Pallet::<Self::T>::bare_call(
245245
origin,
246246
address,
247-
balance_to_evm_value::<BalanceOf<Self::T>>(value),
247+
balance_to_evm_value::<Self::T>(value),
248248
gas_limit,
249249
storage_deposit_limit,
250250
data,

crates/e2e/sandbox/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use pallet_revive::{
2626
ExecReturnValue,
2727
InstantiateReturnValue,
2828
};
29+
use sp_core::Get;
2930
/// Export pallets that are used in [`crate::create_sandbox`]
3031
pub use {
3132
frame_support::sp_runtime::testing::H256,
@@ -158,11 +159,14 @@ pub trait Sandbox {
158159
/// the other way (from `U256` to `Balance`).
159160
///
160161
/// See <https://github.com/paritytech/polkadot-sdk/pull/9101> for more details.
161-
pub fn balance_to_evm_value<Balance>(value: Balance) -> U256
162+
pub fn balance_to_evm_value<R>(value: BalanceOf<R>) -> U256
162163
where
163-
Balance: Into<U256>,
164+
R: pallet_revive::Config,
165+
BalanceOf<R>: Into<U256>,
166+
U256: From<u32>,
164167
{
165-
let native_to_eth_ratio: U256 = 100_000_000.into();
168+
let native_to_eth_ratio: U256 =
169+
<R as pallet_revive::Config>::NativeToEthRatio::get().into();
166170
let evm_value: U256 = value.into();
167-
native_to_eth_ratio.saturating_mul(evm_value)
171+
native_to_eth_ratio.saturating_mul(evm_value.into())
168172
}

crates/ink/codegen/src/generator/env.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl GenerateCode for Env<'_> {
4141
type BlockNumber = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::BlockNumber;
4242
type ChainExtension = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::ChainExtension;
4343
const MAX_EVENT_TOPICS: usize = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::MAX_EVENT_TOPICS;
44+
const NATIVE_TO_ETH_RATIO: u32 = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::NATIVE_TO_ETH_RATIO;
4445
type EventRecord = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::EventRecord;
4546

4647
type Address = ::ink::primitives::Address;

crates/ink/macro/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ pub fn selector_bytes(input: TokenStream) -> TokenStream {
167167
///
168168
/// impl ink_env::Environment for MyEnvironment {
169169
/// const MAX_EVENT_TOPICS: usize = 3;
170+
/// const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
170171
/// type AccountId = [u8; 16];
171172
/// type Balance = u128;
172173
/// type Hash = [u8; 32];
@@ -186,6 +187,7 @@ pub fn selector_bytes(input: TokenStream) -> TokenStream {
186187
/// #
187188
/// # impl ink_env::Environment for MyEnvironment {
188189
/// # const MAX_EVENT_TOPICS: usize = 3;
190+
/// # const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
189191
/// # type AccountId = [u8; 16];
190192
/// # type Balance = u128;
191193
/// # type Hash = [u8; 32];
@@ -1180,6 +1182,8 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
11801182
/// impl Environment for CustomEnvironment {
11811183
/// const MAX_EVENT_TOPICS: usize =
11821184
/// <DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;
1185+
/// const NATIVE_TO_ETH_RATIO: u32 =
1186+
/// <DefaultEnvironment as Environment>::NATIVE_TO_ETH_RATIO;
11831187
///
11841188
/// type AccountId = <DefaultEnvironment as Environment>::AccountId;
11851189
/// type Balance = <DefaultEnvironment as Environment>::Balance;
@@ -1322,6 +1326,8 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
13221326
/// # impl ink_env::Environment for CustomEnvironment {
13231327
/// # const MAX_EVENT_TOPICS: usize =
13241328
/// # <ink_env::DefaultEnvironment as ink_env::Environment>::MAX_EVENT_TOPICS;
1329+
/// # const NATIVE_TO_ETH_RATIO: u32 =
1330+
/// # <ink_env::DefaultEnvironment as ink_env::Environment>::NATIVE_TO_ETH_RATIO;
13251331
/// #
13261332
/// # type AccountId = <ink_env::DefaultEnvironment as ink_env::Environment>::AccountId;
13271333
/// # type Balance = <ink_env::DefaultEnvironment as ink_env::Environment>::Balance;

crates/ink/src/contract_ref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ use ink_primitives::Address;
153153
///
154154
/// impl ink_env::Environment for CustomEnv {
155155
/// const MAX_EVENT_TOPICS: usize = 3;
156+
/// const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
156157
/// type AccountId = [u8; 32];
157158
/// type Balance = u64;
158159
/// type Hash = [u8; 32];

crates/ink/src/message_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
///
7979
/// impl ink_env::Environment for CustomEnv {
8080
/// const MAX_EVENT_TOPICS: usize = 3;
81+
/// const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
8182
/// type AccountId = [u8; 32];
8283
/// type Balance = u64;
8384
/// type Hash = [u8; 32];

crates/ink/tests/ui/chain_extension/E-01-simple.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub enum CustomEnvironment {}
111111
impl Environment for CustomEnvironment {
112112
const MAX_EVENT_TOPICS: usize =
113113
<ink_env::DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;
114+
const NATIVE_TO_ETH_RATIO: u32 =
115+
<ink_env::DefaultEnvironment as Environment>::NATIVE_TO_ETH_RATIO;
114116

115117
type AccountId = <ink_env::DefaultEnvironment as Environment>::AccountId;
116118
type Balance = <ink_env::DefaultEnvironment as Environment>::Balance;

crates/ink/tests/ui/contract/pass/config-custom-env.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub struct CustomEnv;
55

66
impl ink_env::Environment for CustomEnv {
77
const MAX_EVENT_TOPICS: usize = 3;
8+
const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
89
type AccountId = [u8; 32];
910
type Balance = u64;
1011
type Hash = [u8; 32];

crates/ink/tests/ui/contract/pass/event/event-config-more-topics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct EnvironmentMoreTopics;
1010

1111
impl ink_env::Environment for EnvironmentMoreTopics {
1212
const MAX_EVENT_TOPICS: usize = 10; // Default is 4.
13+
const NATIVE_TO_ETH_RATIO: u32 = 100_000_000;
1314

1415
type AccountId = <DefaultEnvironment as Environment>::AccountId;
1516
type Balance = <DefaultEnvironment as Environment>::Balance;

0 commit comments

Comments
 (0)