8 releases

0.2.0 Oct 29, 2023
0.1.2 Jun 23, 2023
0.0.4 Jun 19, 2023
0.0.3 Nov 20, 2022
0.0.1 Mar 26, 2022

#24 in Magic Beans

Download history 3/week @ 2024-01-11 14/week @ 2024-01-18 2/week @ 2024-01-25 4/week @ 2024-02-08 10/week @ 2024-02-15 15/week @ 2024-02-22 25/week @ 2024-02-29 17/week @ 2024-03-07 305/week @ 2024-03-14 33/week @ 2024-03-21 17/week @ 2024-03-28

373 downloads per month

MIT/Apache

240KB
6K SLoC

solabi

crates.io docs.rs

Solidity ABI implementation in Rust.

Introduction

This crate aims to provide a comprehensive toolkit for working with the Solidity ABI in Rust, including encoding and decoding values as well as parsing JSON ABI generated by the Solidity complier.

Usage

Just add solabi as a dependency!

solabi = "*"

solabi depends on the ethprim crate which gets re-exported for convenience. This crate notably includes macros for compile-time computed literals (for things like 256-bit integers and checksummed addresses) as well as compile-time computed Keccak-256 digests:

use solabi::*;

const DIGEST: Digest = keccak!("hello world!");
const PRIME: I256 = int!("57896044618658097711785492504343953926634992332820282019728792003956564819949");
const ADDRESS: Address = address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

Future Work

While the core components of the Solidity ABI implementation are mostly complete, there are a couple of features that expected to be implemented before v1 of this crate, namely:

  • Procedural macros for Solidity ABI item generation. This includes generating types for encoding and decoding Solidity contract items. While the exact API is not yet determined, it is expected to look something like:
    #[derive(solabi::Function)]
    #[solabi(signature = "function transferFrom(address, address, uint256) returns (bool)")]
    struct TransferFrom;
    
    #[derive(solabi::Event)]
    struct Transfer {
        #[solabi(indexed)]
        pub from: Address,
        #[solabi(indexed)]
        pub to: Address,
        pub value: U256,
    }
    
    fn main() {
        TransferFrom.encode_params(...);
        TransferFrom.decode_result(...);
    
        Transfer { ... }.encode();
        Transfer::decode(Log {
            topics: ...,
            data: ...,
        })
    }
    
  • Derive macro for Encode and Decode.
    #[derive(solabi::Decode, solabi::Encode)]
    struct MyStruct {
        name: String,
        age: uint256,
        occupation: Occupation,
    }
    
    #[derive(solabi::Decode, solabi::Encode)]
    enum Occupation {
        Crypto,
        Other,
    }
    
  • Solidity packed encoding.

Dependencies

~0.8–1.6MB
~33K SLoC