faster-eth-abi 5.2.3__cp314-cp314t-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. a1f8aa123fabc88e2b56__mypyc.cp314t-win32.pyd +0 -0
  2. faster_eth_abi/__init__.py +12 -0
  3. faster_eth_abi/abi.cp314t-win32.pyd +0 -0
  4. faster_eth_abi/abi.py +17 -0
  5. faster_eth_abi/base.py +41 -0
  6. faster_eth_abi/codec.py +167 -0
  7. faster_eth_abi/constants.cp314t-win32.pyd +0 -0
  8. faster_eth_abi/constants.py +7 -0
  9. faster_eth_abi/decoding.py +563 -0
  10. faster_eth_abi/encoding.py +699 -0
  11. faster_eth_abi/exceptions.py +115 -0
  12. faster_eth_abi/from_type_str.cp314t-win32.pyd +0 -0
  13. faster_eth_abi/from_type_str.py +135 -0
  14. faster_eth_abi/grammar.py +467 -0
  15. faster_eth_abi/io.py +103 -0
  16. faster_eth_abi/packed.cp314t-win32.pyd +0 -0
  17. faster_eth_abi/packed.py +15 -0
  18. faster_eth_abi/py.typed +0 -0
  19. faster_eth_abi/registry.py +640 -0
  20. faster_eth_abi/tools/__init__.cp314t-win32.pyd +0 -0
  21. faster_eth_abi/tools/__init__.py +3 -0
  22. faster_eth_abi/tools/_strategies.cp314t-win32.pyd +0 -0
  23. faster_eth_abi/tools/_strategies.py +237 -0
  24. faster_eth_abi/utils/__init__.cp314t-win32.pyd +0 -0
  25. faster_eth_abi/utils/__init__.py +0 -0
  26. faster_eth_abi/utils/numeric.cp314t-win32.pyd +0 -0
  27. faster_eth_abi/utils/numeric.py +86 -0
  28. faster_eth_abi/utils/padding.cp314t-win32.pyd +0 -0
  29. faster_eth_abi/utils/padding.py +22 -0
  30. faster_eth_abi/utils/string.cp314t-win32.pyd +0 -0
  31. faster_eth_abi/utils/string.py +19 -0
  32. faster_eth_abi/utils/validation.cp314t-win32.pyd +0 -0
  33. faster_eth_abi/utils/validation.py +22 -0
  34. faster_eth_abi-5.2.3.dist-info/METADATA +95 -0
  35. faster_eth_abi-5.2.3.dist-info/RECORD +38 -0
  36. faster_eth_abi-5.2.3.dist-info/WHEEL +5 -0
  37. faster_eth_abi-5.2.3.dist-info/licenses/LICENSE +21 -0
  38. faster_eth_abi-5.2.3.dist-info/top_level.txt +3 -0
