encode method

  1. @override
Uint8List encode(
  1. dynamic value
)
override

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 BigInt v;
  if (value is BigInt) {
    v = value;
  } else if (value is int) {
    v = BigInt.from(value);
  } else {
    throw ArgumentError('Expected int or BigInt, got ${value.runtimeType}');
  }

  if (v < BigInt.zero) {
    throw ArgumentError('uint$bits cannot encode negative value: $v');
  }
  final maxVal = (BigInt.one << bits) - BigInt.one;
  if (v > maxVal) {
    throw ArgumentError('Value $v exceeds uint$bits max ($maxVal)');
  }

  return _bigIntToBytes32(v);
}