multicall<T> method

Future<List<T>> multicall<T>(
  1. String method,
  2. List<List> args, [
  3. Multicall? multicall,
  4. bool eagerError = false,
])

Multicall read-only constant method with args. Will use multiple https call unless multicall is provided.

If eagerError is true, returns the error immediately on the first error found.

Implementation

Future<List<T>> multicall<T>(
  String method,
  List<List<dynamic>> args, [
  Multicall? multicall,
  bool eagerError = false,
]) async {
  if (multicall != null) {
    final res = await multicall.aggregate(
      args
          .map(
            (e) => MulticallPayload.fromInterfaceFunction(
                address, interface, method, e),
          )
          .toList(),
    );
    final decoded = res.returnData
        .map((e) => interface.decodeFunctionResult(method, e))
        .toList();
    switch (T) {
      case List:
        return decoded as List<T>;
      case BigInt:
        return decoded.map((e) => BigInt.parse(e[0].toString())).toList()
            as List<T>;
      default:
        return decoded.map((e) => e[0]).toList() as List<T>;
    }
  } else {
    return Future.wait(
      Iterable<int>.generate(args.length).map(
        (e) => _call<T>(method, args[e]),
      ),
      eagerError: eagerError,
    );
  }
}