[][src]Crate ethcontract

Generate bindings for Ethereum smart contracts. Internally, the generated types use web3 crate to interact with the Ethereum network and uses a custom Instance runtime that can be used directly without code generation.

This crate is using std::future::Future for futures by wrapping web3 futures 0.1 with futures 0.3 compatibility layer. This means that this crate is ready for async/await!

Here is an example of interacing with a smart contract MyContract. The builder pattern is used for configuring transactions.

pragma solidity ^0.5.0;

contract MyContract {
  function my_view_function(uint64 some_val) public view returns (string) {
    // ...

  function my_function(bool some_bool, string some_str) public returns (uint256) {
    // ...

  function my_other_function() public {
    // ...

Once this contract is built and deployed with truffle the following example demonstrates how to interact with it from Rust.

This example is not tested
use ethcontract::transaction::Account;
use std::time::Duration;
use web3::api::Web3;
use web3::types::*;

// this proc macro generates a `MyContract` type with type-safe bindings to
// contract functions

// create a web3 instance as usual
let transport = ...;
let web3 = Web3::new(transport);

// now create an instance of an interface to the contract
let instance = MyContract::deployed(web3).await?;

let addr: Address = "0x000102030405060708090a0b0c0d0e0f10111213".parse()?;
let some_uint: U256 = U256::from_dec_str("1000000000000000000")?;
let some_bool = true;
let some_val = u64;

// call instance view functions with type-safe bindings! will only compile
// if contract function accepts a single `u64` value parameter and returns
// a concrete type based on the contract function's return type
let value = instance

// contract functions also have type-safe bindings and return the tx hash
// of the submitted transaction; allows for multiple ways of signing txs
let tx = instance
    .my_function(some_bool, value)
    .from(Account::Locked(addr, "password".into(), None))

// wait for confirmations when needed
let receipt = instance

See contract! proc macro documentation for more information on usage and parameters.


pub use crate::contract::Instance;
pub use crate::transaction::Account;
pub use crate::transaction::GasPrice;
pub use crate::transport::DynTransport;
pub use ethcontract_common as common;
pub use jsonrpc_core as jsonrpc;
pub use secret::Password;
pub use secret::PrivateKey;
pub use serde_json as json;
pub use web3;



Abtraction for interacting with ethereum smart contracts. Provides methods for sending transactions to contracts as well as querying current contract state.


Module with common error types.


This module implements secrets in the form of protected memory.


Utility for signing transactions and generating RLP encoded raw transactions. Hopefully we can move this functionailly upstream to the web3 crate as part of the missing accounts namespace.


Implementation for setting up, signing, estimating gas and sending transactions on the Ethereum network.


Module containing additional transports used by the ethcontract runtime. In particular, this module includes DynTransport which wraps other valid transports and using dynamic dispatch to call the underlying transport implementation. This transport is used by default for generated contract APIs to help create a more ergonimic experience by making the generated struct not be generic on the underlying transport (at the small cost of some dynamic dispatch and extra allocations).



Proc macro to generate type-safe bindings to a contract. This macro accepts a path to a Truffle artifact JSON file. Note that this path is rooted in the crate's root CARGO_MANIFEST_DIR.



Represents a truffle artifact.


Fixed-size uninterpreted hash type with 20 bytes (160 bits) size.


Fixed-size uninterpreted hash type with 32 bytes (256 bits) size.


Little-endian large integer type



Block Number


Represents condition on minimum block number or block timestamp.

Type Definitions




Type alias for a DeployBuilder with an underlying DynTransport.


Type alias for a contract Instance with an underyling DynTransport.


Type alias for a MethodBuilder with an underlying DynTransport.


Type alias for a ViewMethodBuilder with an underlying DynTransport.


Type alias for a Web3 provider with an underlying DynTransport.