Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/env/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ where
})
}

/// Returns the current price for gas.
/// Returns the price for the specified amount of gas.
///
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn gas_price<T>() -> Result<T::Balance>
pub fn gas_price<T>(gas: u64) -> Result<T::Balance>
where
T: EnvTypes,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnv::gas_price::<T>(instance)
TypedEnv::gas_price::<T>(instance, gas)
})
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/env/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ pub trait TypedEnv: Env {
/// For more details visit: [`ink_core::env::transferred_balance`]
fn transferred_balance<T: EnvTypes>(&mut self) -> Result<T::Balance>;

/// Returns the current price for gas.
/// Returns the price for the specified amount of gas.
///
/// # Note
///
/// For more details visit: [`ink_core::env::gas_price`]
fn gas_price<T: EnvTypes>(&mut self) -> Result<T::Balance>;
fn gas_price<T: EnvTypes>(&mut self, gas: u64) -> Result<T::Balance>;

/// Returns the amount of gas left for the contract execution.
///
Expand Down
2 changes: 1 addition & 1 deletion core/src/env/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl TypedEnv for EnvInstance {
.map_err(Into::into)
}

fn gas_price<T: EnvTypes>(&mut self) -> Result<T::Balance> {
fn gas_price<T: EnvTypes>(&mut self, _gas: u64) -> Result<T::Balance> {
self.chain_spec
.gas_price::<T>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if there was an actual conversion config for the off-chain environment so that the returned amount at least scales linearly to the input.

.map_err(|_| scale::Error::from("could not decode gas price"))
Expand Down
7 changes: 5 additions & 2 deletions core/src/env/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod sys {
pub fn ext_block_number();
pub fn ext_address();
pub fn ext_balance();
pub fn ext_gas_price();
pub fn ext_gas_price(gas: u64);
pub fn ext_gas_left();
pub fn ext_value_transferred();
pub fn ext_now();
Expand Down Expand Up @@ -285,7 +285,6 @@ impl_ext_wrapper_for! {
(block_number => ext_block_number),
(address => ext_address),
(balance => ext_balance),
(gas_price => ext_gas_price),
(gas_left => ext_gas_left),
(value_transferred => ext_value_transferred),
(now => ext_now),
Expand All @@ -294,6 +293,10 @@ impl_ext_wrapper_for! {
(tombstone_deposit => ext_tombstone_deposit),
}

pub fn gas_price(gas: u64) {
unsafe { sys::ext_gas_price(gas) }
}

pub fn set_rent_allowance(value: &[u8]) {
unsafe { sys::ext_set_rent_allowance(value.as_ptr() as u32, value.len() as u32) }
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/env/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,6 @@ impl TypedEnv for EnvInstance {
self.get_property::<T::Balance>(ext::value_transferred)
}

fn gas_price<T: EnvTypes>(&mut self) -> Result<T::Balance> {
self.get_property::<T::Balance>(ext::gas_price)
}

fn gas_left<T: EnvTypes>(&mut self) -> Result<T::Balance> {
self.get_property::<T::Balance>(ext::gas_left)
}
Expand Down Expand Up @@ -380,6 +376,11 @@ impl TypedEnv for EnvInstance {
ext::transfer(destination, value)
}

fn gas_price<T: EnvTypes>(&mut self, gas: u64) -> Result<T::Balance> {
ext::gas_price(gas);
self.decode_scratch_buffer().map_err(Into::into)
}

fn random<T>(&mut self, subject: &[u8]) -> Result<T::Hash>
where
T: EnvTypes,
Expand Down
6 changes: 3 additions & 3 deletions lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ where
env::transferred_balance::<T>().expect("couldn't decode transferred balance")
}

/// Returns the current price for gas.
/// Returns the price for the specified amount of gas.
///
/// # Note
///
/// For more details visit: [`ink_core::env::gas_price`]
pub fn gas_price(self) -> T::Balance {
env::gas_price::<T>().expect("couldn't decode gas price")
pub fn gas_price(self, gas: u64) -> T::Balance {
env::gas_price::<T>(gas).expect("couldn't decode gas price")
}

/// Returns the amount of gas left for the contract execution.
Expand Down