git.delta.rocks / unique-network / refs/commits / 9a483c86e2bb

difftreelog

source

crates/evm-coder/src/lib.rs4.0 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![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;2930/// Solidity type definitions31pub 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	//#region Special types60	/// Makes function payable61	pub type value = U256;62	/// Makes function caller-sensitive63	pub type caller = address;64	//#endregion6566	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}8687/// Implementation is implicitly provided for all interfaces88///89/// Note: no Callable implementation is provided90#[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}109110/// Generate "tests", which will generate solidity code on execution and print it to stdout111/// Script at .maintain/scripts/generate_api.sh can split this output from test runtime112///113/// This macro receives type usage as second argument, but you can use anything as generics,114/// because no bounds are implied115#[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}