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

difftreelog

source

crates/evm-coder/src/lib.rs3.2 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, weight, ToLog};7pub mod abi;8pub mod events;9pub use events::ToLog;10use execution::DispatchInfo;11pub mod execution;12pub mod solidity;1314/// Solidity type definitions15pub mod types {16	#![allow(non_camel_case_types)]1718	#[cfg(not(feature = "std"))]19	use alloc::{vec::Vec};20	use primitive_types::{U256, H160, H256};2122	pub type address = H160;2324	pub type uint8 = u8;25	pub type uint16 = u16;26	pub type uint32 = u32;27	pub type uint64 = u64;28	pub type uint128 = u128;29	pub type uint256 = U256;3031	pub type bytes4 = u32;3233	pub type topic = H256;3435	#[cfg(not(feature = "std"))]36	pub type string = ::alloc::string::String;37	#[cfg(feature = "std")]38	pub type string = ::std::string::String;39	pub type bytes = Vec<u8>;4041	pub type void = ();4243	//#region Special types44	/// Makes function payable45	pub type value = U256;46	/// Makes function caller-sensitive47	pub type caller = address;48	//#endregion4950	pub struct Msg<C> {51		pub call: C,52		pub caller: H160,53		pub value: U256,54	}55}5657pub trait Call: Sized {58	fn parse(selector: u32, input: &mut AbiReader) -> execution::Result<Option<Self>>;59}6061pub type Weight = u64;6263pub trait Weighted: Call {64	fn weight(&self) -> DispatchInfo;65}6667pub trait Callable<C: Call> {68	fn call(&mut self, call: types::Msg<C>) -> execution::ResultWithPostInfo<AbiWriter>;69}7071/// Implementation is implicitly provided for all interfaces72///73/// Note: no Callable implementation is provided74#[derive(Debug)]75pub enum ERC165Call {76	SupportsInterface { interface_id: types::bytes4 },77}7879impl ERC165Call {80	pub const INTERFACE_ID: types::bytes4 = 0x01ffc9a7;81}8283impl Call for ERC165Call {84	fn parse(selector: u32, input: &mut AbiReader) -> execution::Result<Option<Self>> {85		if selector != Self::INTERFACE_ID {86			return Ok(None);87		}88		Ok(Some(Self::SupportsInterface {89			interface_id: input.abi_read()?,90		}))91	}92}9394/// Generate "tests", which will generate solidity code on execution and print it to stdout95/// Script at .maintain/scripts/generate_api.sh can split this output from test runtime96///97/// This macro receives type usage as second argument, but you can use anything as generics,98/// because no bounds are implied99#[macro_export]100macro_rules! generate_stubgen {101	($name:ident, $decl:ty, $is_impl:literal) => {102		#[test]103		#[ignore]104		fn $name() {105			use evm_coder::solidity::TypeCollector;106			let mut out = TypeCollector::new();107			<$decl>::generate_solidity_interface(&mut out, $is_impl);108			println!("=== SNIP START ===");109			println!("// SPDX-License-Identifier: OTHER");110			println!("// This code is automatically generated");111			println!();112			println!("pragma solidity >=0.8.0 <0.9.0;");113			println!();114			for b in out.finish() {115				println!("{}", b);116			}117			println!("=== SNIP END ===");118		}119	};120}121122#[cfg(test)]123mod tests {124	use super::*;125126	#[test]127	fn function_selector_generation() {128		assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);129	}130131	#[test]132	fn event_topic_generation() {133		assert_eq!(134			hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),135			"ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",136		);137	}138}