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

difftreelog

feat implement #[weight] macro for evm support

Yaroslav Bolyukin2021-11-05parent: #e4d3792.patch.diff
in: master

35 files changed

modifiedcrates/evm-coder-macros/src/lib.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/lib.rs
+++ b/crates/evm-coder-macros/src/lib.rs
@@ -211,6 +211,10 @@
 pub fn solidity(_args: TokenStream, stream: TokenStream) -> TokenStream {
 	stream
 }
+#[proc_macro_attribute]
+pub fn weight(_args: TokenStream, stream: TokenStream) -> TokenStream {
+	stream
+}
 
 #[proc_macro_derive(ToLog, attributes(indexed))]
 pub fn to_log(value: TokenStream) -> TokenStream {
modifiedcrates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/solidity_interface.rs
+++ b/crates/evm-coder-macros/src/solidity_interface.rs
@@ -30,11 +30,11 @@
 		})
 	}
 
-	fn expand_call_def(&self) -> proc_macro2::TokenStream {
+	fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
 		let name = &self.name;
 		let pascal_call_name = &self.pascal_call_name;
 		quote! {
-			#name(#pascal_call_name)
+			#name(#pascal_call_name #gen_ref)
 		}
 	}
 
@@ -45,18 +45,32 @@
 		}
 	}
 
-	fn expand_supports_interface(&self) -> proc_macro2::TokenStream {
+	fn expand_supports_interface(
+		&self,
+		generics: &proc_macro2::TokenStream,
+	) -> proc_macro2::TokenStream {
 		let pascal_call_name = &self.pascal_call_name;
 		quote! {
-			#pascal_call_name::supports_interface(interface_id)
+			<#pascal_call_name #generics>::supports_interface(interface_id)
+		}
+	}
+
+	fn expand_variant_weight(&self) -> proc_macro2::TokenStream {
+		let name = &self.name;
+		quote! {
+			Self::#name(call) => call.weight()
 		}
 	}
 
-	fn expand_variant_call(&self) -> proc_macro2::TokenStream {
+	fn expand_variant_call(
+		&self,
+		call_name: &proc_macro2::Ident,
+		generics: &proc_macro2::TokenStream,
+	) -> proc_macro2::TokenStream {
 		let name = &self.name;
 		let pascal_call_name = &self.pascal_call_name;
 		quote! {
-			InternalCall::#name(call) => return <Self as ::evm_coder::Callable<#pascal_call_name>>::call(self, Msg {
+			#call_name::#name(call) => return <Self as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self, Msg {
 				call,
 				caller: c.caller,
 				value: c.value,
@@ -64,20 +78,20 @@
 		}
 	}
 
-	fn expand_parse(&self) -> proc_macro2::TokenStream {
+	fn expand_parse(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
 		let name = &self.name;
 		let pascal_call_name = &self.pascal_call_name;
 		quote! {
-			if let Some(parsed_call) = #pascal_call_name::parse(method_id, reader)? {
+			if let Some(parsed_call) = <#pascal_call_name #generics>::parse(method_id, reader)? {
 				return Ok(Some(Self::#name(parsed_call)))
 			}
 		}
 	}
 
-	fn expand_generator(&self) -> proc_macro2::TokenStream {
+	fn expand_generator(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
 		let pascal_call_name = &self.pascal_call_name;
 		quote! {
-			#pascal_call_name::generate_solidity_interface(tc, is_impl);
+			<#pascal_call_name #generics>::generate_solidity_interface(tc, is_impl);
 		}
 	}
 
@@ -351,6 +365,25 @@
 	Pure,
 }
 
+pub struct WeightAttr(syn::Expr);
+
+mod keyword {
+	syn::custom_keyword!(weight);
+}
+
+impl syn::parse::Parse for WeightAttr {
+	fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+		input.parse::<syn::Token![#]>()?;
+		let content;
+		syn::bracketed!(content in input);
+		content.parse::<keyword::weight>()?;
+
+		let weight_content;
+		syn::parenthesized!(weight_content in content);
+		Ok(WeightAttr(weight_content.parse::<syn::Expr>()?))
+	}
+}
+
 struct Method {
 	name: Ident,
 	camel_name: String,
@@ -362,6 +395,7 @@
 	has_normal_args: bool,
 	mutability: Mutability,
 	result: Type,
+	weight: Option<Expr>,
 	docs: Vec<String>,
 }
 impl Method {
@@ -370,6 +404,7 @@
 			rename_selector: None,
 		};
 		let mut docs = Vec::new();
+		let mut weight = None;
 		for attr in &value.attrs {
 			let ident = parse_ident_from_path(&attr.path, false)?;
 			if ident == "solidity" {
@@ -384,6 +419,8 @@
 					_ => unreachable!(),
 				};
 				docs.push(value);
+			} else if ident == "weight" {
+				weight = Some(syn::parse2::<WeightAttr>(attr.to_token_stream())?.0);
 			}
 		}
 		let ident = &value.sig.ident;
@@ -466,6 +503,7 @@
 			has_normal_args,
 			mutability,
 			result: result.clone(),
+			weight,
 			docs,
 		})
 	}
@@ -528,7 +566,7 @@
 		}
 	}
 
-	fn expand_variant_call(&self) -> proc_macro2::TokenStream {
+	fn expand_variant_call(&self, call_name: &proc_macro2::Ident) -> proc_macro2::TokenStream {
 		let pascal_name = &self.pascal_name;
 		let name = &self.name;
 
@@ -555,17 +593,50 @@
 		let args = self.args.iter().map(|a| a.expand_call_arg());
 
 		quote! {
-			InternalCall::#pascal_name #matcher => {
+			#call_name::#pascal_name #matcher => {
 				let result = #receiver #name(
 					#(
 						#args,
 					)*
 				)?;
-				(&result).abi_write(&mut writer);
+				(&result).into_result()
 			}
 		}
 	}
 
+	fn expand_variant_weight(&self) -> proc_macro2::TokenStream {
+		let pascal_name = &self.pascal_name;
+		if let Some(weight) = &self.weight {
+			let matcher = if self.has_normal_args {
+				let names = self
+					.args
+					.iter()
+					.filter(|a| !a.is_special())
+					.map(|a| &a.name);
+
+				quote! {{
+					#(
+						#names,
+					)*
+				}}
+			} else {
+				quote! {}
+			};
+			quote! {
+				Self::#pascal_name #matcher => (#weight).into()
+			}
+		} else {
+			let matcher = if self.has_normal_args {
+				quote! {{..}}
+			} else {
+				quote! {}
+			};
+			quote! {
+				Self::#pascal_name #matcher => ().into()
+			}
+		}
+	}
+
 	fn expand_solidity_function(&self) -> proc_macro2::TokenStream {
 		let camel_name = &self.camel_name;
 		let mutability = match self.mutability {
@@ -600,6 +671,42 @@
 	}
 }
 
