Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5030d89
implement `gas_limit` host fn
LucasGrasso Oct 24, 2025
f04d729
add tests for `gas_limit` host fn
LucasGrasso Oct 24, 2025
0eec69d
remove off-chain tests for `gas_limit`
LucasGrasso Oct 24, 2025
fddfab0
Merge branch 'use-ink:master' into master
LucasGrasso Oct 24, 2025
95162a9
add changes to changelog
LucasGrasso Oct 24, 2025
1e63416
run formatting
LucasGrasso Oct 26, 2025
9c3ab41
fix error in env-access ui test
LucasGrasso Oct 26, 2025
e0c8351
run formatting
LucasGrasso Oct 27, 2025
eb2f8a3
Apply suggestions from code review
LucasGrasso Oct 28, 2025
4a1f7e3
update versions in gas and misc hostfns internal tests
LucasGrasso Oct 28, 2025
6609561
Merge branch 'master' of https://github.com/LucasGrasso/ink
LucasGrasso Oct 28, 2025
dc87592
Merge remote-tracking branch 'upstream/master'
LucasGrasso Oct 29, 2025
21a5e46
impl `gas_price` hostfn
LucasGrasso Oct 29, 2025
be3bf7a
test `gas_price` hostfn
LucasGrasso Oct 29, 2025
b707359
add changes to changelog
LucasGrasso Oct 29, 2025
ab655d1
impl `call_data_size` hostfn
LucasGrasso Oct 29, 2025
ce50625
impl `return_data_size` hostfn
LucasGrasso Oct 29, 2025
6dc1f7b
test `call_data_size` and `return_data_size`
LucasGrasso Oct 29, 2025
08989e2
implement and test `ref_time_left`
LucasGrasso Oct 29, 2025
e1509fa
run formatting
LucasGrasso Oct 29, 2025
5f11547
Merge branch 'master' into impl_0ary_hostfns
LucasGrasso Oct 29, 2025
e02de04
revert changelog auto format
LucasGrasso Oct 30, 2025
fda9fb7
Merge branch 'impl_0ary_hostfns' of https://github.com/LucasGrasso/in…
LucasGrasso Oct 30, 2025
87536bf
Merge branch 'use-ink:master' into impl_0ary_hostfns
LucasGrasso Nov 3, 2025
3aa2e54
ref_time_left -> gas_left
LucasGrasso Nov 4, 2025
62d027d
Update crates/env/src/engine/on_chain/pallet_revive.rs
LucasGrasso Nov 4, 2025
06fce27
apply proposed comment changes
LucasGrasso Nov 4, 2025
b307f3c
modify gas_limit comments
LucasGrasso Nov 4, 2025
4317e6f
add CALLDATASIZE opcode
LucasGrasso Nov 4, 2025
8edc1ad
Apply suggestions from code review
LucasGrasso Nov 6, 2025
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Version 6.0.0-beta

### Added
- Implements the API for the `pallet-revive` host functions `gas_price`, `call_data_size`, `return_data_size`, `gas_left` - [#2694](https://github.com/use-ink/ink/pull/2694)
- Implements the API for the `pallet-revive` host function `gas_limit` - [#2691](https://github.com/use-ink/ink/pull/2691)
- Implements the API for the `pallet-revive` host function `to_account_id` - [#2578](https://github.com/use-ink/ink/pull/2578)
- Add `#[ink::contract_ref]` attribute - [#2648](https://github.com/use-ink/ink/pull/2648)
Expand Down Expand Up @@ -2635,4 +2636,4 @@ impl Contract {

This is useful if the `impl` block itself does not contain any ink! constructors or messages, but you
still need to access some of the "magic" provided by ink!. In the example above, you would not have
access to `emit_event` without `#[ink(impl)]`.
access to `emit_event` without `#[ink(impl)]`.
29 changes: 29 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,41 @@ pub fn caller() -> Address {
}

/// Returns the block's `ref_time` limit.
/// [GASLIMIT](https://www.evm.codes/?fork=cancun#45) opcode.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
pub fn gas_limit() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::gas_limit)
}

/// Returns the price per `ref_time`, akin to the EVM
/// [GASPRICE](https://www.evm.codes/?fork=cancun#3a) opcode.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
pub fn gas_price() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::gas_price)
}

/// Returns the amount of gas left.
/// This is the `ref_time` left.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
pub fn gas_left() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::gas_left)
}

