encode method
ABI-encode value as a 32-byte-aligned Uint8List.
For dynamic types, returns the tail; the caller places the offset in the head.
Implementation
@override
Uint8List encode(dynamic value) {
final list = value as List;
final length = fixedLength ?? list.length;
if (fixedLength != null && list.length != fixedLength) {
throw ArgumentError(
'$name expects $fixedLength elements, got ${list.length}');
}
if (!elementType.isDynamic) {
final parts = <Uint8List>[];
if (fixedLength == null) {
parts.add(_uint256(BigInt.from(length)));
}
for (var i = 0; i < length; i++) {
parts.add(elementType.encode(list[i]));
}
return _concat(parts);
}
// Dynamic elements: offsets in head, data in tail.
final heads = <Uint8List>[];
final tails = <Uint8List>[];
if (fixedLength == null) {
heads.add(_uint256(BigInt.from(length)));
}
var tailOffset = length * 32;
final encodedItems = <Uint8List>[];
for (var i = 0; i < length; i++) {
final encoded = elementType.encode(list[i]);
encodedItems.add(encoded);
}
for (var i = 0; i < length; i++) {
heads.add(_uint256(BigInt.from(tailOffset)));
tailOffset += encodedItems[i].length;
tails.add(encodedItems[i]);
}
return _concat([...heads, ...tails]);
}