+fn generics_list(gen: &Generics) -> proc_macro2::TokenStream {
+	if gen.params.is_empty() {
+		return quote! {};
+	}
+	let params = gen.params.iter().map(|p| match p {
+		syn::GenericParam::Type(id) => {
+			let v = &id.ident;
+			quote! {#v}
+		}
+		syn::GenericParam::Lifetime(lt) => {
+			let v = &lt.lifetime;
+			quote! {#v}
+		}
+		syn::GenericParam::Const(c) => {
+			let i = &c.ident;
+			quote! {#i}
+		}
+	});
+	quote! { #(#params),* }
+}
+fn generics_reference(gen: &Generics) -> proc_macro2::TokenStream {
+	if gen.params.is_empty() {
+		return quote! {};
+	}
+	let list = generics_list(gen);
+	quote! { <#list> }
+}
+fn generics_data(gen: &Generics) -> proc_macro2::TokenStream {
+	let list = generics_list(gen);
+	if gen.params.len() == 1 {
+		quote! {#list}
+	} else {
+		quote! { (#list) }
+	}
+}
+
 pub struct SolidityInterface {
 	generics: Generics,
 	name: Box<syn::Type>,
@@ -628,6 +735,8 @@
 		let solidity_name = self.info.name.to_string();
 		let call_name = pascal_ident_to_call(&self.info.name);
 		let generics = self.generics;
+		let gen_ref = generics_reference(&generics);
+		let gen_data = generics_data(&generics);
 
 		let call_sub = self
 			.info
@@ -635,30 +744,46 @@
 			.0
 			.iter()
 			.chain(self.info.is.0.iter())
-			.map(Is::expand_call_def);
+			.map(|c| Is::expand_call_def(c, &gen_ref));
 		let call_parse = self
 			.info
 			.inline_is
 			.0
 			.iter()
 			.chain(self.info.is.0.iter())
-			.map(Is::expand_parse);
+			.map(|is| Is::expand_parse(is, &gen_ref));
 		let call_variants = self
 			.info
 			.inline_is
 			.0
 			.iter()
 			.chain(self.info.is.0.iter())
-			.map(Is::expand_variant_call);
+			.map(|c| Is::expand_variant_call(c, &call_name, &gen_ref));
+		let weight_variants = self
+			.info
+			.inline_is
+			.0
+			.iter()
+			.chain(self.info.is.0.iter())
+			.map(Is::expand_variant_weight);
 
 		let inline_interface_id = self.info.inline_is.0.iter().map(Is::expand_interface_id);
-		let supports_interface = self.info.is.0.iter().map(Is::expand_supports_interface);
+		let supports_interface = self
+			.info
+			.is
+			.0
+			.iter()
+			.map(|is| Is::expand_supports_interface(is, &gen_ref));
 
 		let calls = self.methods.iter().map(Method::expand_call_def);
 		let consts = self.methods.iter().map(Method::expand_const);
 		let interface_id = self.methods.iter().map(Method::expand_interface_id);
 		let parsers = self.methods.iter().map(Method::expand_parse);
-		let call_variants_this = self.methods.iter().map(Method::expand_variant_call);
+		let call_variants_this = self
+			.methods
+			.iter()
+			.map(|m| Method::expand_variant_call(m, &call_name));
+		let weight_variants_this = self.methods.iter().map(Method::expand_variant_weight);
 		let solidity_functions = self.methods.iter().map(Method::expand_solidity_function);
 
 		// TODO: Inline inline_is
@@ -676,15 +801,15 @@
 			.0
 			.iter()
 			.chain(self.info.inline_is.0.iter())
-			.map(Is::expand_generator);
+			.map(|is| Is::expand_generator(is, &gen_ref));
 		let solidity_event_generators = self.info.events.0.iter().map(Is::expand_event_generator);
 
 		// let methods = self.methods.iter().map(Method::solidity_def);
 
 		quote! {
 			#[derive(Debug)]
-			pub enum #call_name {
-				ERC165Call(::evm_coder::ERC165Call),
+			pub enum #call_name #gen_ref {
+				ERC165Call(::evm_coder::ERC165Call, ::core::marker::PhantomData<#gen_data>),
 				#(
 					#calls,
 				)*
@@ -692,11 +817,11 @@
 					#call_sub,
 				)*
 			}
-			impl #call_name {
+			impl #gen_ref #call_name #gen_ref {
 				#(
 					#consts
 				)*
-				pub const fn interface_id() -> u32 {
+				pub fn interface_id() -> u32 {
 					let mut interface_id = 0;
 					#(#interface_id)*
 					#(#inline_interface_id)*
@@ -748,11 +873,14 @@
 					tc.collect(out);
 				}
 			}
-			impl ::evm_coder::Call for #call_name {
+			impl #gen_ref ::evm_coder::Call for #call_name #gen_ref {
 				fn parse(method_id: u32, reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Option<Self>> {
 					use ::evm_coder::abi::AbiRead;
 					match method_id {
-						::evm_coder::ERC165Call::INTERFACE_ID => return Ok(::evm_coder::ERC165Call::parse(method_id, reader)?.map(Self::ERC165Call)),
+						::evm_coder::ERC165Call::INTERFACE_ID => return Ok(
+							::evm_coder::ERC165Call::parse(method_id, reader)?
+							.map(|c| Self::ERC165Call(c, ::core::marker::PhantomData))
+						),
 						#(
 							#parsers,
 						)*
@@ -764,19 +892,33 @@
 					return Ok(None);
 				}
 			}
-			impl #generics ::evm_coder::Callable<#call_name> for #name {
+			impl #generics ::evm_coder::Weighted for #call_name #gen_ref {
+				#[allow(unused_variables)]
+				fn weight(&self) -> ::evm_coder::execution::DispatchInfo {
+					match self {
+						#(
+							#weight_variants,
+						)*
+						// TODO: It should be very cheap, but not free
+						Self::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {..}, _) => 100u64.into(),
+						#(
+							#weight_variants_this,
+						)*
+					}
+				}
+			}
+			impl #generics ::evm_coder::Callable<#call_name #gen_ref> for #name {
 				#[allow(unreachable_code)] // In case of no inner calls
-				fn call(&mut self, c: Msg<#call_name>) -> Result<::evm_coder::abi::AbiWriter> {
+				fn call(&mut self, c: Msg<#call_name #gen_ref>) -> ::evm_coder::execution::ResultWithPostInfo<::evm_coder::abi::AbiWriter> {
 					use ::evm_coder::abi::AbiWrite;
-					type InternalCall = #call_name;
 					match c.call {
 						#(
 							#call_variants,
 						)*
-						InternalCall::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {interface_id}) => {
+						#call_name::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {interface_id}, _) => {
 							let mut writer = ::evm_coder::abi::AbiWriter::default();
-							writer.bool(&InternalCall::supports_interface(interface_id));
-							return Ok(writer);
+							writer.bool(&<#call_name #gen_ref>::supports_interface(interface_id));
+							return Ok(writer.into());
 						}
 						_ => {},
 					}
@@ -787,7 +929,6 @@
 						)*
 						_ => unreachable!()
 					}
-					Ok(writer)
 				}
 			}
 		}
modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/abi.rs
+++ b/crates/evm-coder/src/abi.rs
@@ -11,7 +11,10 @@
 use evm_core::ExitError;
 use primitive_types::{H160, U256};
 
-use crate::{execution::Error, types::string};
+use crate::{
+	execution::{Error, ResultWithPostInfo, WithPostDispatchInfo},
+	types::string,
+};
 use crate::execution::Result;
 
 const ABI_ALIGNMENT: usize = 32;
@@ -244,6 +247,7 @@
 }
 
 impl_abi_readable!(u32, uint32);
+impl_abi_readable!(u64, uint64);
 impl_abi_readable!(u128, uint128);
 impl_abi_readable!(U256, uint256);
 impl_abi_readable!(H160, address);
@@ -306,6 +310,36 @@
 
 pub trait AbiWrite {
 	fn abi_write(&self, writer: &mut AbiWriter);
+	fn into_result(&self) -> ResultWithPostInfo<AbiWriter> {
+		let mut writer = AbiWriter::new();
+		self.abi_write(&mut writer);
+		Ok(writer.into())
+	}
+}
+
+impl<T: AbiWrite> AbiWrite for ResultWithPostInfo<T> {
+	// this particular AbiWrite implementation should be split to another trait,
+	// which only implements [`into_result`]
+	//
+	// But due to lack of specialization feature in stable Rust, we can't have
+	// blanket impl of this trait `for T where T: AbiWrite`, so here we abusing
+	// default trait methods for it
+	fn abi_write(&self, _writer: &mut AbiWriter) {
+		debug_assert!(false, "shouldn't be called, see comment")
+	}
+	fn into_result(&self) -> ResultWithPostInfo<AbiWriter> {
+		match self {
+			Ok(v) => Ok(WithPostDispatchInfo {
+				post_info: v.post_info.clone(),
+				data: {
+					let mut out = AbiWriter::new();
+					v.data.abi_write(&mut out);
+					out
+				},
+			}),
+			Err(e) => Err(e.clone()),
+		}
+	}
 }
 
 macro_rules! impl_abi_writeable {
modifiedcrates/evm-coder/src/execution.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/execution.rs
+++ b/crates/evm-coder/src/execution.rs
@@ -4,7 +4,9 @@
 #[cfg(feature = "std")]
 use std::string::{String, ToString};
 
-#[derive(Debug)]
+use crate::Weight;
+
+#[derive(Debug, Clone)]
 pub enum Error {
 	Revert(String),
 	Fatal(ExitFatal),
@@ -21,3 +23,55 @@
 }
 
 pub type Result<T> = core::result::Result<T, Error>;
+
+pub struct DispatchInfo {
+	pub weight: Weight,
+}
+
+impl From<Weight> for DispatchInfo {
+	fn from(weight: Weight) -> Self {
+		Self { weight }
+	}
+}
+impl From<()> for DispatchInfo {
+	fn from(_: ()) -> Self {
+		Self { weight: 0 }
+	}
+}
+
+#[derive(Default, Clone)]
+pub struct PostDispatchInfo {
+	actual_weight: Option<Weight>,
+}
+
+impl PostDispatchInfo {
+	pub fn calc_unspent(&self, info: &DispatchInfo) -> Weight {
+		info.weight - self.calc_actual_weight(info)
+	}
+
+	pub fn calc_actual_weight(&self, info: &DispatchInfo) -> Weight {
+		if let Some(actual_weight) = self.actual_weight {
+			actual_weight.min(info.weight)
+		} else {
+			info.weight
+		}
+	}
+}
+
+#[derive(Clone)]
+pub struct WithPostDispatchInfo<T> {
+	pub data: T,
+	pub post_info: PostDispatchInfo,
+}
+
+impl<T> From<T> for WithPostDispatchInfo<T> {
+	fn from(data: T) -> Self {
+		Self {
+			data,
+			post_info: Default::default(),
+		}
+	}
+}
+
+pub type ResultWithPostInfo<T> =
+	core::result::Result<WithPostDispatchInfo<T>, WithPostDispatchInfo<Error>>;
modifiedcrates/evm-coder/src/lib.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/lib.rs
+++ b/crates/evm-coder/src/lib.rs
@@ -3,10 +3,11 @@
 extern crate alloc;
 
 use abi::{AbiRead, AbiReader, AbiWriter};
-pub use evm_coder_macros::{event_topic, fn_selector, solidity_interface, solidity, ToLog};
+pub use evm_coder_macros::{event_topic, fn_selector, solidity_interface, solidity, weight, ToLog};
 pub mod abi;
 pub mod events;
 pub use events::ToLog;
+use execution::DispatchInfo;
 pub mod execution;
 pub mod solidity;
 
@@ -57,8 +58,14 @@
 	fn parse(selector: u32, input: &mut AbiReader) -> execution::Result<Option<Self>>;
 }
 
+pub type Weight = u64;
+
+pub trait Weighted: Call {
+	fn weight(&self) -> DispatchInfo;
+}
+
 pub trait Callable<C: Call> {
-	fn call(&mut self, call: types::Msg<C>) -> execution::Result<AbiWriter>;
+	fn call(&mut self, call: types::Msg<C>) -> execution::ResultWithPostInfo<AbiWriter>;
 }
 
 /// Implementation is implicitly provided for all interfaces
@@ -84,15 +91,20 @@
 	}
 }
 
