difftreelog
refactor move evm-coder impl to its own crate
in: master
6 files changed
crates/evm-coder/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/crates/evm-coder/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+name = "evm-coder"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+evm-coder-macros = { path = "../evm-coder-macros" }
+primitive-types = { version = "0.9", default-features = false }
+hex-literal = "0.3"
+ethereum = { version = "0.7.1", default-features = false }
+
+[dev-dependencies]
+hex = "0.4.3"
+
+[features]
+default = ["std"]
+std = ["ethereum/std", "primitive-types/std"]
\ No newline at end of file
crates/evm-coder/src/abi.rsdiffbeforeafterbothno changes
crates/evm-coder/src/events.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/evm-coder/src/events.rs
@@ -0,0 +1,42 @@
+use ethereum::Log;
+use primitive_types::{H160, H256};
+
+use crate::types::*;
+
+pub trait ToLog {
+ fn to_log(&self, contract: H160) -> Log;
+}
+
+pub trait ToTopic {
+ fn to_topic(&self) -> H256;
+}
+
+impl ToTopic for H256 {
+ fn to_topic(&self) -> H256 {
+ *self
+ }
+}
+
+impl ToTopic for uint256 {
+ fn to_topic(&self) -> H256 {
+ let mut out = [0u8; 32];
+ self.to_big_endian(&mut out);
+ H256(out)
+ }
+}
+
+impl ToTopic for address {
+ fn to_topic(&self) -> H256 {
+ let mut out = [0u8; 32];
+ out[12..32].copy_from_slice(&self.0);
+ H256(out)
+ }
+}
+
+impl ToTopic for uint32 {
+ fn to_topic(&self) -> H256 {
+ let mut out = [0u8; 32];
+ out[28..32].copy_from_slice(&self.to_be_bytes());
+ H256(out)
+ }
+}
crates/evm-coder/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/evm-coder/src/lib.rs
@@ -0,0 +1,69 @@
+#![cfg_attr(not(feature = "std"), no_std)]
+#[cfg(not(feature = "std"))]
+extern crate alloc;
+
+pub use evm_coder_macros::{event_topic, fn_selector, solidity_interface, solidity, ToLog};
+pub mod abi;
+pub mod events;
+pub use events::ToLog;
+
+/// Solidity type definitions
+pub mod types {
+ #![allow(non_camel_case_types)]
+
+ #[cfg(not(feature = "std"))]
+ use alloc::{vec::Vec};
+ use primitive_types::{U256, H160, H256};
+
+ pub type address = H160;
+
+ pub type uint8 = u8;
+ pub type uint16 = u16;
+ pub type uint32 = u32;
+ pub type uint64 = u64;
+ pub type uint128 = u128;
+ pub type uint256 = U256;
+
+ pub type bytes4 = u32;
+
+ pub type topic = H256;
+
+ #[cfg(not(feature = "std"))]
+ pub type string = ::alloc::string::String;
+ #[cfg(feature = "std")]
+ pub type string = ::std::string::String;
+ pub type bytes = Vec<u8>;
+
+ pub type void = ();
+
+ //#region Special types
+ /// Makes function payable
+ pub type value = U256;
+ /// Makes function caller-sensitive
+ pub type caller = address;
+ //#endregion
+
+ pub struct Msg<C> {
+ pub call: C,
+ pub caller: H160,
+ pub value: U256,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn function_selector_generation() {
+ assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);
+ }
+
+ #[test]
+ fn event_topic_generation() {
+ assert_eq!(
+ hex::encode(&event_topic!(Transfer(address, address, uint256))[..]),
+ "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
+ );
+ }
+}
crates/evm-coder/tests/a.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/evm-coder/tests/a.rs
@@ -0,0 +1,69 @@
+use evm_coder::{solidity_interface, types::*, ToLog};
+use evm_coder_macros::solidity;
+
+#[solidity_interface]
+trait OurInterface {
+ type Error;
+ fn fn_a(&self, input: uint256) -> Result<bool, Self::Error>;
+}
+
+#[solidity_interface]
+trait OurInterface1 {
+ type Error;
+ fn fn_b(&self, input: uint128) -> Result<uint32, Self::Error>;
+}
+
+#[solidity_interface(is(OurInterface), inline_is(OurInterface1), events(ERC721Log))]
+trait OurInterface2 {
+ type Error;
+ #[solidity(rename_selector = "fnK")]
+ fn fn_c(&self, input: uint32) -> Result<uint8, Self::Error>;
+ fn fn_d(&self, value: uint32) -> Result<uint32, Self::Error>;
+
+ fn caller_sensitive(&self, caller: caller) -> Result<uint8, Self::Error>;
+ fn payable(&mut self, value: value) -> Result<uint8, Self::Error>;
+}
+
+#[derive(ToLog)]
+enum ERC721Log {
+ Transfer {
+ #[indexed]
+ from: address,
+ #[indexed]
+ to: address,
+ value: uint256,
+ },
+ Eee {
+ #[indexed]
+ aaa: address,
+ bbb: uint256,
+ },
+}
+
+#[solidity_interface]
+trait ERC20 {
+ type Error;
+
+ fn decimals(&self) -> Result<uint8, Self::Error>;
+ fn balance_of(&self, owner: address) -> Result<uint256, Self::Error>;
+ fn transfer(
+ &mut self,
+ caller: caller,
+ to: address,
+ value: uint256,
+ ) -> Result<bool, Self::Error>;
+ fn transfer_from(
+ &mut self,
+ caller: caller,
+ from: address,
+ to: address,
+ value: uint256,
+ ) -> Result<bool, Self::Error>;
+ fn approve(
+ &mut self,
+ caller: caller,
+ spender: address,
+ value: uint256,
+ ) -> Result<bool, Self::Error>;
+ fn allowance(&self, owner: address, spender: address) -> Result<uint256, Self::Error>;
+}
pallets/nft/src/eth/abi.rsdiffbeforeafterboth--- a/pallets/nft/src/eth/abi.rs
+++ /dev/null
@@ -1,204 +0,0 @@
-//! TODO: I misunterstood therminology, abi IS rlp encoded, so
-//! this module should be replaced with rlp crate
-
-use sp_core::H160;
-
-#[cfg(feature = "std")]
-use std;
-#[cfg(not(feature = "std"))]
-use sp_std as std;
-
-use std::vec::Vec;
-
-const ABI_ALIGNMENT: usize = 32;
-
-type Result<T> = std::result::Result<T, &'static str>;
-
-pub struct AbiReader<'i> {
- buf: &'i [u8],
- offset: usize,
-}
-impl<'i> AbiReader<'i> {
- pub fn new_call(buf: &'i [u8]) -> Result<(u32, Self)> {
- if buf.len() < 4 {
- return Err("missing method id")
- }
- let mut method_id = [0; 4];
- method_id.copy_from_slice(&buf[0..4]);
-
- Ok((u32::from_be_bytes(method_id), Self {
- buf,
- offset: 4,
- }))
- }
-
- fn read_padleft<const S: usize>(&mut self) -> Result<[u8; S]> {
- if self.buf.len() - self.offset < 32 {
- return Err("missing padding")
- }
- let mut block = [0; S];
- // Verify padding is empty
- if !self.buf[self.offset..self.offset + ABI_ALIGNMENT - S].iter().all(|&v| v == 0) {
- return Err("non zero padding (wrong types?)")
- }
- block.copy_from_slice(&self.buf[self.offset + ABI_ALIGNMENT - S..self.offset + ABI_ALIGNMENT]);
- self.offset += ABI_ALIGNMENT;
- Ok(block)
- }
-
- pub fn address(&mut self) -> Result<H160> {
- Ok(H160(self.read_padleft()?))
- }
-
- pub fn uint32(&mut self) -> Result<u32> {
- Ok(u32::from_be_bytes(self.read_padleft()?))
- }
-
- pub fn uint128(&mut self) -> Result<u128> {
- Ok(u128::from_be_bytes(self.read_padleft()?))
- }
-
- pub fn uint256(&mut self) -> Result<u128> {
- self.uint128()
- }
-
- pub fn uint64(&mut self) -> Result<usize> {
- Ok(usize::from_be_bytes(self.read_padleft()?))
- }
-
- fn subresult(&mut self) -> Result<AbiReader<'i>> {
- let offset = self.uint64()?;
- if offset > usize::MAX {
- return Err("subresult overflow");
- }
- Ok(AbiReader {
- buf: &self.buf,
- offset: offset + self.offset,
- })
- }
-
- pub fn is_finished(&self) -> bool {
- self.buf.len() == self.offset
- }
-}
-
-pub struct AbiWriter {
- static_part: Vec<u8>,
- dynamic_part: Vec<(usize, AbiWriter)>,
-}
-impl AbiWriter {
- pub fn new() -> Self {
- Self {
- static_part: Vec::new(),
- dynamic_part: Vec::new(),
- }
- }
- pub fn new_call(method_id: u32) -> Self {
- let mut val = Self::new();
- val.static_part.extend(&method_id.to_be_bytes());
- val
- }
-
- fn write_padleft(&mut self, block: &[u8]) {
- assert!(block.len() <= ABI_ALIGNMENT);
- self.static_part.extend(&[0; ABI_ALIGNMENT][0..ABI_ALIGNMENT - block.len()]);
- self.static_part.extend(block);
- }
-
- fn write_padright(&mut self, bytes: &[u8]) {
- assert!(bytes.len() <= ABI_ALIGNMENT);
- self.static_part.extend(bytes);
- self.static_part.extend(&[0; ABI_ALIGNMENT][0..ABI_ALIGNMENT - bytes.len()]);
- }
-
- pub fn address(&mut self, address: H160) {
- self.write_padleft(&address.0)
- }
-
- pub fn bool(&mut self, value: bool) {
- self.write_padleft(&[if value { 1 } else { 0 }])
- }
-
- pub fn uint8(&mut self, value: u8) {
- self.write_padleft(&[value])
- }
-
- pub fn uint128(&mut self, value: u128) {
- self.write_padleft(&u128::to_be_bytes(value))
- }
-
- /// This method writes u128, and exists only for convenience, because there is
- /// no u256 support in rust
- pub fn uint256(&mut self, value: u128) {
- self.uint128(value)
- }
-
- pub fn write_usize(&mut self, value: usize) {
- self.write_padleft(&usize::to_be_bytes(value))
- }
-
- pub fn write_u8(&mut self, value: u8) {
- self.write_padleft(&[value])
- }
-
- pub fn write_subresult(&mut self, result: Self) {
- self.dynamic_part.push((self.static_part.len(), result));
- // Empty block, to be filled later
- self.write_padleft(&[]);
- }
-
- pub fn memory(&mut self, value: &[u8]) {
- let mut sub = Self::new();
- sub.write_usize(value.len());
- for chunk in value.chunks(ABI_ALIGNMENT) {
- sub.write_padright(chunk);
- }
- self.write_subresult(sub);
- }
-
- pub fn string(&mut self, value: &str) {
- self.memory(value.as_bytes())
- }
-
- pub fn finish(mut self) -> Vec<u8> {
- for (static_offset, part) in self.dynamic_part {
- let part_offset = self.static_part.len();
-
- let encoded_dynamic_offset = usize::to_be_bytes(part_offset - static_offset);
- self.static_part
- [static_offset + ABI_ALIGNMENT - encoded_dynamic_offset.len()..static_offset + ABI_ALIGNMENT]
- .copy_from_slice(&encoded_dynamic_offset);
- self.static_part.extend(part.finish())
- }
- self.static_part
- }
-}
-
-#[macro_export]
-macro_rules! abi_decode {
- ($reader:expr, $($name:ident: $typ:ident),+ $(,)?) => {
- $(
- let $name = $reader.$typ()?;
- )+
- }
-}
-
-#[macro_export]
-macro_rules! abi_encode {
- ($($typ:ident($value:expr)),* $(,)?) => {{
- #[allow(unused_mut)]
- let mut writer = crate::eth::abi::AbiWriter::new();
- $(
- writer.$typ($value);
- )*
- writer
- }};
- (call $val:expr; $($typ:ident($value:expr)),* $(,)?) => {{
- #[allow(unused_mut)]
- let mut writer = AbiWriter::new_call($val);
- $(
- writer.$typ($value);
- )*
- writer
- }}
-}
\ No newline at end of file