git.delta.rocks / unique-network / refs/commits / e69af78ae3cb

difftreelog

source

crates/evm-coder/src/lib.rs1.7 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]2#[cfg(not(feature = "std"))]3extern crate alloc;45use abi::{AbiReader, AbiWriter};6pub use evm_coder_macros::{event_topic, fn_selector, solidity_interface, solidity, ToLog};7pub mod abi;8pub mod events;9pub use events::ToLog;10pub mod execution;11pub mod solidity;1213/// Solidity type definitions14pub mod types {15	#![allow(non_camel_case_types)]1617	#[cfg(not(feature = "std"))]18	use alloc::{vec::Vec};19	use primitive_types::{U256, H160, H256};2021	pub type address = H160;2223	pub type uint8 = u8;24	pub type uint16 = u16;25	pub type uint32 = u32;26	pub type uint64 = u64;27	pub type uint128 = u128;28	pub type uint256 = U256;2930	pub type bytes4 = u32;3132	pub type topic = H256;3334	#[cfg(not(feature = "std"))]35	pub type string = ::alloc::string::String;36	#[cfg(feature = "std")]37	pub type string = ::std::string::String;38	pub type bytes = Vec<u8>;3940	pub type void = ();4142	//#region Special types43	/// Makes function payable44	pub type value = U256;45	/// Makes function caller-sensitive46	pub type caller = address;47	//#endregion4849	pub struct Msg<C> {50		pub call: C,51		pub caller: H160,52		pub value: U256,53	}54}5556pub trait Call: Sized {57	fn parse(selector: u32, input: &mut AbiReader) -> execution::Result<Option<Self>>;58}5960pub trait Callable<C: Call> {61	fn call(&mut self, call: types::Msg<C>) -> execution::Result<AbiWriter>;62}6364#[cfg(test)]65mod tests {66	use super::*;6768	#[test]69	fn function_selector_generation() {70		assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);71	}7273	#[test]74	fn event_topic_generation() {75		assert_eq!(76			hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),77			"ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",78		);79	}80}