+/// Generate "tests", which will generate solidity code on execution and print it to stdout
+/// Script at .maintain/scripts/generate_api.sh can split this output from test runtime
+///
+/// This macro receives type usage as second argument, but you can use anything as generics,
+/// because no bounds are implied
 #[macro_export]
 macro_rules! generate_stubgen {
-	($name:ident, $decl:ident, $is_impl:literal) => {
+	($name:ident, $decl:ty, $is_impl:literal) => {
 		#[test]
 		#[ignore]
 		fn $name() {
 			use evm_coder::solidity::TypeCollector;
 			let mut out = TypeCollector::new();
-			$decl::generate_solidity_interface(&mut out, $is_impl);
+			<$decl>::generate_solidity_interface(&mut out, $is_impl);
 			println!("=== SNIP START ===");
 			println!("// SPDX-License-Identifier: OTHER");
 			println!("// This code is automatically generated");
deletedcrates/evm-coder/tests/a.rsdiffbeforeafterboth
--- a/crates/evm-coder/tests/a.rs
+++ /dev/null
@@ -1,89 +0,0 @@
-#![allow(dead_code)] // This test only checks that macros is not panicking
-
-use evm_coder::{solidity_interface, types::*, ToLog, execution::Result};
-use evm_coder_macros::solidity;
-
-struct Impls;
-
-#[solidity_interface(name = "OurInterface")]
-impl Impls {
-	fn fn_a(&self, _input: uint256) -> Result<bool> {
-		todo!()
-	}
-}
-
-#[solidity_interface(name = "OurInterface1")]
-impl Impls {
-	fn fn_b(&self, _input: uint128) -> Result<uint32> {
-		todo!()
-	}
-}
-
-#[solidity_interface(
-	name = "OurInterface2",
-	is(OurInterface),
-	inline_is(OurInterface1),
-	events(ERC721Log)
-)]
-impl Impls {
-	#[solidity(rename_selector = "fnK")]
-	fn fn_c(&self, _input: uint32) -> Result<uint8> {
-		todo!()
-	}
-	fn fn_d(&self, _value: uint32) -> Result<uint32> {
-		todo!()
-	}
-
-	fn caller_sensitive(&self, _caller: caller) -> Result<uint8> {
-		todo!()
-	}
-	fn payable(&mut self, _value: value) -> Result<uint8> {
-		todo!()
-	}
-}
-
-#[derive(ToLog)]
-enum ERC721Log {
-	Transfer {
-		#[indexed]
-		from: address,
-		#[indexed]
-		to: address,
-		value: uint256,
-	},
-	Eee {
-		#[indexed]
-		aaa: address,
-		bbb: uint256,
-	},
-}
-
-struct ERC20;
-
-#[solidity_interface(name = "ERC20")]
-impl ERC20 {
-	fn decimals(&self) -> Result<uint8> {
-		todo!()
-	}
-	fn balance_of(&self, _owner: address) -> Result<uint256> {
-		todo!()
-	}
-	fn transfer(&mut self, _caller: caller, _to: address, _value: uint256) -> Result<bool> {
-		todo!()
-	}
-	fn transfer_from(
-		&mut self,
-		_caller: caller,
-		_from: address,
-		_to: address,
-		_value: uint256,
-	) -> Result<bool> {
-		todo!()
-	}
-	fn approve(&mut self, _caller: caller, _spender: address, _value: uint256) -> Result<bool> {
-		todo!()
-	}
-	fn allowance(&self, _owner: address, _spender: address) -> Result<uint256> {
-		todo!()
-	}
-}
addedcrates/evm-coder/tests/generics.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/evm-coder/tests/generics.rs
@@ -0,0 +1,20 @@
+use std::marker::PhantomData;
+use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};
+
+struct Generic<T>(PhantomData<T>);
+
+#[solidity_interface(name = "GenericIs")]
+impl<T> Generic<T> {
+	fn test_1(&self) -> Result<uint256> {
+		todo!()
+	}
+}
+
+#[solidity_interface(name = "Generic", is(GenericIs))]
+impl<T: Into<u32>> Generic<T> {
+	fn test_2(&self) -> Result<uint256> {
+		todo!()
+	}
+}
+
+generate_stubgen!(gen_iface, GenericCall<()>, false);
addedcrates/evm-coder/tests/log.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/evm-coder/tests/log.rs
@@ -0,0 +1,17 @@
+use evm_coder::{ToLog, types::*};
+
+#[derive(ToLog)]
+enum ERC721Log {
+	Transfer {
+		#[indexed]
+		from: address,
+		#[indexed]
+		to: address,
+		value: uint256,
+	},
+	Eee {
+		#[indexed]
+		aaa: address,
+		bbb: uint256,
+	},
+}
addedcrates/evm-coder/tests/random.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/evm-coder/tests/random.rs
@@ -0,0 +1,65 @@
+#![allow(dead_code)] // This test only checks that macros is not panicking
+
+use evm_coder::{ToLog, execution::Result, solidity_interface, types::*};
+use evm_coder_macros::{solidity, weight};
+
+struct Impls;
+
+#[solidity_interface(name = "OurInterface")]
+impl Impls {
+	fn fn_a(&self, _input: uint256) -> Result<bool> {
+		todo!()
+	}
+}
+
+#[solidity_interface(name = "OurInterface1")]
+impl Impls {
+	fn fn_b(&self, _input: uint128) -> Result<uint32> {
+		todo!()
+	}
+}
+
+#[derive(ToLog)]
+enum OurEvents {
+	Event1 {
+		field1: uint32,
+	},
+	Event2 {
+		field1: uint32,
+		#[indexed]
+		field2: uint32,
+	},
+}
+
+#[solidity_interface(
+	name = "OurInterface2",
+	is(OurInterface),
+	inline_is(OurInterface1),
+	events(OurEvents)
+)]
+impl Impls {
+	#[solidity(rename_selector = "fnK")]
+	fn fn_c(&self, _input: uint32) -> Result<uint8> {
+		todo!()
+	}
+	fn fn_d(&self, _value: uint32) -> Result<uint32> {
+		todo!()
+	}
+
+	fn caller_sensitive(&self, _caller: caller) -> Result<uint8> {
+		todo!()
+	}
+	fn payable(&mut self, _value: value) -> Result<uint8> {
+		todo!()
+	}
+
+	#[weight(*_weight)]
+	fn with_weight(&self, _weight: uint64) -> Result<void> {
+		todo!()
+	}
+
+	/// Doccoment example
+	fn with_doc(&self) -> Result<void> {
+		todo!()
+	}
+}
addedcrates/evm-coder/tests/solidity_generation.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/evm-coder/tests/solidity_generation.rs
@@ -0,0 +1,35 @@
+use evm_coder::{execution::Result, generate_stubgen, solidity_interface, types::*};
+
+struct ERC20;
+
+#[solidity_interface(name = "ERC20")]
+impl ERC20 {
+	fn decimals(&self) -> Result<uint8> {
+		todo!()
+	}
+	/// Get balance of specified owner
+	fn balance_of(&self, _owner: address) -> Result<uint256> {
+		todo!()
+	}
+	fn transfer(&mut self, _caller: caller, _to: address, _value: uint256) -> Result<bool> {
+		todo!()
+	}
+	fn transfer_from(
+		&mut self,
+		_caller: caller,
+		_from: address,
+		_to: address,
+		_value: uint256,
+	) -> Result<bool> {
+		todo!()
+	}
+	fn approve(&mut self, _caller: caller, _spender: address, _value: uint256) -> Result<bool> {
+		todo!()
+	}
+	fn allowance(&self, _owner: address, _spender: address) -> Result<uint256> {
+		todo!()
+	}
+}
+
+generate_stubgen!(gen_impl, ERC20Call, true);
+generate_stubgen!(gen_iface, ERC20Call, false);
modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -268,13 +268,13 @@
 jsonrpc-core = '18.0.0'
 jsonrpc-pubsub = "18.0.0"
 
-fc-rpc-core = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-consensus = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-mapping-sync = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-rpc = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-db = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-rpc = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-pallet-ethereum = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+fc-rpc-core = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-consensus = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-mapping-sync = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-rpc = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-db = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-rpc = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+pallet-ethereum = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 
 nft-rpc = { path = "../rpc" }
 
