ethcontract 0.25.8

Runtime library and proc macro for interacting and generating type-safe bindings to Ethereum smart contracts.
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Tokenization related functionality allowing rust types to be mapped to solidity types.

// This file is based on https://github.com/tomusdrw/rust-web3/blob/e6d044a28458be9a3ee31108475d787e0440ce8b/src/contract/tokens.rs .
// Generated contract bindings should operate on native rust types for ease of use. To encode them
// with ethabi we need to map them to ethabi tokens. Tokenize does this for base types like
// u32 and compounds of other Tokenize in the form of vectors, arrays and tuples.
//
// In some cases like when passing arguments to `MethodBuilder` or decoding events we need to be
// able to pack multiple types into a single generic parameter. This is accomplished by representing
// the collection of arguments as a tuple.
//
// A completely different approach could be to avoid using the trait system and instead encode all
// rust types into tokens directly in the ethcontract generated bindings.

use crate::I256;
use arrayvec::ArrayVec;
use ethcontract_common::{abi::Token, TransactionHash};
use serde::{Deserialize, Serialize};
use web3::types::{Address, U256};

/// A tokenization related error.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Tokenize::from_token token type doesn't match the rust type.
    #[error("expected a different token type")]
    TypeMismatch,
    /// Tokenize::from_token is called with integer that doesn't fit in the rust type.
    #[error("abi integer does not fit rust integer")]
    IntegerMismatch,
    /// Tokenize::from_token token is fixed bytes with wrong length.
    #[error("expected a different number of fixed bytes")]
    FixedBytesLengthsMismatch,
    /// Tokenize::from_token token is fixed array with wrong length.
    #[error("expected a different number of tokens in fixed array")]
    FixedArrayLengthsMismatch,
    /// Tokenize::from_token token is tuple with wrong length.
    #[error("expected a different number of tokens in tuple")]
    TupleLengthMismatch,
}

/// Rust type and single token conversion.
pub trait Tokenize {
    /// Convert self into token.
    fn from_token(token: Token) -> Result<Self, Error>
    where
        Self: Sized;

    /// Convert token into Self.
    fn into_token(self) -> Token;
}

/// Wrapper around Vec<u8> and [u8; N] representing Token::{Bytes, FixedBytes}. Distinguishes a list
/// of u8 from bytes.
#[derive(
    Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize,
)]
pub struct Bytes<T>(pub T);

impl Tokenize for Token {
    fn from_token(token: Token) -> Result<Self, Error>
    where
        Self: Sized,
    {
        Ok(token)
    }

    fn into_token(self) -> Token {
        self
    }
}

impl Tokenize for Bytes<Vec<u8>> {
    fn from_token(token: Token) -> Result<Self, Error>
    where
        Self: Sized,
    {
        match token {
            Token::Bytes(bytes) => Ok(Self(bytes)),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::Bytes(self.0)
    }
}

impl<const N: usize> Tokenize for Bytes<[u8; N]> {
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::FixedBytes(bytes) => bytes
                .try_into()
                .map(Self)
                .map_err(|_| Error::FixedBytesLengthsMismatch),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::FixedBytes(self.0.to_vec())
    }
}

impl Tokenize for String {
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::String(s) => Ok(s),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::String(self)
    }
}

impl Tokenize for Address {
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::Address(data) => Ok(data),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::Address(self)
    }
}

impl Tokenize for U256 {
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::Uint(u256) => Ok(u256),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::Uint(self)
    }
}

impl Tokenize for I256 {
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::Int(u256) => Ok(Self::from_raw(u256)),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::Int(self.into_raw())
    }
}

impl Tokenize for TransactionHash {
    fn from_token(token: Token) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let bytes = Bytes::from_token(token)?;
        Ok(Self(bytes.0))
    }

    fn into_token(self) -> Token {
        Bytes(self.0).into_token()
    }
}

macro_rules! uint_tokenize {
    ($int: ident, $token: ident) => {
        impl Tokenize for $int {
            fn from_token(token: Token) -> Result<Self, Error> {
                let u256 = match token {
                    Token::Uint(u256) => u256,
                    _ => return Err(Error::TypeMismatch),
                };
                u256.try_into().map_err(|_| Error::IntegerMismatch)
            }

            fn into_token(self) -> Token {
                Token::Uint(self.into())
            }
        }
    };
}

