From 3f55e640712f415ae7ef30ac0326611511a09ddb Mon Sep 17 00:00:00 2001 From: clearloop Date: Tue, 27 Feb 2024 16:08:04 +0800 Subject: [PATCH] feat(abi): support all primitive types --- evm/abi/README.md | 47 ++++++++++++++++++++++++++++++++++++++++------ evm/abi/src/arg.rs | 21 ++++++++++++++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/evm/abi/README.md b/evm/abi/README.md index 15784e074..2f614e530 100644 --- a/evm/abi/README.md +++ b/evm/abi/README.md @@ -1,14 +1,49 @@ -# Solidity ABI +# Solidity ABIA -An straightforward solidity ABI implementation in rust. +An straightforward solidity ABI implementation for zink. -Currently only used by the zink language, so the provided features -is syncing with the development of zink. +This library only contains a part of the solidity ABI implementation, +if you are looking for a complete implementation of solidity ABI +in rust, see [alloy-core][alloy-core]. -If you are looking for a complete implementation of solidity ABI -in rust, plz check [alloy-core](https://github.com/alloy-rs/core). +## Static Types + +Only rust primitive types are supported in this static type port, + +| rust | solidity | +|--------|-----------| +| `i8` | `int8` | +| `u8` | `uint8` | +| `i16` | `int16` | +| `u16` | `uint16` | +| `i32` | `int32` | +| `u32` | `uint32` | +| `i64` | `int64` | +| `u64` | `uint64` | +| `i128` | `int128` | +| `u128` | `uint128` | +| `bool` | `bool` | + + +## Dynamic Types + +The implementation of dynamic arguments follows [use-of-dynamic-types][dyn-types], +same as the static types, only ports the rust types: + +| rust | solidity | +|------------|-----------| +| `Vec` | `bytes` | +| `[u8; 20]` | `address` | +| `String` | `string` | + +More complex types are currently not supported. ## LICENSE GPL-3.0 + + +[alloy-core]: https://github.com/alloy-rs/core +[dyn-types]: https://docs.soliditylang.org/en/latest/abi-spec.html#use-of-dynamic-types +[zink]: https://github.com/zink-lang/zink diff --git a/evm/abi/src/arg.rs b/evm/abi/src/arg.rs index e6ff2a582..740ab2711 100644 --- a/evm/abi/src/arg.rs +++ b/evm/abi/src/arg.rs @@ -21,18 +21,26 @@ pub struct Arg { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] pub enum Param { + /// A 8-bit integer. + Int8, + /// A 16-bit integer. + Int16, /// A 32-bit integer. Int32, /// A 64-bit integer. Int64, + /// A 8-bit unsigned integer. + UInt8, + /// A 16-bit unsigned integer. + UInt16, /// A 32-bit unsigned integer. UInt32, /// A 64-bit unsigned integer. UInt64, - /// An EVM address. - Address, /// A boolean type. Bool, + /// An EVM address. + Address, /// A byte array. #[default] Bytes, @@ -45,10 +53,13 @@ pub enum Param { impl From<&str> for Param { fn from(s: &str) -> Self { match s { + "i8" | "int8" => Param::Int8, + "u8" | "uint8" => Param::UInt8, "i32" | "int32" => Param::Int32, "i64" | "int64" => Param::Int64, + "u16" | "uint16" => Param::UInt16, "u32" | "uint32" => Param::UInt32, - "usize" | "u64" | "uint64" => Param::UInt64, + "u64" | "uint64" => Param::UInt64, "Address" => Param::Address, "Bytes" | "Vec" => Param::Bytes, "String" => Param::String, @@ -68,8 +79,12 @@ impl FromStr for Param { impl AsRef for Param { fn as_ref(&self) -> &str { match self { + Param::Int8 => "int8", + Param::Int16 => "int16", Param::Int32 => "int32", Param::Int64 => "int64", + Param::UInt8 => "uint8", + Param::UInt16 => "uint16", Param::UInt32 => "uint32", Param::UInt64 => "uint64", Param::Address => "address",