modifiednode/rpc/Cargo.tomldiffbeforeafterboth
--- a/node/rpc/Cargo.toml
+++ b/node/rpc/Cargo.toml
@@ -41,12 +41,12 @@
 substrate-frame-rpc-system = { git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 tokio = { version = "0.2.25", features = ["macros", "sync"] }
 
-pallet-ethereum = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-rpc = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-rpc = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-rpc-core = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-db = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fc-mapping-sync = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-ethereum = { git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-rpc = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-rpc = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-rpc-core = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-db = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fc-mapping-sync = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 
 pallet-nft = { path = "../../pallets/nft" }
 uc-rpc = { path = "../../client/rpc" }
modifiedpallets/common/Cargo.tomldiffbeforeafterboth
--- a/pallets/common/Cargo.toml
+++ b/pallets/common/Cargo.toml
@@ -19,7 +19,7 @@
 pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' }
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 serde = { version = "1.0.130", default-features = false }
 scale-info = { version = "1.0.0", default-features = false, features = [
     "derive",
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1,6 +1,7 @@
 #![cfg_attr(not(feature = "std"), no_std)]
 
 use core::ops::{Deref, DerefMut};
+use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
 use sp_std::vec::Vec;
 use account::CrossAccountId;
 use frame_support::{
@@ -8,6 +9,7 @@
 	ensure, fail,
 	traits::{Imbalance, Get, Currency},
 };
+use pallet_evm::GasWeightMapping;
 use nft_data_structs::{
 	COLLECTION_NUMBER_LIMIT, Collection, CollectionId, CreateItemData, ExistenceRequirement,
 	MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_COLLECTION_NAME_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
@@ -27,17 +29,22 @@
 pub struct CollectionHandle<T: Config> {
 	pub id: CollectionId,
 	collection: Collection<T>,
-	pub recorder: pallet_evm_coder_substrate::SubstrateRecorder<T>,
+	pub recorder: SubstrateRecorder<T>,
 }
+impl<T: Config> WithRecorder<T> for CollectionHandle<T> {
+	fn recorder(&self) -> &SubstrateRecorder<T> {
+		&self.recorder
+	}
+	fn into_recorder(self) -> SubstrateRecorder<T> {
+		self.recorder
+	}
+}
 impl<T: Config> CollectionHandle<T> {
 	pub fn new_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {
 		<CollectionById<T>>::get(id).map(|collection| Self {
 			id,
 			collection,
-			recorder: pallet_evm_coder_substrate::SubstrateRecorder::new(
-				eth::collection_id_to_address(id),
-				gas_limit,
-			),
+			recorder: SubstrateRecorder::new(eth::collection_id_to_address(id), gas_limit),
 		})
 	}
 	pub fn new(id: CollectionId) -> Option<Self> {
@@ -46,33 +53,30 @@
 	pub fn try_get(id: CollectionId) -> Result<Self, DispatchError> {
 		Ok(Self::new(id).ok_or_else(|| <Error<T>>::CollectionNotFound)?)
 	}
-	pub fn log(&self, log: impl evm_coder::ToLog) -> DispatchResult {
-		self.recorder.log_sub(log)
+	pub fn log(&self, log: impl evm_coder::ToLog) {
+		self.recorder.log(log)
 	}
-	pub fn log_infallible(&self, log: impl evm_coder::ToLog) {
-		self.recorder.log_infallible(log)
+	pub fn consume_store_reads(&self, reads: u64) -> evm_coder::execution::Result<()> {
+		self.recorder
+			.consume_gas(T::GasWeightMapping::weight_to_gas(
+				<T as frame_system::Config>::DbWeight::get()
+					.read
+					.saturating_mul(reads),
+			))
 	}
-	#[allow(dead_code)]
-	fn consume_gas(&self, gas: u64) -> DispatchResult {
-		self.recorder.consume_gas_sub(gas)
-	}
-	pub fn consume_sload(&self) -> DispatchResult {
-		self.recorder.consume_sload_sub()
+	pub fn consume_store_writes(&self, writes: u64) -> evm_coder::execution::Result<()> {
+		self.recorder
+			.consume_gas(T::GasWeightMapping::weight_to_gas(
+				<T as frame_system::Config>::DbWeight::get()
+					.write
+					.saturating_mul(writes),
+			))
 	}
-	pub fn consume_sstores(&self, amount: usize) -> DispatchResult {
-		self.recorder.consume_sstores_sub(amount)
-	}
-	pub fn consume_sstore(&self) -> DispatchResult {
-		self.recorder.consume_sstore_sub()
-	}
-	pub fn consume_log(&self, topics: usize, data: usize) -> DispatchResult {
-		self.recorder.consume_log_sub(topics, data)
-	}
-	pub fn submit_logs(self) -> DispatchResult {
+	pub fn submit_logs(self) {
 		self.recorder.submit_logs()
 	}
 	pub fn save(self) -> DispatchResult {
-		self.recorder.submit_logs()?;
+		self.recorder.submit_logs();
 		<CollectionById<T>>::insert(self.id, self.collection);
 		Ok(())
 	}
@@ -96,24 +100,20 @@
 		ensure!(*subject.as_sub() == self.owner, <Error<T>>::NoPermission);
 		Ok(())
 	}
-	pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> Result<bool, DispatchError> {
-		self.consume_sload()?;
-
-		Ok(*subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject)))
+	pub fn is_owner_or_admin(&self, subject: &T::CrossAccountId) -> bool {
+		*subject.as_sub() == self.owner || <IsAdmin<T>>::get((self.id, subject))
 	}
 	pub fn check_is_owner_or_admin(&self, subject: &T::CrossAccountId) -> DispatchResult {
-		ensure!(self.is_owner_or_admin(subject)?, <Error<T>>::NoPermission);
+		ensure!(self.is_owner_or_admin(subject), <Error<T>>::NoPermission);
 		Ok(())
 	}
-	pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> Result<bool, DispatchError> {
-		Ok(self.limits.owner_can_transfer() && self.is_owner_or_admin(user)?)
+	pub fn ignores_allowance(&self, user: &T::CrossAccountId) -> bool {
+		self.limits.owner_can_transfer() && self.is_owner_or_admin(user)
 	}
-	pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> Result<bool, DispatchError> {
-		Ok(self.limits.owner_can_transfer() && self.is_owner_or_admin(user)?)
+	pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> bool {
+		self.limits.owner_can_transfer() && self.is_owner_or_admin(user)
 	}
 	pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {
-		self.consume_sload()?;
-
 		ensure!(
 			<Allowlist<T>>::get((self.id, user)),
 			<Error<T>>::AddressNotInAllowlist
modifiedpallets/evm-coder-substrate/Cargo.tomldiffbeforeafterboth
--- a/pallets/evm-coder-substrate/Cargo.toml
+++ b/pallets/evm-coder-substrate/Cargo.toml
@@ -4,15 +4,18 @@
 edition = "2018"
 
 [dependencies]
-scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
+scale-info = { version = "1.0.0", default-features = false, features = [
+    "derive",
+] }
 sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 ethereum = { version = "0.9", git = "https://github.com/purestake/ethereum", branch = "joshy-scale-info", default-features = false }
 evm-coder = { default-features = false, path = "../../crates/evm-coder" }
-pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 frame-system = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
+frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 
 [dependencies.codec]
 default-features = false
@@ -31,4 +34,6 @@
     "pallet-evm/std",
     "frame-support/std",
     "frame-system/std",
+    'frame-benchmarking/std',
 ]
+runtime-benchmarks = ['frame-benchmarking']
addedpallets/evm-coder-substrate/src/benchmarking.rsdiffbeforeafterboth
--- /dev/null
+++ b/pallets/evm-coder-substrate/src/benchmarking.rs
@@ -0,0 +1,8 @@
+use super::*;
+
+use frame_benchmarking::benchmarks;
+
+benchmarks! {
+	// gas_per_second {
+	// }: (pallet_evm::)
+}
modifiedpallets/evm-coder-substrate/src/lib.rsdiffbeforeafterboth
--- a/pallets/evm-coder-substrate/src/lib.rs
+++ b/pallets/evm-coder-substrate/src/lib.rs
@@ -2,6 +2,8 @@
 
 #[cfg(not(feature = "std"))]
 extern crate alloc;
+// #[cfg(feature = "runtime-benchmarks")]
+// pub mod benchmarking;
 
 pub use pallet::*;
 
@@ -17,7 +19,10 @@
 		types::{Msg, value},
 	};
 	use frame_support::{ensure};
-	use pallet_evm::{ExitError, ExitReason, ExitRevert, ExitSucceed, PrecompileOutput};
+	use pallet_evm::{
+		ExitError, ExitReason, ExitRevert, ExitSucceed, PrecompileOutput, GasWeightMapping,
+	};
+	use frame_system::ensure_signed;
 	pub use frame_support::dispatch::DispatchResult;
 	use pallet_ethereum::EthereumTransactionSender;
 	use sp_std::cell::RefCell;
@@ -25,6 +30,7 @@
 	use sp_core::{H160, H256};
 	use ethereum::Log;
 	use frame_support::{pallet_prelude::*, traits::PalletInfo};
+	use frame_system::pallet_prelude::*;
 
 	/// DispatchError is opaque, but we need to somehow extract correct error in case of OutOfGas failure
 	/// So we have this pallet, which defines OutOfGas error, and knews its own id to check if DispatchError
@@ -40,25 +46,24 @@
 	#[pallet::config]
 	pub trait Config: frame_system::Config {
 		type EthereumTransactionSender: pallet_ethereum::EthereumTransactionSender;
+		type GasWeightMapping: pallet_evm::GasWeightMapping;
 	}
 
 	#[pallet::pallet]
 	pub struct Pallet<T>(_);
 
-	// FIXME: those items are defined as private in evm_gasometer::consts, and we can't directly use it
-	pub const G_LOG: u64 = 375;
-	pub const G_LOGDATA: u64 = 8;
-	pub const G_LOGTOPIC: u64 = 375;
+	#[pallet::call]
+	impl<T: Config> Pallet<T> {
+		#[pallet::weight(0)]
+		pub fn empty_call(origin: OriginFor<T>) -> DispatchResult {
+			let _sender = ensure_signed(origin)?;
+			Ok(())
+		}
+	}
 
 	// From instabul hardfork configuration: https://github.com/rust-blockchain/evm/blob/fd4fd6acc0ca3208d6770fdb3ba407c94cdf97c6/runtime/src/lib.rs#L284
 	pub const G_SLOAD_WORD: u64 = 800;
 	pub const G_SSTORE_WORD: u64 = 20000;
-
-	fn log_price(data: usize, topics: usize) -> u64 {
-		G_LOG
-			.saturating_add((data as u64).saturating_mul(G_LOGDATA))
-			.saturating_add((topics as u64).saturating_mul(G_LOGTOPIC))
-	}
 
 	pub fn generate_transaction() -> ethereum::TransactionV0 {
 		use ethereum::{TransactionV0, TransactionAction, TransactionSignature};
@@ -98,16 +103,10 @@
 		pub fn is_empty(&self) -> bool {
 			self.logs.borrow().is_empty()
 		}
-		pub fn log_sub(&self, log: impl ToLog) -> DispatchResult {
-			self.log_raw_sub(log.to_log(self.contract))
-		}
-		pub fn log_raw_sub(&self, log: Log) -> DispatchResult {
-			self.consume_gas_sub(log_price(log.data.len(), log.topics.len()))?;
-			self.logs.borrow_mut().push(log);
-			Ok(())
-		}
+		// TODO: Replace with real storage in pallet-ethereum,
+		// same way as it is done with frame_system's Events
 		/// Doesn't consumes any gas, should be used after consume_log_sub
-		pub fn log_infallible(&self, log: impl ToLog) {
+		pub fn log(&self, log: impl ToLog) {
 			self.logs.borrow_mut().push(log.to_log(self.contract));
 		}
 		pub fn retrieve_logs(self) -> Vec<Log> {
@@ -125,9 +124,6 @@
 		}
 		pub fn consume_sstore_sub(&self) -> DispatchResult {
 			self.consume_gas_sub(G_SSTORE_WORD)
-		}
-		pub fn consume_log_sub(&self, topics: usize, data: usize) -> DispatchResult {
-			self.consume_gas_sub(log_price(data, topics))
 		}
 		pub fn consume_gas_sub(&self, gas: u64) -> DispatchResult {
 			ensure!(gas != u64::MAX, Error::<T>::OutOfGas);
@@ -154,6 +150,10 @@
 			*gas_limit -= gas;
 			Ok(())
 		}
+		pub fn return_gas(&self, gas: u64) {
+			let mut gas_limit = self.gas_limit.borrow_mut();
+			*gas_limit += gas;
+		}
 
 		pub fn evm_to_precompile_output(
 			self,
@@ -181,12 +181,16 @@
 			})
 		}
 
-		pub fn submit_logs(self) -> DispatchResult {
+		pub fn submit_logs(self) {
 			let logs = self.retrieve_logs();
 			if logs.is_empty() {
-				return Ok(());
+				return;
 			}
-			T::EthereumTransactionSender::submit_logs_transaction(generate_transaction(), logs)
+			T::EthereumTransactionSender::submit_logs_transaction(
+				Default::default(),
+				generate_transaction(),
+				logs,
+			)
 		}
 	}
 
@@ -215,7 +219,31 @@
 		}
 	}
 
-	pub fn call_internal<C: evm_coder::Call, E: evm_coder::Callable<C>>(
+	pub trait WithRecorder<T: Config> {
+		fn recorder(&self) -> &SubstrateRecorder<T>;
+		fn into_recorder(self) -> SubstrateRecorder<T>;
+	}
+
+	/// Helper to simplify implementing bridge between evm-coder definitions and pallet-evm
+	pub fn call<
+		T: Config,
+		C: evm_coder::Call + evm_coder::Weighted,
+		E: evm_coder::Callable<C> + WithRecorder<T>,
+	>(
+		caller: H160,
+		mut e: E,
+		value: value,
+		input: &[u8],
+	) -> Option<PrecompileOutput> {
+		let result = call_internal(caller, &mut e, value, input);
+		e.into_recorder().evm_to_precompile_output(result)
+	}
+
+	fn call_internal<
+		T: Config,
+		C: evm_coder::Call + evm_coder::Weighted,
+		E: evm_coder::Callable<C> + WithRecorder<T>,
+	>(
 		caller: H160,
 		e: &mut E,
 		value: value,
@@ -227,11 +255,28 @@
 			return Ok(None);
 		}
 		let call = call.unwrap();
-		e.call(Msg {
+
+		let dispatch_info = call.weight();
+		e.recorder()
+			.consume_gas(T::GasWeightMapping::weight_to_gas(dispatch_info.weight))?;
+
+		match e.call(Msg {
 			call,
 			caller,
 			value,
-		})
-		.map(Some)
+		}) {
+			Ok(v) => {
+				let unspent = v.post_info.calc_unspent(&dispatch_info);
+				e.recorder()
+					.return_gas(T::GasWeightMapping::weight_to_gas(unspent));
+				Ok(Some(v.data))
+			}
+			Err(v) => {
+				let unspent = v.post_info.calc_unspent(&dispatch_info);
+				e.recorder()
+					.return_gas(T::GasWeightMapping::weight_to_gas(unspent));
+				Err(v.data)
+			}
+		}
 	}
 }
modifiedpallets/evm-contract-helpers/Cargo.tomldiffbeforeafterboth
--- a/pallets/evm-contract-helpers/Cargo.toml
+++ b/pallets/evm-contract-helpers/Cargo.toml
@@ -4,7 +4,9 @@
 edition = "2018"
 
 [dependencies]
-scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
+scale-info = { version = "1.0.0", default-features = false, features = [
+    "derive",
+] }
 frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 frame-system = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
@@ -12,7 +14,7 @@
 sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 evm-coder = { default-features = false, path = '../../crates/evm-coder' }
 pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' }
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 up-sponsorship = { default-features = false, path = '../../primitives/sponsorship' }
 log = "0.4.14"
 
addedpallets/evm-contract-helpers/exp.rsdiffbeforeafterboth

no changes

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -1,6 +1,6 @@
 use core::marker::PhantomData;
 use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};
-use pallet_evm_coder_substrate::SubstrateRecorder;
+use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
 use pallet_evm::{ExitReason, ExitRevert, OnCreate, OnMethodCall, PrecompileOutput};
 use sp_core::H160;
 use crate::{
@@ -11,16 +11,23 @@
 use sp_std::{convert::TryInto, vec::Vec};
 
 struct ContractHelpers<T: Config>(SubstrateRecorder<T>);
+impl<T: Config> WithRecorder<T> for ContractHelpers<T> {
+	fn recorder(&self) -> &SubstrateRecorder<T> {
+		&self.0
+	}
 
+	fn into_recorder(self) -> SubstrateRecorder<T> {
+		self.0
+	}
+}
+
 #[solidity_interface(name = "ContractHelpers")]
 impl<T: Config> ContractHelpers<T> {
 	fn contract_owner(&self, contract_address: address) -> Result<address> {
-		self.0.consume_sload()?;
 		Ok(<Owner<T>>::get(contract_address))
 	}
 
 	fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {
-		self.0.consume_sload()?;
 		Ok(<SelfSponsoring<T>>::get(contract_address))
 	}
 
@@ -30,9 +37,7 @@
 		contract_address: address,
 		enabled: bool,
 	) -> Result<void> {
-		self.0.consume_sload()?;
 		<Pallet<T>>::ensure_owner(contract_address, caller)?;
-		self.0.consume_sstore()?;
 		<Pallet<T>>::toggle_sponsoring(contract_address, enabled);
 		Ok(())
 	}
@@ -43,27 +48,22 @@
 		contract_address: address,
 		rate_limit: uint32,
 	) -> Result<void> {
-		self.0.consume_sload()?;
 		<Pallet<T>>::ensure_owner(contract_address, caller)?;
-		self.0.consume_sstore()?;
 		<Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());
 		Ok(())
 	}
 
 	fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {
-		self.0.consume_sload()?;
 		Ok(<SponsoringRateLimit<T>>::get(contract_address)
 			.try_into()
 			.map_err(|_| "rate limit > u32::MAX")?)
 	}
 
 	fn allowed(&self, contract_address: address, user: address) -> Result<bool> {
-		self.0.consume_sload()?;
 		Ok(<Pallet<T>>::allowed(contract_address, user, true))
 	}
 
 	fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {
-		self.0.consume_sload()?;
 		Ok(<AllowlistEnabled<T>>::get(contract_address))
 	}
 
@@ -73,9 +73,7 @@
 		contract_address: address,
 		enabled: bool,
 	) -> Result<void> {
-		self.0.consume_sload()?;
 		<Pallet<T>>::ensure_owner(contract_address, caller)?;
-		self.0.consume_sstore()?;
 		<Pallet<T>>::toggle_allowlist(contract_address, enabled);
 		Ok(())
 	}
@@ -87,9 +85,7 @@
 		user: address,
 		allowed: bool,
 	) -> Result<void> {
-		self.0.consume_sload()?;
 		<Pallet<T>>::ensure_owner(contract_address, caller)?;
-		self.0.consume_sstore()?;
 		<Pallet<T>>::toggle_allowed(contract_address, user, allowed);
 		Ok(())
 	}
@@ -130,9 +126,8 @@
 			return None;
 		}
 
-		let mut helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));
-		let result = pallet_evm_coder_substrate::call_internal(*source, &mut helpers, value, input);
-		helpers.0.evm_to_precompile_output(result)
+		let helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));
+		pallet_evm_coder_substrate::call(*source, helpers, value, input)
 	}
 
 	fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {
@@ -170,5 +165,5 @@
 	}
 }
 
