1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
mod ganache;
mod geth;
mod parity;
pub(crate) mod revert;
use crate::transaction::TransactionResult;
use ethcontract_common::abi::{Error as AbiError, Event, Function};
use ethcontract_common::abiext::EventExt;
pub use ethcontract_common::errors::*;
use secp256k1::Error as Secp256k1Error;
use std::num::ParseIntError;
use thiserror::Error;
use uint::FromDecStrErr;
use web3::error::Error as Web3Error;
use web3::types::{Log, TransactionReceipt, H256};
#[derive(Debug, Error)]
pub enum DeployError {
#[error("web3 error: {0}")]
Web3(#[from] Web3Error),
#[error("could not find deployed contract for network {0}")]
NotFound(String),
#[error("could not link library {0}")]
Link(#[from] LinkError),
#[error("can not deploy contract with empty bytecode")]
EmptyBytecode,
#[error("error ABI ecoding deployment parameters: {0}")]
Abi(#[from] AbiError),
#[error("error executing contract deployment transaction: {0}")]
Tx(#[from] ExecutionError),
#[error("contract deployment transaction pending: {0}")]
Pending(H256),
}
#[derive(Debug, Error)]
pub enum ExecutionError {
#[error("web3 error: {0}")]
Web3(Web3Error),
#[error("abi decode error: {0}")]
AbiDecode(#[from] AbiError),
#[error("parse chain ID error: {0}")]
Parse(#[from] ParseIntError),
#[error("no local accounts")]
NoLocalAccounts,
#[error("contract call reverted with message: {0:?}")]
Revert(Option<String>),
#[error("contract call executed an invalid opcode")]
InvalidOpcode,
#[error("transaction confirmation timed-out")]
ConfirmTimeout(Box<TransactionResult>),
#[error("transaction failed: {:?}", .0.transaction_hash)]
Failure(Box<TransactionReceipt>),
#[error("missing transaction {0:?}")]
MissingTransaction(H256),
#[error("pending transaction {0:?}, not yet part of a block")]
PendingTransaction(H256),
#[error("unexepected removed log when querying past logs")]
RemovedLog(Box<Log>),
#[error("log stream ended unexpectedly")]
StreamEndedUnexpectedly,
#[error("tokenization error: {0}")]
Tokenization(#[from] crate::tokens::Error),
}
impl From<Web3Error> for ExecutionError {
fn from(err: Web3Error) -> Self {
if let Web3Error::Rpc(jsonrpc_err) = &err {
if let Some(err) = ganache::get_encoded_error(jsonrpc_err) {
return err;
}
if let Some(err) = parity::get_encoded_error(jsonrpc_err) {
return err;
}
if let Some(err) = geth::get_encoded_error(jsonrpc_err) {
return err;
}
}
ExecutionError::Web3(err)
}
}
#[derive(Debug, Error)]
#[error("method '{signature}' failure: {inner}")]
pub struct MethodError {
pub signature: String,
#[source]
pub inner: ExecutionError,
}
impl MethodError {
pub fn new<I: Into<ExecutionError>>(function: &Function, inner: I) -> Self {
MethodError::from_parts(function.signature(), inner.into())
}
pub fn from_parts(signature: String, inner: ExecutionError) -> Self {
MethodError { signature, inner }
}
}
#[derive(Debug, Error)]
#[error("event '{signature}' failure: {inner}")]
pub struct EventError {
pub signature: String,
#[source]
pub inner: ExecutionError,
}
impl EventError {
pub fn new<I: Into<ExecutionError>>(event: &Event, inner: I) -> Self {
EventError::from_parts(event.abi_signature(), inner.into())
}
pub fn from_parts(signature: String, inner: ExecutionError) -> Self {
EventError { signature, inner }
}
}
#[derive(Debug, Error)]
#[error("invalid private key")]
pub struct InvalidPrivateKey;
impl From<Secp256k1Error> for InvalidPrivateKey {
fn from(err: Secp256k1Error) -> Self {
match err {
Secp256k1Error::InvalidSecretKey => {}
_ => {
debug_assert!(false, "invalid conversion to InvalidPrivateKey error");
}
}
InvalidPrivateKey
}
}
#[derive(Clone, Copy, Debug, Error)]
#[error("output of range integer conversion attempted")]
pub struct TryFromBigIntError;
#[derive(Clone, Copy, Debug, Error)]
pub enum ParseI256Error {
#[error("invalid digit found in string")]
InvalidDigit,
#[error("number does not fit in 256-bit integer")]
IntegerOverflow,
}
impl From<FromDecStrErr> for ParseI256Error {
fn from(err: FromDecStrErr) -> Self {
match err {
FromDecStrErr::InvalidCharacter => ParseI256Error::InvalidDigit,
FromDecStrErr::InvalidLength => ParseI256Error::IntegerOverflow,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
#[test]
fn from_ganache_encoded_error() {
let web3_err = Web3Error::Rpc(ganache::rpc_error("invalid opcode", None));
let err = ExecutionError::from(web3_err);
assert!(
matches!(err, ExecutionError::InvalidOpcode),
"bad error conversion {:?}",
err
);
}
#[test]
fn from_parity_encoded_error() {
let web3_err = Web3Error::Rpc(parity::rpc_error("Bad instruction fd"));
let err = ExecutionError::from(web3_err);
assert!(
matches!(err, ExecutionError::InvalidOpcode),
"bad error conversion {:?}",
err
);
}
#[test]
fn all_errors_are_boxable_errors() {
fn assert_boxable_error<T: Error + Send + Sync + 'static>() {}
assert_boxable_error::<DeployError>();
assert_boxable_error::<ExecutionError>();
assert_boxable_error::<MethodError>();
assert_boxable_error::<InvalidPrivateKey>();
}
}