1234567891011121314151617#![cfg_attr(not(feature = "std"), no_std)]18#[cfg(not(feature = "std"))]19extern crate alloc;2021use abi::{AbiRead, AbiReader, AbiWriter};22pub use evm_coder_macros::{event_topic, fn_selector, solidity_interface, solidity, weight, ToLog};23pub mod abi;24pub mod events;25pub use events::ToLog;26use execution::DispatchInfo;27pub mod execution;28pub mod solidity;293031pub mod types {32 #![allow(non_camel_case_types)]3334 #[cfg(not(feature = "std"))]35 use alloc::{vec::Vec};36 use primitive_types::{U256, H160, H256};3738 pub type address = H160;3940 pub type uint8 = u8;41 pub type uint16 = u16;42 pub type uint32 = u32;43 pub type uint64 = u64;44 pub type uint128 = u128;45 pub type uint256 = U256;4647 pub type bytes4 = [u8; 4];4849 pub type topic = H256;5051 #[cfg(not(feature = "std"))]52 pub type string = ::alloc::string::String;53 #[cfg(feature = "std")]54 pub type string = ::std::string::String;55 pub type bytes = Vec<u8>;5657 pub type void = ();5859 60 61 pub type value = U256;62 63 pub type caller = address;64 6566 pub struct Msg<C> {67 pub call: C,68 pub caller: H160,69 pub value: U256,70 }71}7273pub trait Call: Sized {74 fn parse(selector: types::bytes4, input: &mut AbiReader) -> execution::Result<Option<Self>>;75}7677pub type Weight = u64;7879pub trait Weighted: Call {80 fn weight(&self) -> DispatchInfo;81}8283pub trait Callable<C: Call> {84 fn call(&mut self, call: types::Msg<C>) -> execution::ResultWithPostInfo<AbiWriter>;85}8687888990#[derive(Debug)]91pub enum ERC165Call {92 SupportsInterface { interface_id: types::bytes4 },93}9495impl ERC165Call {96 pub const INTERFACE_ID: types::bytes4 = u32::to_be_bytes(0x01ffc9a7);97}9899impl Call for ERC165Call {100 fn parse(selector: types::bytes4, input: &mut AbiReader) -> execution::Result<Option<Self>> {101 if selector != Self::INTERFACE_ID {102 return Ok(None);103 }104 Ok(Some(Self::SupportsInterface {105 interface_id: input.abi_read()?,106 }))107 }108}109110111112113114115#[macro_export]116macro_rules! generate_stubgen {117 ($name:ident, $decl:ty, $is_impl:literal) => {118 #[test]119 #[ignore]120 fn $name() {121 use evm_coder::solidity::TypeCollector;122 let mut out = TypeCollector::new();123 <$decl>::generate_solidity_interface(&mut out, $is_impl);124 println!("=== SNIP START ===");125 println!("// SPDX-License-Identifier: OTHER");126 println!("// This code is automatically generated");127 println!();128 println!("pragma solidity >=0.8.0 <0.9.0;");129 println!();130 for b in out.finish() {131 println!("{}", b);132 }133 println!("=== SNIP END ===");134 }135 };136}137138#[cfg(test)]139mod tests {140 use super::*;141142 #[test]143 fn function_selector_generation() {144 assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);145 }146147 #[test]148 fn event_topic_generation() {149 assert_eq!(150 hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),151 "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",152 );153 }154}