-generate_stubgen!(contract_helpers_impl, ContractHelpersCall, true);
-generate_stubgen!(contract_helpers_iface, ContractHelpersCall, false);
+generate_stubgen!(contract_helpers_impl, ContractHelpersCall<()>, true);
+generate_stubgen!(contract_helpers_iface, ContractHelpersCall<()>, false);
modifiedpallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth
--- a/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
+++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
@@ -21,6 +21,7 @@
 	}
 }
 
+// Selector: 31acb1fe
 contract ContractHelpers is Dummy, ERC165 {
 	// Selector: contractOwner(address) 5152b14c
 	function contractOwner(address contractAddress)
modifiedpallets/evm-migration/Cargo.tomldiffbeforeafterboth
--- a/pallets/evm-migration/Cargo.toml
+++ b/pallets/evm-migration/Cargo.toml
@@ -4,7 +4,9 @@
 edition = "2018"
 
 [dependencies]
-scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
+scale-info = { version = "1.0.0", default-features = false, features = [
+    "derive",
+] }
 frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 frame-system = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
@@ -12,8 +14,8 @@
 sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-io = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 
 [dependencies.codec]
 default-features = false
modifiedpallets/evm-transaction-payment/Cargo.tomldiffbeforeafterboth
--- a/pallets/evm-transaction-payment/Cargo.toml
+++ b/pallets/evm-transaction-payment/Cargo.toml
@@ -4,16 +4,18 @@
 edition = "2018"
 
 [dependencies]
-scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
+scale-info = { version = "1.0.0", default-features = false, features = [
+    "derive",
+] }
 frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 frame-system = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-io = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
 sp-core = { default-features = false, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.12' }
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 up-sponsorship = { default-features = false, path = '../../primitives/sponsorship' }
 
 [dependencies.codec]
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -1,15 +1,18 @@
 use core::char::{REPLACEMENT_CHARACTER, decode_utf16};
 use core::convert::TryInto;
-use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*};
+use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};
 use nft_data_structs::CollectionMode;
 use pallet_common::erc::CommonEvmHandler;
 use sp_core::{H160, U256};
 use sp_std::vec::Vec;
 use pallet_common::account::CrossAccountId;
 use pallet_common::erc::PrecompileOutput;
