build method

Future<RawTransaction> build(
  1. String func,
  2. List<String> typeArgs,
  3. List args
)

Implementation

Future<RawTransaction> build(String func, List<String> typeArgs, List<dynamic> args) async {
  func = func.replaceAll(RegExp(r"^0[xX]0*"), "0x");
  final funcNameParts = func.split("::");
  if (funcNameParts.length != 3) {
    throw ArgumentError(
      "'func' needs to be a fully qualified function name in format <address>::<module>::<function>, e.g. 0x1::coins::transfer",
    );
  }

  final addr = funcNameParts[0];
  final module = funcNameParts[1];

  final abiMap = await fetchABI(addr);
  if (!abiMap.containsKey(func)) {
    throw ArgumentError("$func doesn't exist.");
  }

  final funcAbi = abiMap[func];

  // Remove all `signer` and `&signer` from argument list because the Move VM injects those arguments. Clients do not
  // need to care about those args. `signer` and `&signer` are required be in the front of the argument list. But we
  // just loop through all arguments and filter out `signer` and `&signer`.
  final originalArgs = (funcAbi["params"] as List).where((param) => param != "signer" && param != "&signer").toList();

  // Convert string arguments to TypeArgumentABI
  final typeArgABIs = <ArgumentABI>[];
  for (var i = 0; i < originalArgs.length; i++) {
    typeArgABIs.add(ArgumentABI("var$i", TypeTagParser(originalArgs[i]).parseTypeTag()));
  }

  final genericTypeParams = funcAbi["generic_type_params"];
  final genericTypeParamsList = <TypeArgumentABI>[];
  for (var i = 0; i < genericTypeParams.length; i++) {
    genericTypeParamsList.add(TypeArgumentABI("$i"));
  }

  final entryFunctionABI = EntryFunctionABI(
    funcAbi["name"],
    ModuleId.fromStr("$addr::$module"),
    "",
    genericTypeParamsList,
    typeArgABIs,
  );

  final sender = builderConfig.sender;
  final senderAddress = sender is AccountAddress ? HexString.fromUint8Array(sender.address).hex() : sender;
  final sequenceNumber = builderConfig.sequenceNumber ?? BigInt.parse((await aptosClient.getAccount(senderAddress)).sequenceNumber);
  final chainId = builderConfig.chainId ?? await aptosClient.getChainId();
  final gasUnitPrice = builderConfig.gasUnitPrice ?? BigInt.from(await aptosClient.estimateGasPrice());

  final config = ABIBuilderConfig(
    sender: sender,
    sequenceNumber: sequenceNumber,
    gasUnitPrice: gasUnitPrice,
    chainId: chainId,
    maxGasAmount: builderConfig.maxGasAmount,
    expSecFromNow: builderConfig.expSecFromNow
  );
  final builderABI = TransactionBuilderABI([bcsToBytes(entryFunctionABI)], builderConfig: config);

  return builderABI.build(func, typeArgs, args);
}