lyquid 0.4.4

Lyquid Development Kit (LDK).
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
409
410
411
412
413
414
415
416
417
418
419
420
use std::string::String;
use std::vec::Vec;

use alloy_sol_types::{SolType, sol_data};

use lyquor_primitives::{Address, B256, Bytes, LyquidID, NodeID, RequiredLyquid, U64, U128, U256};

/// Compile-time Ethereum ABI type descriptor used in export metadata.
#[derive(Copy, Clone)]
pub struct EthAbiTypeDesc {
    pub base: &'static str,
    pub dims: [Option<u32>; MAX_DIMS],
    pub dims_len: u8,
    pub is_dynamic: bool,
}

const MAX_DIMS: usize = 8;
const EMPTY_DIMS: [Option<u32>; MAX_DIMS] = [None; MAX_DIMS];

impl EthAbiTypeDesc {
    /// Returns the string length of the canonical ABI type representation.
    pub const fn len(self) -> usize {
        let mut len = self.base.len();
        let mut i = 0usize;
        while i < self.dims_len as usize {
            len += dim_len(self.dims[i]);
            i += 1;
        }
        len
    }

    /// Appends one array dimension to this descriptor.
    pub const fn with_dim(mut self, dim: Option<u32>) -> Self {
        if self.dims_len as usize >= MAX_DIMS {
            panic!("ethabi dims overflow");
        }
        self.dims[self.dims_len as usize] = dim;
        self.dims_len += 1;
        self
    }
}

const fn dim_len(dim: Option<u32>) -> usize {
    match dim {
        Some(val) => 2 + digits_u32(val),
        None => 2,
    }
}

const fn digits_u32(mut val: u32) -> usize {
    let mut digits = 1usize;
    while val >= 10 {
        val /= 10;
        digits += 1;
    }
    digits
}

const fn base_desc(base: &'static str, is_dynamic: bool) -> EthAbiTypeDesc {
    EthAbiTypeDesc {
        base,
        dims: EMPTY_DIMS,
        dims_len: 0,
        is_dynamic,
    }
}

/// Conversion contract between Lyquid Rust values and alloy Solidity ABI values.
pub trait EthAbiType: Sized {
    /// Alloy Solidity type that represents this Lyquid value.
    type SolType: SolType;

    /// Canonical ABI descriptor for this type.
    const DESC: EthAbiTypeDesc;

    /// Converts this value into its ABI representation.
    fn into_sol(self) -> <Self::SolType as SolType>::RustType;

    /// Converts an ABI representation back into this Lyquid value.
    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self>;
}

/// Tuple-level encoder and decoder for Ethereum method parameters.
pub trait EthAbiParams: Sized {
    /// Decodes ABI parameter bytes into this tuple.
    fn decode_params(data: &[u8]) -> Option<Self>;

    /// Encodes this tuple as ABI parameter bytes.
    fn encode_params(self) -> Vec<u8>;
}

/// Encoder for a single Ethereum ABI return value.
pub trait EthAbiReturnValue {
    /// Encodes this value as ABI return bytes.
    fn encode_return(self) -> Vec<u8>;
}

/// Metadata and encoder contract for Ethereum ABI return tuples.
pub trait EthAbiReturn: EthAbiReturnValue {
    /// Number of returned values.
    const COUNT: usize;
    /// ABI descriptors for returned values.
    const TYPES: &'static [EthAbiTypeDesc];
}

impl EthAbiParams for () {
    fn decode_params(data: &[u8]) -> Option<Self> {
        <() as SolType>::abi_decode_params_validate(data).ok()
    }

    fn encode_params(self) -> Vec<u8> {
        <() as SolType>::abi_encode_params(&self)
    }
}

macro_rules! impl_eth_abi_params {
    ($($ty:ident $value:ident),+) => {
        impl<$($ty: EthAbiType),+> EthAbiParams for ($($ty,)+) {
            fn decode_params(data: &[u8]) -> Option<Self> {
                type SolTuple<$($ty),+> = ($(<$ty as EthAbiType>::SolType,)+);
                let ($($value,)+) =
                    <SolTuple<$($ty),+> as SolType>::abi_decode_params_validate(data).ok()?;
                Some(($(<$ty as EthAbiType>::from_sol($value)?,)+))
            }

            fn encode_params(self) -> Vec<u8> {
                type SolTuple<$($ty),+> = ($(<$ty as EthAbiType>::SolType,)+);
                let ($($value,)+) = self;
                let value = ($(<$ty as EthAbiType>::into_sol($value),)+);
                <SolTuple<$($ty),+> as SolType>::abi_encode_params(&value)
            }
        }
    };
}

impl_eth_abi_params!(A a);
impl_eth_abi_params!(A a, B b);
impl_eth_abi_params!(A a, B b, C c);
impl_eth_abi_params!(A a, B b, C c, D d);
impl_eth_abi_params!(A a, B b, C c, D d, E e);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o);
impl_eth_abi_params!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p);

impl EthAbiReturnValue for () {
    fn encode_return(self) -> Vec<u8> {
        Vec::new()
    }
}

impl<T: EthAbiType> EthAbiReturnValue for T {
    fn encode_return(self) -> Vec<u8> {
        let value = T::into_sol(self);
        <T::SolType as SolType>::abi_encode(&value)
    }
}

