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

difftreelog

source

crates/evm-coder/src/lib.rs2.3 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#[macro_export]65macro_rules! generate_stubgen {66	($name:ident, $decl:ident, $is_impl:literal) => {67		#[test]68		#[ignore]69		fn $name() {70			use evm_coder::solidity::TypeCollector;71			let mut out = TypeCollector::new();72			$decl::generate_solidity_interface(&mut out, $is_impl);73			println!("=== SNIP START ===");74			println!("// SPDX-License-Identifier: OTHER");75			println!("// This code is automatically generated");76			println!();77			println!("pragma solidity >=0.8.0 <0.9.0;");78			println!();79			for b in out.finish() {80				println!("{}", b);81			}82			println!("=== SNIP END ===");83		}84	};85}8687#[cfg(test)]88mod tests {89	use super::*;9091	#[test]92	fn function_selector_generation() {93		assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);94	}9596	#[test]97	fn event_topic_generation() {98		assert_eq!(99			hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),100			"ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",101		);102	}103}