macro_rules! int_tokenize {
    ($int: ident, $token: ident) => {
        impl Tokenize for $int {
            fn from_token(token: Token) -> Result<Self, Error> {
                let u256 = match token {
                    Token::Int(u256) => u256,
                    _ => return Err(Error::TypeMismatch),
                };
                let i256 = I256::from_raw(u256);
                i256.try_into().map_err(|_| Error::IntegerMismatch)
            }

            fn into_token(self) -> Token {
                Token::Int(I256::from(self).into_raw())
            }
        }
    };
}

int_tokenize!(i8, Int);
int_tokenize!(i16, Int);
int_tokenize!(i32, Int);
int_tokenize!(i64, Int);
int_tokenize!(i128, Int);
uint_tokenize!(u8, Uint);
uint_tokenize!(u16, Uint);
uint_tokenize!(u32, Uint);
uint_tokenize!(u64, Uint);
uint_tokenize!(u128, Uint);

impl Tokenize for bool {
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::Bool(data) => Ok(data),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::Bool(self)
    }
}

impl<T, const N: usize> Tokenize for [T; N]
where
    T: Tokenize,
{
    fn from_token(token: Token) -> Result<Self, Error>
    where
        Self: Sized,
    {
        let tokens = match token {
            Token::FixedArray(tokens) => tokens,
            _ => return Err(Error::TypeMismatch),
        };
        let arr_vec = tokens
            .into_iter()
            .map(T::from_token)
            .collect::<Result<ArrayVec<T, N>, _>>()?;
        arr_vec
            .into_inner()
            .map_err(|_| Error::FixedArrayLengthsMismatch)
    }

    fn into_token(self) -> Token {
        Token::FixedArray(
            ArrayVec::from(self)
                .into_iter()
                .map(T::into_token)
                .collect(),
        )
    }
}

impl<T> Tokenize for Vec<T>
where
    T: Tokenize,
{
    fn from_token(token: Token) -> Result<Self, Error> {
        match token {
            Token::Array(tokens) => tokens.into_iter().map(Tokenize::from_token).collect(),
            _ => Err(Error::TypeMismatch),
        }
    }

    fn into_token(self) -> Token {
        Token::Array(self.into_iter().map(Tokenize::into_token).collect())
    }
}

macro_rules! impl_single_tokenize_for_tuple {
    ($count: expr, $( $ty: ident : $no: tt, )*) => {
        impl<$($ty, )*> Tokenize for ($($ty,)*)
        where
            $($ty: Tokenize,)*
        {
            fn from_token(token: Token) -> Result<Self, Error>
            {
                let tokens = match token {
                    Token::Tuple(tokens) => tokens,
                    _ => return Err(Error::TypeMismatch),
                };
                if tokens.len() != $count {
                    return Err(Error::TupleLengthMismatch);
                }
                #[allow(unused_variables)]
                #[allow(unused_mut)]
                let mut drain = tokens.into_iter();
                Ok(($($ty::from_token(drain.next().unwrap())?,)*))
            }

            fn into_token(self) -> Token {
                Token::Tuple(vec![$(self.$no.into_token(),)*])
            }
        }
    }
}

impl_single_tokenize_for_tuple!(0,);
impl_single_tokenize_for_tuple!(1, A:0, );
impl_single_tokenize_for_tuple!(2, A:0, B:1, );
impl_single_tokenize_for_tuple!(3, A:0, B:1, C:2, );
impl_single_tokenize_for_tuple!(4, A:0, B:1, C:2, D:3, );
impl_single_tokenize_for_tuple!(5, A:0, B:1, C:2, D:3, E:4, );
impl_single_tokenize_for_tuple!(6, A:0, B:1, C:2, D:3, E:4, F:5, );
impl_single_tokenize_for_tuple!(7, A:0, B:1, C:2, D:3, E:4, F:5, G:6, );
impl_single_tokenize_for_tuple!(8, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, );
impl_single_tokenize_for_tuple!(9, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, );
impl_single_tokenize_for_tuple!(10, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, );
impl_single_tokenize_for_tuple!(11, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, );
impl_single_tokenize_for_tuple!(12, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, );
impl_single_tokenize_for_tuple!(13, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, );
impl_single_tokenize_for_tuple!(14, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, );
impl_single_tokenize_for_tuple!(15, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, );
impl_single_tokenize_for_tuple!(16, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, );
impl_single_tokenize_for_tuple!(17, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q: 16, );
impl_single_tokenize_for_tuple!(18, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q: 16, R: 17, );
impl_single_tokenize_for_tuple!(19, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q: 16, R: 17, S:18, );
impl_single_tokenize_for_tuple!(20, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q: 16, R: 17, S:18, T: 19, );
impl_single_tokenize_for_tuple!(21, A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q: 16, R: 17, S:18, T: 19, U:20, );

