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

difftreelog

source

crates/evm-coder/src/lib.rs2.8 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]2#[cfg(not(feature = "std"))]3extern crate alloc;45use abi::{AbiRead, 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/// Implementation is implicitly provided for all interfaces65///66/// Note: no Callable implementation is provided67#[derive(Debug)]68pub enum ERC165Call {69	SupportsInterface { interface_id: types::bytes4 },70}7172impl ERC165Call {73	pub const INTERFACE_ID: types::bytes4 = 0x01ffc9a7;74}7576impl Call for ERC165Call {77	fn parse(selector: u32, input: &mut AbiReader) -> execution::Result<Option<Self>> {78		if selector != Self::INTERFACE_ID {79			return Ok(None);80		}81		Ok(Some(Self::SupportsInterface {82			interface_id: input.abi_read()?,83		}))84	}85}8687#[macro_export]88macro_rules! generate_stubgen {89	($name:ident, $decl:ident, $is_impl:literal) => {90		#[test]91		#[ignore]92		fn $name() {93			use evm_coder::solidity::TypeCollector;94			let mut out = TypeCollector::new();95			$decl::generate_solidity_interface(&mut out, $is_impl);96			println!("=== SNIP START ===");97			println!("// SPDX-License-Identifier: OTHER");98			println!("// This code is automatically generated");99			println!();100			println!("pragma solidity >=0.8.0 <0.9.0;");101			println!();102			for b in out.finish() {103				println!("{}", b);104			}105			println!("=== SNIP END ===");106		}107	};108}109110#[cfg(test)]111mod tests {112	use super::*;113114	#[test]115	fn function_selector_generation() {116		assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);117	}118119	#[test]120	fn event_topic_generation() {121		assert_eq!(122			hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),123			"ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",124		);125	}126}