1234567891011121314151617#![doc = include_str!("../README.md")]1819#![warn(missing_docs)]20#![macro_use]21#![cfg_attr(not(feature = "std"), no_std)]22#[cfg(not(feature = "std"))]23extern crate alloc;2425use abi::{AbiRead, AbiReader, AbiWriter};26pub use evm_coder_procedural::{event_topic, fn_selector};27pub mod abi;28pub use events::{ToLog, ToTopic};29use execution::DispatchInfo;30pub mod execution;31#[macro_use]32pub mod custom_signature;333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192pub use evm_coder_procedural::solidity_interface;9394pub use evm_coder_procedural::solidity;9596pub use evm_coder_procedural::weight;97pub use const_format;98pub use sha3_const;99100101102103104105106107pub use evm_coder_procedural::ToLog;108109110#[doc(hidden)]111pub mod events;112#[doc(hidden)]113pub mod solidity;114115116117118pub mod types {119 #![allow(non_camel_case_types, missing_docs)]120121 #[cfg(not(feature = "std"))]122 use alloc::{vec::Vec};123 use pallet_evm::account::CrossAccountId;124 use primitive_types::{U256, H160, H256};125 use core::str::from_utf8;126127 use crate::custom_signature::{SignatureUnit, SIGNATURE_SIZE_LIMIT};128129 pub trait Signature {130 const SIGNATURE: SignatureUnit;131132 fn as_str() -> &'static str {133 from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")134 }135 }136137 impl Signature for bool {138 make_signature!(new fixed("bool"));139 }140141 macro_rules! define_simple_type {142 (type $ident:ident = $ty:ty) => {143 pub type $ident = $ty;144 impl Signature for $ty {145 make_signature!(new fixed(stringify!($ident)));146 }147 };148 }149150 define_simple_type!(type address = H160);151152 define_simple_type!(type uint8 = u8);153 define_simple_type!(type uint16 = u16);154 define_simple_type!(type uint32 = u32);155 define_simple_type!(type uint64 = u64);156 define_simple_type!(type uint128 = u128);157 define_simple_type!(type uint256 = U256);158 define_simple_type!(type bytes4 = [u8; 4]);159160 define_simple_type!(type topic = H256);161162 #[cfg(not(feature = "std"))]163 define_simple_type!(type string = ::alloc::string::String);164 #[cfg(feature = "std")]165 define_simple_type!(type string = ::std::string::String);166167 #[derive(Default, Debug)]168 pub struct bytes(pub Vec<u8>);169 impl Signature for bytes {170 make_signature!(new fixed("bytes"));171 }172173 174 175 pub type void = ();176177 178 179 pub type value = U256;180 181 pub type caller = address;182 183184 185 186 pub struct Msg<C> {187 pub call: C,188 189 pub caller: H160,190 191 192 193 pub value: U256,194 }195196 impl From<Vec<u8>> for bytes {197 fn from(src: Vec<u8>) -> Self {198 Self(src)199 }200 }201202 #[allow(clippy::from_over_into)]203 impl Into<Vec<u8>> for bytes {204 fn into(self) -> Vec<u8> {205 self.0206 }207 }208209 impl bytes {210 #[must_use]211 pub fn len(&self) -> usize {212 self.0.len()213 }214215 #[must_use]216 pub fn is_empty(&self) -> bool {217 self.len() == 0218 }219 }220221 #[derive(Debug, Default)]222 pub struct EthCrossAccount {223 pub(crate) eth: address,224 pub(crate) sub: uint256,225 }226227 impl EthCrossAccount {228 pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self229 where230 T: pallet_evm::account::Config,231 T::AccountId: AsRef<[u8; 32]>,232 {233 if cross_account_id.is_canonical_substrate() {234 Self {235 eth: Default::default(),236 sub: convert_cross_account_to_uint256::<T>(cross_account_id),237 }238 } else {239 Self {240 eth: *cross_account_id.as_eth(),241 sub: Default::default(),242 }243 }244 }245246 pub fn into_sub_cross_account<T>(&self) -> crate::execution::Result<T::CrossAccountId>247 where248 T: pallet_evm::account::Config,249 T::AccountId: From<[u8; 32]>,250 {251 if self.eth == Default::default() && self.sub == Default::default() {252 Err("All fields of cross account is zeroed".into())253 } else if self.eth == Default::default() {254 Ok(convert_uint256_to_cross_account::<T>(self.sub))255 } else if self.sub == Default::default() {256 Ok(T::CrossAccountId::from_eth(self.eth))257 } else {258 Err("All fields of cross account is non zeroed".into())259 }260 }261 }262263 impl Signature for EthCrossAccount {264 make_signature!(new fixed("(address,uint256)"));265 }266267 268 pub fn convert_cross_account_to_uint256<T: pallet_evm::account::Config>(269 from: &T::CrossAccountId,270 ) -> uint256271 where272 T::AccountId: AsRef<[u8; 32]>,273 {274 let slice = from.as_sub().as_ref();275 uint256::from_big_endian(slice)276 }277278 279 pub fn convert_uint256_to_cross_account<T: pallet_evm::account::Config>(280 from: uint256,281 ) -> T::CrossAccountId282 where283 T::AccountId: From<[u8; 32]>,284 {285 let mut new_admin_arr = [0_u8; 32];286 from.to_big_endian(&mut new_admin_arr);287 let account_id = T::AccountId::from(new_admin_arr);288 T::CrossAccountId::from_sub(account_id)289 }290}291292293pub trait Call: Sized {294 295 fn parse(selector: types::bytes4, input: &mut AbiReader) -> execution::Result<Option<Self>>;296}297298299300301302pub type Weight = frame_support::weights::Weight;303304305306pub trait Weighted: Call {307 308 fn weight(&self) -> DispatchInfo;309}310311312313pub trait Callable<C: Call> {314 315 fn call(&mut self, call: types::Msg<C>) -> execution::ResultWithPostInfo<AbiWriter>;316}317318319320321322323324325#[derive(Debug)]326pub enum ERC165Call {327 328 329 SupportsInterface {330 331 interface_id: types::bytes4,332 },333}334335impl ERC165Call {336 337 pub const INTERFACE_ID: types::bytes4 = u32::to_be_bytes(0x01ffc9a7);338}339340impl Call for ERC165Call {341 fn parse(selector: types::bytes4, input: &mut AbiReader) -> execution::Result<Option<Self>> {342 if selector != Self::INTERFACE_ID {343 return Ok(None);344 }345 Ok(Some(Self::SupportsInterface {346 interface_id: input.abi_read()?,347 }))348 }349}350351352353354355356#[macro_export]357macro_rules! generate_stubgen {358 ($name:ident, $decl:ty, $is_impl:literal) => {359 #[test]360 #[ignore]361 fn $name() {362 use evm_coder::solidity::TypeCollector;363 let mut out = TypeCollector::new();364 <$decl>::generate_solidity_interface(&mut out, $is_impl);365 println!("=== SNIP START ===");366 println!("// SPDX-License-Identifier: OTHER");367 println!("// This code is automatically generated");368 println!();369 println!("pragma solidity >=0.8.0 <0.9.0;");370 println!();371 for b in out.finish() {372 println!("{}", b);373 }374 println!("=== SNIP END ===");375 }376 };377}378379#[cfg(test)]380mod tests {381 use super::*;382383 #[test]384 fn function_selector_generation() {385 assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);386 }387388 389 390 391 392 393 394 395 396 397 398 399400 #[test]401 fn event_topic_generation() {402 assert_eq!(403 hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),404 "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",405 );406 }407}