Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
feat: expose block hash as library call (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Feb 13, 2024
1 parent 37b4e2f commit c1d6812
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crates/contracts/src/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,36 @@ trait IPrecompiles<T> {
fn exec_precompile(self: @T, address: felt252, data: Span<u8>) -> (bool, u128, Span<u8>);
}

#[starknet::interface]
trait IHelpers<T> {
/// Gets the hash of a specific StarkNet block within the range of
/// [first_v0_12_0_block, current_block - 10].
///
/// # Arguments
///
/// * `block_number` - The block number for which to get the hash.
///
/// # Returns
/// The hash of the specified block.
///
/// # Errors
/// `Block number out of range` - If the block number is greater than `current_block - 10`.
/// `0`: The block number is inferior to `first_v0_12_0_block`.
fn get_block_hash(self: @T, block_number: u64) -> felt252;
}

#[starknet::contract]
mod Precompiles {
use core::traits::Into;
use core::{starknet, starknet::SyscallResultTrait};
use evm::errors::EVMError;
use evm::precompiles::blake2f::Blake2f;
use evm::precompiles::ec_recover::EcRecover;
use evm::precompiles::identity::Identity;
use evm::precompiles::modexp::ModExp;
use evm::precompiles::sha256::Sha256;

use super::IPrecompiles;
use super::{IPrecompiles, IHelpers};

#[storage]
struct Storage {}
Expand All @@ -51,4 +70,11 @@ mod Precompiles {
}
}
}

#[abi(embed_v0)]
impl Helpers of IHelpers<ContractState> {
fn get_block_hash(self: @ContractState, block_number: u64) -> felt252 {
starknet::get_block_hash_syscall(block_number).unwrap_syscall()
}
}
}

0 comments on commit c1d6812

Please sign in to comment.