#abi #ethereum

ethereum_abi

Ethereum Smart Contract ABI parsing library

5 releases (3 breaking)

0.4.0 Mar 11, 2022
0.3.1 Mar 11, 2022
0.3.0 Jan 19, 2021
0.2.0 Dec 21, 2020
0.1.0 Dec 19, 2020

#116 in #abi

Download history 49/week @ 2024-07-19 103/week @ 2024-07-26 71/week @ 2024-08-02 91/week @ 2024-08-09 83/week @ 2024-08-16 97/week @ 2024-08-23 205/week @ 2024-08-30 97/week @ 2024-09-06 103/week @ 2024-09-13 139/week @ 2024-09-20 141/week @ 2024-09-27 177/week @ 2024-10-04 83/week @ 2024-10-11 39/week @ 2024-10-18 98/week @ 2024-10-25 59/week @ 2024-11-01

303 downloads per month
Used in 2 crates

MIT license

82KB
2K SLoC

Ethereum ABI

Crates.io Docs.rs codecov

ethereum_abi is a Rust library to help writing code that interacts with Ethereum Smart Contracts.

Examples

Decoding function inputs

use std::fs::File;
use std::io;

use ethereum_abi::Abi;

fn main() {
    // Parse ABI JSON file
    let abi = {
        let file = File::open("some_abi.json").expect("failed to open ABI file");

        Abi::from_reader(file).expect("failed to parse ABI")
    };

    // Read some ABI encoded function input
    let mut encoded_input = String::new();
    io::stdin()
        .read_line(&mut encoded_input)
        .expect("failed to read encoded input");

    // Decode
    let (func, decoded_input) = abi
        .decode_input_from_hex(&encoded_input.trim())
        .expect("failed decoding input");

    println!("function called: {}\ninput: {:?}", func.name, decoded_input);
}

Decoding log data

use std::{fs::File, str::FromStr};

use ethereum_abi::Abi;
use web3::types::H256;

fn main() {
    // Parse ABI JSON file
    let abi = {
        let file = File::open("some_abi.json").expect("failed to open ABI file");

        Abi::from_reader(file).expect("failed to parse ABI")
    };

    // Log data
    let topics = vec![
        H256::from_str("...").unwrap(),
        H256::from_str("...").unwrap(),
    ];

    let data = "0000000...".as_bytes();

    // Decode
    let (evt, decoded_data) = abi
        .decode_log_from_slice(&topics, data)
        .expect("failed decoding log");

    println!("event: {}\ndata: {:?}", evt.name, decoded_data);
}

Features

ABI encoder V1

  • JSON parsing
  • Function selectors (method ID)
  • argument encoding and decoding

ABI encoder V2

  • JSON parsing
  • Function selectors (method ID)
  • argument encoding and decoding

License

This project is licensed under the MIT License

Dependencies

~11MB
~215K SLoC