/// Returns the total size of the contract call input data, akin to the EVM
/// [CALLDATASIZE](https://www.evm.codes/?fork=cancun#36) opcode.
pub fn call_data_size() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::call_data_size)
}

/// Returns the size of the returned data of the last contract call or instantiation,
/// akin to the EVM [RETURNDATASIZE](https://www.evm.codes/?fork=cancun#3d) opcode.
pub fn return_data_size() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::return_data_size)
}

/// Returns the transferred value for the contract execution.
///
/// # Errors
Expand Down
37 changes: 37 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,49 @@ pub trait TypedEnvBackend: EnvBackend {
fn caller(&mut self) -> Address;

/// Returns the block's `ref_time` limit.
/// This is akin to the EVM [GASLIMIT](https://www.evm.codes/?fork=cancun#45) opcode.
///
/// # Note
///
/// For more details visit: [`gas_limit`][`crate::gas_limit`]
fn gas_limit(&mut self) -> u64;

/// Returns the price per `ref_time`, akin to the EVM
/// [GASPRICE](https://www.evm.codes/?fork=cancun#3a) opcode.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
///
/// # Note
///
/// For more details visit: [`gas_price`][`crate::gas_price`]
fn gas_price(&mut self) -> u64;

/// Returns the amount of gas left.
/// This is the `ref_time` left.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
///
/// # Note
///
/// For more details visit: [`gas_left`][`crate::gas_left`]
fn gas_left(&mut self) -> u64;

/// Returns the total size of the contract call input data.
/// This is akin to the EVM [CALLDATASIZE](https://www.evm.codes/?fork=cancun#36) opcode.
///
/// # Note
///
/// For more details visit: [`call_data_size`][`crate::call_data_size]
fn call_data_size(&mut self) -> u64;

/// Returns the size of the returned data of the last contract call or instantiation.
/// This is akin to the EVM [RETURNDATASIZE](https://www.evm.codes/?fork=cancun#3d) opcode.
///
/// # Note
///
/// For more details visit: [`return_data_size`][`crate::return_data_size]
fn return_data_size(&mut self) -> u64;

/// Returns the transferred value for the contract execution.
///
/// # Note
Expand Down
16 changes: 16 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ impl TypedEnvBackend for EnvInstance {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn gas_price(&mut self) -> u64 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn gas_left(&mut self) -> u64 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn call_data_size(&mut self) -> u64 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn return_data_size(&mut self) -> u64 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

fn transferred_value(&mut self) -> U256 {
self.get_property(Engine::value_transferred)
.unwrap_or_else(|error| {
Expand Down
18 changes: 18 additions & 0 deletions crates/env/src/engine/on_chain/pallet_revive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,24 @@ impl TypedEnvBackend for EnvInstance {
ext::gas_limit()
}

fn gas_price(&mut self) -> u64 {
ext::gas_price()
}

fn gas_left(&mut self) -> u64 {
// TODO: Change to `ext::gas_left()` when `pallet-revive-uapi` is updated.
// Ref: https://github.com/paritytech/polkadot-sdk/pull/9968
ext::ref_time_left()
}

fn call_data_size(&mut self) -> u64 {
ext::call_data_size()
}

fn return_data_size(&mut self) -> u64 {
ext::return_data_size()
}

fn transferred_value(&mut self) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();
Expand Down
135 changes: 134 additions & 1 deletion crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ where
ink_env::caller()
}

/// Returns the block ref_time limit.
/// Returns the block's `ref_time` limit, akin to the EVM
/// [GASLIMIT](https://www.evm.codes/?fork=cancun#45) opcode.
///
/// # Example
///
Expand Down Expand Up @@ -141,6 +142,138 @@ where
ink_env::gas_limit()
}

/// Returns the price per `ref_time`, akin to the EVM
/// [GASPRICE](https://www.evm.codes/?fork=cancun#3a) opcode.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
///
/// # Example
///
/// ```
/// #[ink::contract]
/// mod my_contract {
/// #[ink(storage)]
/// pub struct MyContract;
///
/// impl MyContract {
/// #[ink(constructor)]
/// pub fn new() -> Self {
/// Self {}
/// }
///
/// #[ink(message)]
/// pub fn get_gas_price(&self) -> u64 {
/// self.env().gas_price()
/// }
/// }
/// }
/// ```
///
/// # Note
///
/// For more details visit: [`ink_env::gas_price`]
pub fn gas_price(self) -> u64 {
ink_env::gas_price()
}

/// Returns the amount of gas left.
/// This is the `ref_time` left.
///
/// See <https://use.ink/docs/v6/basics/gas/#what-is-gas-in-ink> for more information.
///
/// # Example
///
/// ```
/// #[ink::contract]
/// mod my_contract {
/// #[ink(storage)]
/// pub struct MyContract;
///
/// impl MyContract {
/// #[ink(constructor)]
/// pub fn new() -> Self {
/// Self {}
/// }
///
/// #[ink(message)]
/// pub fn get_gas_left(&self) -> u64 {
/// self.env().gas_left()
/// }
/// }
/// }
/// ```
///
/// # Note
///
/// For more details visit: [`ink_env::gas_left`]
pub fn gas_left(self) -> u64 {
ink_env::gas_left()
}

/// Returns the total size of the contract call input data.
/// This is akin to the EVM [CALLDATASIZE](https://www.evm.codes/?fork=cancun#36) opcode.
///
/// # Example
///
/// ```
/// #[ink::contract]
/// mod my_contract {
/// #[ink(storage)]
/// pub struct MyContract;
///
/// impl MyContract {
/// #[ink(constructor)]
/// pub fn new() -> Self {
/// Self {}
/// }
///
/// #[ink(message)]
/// pub fn get_call_data_size(&self) -> u64 {
/// self.env().call_data_size()
/// }
/// }
/// }
/// ```
///
/// # Note
///
/// For more details visit: [`ink_env::call_data_size`]
pub fn call_data_size(self) -> u64 {
ink_env::call_data_size()
}

/// Returns the size of the returned data of the last contract call or instantiation.
/// This is akin to the EVM [RETURNDATASIZE](https://www.evm.codes/?fork=cancun#3d) opcode.
///
/// # Example
///
/// ```
/// #[ink::contract]
/// mod my_contract {
/// #[ink(storage)]
/// pub struct MyContract;
///
/// impl MyContract {
/// #[ink(constructor)]
/// pub fn new() -> Self {
/// Self {}
/// }
///
/// #[ink(message)]
/// pub fn get_return_data_size(&self) -> u64 {
/// self.env().return_data_size()
/// }
/// }
/// }
/// ```
///
/// # Note
///
/// For more details visit: [`ink_env::return_data_size`]
pub fn return_data_size(self) -> u64 {
ink_env::return_data_size()
}

/// Returns the transferred value for the contract execution.
///
/// # Example
Expand Down
8 changes: 8 additions & 0 deletions crates/ink/tests/ui/contract/pass/env-access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ mod contract {
let _ = Self::env().caller();
let _ = Self::env().minimum_balance();
let _ = Self::env().gas_limit();
let _ = Self::env().gas_price();
let _ = Self::env().gas_left();
let _ = Self::env().call_data_size();
let _ = Self::env().return_data_size();
let _ = Self::env().transferred_value();
let _ = Self::env().weight_to_fee(0);
Self {}
Expand All @@ -29,6 +33,10 @@ mod contract {
let _ = self.env().caller();
let _ = self.env().minimum_balance();
let _ = self.env().gas_limit();
let _ = self.env().gas_price();
let _ = self.env().gas_left();
let _ = self.env().call_data_size();
let _ = self.env().return_data_size();
let _ = self.env().transferred_value();
let _ = self.env().weight_to_fee(0);
}
Expand Down
27 changes: 27 additions & 0 deletions integration-tests/internal/data-hostfns/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "data_hostfns"
description = "E2E tests for data related host functions"
version = "6.0.0-alpha.4"
authors = ["Use Ink <ink@use.ink>"]
edition = "2021"
publish = false

[dependencies]
ink = { path = "../../../crates/ink", default-features = false, features = ["unstable-hostfn"] }

[dev-dependencies]
ink_e2e = { path = "../../../crates/e2e" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
]
ink-as-dependency = []
e2e-tests = []

[package.metadata.ink-lang]
abi = "ink"
Loading
Loading