45 releases (23 breaking)

0.25.5 Feb 22, 2024
0.25.4 Nov 24, 2023
0.25.2 Sep 27, 2023
0.25.0 Jul 13, 2023
0.2.0 Dec 20, 2019

#2383 in Magic Beans

Download history 97/week @ 2023-12-05 278/week @ 2023-12-12 125/week @ 2023-12-19 98/week @ 2023-12-26 128/week @ 2024-01-02 133/week @ 2024-01-09 152/week @ 2024-01-16 269/week @ 2024-01-23 173/week @ 2024-01-30 60/week @ 2024-02-06 86/week @ 2024-02-13 434/week @ 2024-02-20 326/week @ 2024-02-27 330/week @ 2024-03-05 322/week @ 2024-03-12 218/week @ 2024-03-19

1,246 downloads per month
Used in 9 crates (4 directly)

MIT/Apache

160KB
3.5K SLoC

ethcontract-generate

An alternative API for generating type-safe contract bindings from build.rs scripts. Using this method instead of the procedural macro has a couple advantages:

  • proper integration with with RLS and Racer for autocomplete support;
  • ability to inspect the generated code.

The downside of using the generator API is the requirement of having a build script instead of a macro invocation.

Getting Started

Using crate requires two dependencies - one for the runtime and one for the generator:

[dependencies]
ethcontract = { version = "...", default-features = false }

[build-dependencies]
ethcontract-generate = "..."

It is recommended that both versions be kept in sync or else unexpected behaviour may occur.

Then, in your build.rs include the following code:

use ethcontract_generate::loaders::TruffleLoader;
use ethcontract_generate::ContractBuilder;

fn main() {
    // Prepare filesystem paths.
    let out_dir = std::env::var("OUT_DIR").unwrap();
    let dest = std::path::Path::new(&out_dir).join("rust_coin.rs");
    
    // Load a contract.
    let contract = TruffleLoader::new()
        .load_contract_from_file("../build/Contract.json")
        .unwrap();
    
    // Generate bindings for it.
    ContractBuilder::new()
        .generate(&contract)
        .unwrap()
        .write_to_file(dest)
        .unwrap();
}

Relation to ethcontract-derive

ethcontract-derive uses ethcontract-generate under the hood so their generated bindings should be identical, they just provide different APIs to the same functionality.

The long term goal of this project is to maintain ethcontract-derive. For now there is no extra work in having it split into two separate crates. That being said if RLS support improves for procedural macro generated code, it is possible that this crate be deprecated in favour of ethcontract-derive as long as there is no good argument to keep it around.

Dependencies

~6–19MB
~258K SLoC