@@ -0,0 +1,699 @@
1
+ import abc
2
+ import codecs
3
+ import decimal
4
+ from itertools import (
5
+ accumulate,
6
+ )
7
+ from typing import (
8
+ Any,
9
+ Optional,
10
+ Tuple,
11
+ Type,
12
+ )
13
+
14
+ from faster_eth_utils import (
15
+ int_to_big_endian,
16
+ is_address,
17
+ is_boolean,
18
+ is_bytes,
19
+ is_integer,
20
+ is_list_like,
21
+ is_number,
22
+ is_text,
23
+ to_canonical_address,
24
+ )
25
+
26
+ from faster_eth_abi.base import (
27
+ BaseCoder,
28
+ )
29
+ from faster_eth_abi.exceptions import (
30
+ EncodingTypeError,
31
+ IllegalValue,
32
+ ValueOutOfBounds,
33
+ )
34
+ from faster_eth_abi.from_type_str import (
35
+ parse_tuple_type_str,
36
+ parse_type_str,
37
+ )
38
+ from faster_eth_abi.utils.numeric import (
39
+ TEN,
40
+ abi_decimal_context,
41
+ ceil32,
42
+ compute_signed_fixed_bounds,
43
+ compute_signed_integer_bounds,
44
+ compute_unsigned_fixed_bounds,
45
+ compute_unsigned_integer_bounds,
46
+ )
47
+ from faster_eth_abi.utils.padding import (
48
+ fpad,
49
+ zpad,
50
+ zpad_right,
51
+ )
52
+ from faster_eth_abi.utils.string import (
53
+ abbr,
54
+ )
55
+
56
+
57
+ class BaseEncoder(BaseCoder, metaclass=abc.ABCMeta):
58
+ """
59
+ Base class for all encoder classes. Subclass this if you want to define a
60
+ custom encoder class. Subclasses must also implement
61
+ :any:`BaseCoder.from_type_str`.
62
+ """
63
+
64
+ @abc.abstractmethod
65
+ def encode(self, value: Any) -> bytes: # pragma: no cover
66
+ """
67
+ Encodes the given value as a sequence of bytes. Should raise
68
+ :any:`exceptions.EncodingError` if ``value`` cannot be encoded.
69
+ """
70
+
71
+ @abc.abstractmethod
72
+ def validate_value(self, value: Any) -> None: # pragma: no cover
73
+ """
74
+ Checks whether or not the given value can be encoded by this encoder.
75
+ If the given value cannot be encoded, must raise
76
+ :any:`exceptions.EncodingError`.
77
+ """
78
+
79
+ @classmethod
80
+ def invalidate_value(
81
+ cls,
82
+ value: Any,
83
+ exc: Type[Exception] = EncodingTypeError,
84
+ msg: Optional[str] = None,
85
+ ) -> None:
86
+ """
87
+ Throws a standard exception for when a value is not encodable by an
88
+ encoder.
89
+ """
90
+ raise exc(
91
+ f"Value `{abbr(value)}` of type {type(value)} cannot be encoded by "
92
+ f"{cls.__name__}{'' if msg is None else (': ' + msg)}"
93
+ )
94
+
95
+ def __call__(self, value: Any) -> bytes:
96
+ return self.encode(value)
97
+
98
+
99
+ class TupleEncoder(BaseEncoder):
100
+ encoders: Tuple[BaseEncoder, ...] = ()
101
+
102
+ def __init__(self, encoders: Tuple[BaseEncoder, ...], **kwargs: Any) -> None:
103
+ super().__init__(encoders=encoders, **kwargs)
104
+
105
+ self.is_dynamic = any(getattr(e, "is_dynamic", False) for e in self.encoders)
106
+
107
+ def validate(self):
108
+ super().validate()
109
+
110
+ if self.encoders is None:
111
+ raise ValueError("`encoders` may not be none")
112
+
113
+ def validate_value(self, value):
114
+ if not is_list_like(value):
115
+ self.invalidate_value(
116
+ value,
117
+ msg="must be list-like object such as array or tuple",
118
+ )
119
+
120
+ if len(value) != len(self.encoders):
121
+ self.invalidate_value(
122
+ value,
123
+ exc=ValueOutOfBounds,
124
+ msg=f"value has {len(value)} items when {len(self.encoders)} "
125
+ "were expected",
126
+ )
127
+
128
+ for item, encoder in zip(value, self.encoders):
129
+ try:
130
+ encoder.validate_value(item)
131
+ except AttributeError:
132
+ encoder(item)
133
+
134
+ def encode(self, values):
135
+ self.validate_value(values)
136
+
137
+ raw_head_chunks = []
138
+ tail_chunks = []
139
+ for value, encoder in zip(values, self.encoders):
140
+ if getattr(encoder, "is_dynamic", False):
141
+ raw_head_chunks.append(None)
142
+ tail_chunks.append(encoder(value))
143
+ else:
144
+ raw_head_chunks.append(encoder(value))
145
+ tail_chunks.append(b"")
146
+
147
+ head_length = sum(32 if item is None else len(item) for item in raw_head_chunks)
148
+ tail_offsets = (0,) + tuple(accumulate(map(len, tail_chunks[:-1])))
149
+ head_chunks = tuple(
150
+ encode_uint_256(head_length + offset) if chunk is None else chunk
151
+ for chunk, offset in zip(raw_head_chunks, tail_offsets)
152
+ )
153
+
154
+ encoded_value = b"".join(head_chunks + tuple(tail_chunks))
155
+ return encoded_value
156
+
157
+ @parse_tuple_type_str
158
+ def from_type_str(cls, abi_type, registry):
159
+ encoders = tuple(
160
+ registry.get_encoder(c.to_type_str()) for c in abi_type.components
161
+ )
162
+
163
+ return cls(encoders=encoders)
164
+
165
+
166
+ class FixedSizeEncoder(BaseEncoder):
167
+ value_bit_size = None
168
+ data_byte_size = None
169
+ encode_fn = None
170
+ type_check_fn = None
171
+ is_big_endian = None
172
+
173
+ def validate(self):
174
+ super().validate()
175
+
176
+ if self.value_bit_size is None:
177
+ raise ValueError("`value_bit_size` may not be none")
178
+ if self.data_byte_size is None:
179
+ raise ValueError("`data_byte_size` may not be none")
180
+ if self.encode_fn is None:
181
+ raise ValueError("`encode_fn` may not be none")
182
+ if self.is_big_endian is None:
183
+ raise ValueError("`is_big_endian` may not be none")
184
+
185
+ if self.value_bit_size % 8 != 0:
186
+ raise ValueError(
187
+ f"Invalid value bit size: {self.value_bit_size}. "
188
+ "Must be a multiple of 8"
189
+ )
190
+
191
+ if self.value_bit_size > self.data_byte_size * 8:
192
+ raise ValueError("Value byte size exceeds data size")
193
+
194
+ def validate_value(self, value):
195
+ raise NotImplementedError("Must be implemented by subclasses")
196
+
197
+ def encode(self, value):
198
+ self.validate_value(value)
199
+ if self.encode_fn is None:
200
+ raise AssertionError("`encode_fn` is None")
201
+ base_encoded_value = self.encode_fn(value)
202
+
203
+ if self.is_big_endian:
204
+ padded_encoded_value = zpad(base_encoded_value, self.data_byte_size)
205
+ else:
206
+ padded_encoded_value = zpad_right(base_encoded_value, self.data_byte_size)
207
+
208
+ return padded_encoded_value
209
+
210
+
211
+ class Fixed32ByteSizeEncoder(FixedSizeEncoder):
212
+ data_byte_size = 32
213
+
214
+
215
+ class BooleanEncoder(Fixed32ByteSizeEncoder):
216
+ value_bit_size = 8
217
+ is_big_endian = True
218
+
219
+ @classmethod
220
+ def validate_value(cls, value):
221
+ if not is_boolean(value):
222
+ cls.invalidate_value(value)
223
+
224
+ @classmethod
225
+ def encode_fn(cls, value):
226
+ if value is True:
227
+ return b"\x01"
228
+ elif value is False:
229
+ return b"\x00"
230
+ else:
231
+ raise ValueError("Invariant")
232
+
233
+ @parse_type_str("bool")
234
+ def from_type_str(cls, abi_type, registry):
235
+ return cls()
236
+
237
+
238
+ class PackedBooleanEncoder(BooleanEncoder):
239
+ data_byte_size = 1
240
+
241
+
242
+ class NumberEncoder(Fixed32ByteSizeEncoder):
243
+ is_big_endian = True
244
+ bounds_fn = None
245
+ illegal_value_fn = None
246
+ type_check_fn = None
247
+
248
+ def validate(self):
249
+ super().validate()
250
+
251
+ if self.bounds_fn is None:
252
+ raise ValueError("`bounds_fn` cannot be null")
253
+ if self.type_check_fn is None:
254
+ raise ValueError("`type_check_fn` cannot be null")
255
+
256
+ def validate_value(self, value):
257
+ if self.type_check_fn is None:
258
+ raise AssertionError("`type_check_fn` is None")
259
+ if not self.type_check_fn(value):
260
+ self.invalidate_value(value)
261
+
262
+ illegal_value = self.illegal_value_fn is not None and self.illegal_value_fn(
263
+ value
264
+ )
265
+ if illegal_value:
266
+ self.invalidate_value(value, exc=IllegalValue)
267
+
268
+ lower_bound, upper_bound = self.bounds_fn(self.value_bit_size)
269
+ if value < lower_bound or value > upper_bound:
270
+ self.invalidate_value(
271
+ value,
272
+ exc=ValueOutOfBounds,
273
+ msg=f"Cannot be encoded in {self.value_bit_size} bits. Must be bounded "
274
+ f"between [{lower_bound}, {upper_bound}].",
275
+ )
276
+
277
+
278
+ class UnsignedIntegerEncoder(NumberEncoder):
279
+ encode_fn = staticmethod(int_to_big_endian)
280
+ bounds_fn = staticmethod(compute_unsigned_integer_bounds)
281
+ type_check_fn = staticmethod(is_integer)
282
+
283
+ @parse_type_str("uint")
284
+ def from_type_str(cls, abi_type, registry):
285
+ return cls(value_bit_size=abi_type.sub)
286
+
287
+
288
+ encode_uint_256 = UnsignedIntegerEncoder(value_bit_size=256, data_byte_size=32)
289
+
290
+
291
+ class PackedUnsignedIntegerEncoder(UnsignedIntegerEncoder):
292
+ @parse_type_str("uint")
293
+ def from_type_str(cls, abi_type, registry):
294
+ return cls(
295
+ value_bit_size=abi_type.sub,
296
+ data_byte_size=abi_type.sub // 8,
297
+ )
298
+
299
+
300
+ class SignedIntegerEncoder(NumberEncoder):
301
+ bounds_fn = staticmethod(compute_signed_integer_bounds)
302
+ type_check_fn = staticmethod(is_integer)
303
+
304
+ def encode_fn(self, value):
305
+ return int_to_big_endian(value % (2**self.value_bit_size))
306
+
307
+ def encode(self, value):
308
+ self.validate_value(value)
309
+ base_encoded_value = self.encode_fn(value)
310
+
311
+ if value >= 0:
312
+ padded_encoded_value = zpad(base_encoded_value, self.data_byte_size)
313
+ else:
314
+ padded_encoded_value = fpad(base_encoded_value, self.data_byte_size)
315
+
316
+ return padded_encoded_value
317
+
318
+ @parse_type_str("int")
319
+ def from_type_str(cls, abi_type, registry):
320
+ return cls(value_bit_size=abi_type.sub)
321
+
322
+
323
+ class PackedSignedIntegerEncoder(SignedIntegerEncoder):
324
+ @parse_type_str("int")
325
+ def from_type_str(cls, abi_type, registry):
326
+ return cls(
327
+ value_bit_size=abi_type.sub,
328
+ data_byte_size=abi_type.sub // 8,
329
+ )
330
+
331
+
332
+ class BaseFixedEncoder(NumberEncoder):
333
+ frac_places = None
334
+
335
+ @staticmethod
336
+ def type_check_fn(value):
337
+ return is_number(value) and not isinstance(value, float)
338
+
339
+ @staticmethod
340
+ def illegal_value_fn(value):
341
+ if isinstance(value, decimal.Decimal):
342
+ return value.is_nan() or value.is_infinite()
343
+
344
+ return False
345
+
346
+ def validate_value(self, value):
347
+ super().validate_value(value)
348
+
349
+ with decimal.localcontext(abi_decimal_context):
350
+ residue = value % (TEN**-self.frac_places)
351
+
352
+ if residue > 0:
353
+ self.invalidate_value(
354
+ value,
355
+ exc=IllegalValue,
356
+ msg=f"residue {repr(residue)} outside allowed fractional precision of "
357
+ f"{self.frac_places}",
358
+ )
359
+
360
+ def validate(self):
361
+ super().validate()
362
+
363
+ if self.frac_places is None:
364
+ raise ValueError("must specify `frac_places`")
365
+
366
+ if self.frac_places <= 0 or self.frac_places > 80:
367
+ raise ValueError("`frac_places` must be in range (0, 80]")
368
+
369
+
370
+ class UnsignedFixedEncoder(BaseFixedEncoder):
371
+ def bounds_fn(self, value_bit_size):
372
+ return compute_unsigned_fixed_bounds(self.value_bit_size, self.frac_places)
373
+
374
+ def encode_fn(self, value):
375
+ with decimal.localcontext(abi_decimal_context):
376
+ scaled_value = value * TEN**self.frac_places
377
+ integer_value = int(scaled_value)
378
+
379
+ return int_to_big_endian(integer_value)
380
+
381
+ @parse_type_str("ufixed")
382
+ def from_type_str(cls, abi_type, registry):
383
+ value_bit_size, frac_places = abi_type.sub
384
+
385
+ return cls(
386
+ value_bit_size=value_bit_size,
387
+ frac_places=frac_places,
388
+ )
389
+
390
+
391
+ class PackedUnsignedFixedEncoder(UnsignedFixedEncoder):
392
+ @parse_type_str("ufixed")
393
+ def from_type_str(cls, abi_type, registry):
394
+ value_bit_size, frac_places = abi_type.sub
395
+
396
+ return cls(
397
+ value_bit_size=value_bit_size,
398
+ data_byte_size=value_bit_size // 8,
399
+ frac_places=frac_places,
400
+ )
401
+
402
+
403
+ class SignedFixedEncoder(BaseFixedEncoder):
404
+ def bounds_fn(self, value_bit_size):
405
+ return compute_signed_fixed_bounds(self.value_bit_size, self.frac_places)
406
+
407
+ def encode_fn(self, value):
408
+ with decimal.localcontext(abi_decimal_context):
409
+ scaled_value = value * TEN**self.frac_places
410
+ integer_value = int(scaled_value)
411
+
412
+ unsigned_integer_value = integer_value % (2**self.value_bit_size)
413
+
414
+ return int_to_big_endian(unsigned_integer_value)
415
+
416
+ def encode(self, value):
417
+ self.validate_value(value)
418
+ base_encoded_value = self.encode_fn(value)
419
+
420
+ if value >= 0:
421
+ padded_encoded_value = zpad(base_encoded_value, self.data_byte_size)
422
+ else:
423
+ padded_encoded_value = fpad(base_encoded_value, self.data_byte_size)
424
+
425
+ return padded_encoded_value
426
+
427
+ @parse_type_str("fixed")
428
+ def from_type_str(cls, abi_type, registry):
429
+ value_bit_size, frac_places = abi_type.sub
430
+
431
+ return cls(
432
+ value_bit_size=value_bit_size,
433
+ frac_places=frac_places,
434
+ )
435
+
436
+
437
+ class PackedSignedFixedEncoder(SignedFixedEncoder):
438
+ @parse_type_str("fixed")
439
+ def from_type_str(cls, abi_type, registry):
440
+ value_bit_size, frac_places = abi_type.sub
441
+
442
+ return cls(
443
+ value_bit_size=value_bit_size,
444
+ data_byte_size=value_bit_size // 8,
445
+ frac_places=frac_places,
446
+ )
447
+
448
+
449
+ class AddressEncoder(Fixed32ByteSizeEncoder):
450
+ value_bit_size = 20 * 8
451
+ encode_fn = staticmethod(to_canonical_address)
452
+ is_big_endian = True
453
+
454
+ @classmethod
455
+ def validate_value(cls, value):
456
+ if not is_address(value):
457
+ cls.invalidate_value(value)
458
+
459
+ def validate(self):
460
+ super().validate()
461
+
462
+ if self.value_bit_size != 20 * 8:
463
+ raise ValueError("Addresses must be 160 bits in length")
464
+
465
+ @parse_type_str("address")
466
+ def from_type_str(cls, abi_type, registry):
467
+ return cls()
468
+
469
+
470
+ class PackedAddressEncoder(AddressEncoder):
471
+ data_byte_size = 20
472
+
473
+
474
+ class BytesEncoder(Fixed32ByteSizeEncoder):
475
+ is_big_endian = False
476
+
477
+ def validate_value(self, value):
478
+ if not is_bytes(value):
479
+ self.invalidate_value(value)
480
+
481
+ byte_size = self.value_bit_size // 8
482
+ if len(value) > byte_size:
483
+ self.invalidate_value(
484
+ value,
485
+ exc=ValueOutOfBounds,
486
+ msg=f"exceeds total byte size for bytes{byte_size} encoding",
487
+ )
488
+
489
+ @staticmethod
490
+ def encode_fn(value):
491
+ return value
492
+
493
+ @parse_type_str("bytes")
494
+ def from_type_str(cls, abi_type, registry):
495
+ return cls(value_bit_size=abi_type.sub * 8)
496
+
497
+
498
+ class PackedBytesEncoder(BytesEncoder):
499
+ @parse_type_str("bytes")
500
+ def from_type_str(cls, abi_type, registry):
501
+ return cls(
502
+ value_bit_size=abi_type.sub * 8,
503
+ data_byte_size=abi_type.sub,
504
+ )
505
+
506
+
507
+ class ByteStringEncoder(BaseEncoder):
508
+ is_dynamic = True
509
+
510
+ @classmethod
511
+ def validate_value(cls, value):
512
+ if not is_bytes(value):
513
+ cls.invalidate_value(value)
514
+
515
+ @classmethod
516
+ def encode(cls, value):
517
+ cls.validate_value(value)
518
+ value_length = len(value)
519
+
520
+ encoded_size = encode_uint_256(value_length)
521
+ padded_value = zpad_right(value, ceil32(value_length))
522
+
523
+ return encoded_size + padded_value
524
+
525
+ @parse_type_str("bytes")
526
+ def from_type_str(cls, abi_type, registry):
527
+ return cls()
528
+
529
+
530
+ class PackedByteStringEncoder(ByteStringEncoder):
531
+ is_dynamic = False
532
+
533
+ @classmethod
534
+ def encode(cls, value):
535
+ cls.validate_value(value)
536
+ return value
537
+
538
+
539
+ class TextStringEncoder(BaseEncoder):
540
+ is_dynamic = True
541
+
542
+ @classmethod
543
+ def validate_value(cls, value):
544
+ if not is_text(value):
545
+ cls.invalidate_value(value)
546
+
547
+ @classmethod
548
+ def encode(cls, value):
549
+ cls.validate_value(value)
550
+
551
+ value_as_bytes = codecs.encode(value, "utf8")
552
+ value_length = len(value_as_bytes)
553
+
554
+ encoded_size = encode_uint_256(value_length)
555
+ padded_value = zpad_right(value_as_bytes, ceil32(value_length))
556
+
557
+ return encoded_size + padded_value
558
+
559
+ @parse_type_str("string")
560
+ def from_type_str(cls, abi_type, registry):
561
+ return cls()
562
+
563
+
564
+ class PackedTextStringEncoder(TextStringEncoder):
565
+ is_dynamic = False
566
+
567
+ @classmethod
568
+ def encode(cls, value):
569
+ cls.validate_value(value)
570
+ return codecs.encode(value, "utf8")
571
+
572
+
573
+ class BaseArrayEncoder(BaseEncoder):
574
+ item_encoder = None
575
+
576
+ def validate(self):
577
+ super().validate()
578
+
579
+ if self.item_encoder is None:
580
+ raise ValueError("`item_encoder` may not be none")
581
+
582
+ def validate_value(self, value):
583
+ if not is_list_like(value):
584
+ self.invalidate_value(
585
+ value,
586
+ msg="must be list-like such as array or tuple",
587
+ )
588
+
589
+ for item in value:
590
+ self.item_encoder.validate_value(item)
591
+
592
+ def encode_elements(self, value):
593
+ self.validate_value(value)
594
+
595
+ item_encoder = self.item_encoder
596
+ if item_encoder is None:
597
+ raise AssertionError("`item_encoder` is None")
598
+ tail_chunks = tuple(item_encoder(i) for i in value)
599
+
600
+ items_are_dynamic = getattr(item_encoder, "is_dynamic", False)
601
+ if not items_are_dynamic or len(value) == 0:
602
+ return b"".join(tail_chunks)
603
+
604
+ head_length = 32 * len(value)
605
+ tail_offsets = (0,) + tuple(accumulate(map(len, tail_chunks[:-1])))
606
+ head_chunks = tuple(
607
+ encode_uint_256(head_length + offset) for offset in tail_offsets
608
+ )
609
+ return b"".join(head_chunks + tail_chunks)
610
+
611
+ @parse_type_str(with_arrlist=True)
612
+ def from_type_str(cls, abi_type, registry):
613
+ item_encoder = registry.get_encoder(abi_type.item_type.to_type_str())
614
+
615
+ array_spec = abi_type.arrlist[-1]
616
+ if len(array_spec) == 1:
617
+ # If array dimension is fixed
618
+ return SizedArrayEncoder(
619
+ array_size=array_spec[0],
620
+ item_encoder=item_encoder,
621
+ )
622
+ else:
623
+ # If array dimension is dynamic
624
+ return DynamicArrayEncoder(item_encoder=item_encoder)
625
+
626
+
627
+ class PackedArrayEncoder(BaseArrayEncoder):
628
+ array_size = None
629
+
630
+ def validate_value(self, value):
631
+ super().validate_value(value)
632
+
633
+ if self.array_size is not None and len(value) != self.array_size:
634
+ self.invalidate_value(
635
+ value,
636
+ exc=ValueOutOfBounds,
637
+ msg=f"value has {len(value)} items when {self.array_size} were "
638
+ "expected",
639
+ )
640
+
641
+ def encode(self, value):
642
+ encoded_elements = self.encode_elements(value)
643
+
644
+ return encoded_elements
645
+
646
+ @parse_type_str(with_arrlist=True)
647
+ def from_type_str(cls, abi_type, registry):
648
+ item_encoder = registry.get_encoder(abi_type.item_type.to_type_str())
649
+
650
+ array_spec = abi_type.arrlist[-1]
651
+ if len(array_spec) == 1:
652
+ return cls(
653
+ array_size=array_spec[0],
654
+ item_encoder=item_encoder,
655
+ )
656
+ else:
657
+ return cls(item_encoder=item_encoder)
658
+
659
+
660
+ class SizedArrayEncoder(BaseArrayEncoder):
661
+ array_size = None
662
+
663
+ def __init__(self, **kwargs):
664
+ super().__init__(**kwargs)
665
+
666
+ self.is_dynamic = self.item_encoder.is_dynamic
667
+
668
+ def validate(self):
669
+ super().validate()
670
+
671
+ if self.array_size is None:
672
+ raise ValueError("`array_size` may not be none")
673
+
674
+ def validate_value(self, value):
675
+ super().validate_value(value)
676
+
677
+ if len(value) != self.array_size:
678
+ self.invalidate_value(
679
+ value,
680
+ exc=ValueOutOfBounds,
681
+ msg=f"value has {len(value)} items when {self.array_size} were "
682
+ "expected",
683
+ )
684
+
685
+ def encode(self, value):
686
+ encoded_elements = self.encode_elements(value)
687
+
688
+ return encoded_elements
689
+
690
+
691
+ class DynamicArrayEncoder(BaseArrayEncoder):
692
+ is_dynamic = True
693
+
694
+ def encode(self, value):
695
+ encoded_size = encode_uint_256(len(value))
696
+ encoded_elements = self.encode_elements(value)
697
+ encoded_value = encoded_size + encoded_elements
698
+
699
+ return encoded_value