ethcontract/lib.rs
1#![deny(missing_docs, unsafe_code)]
2
3//! Generate bindings for Ethereum smart contracts. Internally, the generated
4//! types use `web3` crate to interact with the Ethereum network and uses a
5//! custom [`Instance`](ethcontract::contract::Instance) runtime that can be
6//! used directly without code generation.
7//!
8//! This crate is using `std::future::Future` for futures by wrapping `web3`
9//! `futures` 0.1 with `futures` 0.3 compatibility layer. This means that this
10//! crate is ready for `async`/`await`!
11//!
12//! Here is an example of interacing with a smart contract `MyContract`. The
13//! builder pattern is used for configuring transactions.
14//!
15//! ```text
16//! pragma solidity ^0.6.0;
17//!
18//! contract MyContract {
19//! function my_view_function(uint64 some_val) public view returns (string) {
20//! // ...
21//! }
22//!
23//! function my_function(bool some_bool, string some_str) public returns (uint256) {
24//! // ...
25//! }
26//!
27//! function my_other_function() public {
28//! // ...
29//! }
30//! }
31//! ```
32//!
33//! Once this contract is built and deployed with truffle the following example
34//! demonstrates how to interact with it from Rust.
35//!
36//! ```ignore
37//! use ethcontract::transaction::Account;
38//! use std::time::Duration;
39//! use web3::api::Web3;
40//! use web3::types::*;
41//!
42//! // this proc macro generates a `MyContract` type with type-safe bindings to
43//! // contract functions
44//! ethcontract::contract!("path/to/MyContract.json");
45//!
46//! // create a web3 instance as usual
47//! let transport = ...;
48//! let web3 = Web3::new(transport);
49//!
50//! // now create an instance of an interface to the contract
51//! let instance = MyContract::deployed(web3).await?;
52//!
53//! let addr: Address = "0x000102030405060708090a0b0c0d0e0f10111213".parse()?;
54//! let some_uint: U256 = U256::from_dec_str("1000000000000000000")?;
55//! let some_bool = true;
56//! let some_val = u64;
57//!
58//! // call instance view functions with type-safe bindings! will only compile
59//! // if contract function accepts a single `u64` value parameter and returns
60//! // a concrete type based on the contract function's return type
61//! let value = instance
62//! .my_view_function(some_val)
63//! .from(addr)
64//! .execute()
65//! .await?;
66//!
67//! // contract functions also have type-safe bindings and return the tx hash
68//! // of the submitted transaction; allows for multiple ways of signing txs
69//! let tx = instance
70//! .my_function(some_bool, value)
71//! .from(Account::Locked(addr, "password".into(), None))
72//! .value(some_uint)
73//! .gas_price(1_000_000.into())
74//! .execute()
75//! .await?;
76//!
77//! // wait for confirmations when needed
78//! let receipt = instance
79//! .my_important_function()
80//! .poll_interval(Duration::from_secs(5))
81//! .confirmations(2)
82//! .execute_confirm()
83//! .await?;
84//! ```
85//!
86//! See [`contract!`](ethcontract::contract) proc macro documentation for more
87//! information on usage and parameters as well on how to use contract ABI
88//! directly from Etherscan.
89
90#[cfg(test)]
91#[allow(missing_docs)]
92#[macro_use]
93#[path = "test/macros.rs"]
94mod test_macros;
95
96pub mod batch;
97pub mod contract;
98pub mod errors;
99mod int;
100pub mod log;
101pub mod secret;
102pub mod tokens;
103pub mod transaction;
104pub mod transport;
105
106pub use crate::contract::Instance;
107pub use crate::prelude::*;
108#[cfg(feature = "aws-kms")]
109pub use aws_config;
110pub use ethcontract_common as common;
111pub use ethcontract_common::contract::Contract;
112#[cfg(feature = "derive")]
113pub use ethcontract_derive::contract;
114pub use futures;
115pub use jsonrpc_core as jsonrpc;
116pub use serde_json as json;
117pub use web3;
118
119pub mod prelude {
120 //! A prelude module for importing commonly used types when interacting with
121 //! generated contracts.
122
123 pub use crate::contract::{Event, EventMetadata, EventStatus, RawLog, StreamEvent, Topic};
124 pub use crate::int::I256;
125 pub use crate::secret::{Password, PrivateKey};
126 pub use crate::tokens::Bytes;
127 pub use crate::transaction::{Account, GasPrice};
128 pub use ethcontract_common::TransactionHash;
129 pub use web3::api::Web3;
130 #[cfg(feature = "http")]
131 pub use web3::transports::Http;
132 pub use web3::types::{Address, BlockId, BlockNumber, TransactionCondition, H160, H256, U256};
133}
134
135pub mod dyns {
136 //! Type aliases to various runtime types that use an underlying
137 //! `DynTransport`. These types are used extensively throughout the
138 //! generated code.
139
140 use crate::contract::{
141 AllEventsBuilder, DeployBuilder, EventBuilder, Instance, MethodBuilder, ViewMethodBuilder,
142 };
143 pub use crate::transport::DynTransport;
144 use web3::api::Web3;
145
146 /// Type alias for a `Web3` with an underlying `DynTransport`.
147 pub type DynWeb3 = Web3<DynTransport>;
148
149 /// Type alias for an `Instance` with an underlying `DynTransport`.
150 pub type DynInstance = Instance<DynTransport>;
151
152 /// Type alias for a `DeployBuilder` with an underlying `DynTransport`.
153 pub type DynDeployBuilder<D> = DeployBuilder<DynTransport, D>;
154
155 /// Type alias for a `MethodBuilder` with an underlying `DynTransport`.
156 pub type DynMethodBuilder<R> = MethodBuilder<DynTransport, R>;
157
158 /// Type alias for a `ViewMethodBuilder` with an underlying `DynTransport`.
159 pub type DynViewMethodBuilder<R> = ViewMethodBuilder<DynTransport, R>;
160
161 /// Type alias for a `EventBuilder` with an underlying `DynTransport`.
162 pub type DynEventBuilder<E> = EventBuilder<DynTransport, E>;
163
164 /// Type alias for a `LogStream` with an underlying `DynTransport`.
165 pub type DynAllEventsBuilder<E> = AllEventsBuilder<DynTransport, E>;
166}
167
168#[doc(hidden)]
169pub mod private {
170 // Private definitions that are needed by the generated contract code or
171 // but do not appear in public interfaces. No documentation is generated
172 // for these definitions.
173
174 pub use lazy_static::lazy_static;
175}
176
177#[cfg(test)]
178#[allow(missing_docs)]
179mod test {
180 pub mod prelude;
181 pub mod transport;
182}