#[cfg(test)]
mod tests {
    use super::*;

    fn assert_single_tokenize_roundtrip<T>(value: T)
    where
        T: Tokenize + Clone + std::fmt::Debug + Eq,
    {
        assert_eq!(value, T::from_token(value.clone().into_token()).unwrap());
    }

    #[test]
    fn single_tokenize_roundtrip() {
        assert_single_tokenize_roundtrip(u8::MIN);
        assert_single_tokenize_roundtrip(u8::MAX);
        assert_single_tokenize_roundtrip(i8::MIN);
        assert_single_tokenize_roundtrip(i8::MAX);
        assert_single_tokenize_roundtrip(u16::MIN);
        assert_single_tokenize_roundtrip(i16::MAX);
        assert_single_tokenize_roundtrip(u32::MIN);
        assert_single_tokenize_roundtrip(i32::MAX);
        assert_single_tokenize_roundtrip(u64::MIN);
        assert_single_tokenize_roundtrip(i64::MAX);
        assert_single_tokenize_roundtrip(u128::MIN);
        assert_single_tokenize_roundtrip(i128::MAX);
        assert_single_tokenize_roundtrip(U256::zero());
        assert_single_tokenize_roundtrip(U256::MAX);
        assert_single_tokenize_roundtrip(I256::MIN);
        assert_single_tokenize_roundtrip(I256::MAX);
        assert_single_tokenize_roundtrip(false);
        assert_single_tokenize_roundtrip(true);
        assert_single_tokenize_roundtrip("abcd".to_string());
        assert_single_tokenize_roundtrip(vec![0u8, 1u8, 2u8]);
        assert_single_tokenize_roundtrip([0u8, 1u8, 2u8]);
        assert_single_tokenize_roundtrip(Bytes(vec![0u8, 1u8, 2u8]));
        assert_single_tokenize_roundtrip(Bytes([0u8, 1u8, 2u8]));
        assert_single_tokenize_roundtrip(Address::from_low_u64_be(42));
        assert_single_tokenize_roundtrip(TransactionHash::from_low_u64_be(42));
        assert_single_tokenize_roundtrip(());
        assert_single_tokenize_roundtrip((-1i8, 1i8));
        assert_single_tokenize_roundtrip([-1i8, 1i8]);
    }

    #[test]
    fn tokenize_bytes() {
        assert!(matches!([0u8].into_token(), Token::FixedArray(_)));
        assert!(matches!(vec![0u8].into_token(), Token::Array(_)));
        assert!(matches!(Bytes([0u8]).into_token(), Token::FixedBytes(_)));
        assert!(matches!(Bytes(vec![0u8]).into_token(), Token::Bytes(_)));
    }

    #[test]
    fn complex() {
        let rust = (vec![[(0u8, 1i8)]], false);
        let token = Token::Tuple(vec![
            Token::Array(vec![Token::FixedArray(vec![Token::Tuple(vec![
                Token::Uint(0.into()),
                Token::Int(1.into()),
            ])])]),
            Token::Bool(false),
        ]);
        assert_eq!(rust.clone().into_token(), token);
        assert_single_tokenize_roundtrip(rust);
    }

    #[test]
    fn i256_tokenization() {
        assert_eq!(I256::from(42).into_token(), 42i32.into_token());
        assert_eq!(I256::minus_one().into_token(), Token::Int(U256::MAX),);
        assert_eq!(
            I256::from_token(Token::Int(U256::MAX)).unwrap(),
            I256::minus_one()
        );

        assert_eq!(
            I256::from_token(42i32.into_token()).unwrap(),
            I256::from(42),
        );
    }
}