-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(zink): add block properties
Showing
5 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! Example for Block and Transaction Properties. | ||
#![cfg_attr(target_arch = "wasm32", no_std)] | ||
#![cfg_attr(target_arch = "wasm32", no_main)] | ||
|
||
extern crate zink; | ||
use zink::primitives::{properties, Bytes32}; | ||
|
||
#[zink::external] | ||
pub fn blockhash(block_number: u64) -> Bytes32 { | ||
properties::blockhash(block_number) | ||
} | ||
|
||
#[zink::external] | ||
pub fn number() -> u64 { | ||
properties::number() | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} | ||
|
||
#[test] | ||
fn test_block_properties() -> anyhow::Result<()> { | ||
use zint::{Bytes32, Contract, EVM}; | ||
|
||
let mut evm = EVM::default().commit(true); | ||
let contract = Contract::search("properties")?.compile()?; | ||
let raw_info = evm.deploy(&contract.bytecode()?)?; | ||
|
||
let info = evm | ||
.calldata(&contract.encode(&[b"number()".to_vec()])?) | ||
.call(raw_info.address)?; | ||
assert_eq!(info.ret, 0u64.to_bytes32(), "{info:?}"); | ||
|
||
let info = evm | ||
.calldata(&contract.encode(&[ | ||
b"blockhash(uint64)".to_vec(), | ||
599423545u64.to_bytes32().to_vec(), | ||
])?) | ||
.call(raw_info.address)?; | ||
assert_eq!(info.ret, 0u64.to_bytes32(), "{info:?}"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use super::{Address, Bytes32}; | ||
use crate::ffi; | ||
|
||
/// Get the current block number. | ||
pub fn number() -> u64 { | ||
unsafe { ffi::evm::number() } | ||
} | ||
|
||
/// Get the hash of one of the 256 most recent complete blocks. | ||
pub fn blockhash(block_number: u64) -> Bytes32 { | ||
unsafe { ffi::evm::blockhash(block_number) } | ||
} | ||
|
||
/// Get versioned hashes. | ||
pub fn blobhash(index: u64) -> Bytes32 { | ||
unsafe { ffi::evm::blobhash(index) } | ||
} | ||
|
||
/// Get the current block’s base fee. | ||
pub fn basefee() -> u64 { | ||
unsafe { ffi::evm::basefee() } | ||
} | ||
|
||
/// Get the current block’s blob base fee. | ||
pub fn blobbasefee() -> u64 { | ||
unsafe { ffi::evm::blobbasefee() } | ||
} | ||
|
||
/// Get the current chain id. | ||
pub fn chainid() -> u64 { | ||
unsafe { ffi::evm::chainid() } | ||
} | ||
|
||
/// Get the block’s beneficiary address. | ||
pub fn coinbase() -> Address { | ||
unsafe { ffi::evm::coinbase() } | ||
} | ||
|
||
/// Get the previous block’s RANDAO mix. | ||
pub fn prevrandao() -> Bytes32 { | ||
unsafe { ffi::evm::prevrandao() } | ||
} | ||
|
||
/// Get the current block gaslimit. | ||
pub fn gaslimit() -> u64 { | ||
unsafe { ffi::evm::gaslimit() } | ||
} | ||
|
||
/// Get the block’s timestamp. | ||
pub fn timestamp() -> u64 { | ||
unsafe { ffi::evm::timestamp() } | ||
} |