Skip to content

Commit

Permalink
feat(abi): support all primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Feb 27, 2024
1 parent 677c35a commit 3f55e64
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
47 changes: 41 additions & 6 deletions evm/abi/README.md
Original file line number Diff line number Diff line change
@@ -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<u8>` | `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
21 changes: 18 additions & 3 deletions evm/abi/src/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<u8>" => Param::Bytes,
"String" => Param::String,
Expand All @@ -68,8 +79,12 @@ impl FromStr for Param {
impl AsRef<str> 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",
Expand Down

0 comments on commit 3f55e64

Please sign in to comment.