-use pallet_evm_coder_substrate::{call_internal, dispatch_to_evm};
+use pallet_evm_coder_substrate::{call, dispatch_to_evm};
 
-use crate::{Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply};
+use crate::{
+	Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,
+	weights::WeightInfo,
+};
 
 #[derive(ToLog)]
 pub enum ERC20Events {
@@ -40,6 +43,7 @@
 		Ok(string::from_utf8_lossy(&self.token_prefix).into())
 	}
 	fn total_supply(&self) -> Result<uint256> {
+		self.consume_store_reads(1)?;
 		Ok(<TotalSupply<T>>::get(self.id).into())
 	}
 
@@ -51,10 +55,12 @@
 		})
 	}
 	fn balance_of(&self, owner: address) -> Result<uint256> {
+		self.consume_store_reads(1)?;
 		let owner = T::CrossAccountId::from_eth(owner);
 		let balance = <Balance<T>>::get((self.id, owner));
 		Ok(balance.into())
 	}
+	#[weight(<SelfWeightOf<T>>::transfer())]
 	fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
@@ -63,6 +69,7 @@
 		<Pallet<T>>::transfer(self, &caller, &to, amount).map_err(|_| "transfer error")?;
 		Ok(true)
 	}
+	#[weight(<SelfWeightOf<T>>::transfer_from())]
 	fn transfer_from(
 		&mut self,
 		caller: caller,
@@ -79,6 +86,7 @@
 			.map_err(dispatch_to_evm::<T>)?;
 		Ok(true)
 	}
+	#[weight(<SelfWeightOf<T>>::approve())]
 	fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let spender = T::CrossAccountId::from_eth(spender);
