Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 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
b408418
Merge branch 'use-ink:master' into master
LucasGrasso Oct 30, 2025
ab37628
Merge branch 'use-ink:master' into master
LucasGrasso Nov 7, 2025
23c0ba3
Merge branch 'use-ink:master' into master
LucasGrasso Nov 8, 2025
560cf92
impl and test `chain_id` hostfn
LucasGrasso Nov 9, 2025
aced728
add entry to changelog
LucasGrasso Nov 9, 2025
b970f07
impl and test `balance_of` host fn
LucasGrasso Nov 9, 2025
8e5e5eb
impl and test `base_fee`
LucasGrasso Nov 9, 2025
efaa0b8
impl and test `origin`
LucasGrasso Nov 9, 2025
071db36
impl and test `code_size` hostfn
LucasGrasso Nov 9, 2025
64c31f9
impl and test `block_author`
LucasGrasso Nov 9, 2025
2b92a3f
impl and test `block_hash`
LucasGrasso Nov 9, 2025
28e25f9
run formatting
LucasGrasso Nov 9, 2025
7b06709
Merge remote-tracking branch 'upstream/master'
LucasGrasso Nov 10, 2025
ca4eb06
fix some ci/cd issues
LucasGrasso Nov 10, 2025
d9b2e3d
fix error[E0412]: cannot find type in this scope
LucasGrasso Nov 10, 2025
a0cd8c5
fix docs for chain_id
LucasGrasso Nov 11, 2025
ef52f4c
fix `block_hash` doc error
LucasGrasso Nov 11, 2025
196e9d7
Merge branch 'master' into master
LucasGrasso Nov 11, 2025
c908a73
run formatting
LucasGrasso Nov 11, 2025
399341d
Merge branch 'master' of https://github.com/LucasGrasso/ink
LucasGrasso Nov 11, 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 @@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `packed` flag to `storage_item` attribute and improve related diagnostics - [](https://github.com/use-ink/ink/pull/2722)

### Changed
- Refactor contract ref generation and add automatic re-exporting ‒ [#2710](https://github.com/use-ink/ink/pull/2710)
- Implements the API for the `pallet-revive` host functions `chain_id`, `balance_of`, `base_fee`, `origin`, `code_size`, `block_hash`, `block_author` - [#2719](https://github.com/use-ink/ink/pull/2719)
- Refactor contract ref generation and add automatic re-exporting - [#2710](https://github.com/use-ink/ink/pull/2710)
- Implement and stabilize `terminate_contract` ‒ [2708](https://github.com/use-ink/ink/pull/2708)

## Version 6.0.0-beta
Expand Down
57 changes: 57 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ink_primitives::{
Sol,
},
sol::SolResultEncode,
types::BlockNumber,
};
use ink_storage_traits::Storable;
use pallet_revive_uapi::ReturnFlags;
Expand Down Expand Up @@ -111,6 +112,62 @@ pub fn return_data_size() -> u64 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::return_data_size)
}

/// Returns the [EIP-155](https://eips.ethereum.org/EIPS/eip-155) chain ID,
/// akin to the EVM [CHAINID](https://www.evm.codes/?fork=cancun#46) opcode.
pub fn chain_id() -> U256 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::chain_id)
}

/// Returns the **reducible** native balance of the supplied address,
/// akin to the EVM [BALANCE](https://www.evm.codes/?fork=cancun#31) opcode.
pub fn balance_of(addr: Address) -> U256 {
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::balance_of(instance, addr)
})
}

/// Returns the base fee.
/// This is akin to the EVM [BASEFEE](https://www.evm.codes/?fork=cancun#48) opcode.
pub fn base_fee() -> U256 {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::base_fee)
}

/// Returns the origin address (initator of the call stack).
/// This is akin to the EVM [ORIGIN](https://www.evm.codes/?fork=cancun#32) opcode.
///
/// # Errors
///
/// - If there is no address associated with the origin (e.g. because the origin is root).
pub fn origin() -> Address {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::origin)
}

/// Returns the code size for a specified contract address.
/// This is akin to the EVM [CODESIZE](https://www.evm.codes/?fork=cancun#38) opcode.
///
/// # Note
///
/// If `addr` is not a contract the `output` will be zero.
pub fn code_size(addr: Address) -> u64 {
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::code_size(instance, addr)
})
}

/// Returns the block hash of the given block number.
/// This is akin to the EVM [BLOCKHASH](https://www.evm.codes/?fork=cancun#40) opcode.
pub fn block_hash(block_number: BlockNumber) -> H256 {
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::block_hash(instance, block_number)
})
}

/// Returns the current block author.
/// This is akin to the EVM [COINBASE](https://www.evm.codes/?fork=cancun#41) opcode.
pub fn block_author() -> Address {
<EnvInstance as OnInstance>::on_instance(TypedEnvBackend::block_author)
}

