#generator #binding #contract #generation #framework #ethereum #ethbind-rs

ethbind

An ethereum contract binding code generation framework

5 releases

0.1.6 Feb 25, 2023
0.1.5 Feb 24, 2023
0.1.4 Feb 15, 2023
0.1.3 Feb 14, 2023
0.1.2 Feb 14, 2023

#232 in #ethereum

Download history 57/week @ 2023-12-04 69/week @ 2023-12-11 2/week @ 2023-12-18 4/week @ 2023-12-25 23/week @ 2024-01-08 15/week @ 2024-01-15 33/week @ 2024-01-22 34/week @ 2024-01-29 106/week @ 2024-02-05 180/week @ 2024-02-12 178/week @ 2024-02-19 130/week @ 2024-02-26 61/week @ 2024-03-04 103/week @ 2024-03-11 101/week @ 2024-03-18

423 downloads per month
Used in 2 crates

Custom license

5KB

ethbind-rs

"Crates.io version" "docs.rs docs"

ethbind-rs is an ethereum contract binding code generation framework for arbitrary programming languages.

Generator

The binding processor generates arbitrary programming languages codes by calling the corresponding generator,The code generator is a rust structure that implements Generator trait.

So far, the only official generator is the rust bind code Generator, you can easily use this generator in your Rust code in two ways:

via proc-macro

Using the builtin proc-macro contract!($binder_json_path,$abi_json_path) to directly derive contract bind interface in your rust code, e.g:

contract!(include_str!("xxx/binder.json"),include_str!("xxx/Lock.json"));

The above line of rust code will generating Lock contract bind codes in place via loading contract abi from Lock.json file.

via build.rs

Of course, you can directly call binding processor in build.rs:

First, Add ethbind-rs in Cargo.toml

[build-dependencies]
ethbind-rs = "^0.1"

Second, Add blow lines of code in build.rs


/// + use rust `gen_codes` fn
use ethbind::gen::{ JsonRuntimeBinder,SaveTo };
use ethbind::rust::*;

fn main() {
    

    println!("cargo:rerun-if-changed=sol");

    // + load runtime type binding information
    let runtime_binder: JsonRuntimeBinder = include_str!("xxx/binder.json").parse().expect("Load binder information");

    // + define output directory
    let output_dir = "src/sol";

    let mut contracts = BindingBuilder::new((RustGenerator::default(), runtime_binder))
            .bind_hardhat(include_str!("xxx/Lock.json"))
            .bind_hardhat(include_str!("xxx/Swap.json"))
            .finalize()
            .expect("Generate data");
    
    contracts.pretty().expect("Pretty");

    contracts.save_to(output_dir).expect("Save generated");

    // other codes..
}

Dependencies

~4–6MB
~102K SLoC