1234567891011121314151617#![doc = include_str!("../README.md")]18#![deny(missing_docs)]19#![macro_use]20#![cfg_attr(not(feature = "std"), no_std)]21#[cfg(not(feature = "std"))]22extern crate alloc;2324use abi::{AbiRead, AbiReader, AbiWriter};25pub use evm_coder_procedural::{event_topic, fn_selector};26pub mod abi;27pub use events::{ToLog, ToTopic};28use execution::DispatchInfo;29pub mod execution;30#[macro_use]31pub mod custom_signature;323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091pub use evm_coder_procedural::solidity_interface;9293pub use evm_coder_procedural::solidity;9495pub use evm_coder_procedural::weight;96pub use sha3_const;979899100101102103104105pub use evm_coder_procedural::ToLog;106107108#[doc(hidden)]109pub mod events;110#[doc(hidden)]111pub mod solidity;112113114115116pub mod types {117 #![allow(non_camel_case_types, missing_docs)]118119 #[cfg(not(feature = "std"))]120 use alloc::{vec::Vec};121 use pallet_evm::account::CrossAccountId;122 use primitive_types::{U256, H160, H256};123 use core::str::from_utf8;124125 use crate::custom_signature::{SignatureUnit, SIGNATURE_SIZE_LIMIT};126127 pub trait Signature {128 const SIGNATURE: SignatureUnit;129130 fn as_str() -> &'static str {131 from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")132 }133 }134135 impl Signature for bool {136 make_signature!(new fixed("bool"));137 }138139 macro_rules! define_simple_type {140 (type $ident:ident = $ty:ty) => {141 pub type $ident = $ty;142 impl Signature for $ty {143 make_signature!(new fixed(stringify!($ident)));144 }145 };146 }147148 define_simple_type!(type address = H160);149150 define_simple_type!(type uint8 = u8);151 define_simple_type!(type uint16 = u16);152 define_simple_type!(type uint32 = u32);153 define_simple_type!(type uint64 = u64);154 define_simple_type!(type uint128 = u128);155 define_simple_type!(type uint256 = U256);156 define_simple_type!(type bytes4 = [u8; 4]);157158 define_simple_type!(type topic = H256);159160 #[cfg(not(feature = "std"))]161 define_simple_type!(type string = ::alloc::string::String);162 #[cfg(feature = "std")]163 define_simple_type!(type string = ::std::string::String);164165 #[derive(Default, Debug)]166 pub struct bytes(pub Vec<u8>);167 impl Signature for bytes {168 make_signature!(new fixed("bytes"));169 }170171 172 173 pub type void = ();174175 176 177 pub type value = U256;178 179 pub type caller = address;180 181182 183 184 pub struct Msg<C> {185 pub call: C,186 187 pub caller: H160,188 189 190 191 pub value: U256,192 }193194 impl From<Vec<u8>> for bytes {195 fn from(src: Vec<u8>) -> Self {196 Self(src)197 }198 }199200 #[allow(clippy::from_over_into)]201 impl Into<Vec<u8>> for bytes {202 fn into(self) -> Vec<u8> {203 self.0204 }205 }206207 impl bytes {208 #[must_use]209 pub fn len(&self) -> usize {210 self.0.len()211 }212213 #[must_use]214 pub fn is_empty(&self) -> bool {215 self.len() == 0216 }217 }218219 #[derive(Debug, Default)]220 pub struct EthCrossAccount {221 pub(crate) eth: address,222 pub(crate) sub: uint256,223 }224225 impl EthCrossAccount {226 pub fn from_sub_cross_account<T>(cross_account_id: &T::CrossAccountId) -> Self227 where228 T: pallet_evm::account::Config,229 T::AccountId: AsRef<[u8; 32]>,230 {231 if cross_account_id.is_canonical_substrate() {232 Self {233 eth: Default::default(),234 sub: convert_cross_account_to_uint256::<T>(cross_account_id),235 }236 } else {237 Self {238 eth: *cross_account_id.as_eth(),239 sub: Default::default(),240 }241 }242 }243244 pub fn into_sub_cross_account<T>(&self) -> crate::execution::Result<T::CrossAccountId>245 where246 T: pallet_evm::account::Config,247 T::AccountId: From<[u8; 32]>,248 {249 if self.eth == Default::default() && self.sub == Default::default() {250 Err("All fields of cross account is zeroed".into())251 } else if self.eth == Default::default() {252 Ok(convert_uint256_to_cross_account::<T>(self.sub))253 } else if self.sub == Default::default() {254 Ok(T::CrossAccountId::from_eth(self.eth))255 } else {256 Err("All fields of cross account is non zeroed".into())257 }258 }259 }260261 impl Signature for EthCrossAccount {262 make_signature!(new fixed("(address,uint256)"));263 }264265 266 pub fn convert_cross_account_to_uint256<T: pallet_evm::account::Config>(267 from: &T::CrossAccountId,268 ) -> uint256269 where270 T::AccountId: AsRef<[u8; 32]>,271 {272 let slice = from.as_sub().as_ref();273 uint256::from_big_endian(slice)274 }275276 277 pub fn convert_uint256_to_cross_account<T: pallet_evm::account::Config>(278 from: uint256,279 ) -> T::CrossAccountId280 where281 T::AccountId: From<[u8; 32]>,282 {283 let mut new_admin_arr = [0_u8; 32];284 from.to_big_endian(&mut new_admin_arr);285 let account_id = T::AccountId::from(new_admin_arr);286 T::CrossAccountId::from_sub(account_id)287 }288}289290291pub trait Call: Sized {292 293 fn parse(selector: types::bytes4, input: &mut AbiReader) -> execution::Result<Option<Self>>;294}295296297298299300pub type Weight = frame_support::weights::Weight;301302303304pub trait Weighted: Call {305 306 fn weight(&self) -> DispatchInfo;307}308309310311pub trait Callable<C: Call> {312 313 fn call(&mut self, call: types::Msg<C>) -> execution::ResultWithPostInfo<AbiWriter>;314}315316317318319320321322323#[derive(Debug)]324pub enum ERC165Call {325 326 327 SupportsInterface {328 329 interface_id: types::bytes4,330 },331}332333impl ERC165Call {334 335 pub const INTERFACE_ID: types::bytes4 = u32::to_be_bytes(0x01ffc9a7);336}337338impl Call for ERC165Call {339 fn parse(selector: types::bytes4, input: &mut AbiReader) -> execution::Result<Option<Self>> {340 if selector != Self::INTERFACE_ID {341 return Ok(None);342 }343 Ok(Some(Self::SupportsInterface {344 interface_id: types::bytes4::abi_read(input)?,345 }))346 }347}348349350351352353354#[macro_export]355macro_rules! generate_stubgen {356 ($name:ident, $decl:ty, $is_impl:literal) => {357 #[test]358 #[ignore]359 fn $name() {360 use evm_coder::solidity::TypeCollector;361 let mut out = TypeCollector::new();362 <$decl>::generate_solidity_interface(&mut out, $is_impl);363 println!("=== SNIP START ===");364 println!("// SPDX-License-Identifier: OTHER");365 println!("// This code is automatically generated");366 println!();367 println!("pragma solidity >=0.8.0 <0.9.0;");368 println!();369 for b in out.finish() {370 println!("{}", b);371 }372 println!("=== SNIP END ===");373 }374 };375}376377#[cfg(test)]378mod tests {379 use super::*;380381 #[test]382 fn function_selector_generation() {383 assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);384 }385386 #[test]387 fn event_topic_generation() {388 assert_eq!(389 hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),390 "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",391 );392 }393}