Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cheatcode for getting hash of the block #2648

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[starknet::interface]
trait ICheatBlockHashChecker<TContractState> {
fn get_block_hash(ref self: TContractState) -> felt252;
fn get_block_hash_and_emit_event(ref self: TContractState) -> felt252;
fn get_block_hash_and_number(ref self: TContractState) -> (felt252, u64);
}

#[starknet::contract]
mod CheatBlockHashChecker {
use box::BoxTrait;
use core::starknet::SyscallResultTrait;
use starknet::{get_block_info, get_block_hash_syscall};

#[storage]
struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
BlockHashEmitted: BlockHashEmitted
}

#[derive(Drop, starknet::Event)]
struct BlockHashEmitted {
block_hash: felt252
}


#[abi(embed_v0)]
impl CheatBlockHashChecker of super::ICheatBlockHashChecker<ContractState> {
fn get_block_hash(ref self: ContractState) -> felt252 {
let block_info = get_block_info().unbox();
let block_hash = get_block_hash_syscall(block_info.block_number - 10).unwrap_syscall();

block_hash
}

fn get_block_hash_and_emit_event(ref self: ContractState) -> u64 {
let block_info = get_block_info().unbox();
let block_hash = get_block_hash_syscall(block_info.block_number - 10).unwrap_syscall();

self.emit(Event::BlockHashEmitted(BlockHashEmitted { block_hash }));
block_hash
}

fn get_block_hash_and_number(ref self: ContractState) -> (u64, u64) {
let block_info = starknet::get_block_info().unbox();
let block_hash = get_block_hash_syscall(block_info.block_number - 10).unwrap_syscall();

(block_info.block_hash, block_info.block_number)
}
}
}