abiEncode method

  1. @override
EncoderResult abiEncode(
  1. AbiParameter params,
  2. List input
)
override

Encodes a array of arbitrary types to ABI-encoded bytes.

Implementation

@override
EncoderResult abiEncode(AbiParameter params, List<dynamic> input) {
  final param = _ABIUtils.toArrayType(params);
  final encodedParams = input.map((e) => param.item1.abiEncode(e)).toList();
  final dynamicItems =
      encodedParams.isNotEmpty && encodedParams.first.isDynamic;
  bool isDynamic = param.item2 == -1;
  if (!isDynamic && input.length != param.item2) {
    throw _ABIValidator.invalidArgrumentsLength;
  }
  if (isDynamic || dynamicItems) {
    final encode = _ABIUtils.encodeDynamicParams(encodedParams);
    if (isDynamic) {
      final length = const NumbersCoder()
          .abiEncode(AbiParameter.uint256, BigInt.from(encodedParams.length))
          .encoded;
      return EncoderResult(
          isDynamic: true,
          encoded: encodedParams.isEmpty ? length : [...length, ...encode]);
    }
    return EncoderResult(isDynamic: true, encoded: encode);
  }
  final resultBytes = encodedParams.map((e) => e.encoded);
  return EncoderResult(
      isDynamic: false, encoded: [for (final i in resultBytes) ...i]);
}