macro_rules! impl_eth_abi_return_tuple {
    ($($ty:ident $value:ident),+) => {
        impl<$($ty: EthAbiType),+> EthAbiReturnValue for ($($ty,)+) {
            fn encode_return(self) -> Vec<u8> {
                type SolTuple<$($ty),+> = ($(<$ty as EthAbiType>::SolType,)+);
                let ($($value,)+) = self;
                let value = ($(<$ty as EthAbiType>::into_sol($value),)+);
                <SolTuple<$($ty),+> as SolType>::abi_encode_sequence(&value)
            }
        }

        impl<$($ty: EthAbiType),+> EthAbiReturn for ($($ty,)+) {
            const COUNT: usize = 0 $(+ {
                let _ = stringify!($ty);
                1
            })+;
            const TYPES: &'static [EthAbiTypeDesc] = &[$($ty::DESC,)+];
        }
    };
}

impl EthAbiReturn for () {
    const COUNT: usize = 0;
    const TYPES: &'static [EthAbiTypeDesc] = &[];
}

impl<T: EthAbiType> EthAbiReturn for T {
    const COUNT: usize = 1;
    const TYPES: &'static [EthAbiTypeDesc] = &[T::DESC];
}

impl_eth_abi_return_tuple!(A a, B b);
impl_eth_abi_return_tuple!(A a, B b, C c);
impl_eth_abi_return_tuple!(A a, B b, C c, D d);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o);
impl_eth_abi_return_tuple!(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j, K k, L l, M m, N n, O o, P p);

impl EthAbiType for U256 {
    type SolType = sol_data::Uint<256>;

    const DESC: EthAbiTypeDesc = base_desc("uint256", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(value)
    }
}

impl EthAbiType for U128 {
    type SolType = sol_data::Uint<128>;

    const DESC: EthAbiTypeDesc = base_desc("uint128", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.to::<u128>()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(Self::from_limbs([value as u64, (value >> 64) as u64]))
    }
}

impl EthAbiType for U64 {
    type SolType = sol_data::Uint<64>;

    const DESC: EthAbiTypeDesc = base_desc("uint64", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.to::<u64>()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(Self::from_limbs([value]))
    }
}

macro_rules! impl_eth_abi_uint {
    ($ty:ty, $bits:literal, $desc:literal) => {
        impl EthAbiType for $ty {
            type SolType = sol_data::Uint<$bits>;

            const DESC: EthAbiTypeDesc = base_desc($desc, false);

            fn into_sol(self) -> <Self::SolType as SolType>::RustType {
                self
            }

            fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
                Some(value)
            }
        }
    };
}

impl_eth_abi_uint!(u64, 64, "uint64");
impl_eth_abi_uint!(u32, 32, "uint32");
impl_eth_abi_uint!(u16, 16, "uint16");
impl_eth_abi_uint!(u8, 8, "uint8");

impl EthAbiType for bool {
    type SolType = sol_data::Bool;

    const DESC: EthAbiTypeDesc = base_desc("bool", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(value)
    }
}

impl EthAbiType for Bytes {
    type SolType = sol_data::Bytes;

    const DESC: EthAbiTypeDesc = base_desc("bytes", true);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.to_vec().into()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(Bytes::copy_from_slice(value.as_ref()))
    }
}

impl EthAbiType for String {
    type SolType = sol_data::String;

    const DESC: EthAbiTypeDesc = base_desc("string", true);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(value)
    }
}

impl EthAbiType for Address {
    type SolType = sol_data::Address;

    const DESC: EthAbiTypeDesc = base_desc("address", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(value)
    }
}

impl EthAbiType for B256 {
    type SolType = sol_data::FixedBytes<32>;

    const DESC: EthAbiTypeDesc = base_desc("bytes32", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(value)
    }
}

impl EthAbiType for LyquidID {
    type SolType = sol_data::Address;

    const DESC: EthAbiTypeDesc = base_desc("address", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.into()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(value.into())
    }
}

impl EthAbiType for RequiredLyquid {
    type SolType = sol_data::Address;

    const DESC: EthAbiTypeDesc = base_desc("address", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.0.into()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(Self(value.into()))
    }
}

impl EthAbiType for NodeID {
    type SolType = sol_data::FixedBytes<32>;

    const DESC: EthAbiTypeDesc = base_desc("bytes32", false);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        <[u8; 32]>::from(self).into()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        Some(NodeID::from(<[u8; 32]>::from(value)))
    }
}

impl<T: EthAbiType> EthAbiType for Vec<T> {
    type SolType = sol_data::Array<T::SolType>;

    const DESC: EthAbiTypeDesc = T::DESC.with_dim(None);

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.into_iter().map(T::into_sol).collect()
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        value.into_iter().map(T::from_sol).collect()
    }
}

impl<T: EthAbiType, const N: usize> EthAbiType for [T; N] {
    type SolType = sol_data::FixedArray<T::SolType, N>;

    const DESC: EthAbiTypeDesc = T::DESC.with_dim(Some(N as u32));

    fn into_sol(self) -> <Self::SolType as SolType>::RustType {
        self.map(T::into_sol)
    }

    fn from_sol(value: <Self::SolType as SolType>::RustType) -> Option<Self> {
        let mut out = Vec::with_capacity(N);
        for value in value {
            out.push(T::from_sol(value)?);
        }
        out.try_into().ok()
    }
}