/// Returns the transferred value for the contract execution.
///
/// # Errors
Expand Down
69 changes: 67 additions & 2 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use ink_primitives::{
H256,
U256,
sol::SolResultEncode,
types::Environment,
types::{
BlockNumber,
Environment,
},
};
use ink_storage_traits::Storable;
pub use pallet_revive_uapi::ReturnFlags;
Expand Down Expand Up @@ -304,9 +307,71 @@ pub trait TypedEnvBackend: EnvBackend {
///
/// # Note
///
/// For more details visit: [`return_data_size`][`crate::return_data_size]
/// For more details visit: [`return_data_size`][`crate::return_data_size`]
fn return_data_size(&mut self) -> u64;

/// Returns the [EIP-155](https://eips.ethereum.org/EIPS/eip-155) chain ID.
/// This is akin to the EVM [CHAINID](https://www.evm.codes/?fork=cancun#46) opcode.
///
/// # Note
///
/// For more details visit: [`chain_id`][`crate::chain_id`]
fn chain_id(&mut self) -> U256;

/// Returns the **reducible** native balance of the supplied address.
/// This is akin to the EVM [BALANCE](https://www.evm.codes/?fork=cancun#31) opcode.
///
/// # Note
///
/// For more details visit: [`balance_of`][`crate::balance_of`]
fn balance_of(&mut self, addr: Address) -> U256;

/// Returns the base fee.
/// This is akin to the EVM [BASEFEE](https://www.evm.codes/?fork=cancun#48) opcode.
///
/// # Note
///
/// For more details visit: [`base_fee`][`crate::base_fee`]
fn base_fee(&mut self) -> U256;

/// Returns the origin address (initator of the call stack).
/// This is akin to the EVM [ORIGIN](https://www.evm.codes/?fork=cancun#32) opcode.
///
/// # Errors
///
/// - If there is no address associated with the origin (e.g. because the origin is
/// root).
///
/// # Note
///
/// For more details visit: [`origin`][`crate::origin`]
fn origin(&mut self) -> Address;

/// Returns the code size for a specified contract address.
/// This is akin to the EVM [CODESIZE](https://www.evm.codes/?fork=cancun#38) opcode.
///
/// # Note
///
/// If `addr` is not a contract the `output` will be zero.
/// For more details visit: [`code_size`][`crate::code_size`]
fn code_size(&mut self, addr: Address) -> u64;

/// Returns the block hash of the given block number.
/// This is akin to the EVM [BLOCKHASH](https://www.evm.codes/?fork=cancun#40) opcode.
///
/// # Note
///
/// For more details visit: [`block_hash`][`crate::block_hash`]
fn block_hash(&mut self, block_number: BlockNumber) -> H256;

/// Returns the current block author.
/// This is akin to the EVM [COINBASE](https://www.evm.codes/?fork=cancun#41) opcode.
///
/// # Note
///
/// For more details visit: [`block_author`][`crate::block_author`]
fn block_author(&mut self) -> Address;

/// Returns the transferred value for the contract execution.
///
/// # Note
Expand Down
29 changes: 29 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use ink_primitives::{
sol::SolResultEncode,
types::{
AccountIdMapper,
BlockNumber,
Environment,
},
};
Expand Down Expand Up @@ -582,6 +583,34 @@ impl TypedEnvBackend for EnvInstance {
unimplemented!("not implemented, the off-chain environment will be removed");
}

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

fn balance_of(&mut self, _addr: Address) -> U256 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

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

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

fn block_hash(&mut self, _block_number: BlockNumber) -> H256 {
unimplemented!("not implemented, the off-chain environment will be removed");
}

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

fn code_size(&mut self, _addr: Address) -> 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");
}
Expand Down
65 changes: 65 additions & 0 deletions crates/env/src/engine/on_chain/pallet_revive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use ink_primitives::{
Sol,
},
sol::SolResultEncode,
types::BlockNumber,
};
use ink_storage_traits::{
Storable,
Expand Down Expand Up @@ -1027,6 +1028,70 @@ impl TypedEnvBackend for EnvInstance {
ext::return_data_size()
}

fn chain_id(&mut self) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

ext::chain_id(u256);
U256::from_le_bytes(*u256)
}

fn balance_of(&mut self, addr: Address) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

let addr = addr.as_fixed_bytes();

ext::balance_of(&addr, u256);
U256::from_le_bytes(*u256)
}

fn base_fee(&mut self) -> U256 {
let mut scope = self.scoped_buffer();
let u256: &mut [u8; 32] = scope.take(32).try_into().unwrap();

ext::base_fee(u256);
U256::from_le_bytes(*u256)
}

fn origin(&mut self) -> Address {
let mut scope = self.scoped_buffer();
let h160: &mut [u8; 20] = scope.take(20).try_into().unwrap();

ext::origin(h160);
h160.into()
}

fn code_size(&mut self, addr: Address) -> u64 {
let addr = addr.as_fixed_bytes();

ext::code_size(addr)
}

fn block_hash(&mut self, block_number: BlockNumber) -> H256 {
let mut scope = self.scoped_buffer();
let output: &mut [u8; 32] = scope.take(32).try_into().unwrap();

let block_number = {
let mut bytes = [0u8; 32];
bytes[..4].copy_from_slice(&block_number.to_le_bytes());
bytes
};

ext::block_hash(&block_number, output);
H256::from_slice(output)
}

fn block_author(&mut self) -> Address {
let h160 = {
let mut scope = self.scoped_buffer();
let h160: &mut [u8; 20] = scope.take(20).try_into().unwrap();
ext::block_author(h160);
*h160
};
h160.into()
}

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
Loading
Loading