@@ -89,6 +97,7 @@
 		Ok(true)
 	}
 	fn allowance(&self, owner: address, spender: address) -> Result<uint256> {
+		self.consume_store_reads(1)?;
 		let owner = T::CrossAccountId::from_eth(owner);
 		let spender = T::CrossAccountId::from_eth(spender);
 
@@ -98,6 +107,7 @@
 
 #[solidity_interface(name = "ERC20UniqueExtensions")]
 impl<T: Config> FungibleHandle<T> {
+	#[weight(<SelfWeightOf<T>>::burn_from())]
 	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let from = T::CrossAccountId::from_eth(from);
@@ -111,14 +121,13 @@
 #[solidity_interface(name = "UniqueFungible", is(ERC20))]
 impl<T: Config> FungibleHandle<T> {}
 
-generate_stubgen!(gen_impl, UniqueFungibleCall, true);
-generate_stubgen!(gen_iface, UniqueFungibleCall, false);
+generate_stubgen!(gen_impl, UniqueFungibleCall<()>, true);
+generate_stubgen!(gen_iface, UniqueFungibleCall<()>, false);
 
 impl<T: Config> CommonEvmHandler for FungibleHandle<T> {
 	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");
 
-	fn call(mut self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {
-		let result = call_internal::<UniqueFungibleCall, _>(*source, &mut self, value, input);
-		self.0.recorder.evm_to_precompile_output(result)
+	fn call(self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {
+		call::<T, UniqueFungibleCall<T>, _>(*source, self, value, input)
 	}
 }
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -6,6 +6,7 @@
 use pallet_common::{
 	Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,
 };
+use pallet_evm_coder_substrate::WithRecorder;
 use sp_core::H160;
 use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
 use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};
@@ -82,6 +83,14 @@
 		self.0
 	}
 }
+impl<T: Config> WithRecorder<T> for FungibleHandle<T> {
+	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {
+		self.0.recorder()
+	}
+	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {
+		self.0.into_recorder()
+	}
+}
 impl<T: Config> Deref for FungibleHandle<T> {
 	type Target = pallet_common::CollectionHandle<T>;
 
@@ -136,7 +145,7 @@
 		}
 		<TotalSupply<T>>::insert(collection.id, total_supply);
 
-		collection.log_infallible(ERC20Events::Transfer {
+		collection.log(ERC20Events::Transfer {
 			from: *owner.as_eth(),
 			to: H160::default(),
 			value: amount.into(),
@@ -179,11 +188,6 @@
 		} else {
 			None
 		};
-
-		collection.consume_sstore()?;
-		collection.consume_sstore()?;
-		collection.consume_log(2, 32)?;
-		collection.consume_sstore()?;
 
 		// =========
 
@@ -197,7 +201,7 @@
 			<Balance<T>>::insert((collection.id, to), balance_to);
 		}
 
-		collection.log_infallible(ERC20Events::Transfer {
+		collection.log(ERC20Events::Transfer {
 			from: *from.as_eth(),
 			to: *to.as_eth(),
 			value: amount.into(),
@@ -217,8 +221,7 @@
 		sender: &T::CrossAccountId,
 		data: Vec<CreateItemData<T>>,
 	) -> DispatchResult {
-		let unrestricted_minting = collection.is_owner_or_admin(sender)?;
-		if !unrestricted_minting {
+		if !collection.is_owner_or_admin(sender) {
 			ensure!(
 				collection.mint_mode,
 				<CommonError<T>>::PublicMintingNotAllowed
@@ -241,20 +244,12 @@
 			.ok_or(ArithmeticError::Overflow)?;
 
 		for (user, amount) in data.into_iter() {
-			collection.consume_sload()?;
 			let balance = balances
 				.entry(user.clone())
 				.or_insert_with(|| <Balance<T>>::get((collection.id, user)));
 			*balance = (*balance)
 				.checked_add(amount)
 				.ok_or(ArithmeticError::Overflow)?;
-		}
-
-		collection.consume_sstore()?;
-		for _ in &balances {
-			collection.consume_sstore()?;
-			collection.consume_log(2, 32)?;
-			collection.consume_sstore()?;
 		}
 
 		// =========
@@ -263,7 +258,7 @@
 		for (user, amount) in balances {
 			<Balance<T>>::insert((collection.id, &user), amount);
 
-			collection.log_infallible(ERC20Events::Transfer {
+			collection.log(ERC20Events::Transfer {
 				from: H160::default(),
 				to: *user.as_eth(),
 				value: amount.into(),
@@ -287,7 +282,7 @@
 	) {
 		<Allowance<T>>::insert((collection.id, owner, spender), amount);
 
-		collection.log_infallible(ERC20Events::Approval {
+		collection.log(ERC20Events::Approval {
 			owner: *owner.as_eth(),
 			spender: *spender.as_eth(),
 			value: amount.into(),
@@ -314,7 +309,7 @@
 
 		if <Balance<T>>::get((collection.id, owner)) < amount {
 			ensure!(
-				collection.ignores_owned_amount(owner)?,
+				collection.ignores_owned_amount(owner),
 				<CommonError<T>>::CantApproveMoreThanOwned
 			);
 		}
@@ -343,7 +338,7 @@
 		let allowance = <Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount);
 		if allowance.is_none() {
 			ensure!(
-				collection.ignores_allowance(spender)?,
+				collection.ignores_allowance(spender),
 				<CommonError<T>>::TokenValueNotEnough
 			);
 		}
@@ -374,7 +369,7 @@
 		let allowance = <Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount);
 		if allowance.is_none() {
 			ensure!(
-				collection.ignores_allowance(spender)?,
+				collection.ignores_allowance(spender),
 				<CommonError<T>>::TokenValueNotEnough
 			);
 		}
modifiedpallets/nft/Cargo.tomldiffbeforeafterboth
--- a/pallets/nft/Cargo.toml
+++ b/pallets/nft/Cargo.toml
@@ -146,9 +146,9 @@
     "serde_no_std",
 ] }
 
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 hex-literal = "0.3.3"
 
 pallet-common = { default-features = false, path = "../common" }
modifiedpallets/nft/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/nft/src/benchmarking.rs
+++ b/pallets/nft/src/benchmarking.rs
@@ -155,12 +155,13 @@
 
 		let cl = CollectionLimits {
 			account_token_ownership_limit: Some(0),
-			sponsored_data_size: 0,
-			token_limit: 1,
-			sponsor_transfer_timeout: 0,
-			owner_can_destroy: true,
-			owner_can_transfer: true,
+			sponsored_data_size: Some(0),
+			token_limit: Some(1),
+			sponsor_transfer_timeout: Some(0),
+			owner_can_destroy: Some(true),
+			owner_can_transfer: Some(true),
 			sponsored_data_rate_limit: None,
+			transfers_enabled: Some(true),
 		};
 	}: set_collection_limits(RawOrigin::Signed(caller.clone()), collection, cl)
 
modifiedpallets/nft/src/dispatch.rsdiffbeforeafterboth
--- a/pallets/nft/src/dispatch.rs
+++ b/pallets/nft/src/dispatch.rs
@@ -83,10 +83,6 @@
 		_ => {}
 	}
 
-	// TODO: Make submit_logs infallible, but it shouldn't fail here anyway
-	dispatched
-		.into_inner()
-		.submit_logs()
-		.expect("should succeed");
+	dispatched.into_inner().submit_logs();
 	result
 }
modifiedpallets/nft/src/eth/sponsoring.rsdiffbeforeafterboth
--- a/pallets/nft/src/eth/sponsoring.rs
+++ b/pallets/nft/src/eth/sponsoring.rs
@@ -31,7 +31,7 @@
 	let (method_id, mut reader) = AbiReader::new_call(call).map_err(|_| AnyError)?;
 	match &collection.mode {
 		crate::CollectionMode::NFT => {
-			let call: UniqueNFTCall = UniqueNFTCall::parse(method_id, &mut reader)
+			let call = <UniqueNFTCall<T>>::parse(method_id, &mut reader)
 				.map_err(|_| AnyError)?
 				.ok_or(AnyError)?;
 			match call {
@@ -63,7 +63,7 @@
 			}
 		}
 		crate::CollectionMode::Fungible(_) => {
-			let call: UniqueFungibleCall = UniqueFungibleCall::parse(method_id, &mut reader)
+			let call = <UniqueFungibleCall<T>>::parse(method_id, &mut reader)
 				.map_err(|_| AnyError)?
 				.ok_or(AnyError)?;
 			#[allow(clippy::single_match)]
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -2,18 +2,19 @@
 	char::{REPLACEMENT_CHARACTER, decode_utf16},
 	convert::TryInto,
 };
-use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*};
+use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};
 use frame_support::BoundedVec;
 use nft_data_structs::TokenId;
 use pallet_evm_coder_substrate::dispatch_to_evm;
 use sp_core::{H160, U256};
 use sp_std::{vec::Vec, vec};
 use pallet_common::{account::CrossAccountId, erc::CommonEvmHandler};
-use pallet_evm_coder_substrate::call_internal;
+use pallet_evm_coder_substrate::call;
 use pallet_common::erc::PrecompileOutput;
 
 use crate::{
 	AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,
+	SelfWeightOf, weights::WeightInfo,
 };
 
 #[derive(ToLog)]
@@ -64,6 +65,7 @@
 	/// Returns token's const_metadata
 	#[solidity(rename_selector = "tokenURI")]
 	fn token_uri(&self, token_id: uint256) -> Result<string> {
+		self.consume_store_reads(1)?;
 		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
 		Ok(string::from_utf8_lossy(
 			&<TokenData<T>>::get((self.id, token_id))
@@ -87,6 +89,7 @@
 	}
 
 	fn total_supply(&self) -> Result<uint256> {
+		self.consume_store_reads(1)?;
 		Ok(<Pallet<T>>::total_supply(self).into())
 	}
 }
@@ -94,11 +97,13 @@
 #[solidity_interface(name = "ERC721", events(ERC721Events))]
 impl<T: Config> NonfungibleHandle<T> {
 	fn balance_of(&self, owner: address) -> Result<uint256> {
+		self.consume_store_reads(1)?;
 		let owner = T::CrossAccountId::from_eth(owner);
 		let balance = <AccountBalance<T>>::get((self.id, owner));
 		Ok(balance.into())
 	}
 	fn owner_of(&self, token_id: uint256) -> Result<address> {
+		self.consume_store_reads(1)?;
 		let token: TokenId = token_id.try_into()?;
 		Ok(*<TokenData<T>>::get((self.id, token))
 			.ok_or("token not found")?
@@ -129,6 +134,7 @@
 		Err("not implemented".into())
 	}
 
+	#[weight(<SelfWeightOf<T>>::transfer_from())]
 	fn transfer_from(
 		&mut self,
 		caller: caller,
@@ -147,6 +153,7 @@
 		Ok(())
 	}
 
+	#[weight(<SelfWeightOf<T>>::approve())]
 	fn approve(
 		&mut self,
 		caller: caller,
@@ -189,6 +196,7 @@
 
 #[solidity_interface(name = "ERC721Burnable")]
 impl<T: Config> NonfungibleHandle<T> {
+	#[weight(<SelfWeightOf<T>>::burn_item())]
 	fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let token = token_id.try_into()?;
@@ -206,6 +214,7 @@
 
 	/// `token_id` should be obtained with `next_token_id` method,
 	/// unlike standard, you can't specify it manually
+	#[weight(<SelfWeightOf<T>>::create_item())]
 	fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
@@ -235,6 +244,7 @@
 	/// `token_id` should be obtained with `next_token_id` method,
 	/// unlike standard, you can't specify it manually
 	#[solidity(rename_selector = "mintWithTokenURI")]
+	#[weight(<SelfWeightOf<T>>::create_item())]
 	fn mint_with_token_uri(
 		&mut self,
 		caller: caller,
@@ -276,6 +286,7 @@
 
 #[solidity_interface(name = "ERC721UniqueExtensions")]
 impl<T: Config> NonfungibleHandle<T> {
+	#[weight(<SelfWeightOf<T>>::transfer())]
 	fn transfer(
 		&mut self,
 		caller: caller,
@@ -291,6 +302,7 @@
 		Ok(())
 	}
 
+	#[weight(<SelfWeightOf<T>>::burn_from())]
 	fn burn_from(
 		&mut self,
 		caller: caller,
@@ -307,12 +319,14 @@
 	}
 
 	fn next_token_id(&self) -> Result<uint256> {
+		self.consume_store_reads(1)?;
 		Ok(<TokensMinted<T>>::get(self.id)
 			.checked_add(1)
 			.ok_or("item id overflow")?
 			.into())
 	}
 
+	#[weight(<SelfWeightOf<T>>::set_variable_metadata(data.len() as u32))]
 	fn set_variable_metadata(
 		&mut self,
 		caller: caller,
@@ -328,6 +342,7 @@
 	}
 
 	fn get_variable_metadata(&self, token_id: uint256) -> Result<bytes> {
+		self.consume_store_reads(1)?;
 		let token: TokenId = token_id.try_into()?;
 
 		Ok(<TokenData<T>>::get((self.id, token))
@@ -335,6 +350,7 @@
 			.variable_data)
 	}
 
+	#[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]
 	fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<uint256>) -> Result<bool> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
@@ -363,6 +379,7 @@
 	}
 
 	#[solidity(rename_selector = "mintBulkWithTokenURI")]
+	#[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]
 	fn mint_bulk_with_token_uri(
 		&mut self,
 		caller: caller,
@@ -411,16 +428,13 @@
 impl<T: Config> NonfungibleHandle<T> {}
 
 // Not a tests, but code generators
-generate_stubgen!(gen_impl, UniqueNFTCall, true);
-generate_stubgen!(gen_iface, UniqueNFTCall, false);
-
-pub const CODE: &[u8] = include_bytes!("./stubs/UniqueNFT.raw");
+generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);
+generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);
 
 impl<T: Config> CommonEvmHandler for NonfungibleHandle<T> {
 	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");
 
-	fn call(mut self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {
-		let result = call_internal::<UniqueNFTCall, _>(*source, &mut self, value, input);
-		self.0.recorder.evm_to_precompile_output(result)
+	fn call(self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {
+		call::<T, UniqueNFTCall<T>, _>(*source, self, value, input)
 	}
 }
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -8,6 +8,7 @@
 use pallet_common::{
 	Error as CommonError, Pallet as PalletCommon, Event as CommonEvent, account::CrossAccountId,
 };
+use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
 use sp_core::H160;
 use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
 use sp_std::{vec::Vec, vec};
@@ -114,6 +115,14 @@
 		self.0
 	}
 }
+impl<T: Config> WithRecorder<T> for NonfungibleHandle<T> {
+	fn recorder(&self) -> &SubstrateRecorder<T> {
+		self.0.recorder()
+	}
+	fn into_recorder(self) -> SubstrateRecorder<T> {
+		self.0.into_recorder()
+	}
+}
 impl<T: Config> Deref for NonfungibleHandle<T> {
 	type Target = pallet_common::CollectionHandle<T>;
 
@@ -164,8 +173,7 @@
 			.ok_or_else(|| <CommonError<T>>::TokenNotFound)?;
 		ensure!(
 			&token_data.owner == sender
-				|| (collection.limits.owner_can_transfer()
-					&& collection.is_owner_or_admin(sender)?),
+				|| (collection.limits.owner_can_transfer() && collection.is_owner_or_admin(sender)),
 			<CommonError<T>>::NoPermission
 		);
 
@@ -194,7 +202,7 @@
 			));
 		}
 
-		collection.log_infallible(ERC721Events::Transfer {
+		collection.log(ERC721Events::Transfer {
 			from: *token_data.owner.as_eth(),
 			to: H160::default(),
 			token_id: token.into(),
@@ -223,8 +231,7 @@
 			.ok_or_else(|| <CommonError<T>>::TokenNotFound)?;
 		ensure!(
 			&token_data.owner == from
-				|| (collection.limits.owner_can_transfer()
-					&& collection.is_owner_or_admin(from)?),
+				|| (collection.limits.owner_can_transfer() && collection.is_owner_or_admin(from)),
 			<CommonError<T>>::NoPermission
 		);
 
@@ -251,9 +258,6 @@
 		} else {
 			None
 		};
-
-		collection.consume_sstores(4)?;
-		collection.consume_log(3, 0)?;
 
 		// =========
 
@@ -278,7 +282,7 @@
 		}
 		Self::set_allowance_unchecked(collection, from, token, None, true);
 
-		collection.log_infallible(ERC721Events::Transfer {
+		collection.log(ERC721Events::Transfer {
 			from: *from.as_eth(),
 			to: *to.as_eth(),
 			token_id: token.into(),
@@ -298,8 +302,7 @@
 		sender: &T::CrossAccountId,
 		data: Vec<CreateItemData<T>>,
 	) -> DispatchResult {
-		let unrestricted_minting = collection.is_owner_or_admin(sender)?;
-		if !unrestricted_minting {
+		if !collection.is_owner_or_admin(sender) {
 			ensure!(
 				collection.mint_mode,
 				<CommonError<T>>::PublicMintingNotAllowed
@@ -313,14 +316,6 @@
 
 		for data in data.iter() {
 			<PalletCommon<T>>::ensure_correct_receiver(&data.owner)?;
-			if !data.const_data.is_empty() {
-				collection.consume_sstore()?;
-			}
-			if !data.variable_data.is_empty() {
-				collection.consume_sstore()?;
-			}
-			collection.consume_sstore()?;
-			collection.consume_log(3, 0)?;
 		}
 
 		let first_token = <TokensMinted<T>>::get(collection.id);
@@ -331,7 +326,6 @@
 			tokens_minted < collection.limits.token_limit(),
 			<CommonError<T>>::CollectionTokenLimitExceeded
 		);
-		collection.consume_sstore()?;
 
 		let mut balances = BTreeMap::new();
 		for data in &data {
@@ -345,7 +339,6 @@
 				<CommonError<T>>::AccountTokenLimitExceeded,
 			);
 		}
-		collection.consume_sstores(balances.len())?;
 
 		// =========
 
@@ -366,7 +359,7 @@
 			);
 			<Owned<T>>::insert((collection.id, &data.owner, token), true);
 
-			collection.log_infallible(ERC721Events::Transfer {
+			collection.log(ERC721Events::Transfer {
 				from: H160::default(),
 				to: *data.owner.as_eth(),
 				token_id: token.into(),
@@ -393,7 +386,7 @@
 			<Allowance<T>>::insert((collection.id, token), spender);
 			// In ERC721 there is only one possible approved user of token, so we set
 			// approved user to spender
-			collection.log_infallible(ERC721Events::Approval {
+			collection.log(ERC721Events::Approval {
 				owner: *sender.as_eth(),
 				approved: *spender.as_eth(),
 				token_id: token.into(),
@@ -423,7 +416,7 @@
 			if !assume_implicit_eth {
 				// In ERC721 there is only one possible approved user of token, so we set
 				// approved user to zero address
-				collection.log_infallible(ERC721Events::Approval {
+				collection.log(ERC721Events::Approval {
 					owner: *sender.as_eth(),
 					approved: H160::default(),
 					token_id: token.into(),
@@ -463,7 +456,7 @@
 			<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
 		if &token_data.owner != sender {
 			ensure!(
-				collection.ignores_owned_amount(sender)?,
+				collection.ignores_owned_amount(sender),
 				<CommonError<T>>::CantApproveMoreThanOwned
 			);
 		}
@@ -491,7 +484,7 @@
 
 		if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {
 			ensure!(
-				collection.ignores_allowance(spender)?,
+				collection.ignores_allowance(spender),
 				<CommonError<T>>::TokenValueNotEnough
 			);
 		}
@@ -519,7 +512,7 @@
 
 		if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {
 			ensure!(
-				collection.ignores_allowance(spender)?,
+				collection.ignores_allowance(spender),
 				<CommonError<T>>::TokenValueNotEnough
 			);
 		}
@@ -542,8 +535,6 @@
 		let token_data =
 			<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
 		collection.check_can_update_meta(sender, &token_data.owner)?;
-
-		collection.consume_sstore()?;
 
 		// =========
 
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -58,7 +58,7 @@
 	}
 
 	fn burn_from() -> Weight {
-		0
+		<SelfWeightOf<T>>::burn_from()
 	}
 
 	fn set_variable_metadata(bytes: u32) -> Weight {
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -359,8 +359,7 @@
 		sender: &T::CrossAccountId,
 		data: Vec<CreateItemData<T>>,
 	) -> DispatchResult {
-		let unrestricted_minting = collection.is_owner_or_admin(sender)?;
-		if !unrestricted_minting {
+		if !collection.is_owner_or_admin(sender) {
 			ensure!(
 				collection.mint_mode,
 				<CommonError<T>>::PublicMintingNotAllowed
@@ -492,7 +491,7 @@
 
 		if <Balance<T>>::get((collection.id, token, sender)) < amount {
 			ensure!(
-				collection.ignores_owned_amount(sender)? && Self::token_exists(collection, token),
+				collection.ignores_owned_amount(sender) && Self::token_exists(collection, token),
 				<CommonError<T>>::CantApproveMoreThanOwned
 			);
 		}
@@ -523,7 +522,7 @@
 			<Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);
 		if allowance.is_none() {
 			ensure!(
-				collection.ignores_allowance(spender)?,
+				collection.ignores_allowance(spender),
 				<CommonError<T>>::TokenValueNotEnough
 			);
 		}
@@ -556,7 +555,7 @@
 			<Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);
 		if allowance.is_none() {
 			ensure!(
-				collection.ignores_allowance(spender)?,
+				collection.ignores_allowance(spender),
 				<CommonError<T>>::TokenValueNotEnough
 			);
 		}
@@ -585,7 +584,6 @@
 			&T::CrossAccountId::from_sub(collection.owner.clone()),
 		)?;
 
-		collection.consume_sstore()?;
 		let token_data = <TokenData<T>>::get((collection.id, token));
 
 		// =========
modifiedruntime/Cargo.tomldiffbeforeafterboth
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -387,10 +387,10 @@
 pallet-evm-transaction-payment = { path = '../pallets/evm-transaction-payment', default-features = false }
 pallet-evm-coder-substrate = { default-features = false, path = "../pallets/evm-coder-substrate" }
 
-pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-rpc = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
-fp-self-contained = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12" }
+pallet-evm = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+pallet-ethereum = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-rpc = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
+fp-self-contained = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.12-weights" }
 
 ################################################################################
 # Build Dependencies
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -66,7 +66,7 @@
 };
 use smallvec::smallvec;
 use codec::{Encode, Decode};
-use pallet_evm::{Account as EVMAccount, FeeCalculator, OnMethodCall};
+use pallet_evm::{Account as EVMAccount, FeeCalculator, GasWeightMapping, OnMethodCall};
 use fp_rpc::TransactionStatus;
 use sp_core::crypto::Public;
 use sp_runtime::{
@@ -247,10 +247,37 @@
 	}
 }
 
+// Assuming slowest ethereum opcode is SSTORE, with gas price of 20000 as our worst case
+// (contract, which only writes a lot of data),
+// approximating on top of our real store write weight
+parameter_types! {
+	pub const WritesPerSecond: u64 = WEIGHT_PER_SECOND / <Runtime as frame_system::Config>::DbWeight::get().write;
+	pub const GasPerSecond: u64 = WritesPerSecond::get() * 20000;
+	pub const WeightPerGas: u64 = WEIGHT_PER_SECOND / GasPerSecond::get();
+}
+
+/// Limiting EVM execution to 50% of block for substrate users and management tasks
+/// EVM transaction consumes more weight than substrate's, so we can't rely on them being
+/// scheduled fairly
+const EVM_DISPATCH_RATIO: Perbill = Perbill::from_percent(50);
+parameter_types! {
+	pub BlockGasLimit: U256 = U256::from(NORMAL_DISPATCH_RATIO * EVM_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT / WeightPerGas::get());
+}
+
+pub enum FixedGasWeightMapping {}
+impl GasWeightMapping for FixedGasWeightMapping {
+	fn gas_to_weight(gas: u64) -> Weight {
+		gas.saturating_mul(WeightPerGas::get())
+	}
+	fn weight_to_gas(weight: Weight) -> u64 {
+		weight / WeightPerGas::get()
+	}
+}
+
 impl pallet_evm::Config for Runtime {
 	type BlockGasLimit = BlockGasLimit;
 	type FeeCalculator = FixedFee;
-	type GasWeightMapping = ();
+	type GasWeightMapping = FixedGasWeightMapping;
 	type BlockHashMapping = pallet_ethereum::EthereumBlockHashMapping<Self>;
 	type CallOrigin = EnsureAddressTruncated;
 	type WithdrawOrigin = EnsureAddressTruncated;
@@ -289,14 +316,9 @@
 	}
 }
 
-parameter_types! {
-	pub BlockGasLimit: U256 = U256::from(u32::max_value());
-}
-
 impl pallet_ethereum::Config for Runtime {
 	type Event = Event;
 	type StateRoot = pallet_ethereum::IntermediateStateRoot;
-	type EvmSubmitLog = pallet_evm::Pallet<Self>;
 }
 
 impl pallet_randomness_collective_flip::Config for Runtime {}
@@ -671,6 +693,7 @@
 
 impl pallet_evm_coder_substrate::Config for Runtime {
 	type EthereumTransactionSender = pallet_ethereum::Pallet<Self>;
+	type GasWeightMapping = FixedGasWeightMapping;
 }
 
 impl pallet_xcm::Config for Runtime {