run method

  1. @override
Future<void> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<void> run() async {
  final account = accountFromArgs(globalResults);
  final contractAddress = Felt.fromHexString(
    argResults?['contract-address'] as String,
  );

  final filePath = argResults?['file'] as String;

  // Read and parse the CSV file
  final input = File(filePath).openRead();
  final fields = await input
      .transform(utf8.decoder)
      .transform(const CsvToListConverter())
      .toList();

  // Create FunctionCall list from CSV data
  final transfers = <FunctionCall>[];
  for (var i = 0; i < fields.length; i++) {
    final row = fields[i];

    // Check if the row has exactly 2 columns
    if (row.length != 2) {
      throw FormatException(
        'Invalid format in CSV file at line ${i + 1}: Expected 2 columns, found ${row.length}.',
      );
    }

    final address = row[0] as String;
    final amountInEth = row[1] as num;
    final amountInWei = ethToWei(amountInEth.toDouble());

    // Validate address and amount formats (implement isValidAddress and isValidAmount according to your criteria)
    if (!isValidAddress(address)) {
      throw FormatException('Invalid data format at line ${i + 1}.');
    }

    transfers.add(
      FunctionCall(
        contractAddress: contractAddress,
        entryPointSelector: getSelectorByName('transfer'),
        calldata: [
          Felt.fromHexString(address),
          Felt(amountInWei),
          Felt.zero,
        ],
      ),
    );
  }

  stdout.writeln(transfers);

  // Execute the multi-call transaction
  final res = await account.execute(functionCalls: transfers);

  stdout.writeln(
    res.when(
      result: (result) => result,
      error: (error) => throw Exception(error),
    ),
  );
}