createSimpleTransactionFromParams method

  1. @override
Future<Either<SimpleTransactionAlgebraError, IoTransaction>> createSimpleTransactionFromParams({
  1. required String keyfile,
  2. required String password,
  3. required String fromFellowship,
  4. required String fromContract,
  5. int? someFromState,
  6. String? someChangeFellowship,
  7. String? someChangeContract,
  8. int? someChangeState,
  9. LockAddress? someToAddress,
  10. String? someToFellowship,
  11. String? someToContract,
  12. required int amount,
  13. required int fee,
  14. required ValueTypeIdentifier tokenType,
})
override

Implementation

@override
Future<Either<SimpleTransactionAlgebraError, IoTransaction>>
    createSimpleTransactionFromParams({
  required String keyfile,
  required String password,
  required String fromFellowship,
  required String fromContract,
  int? someFromState,
  String? someChangeFellowship,
  String? someChangeContract,
  int? someChangeState,
  LockAddress? someToAddress,
  String? someToFellowship,
  String? someToContract,
  required int amount,
  required int fee,
  required ValueTypeIdentifier tokenType,
}) async {
  try {
    final keyPair = await walletManagementUtils.loadKeys(keyfile, password);
    final someCurrentIndices = walletStateApi.getCurrentIndicesForFunds(
        fromFellowship, fromContract, someFromState);
    final predicateFundsToUnlock = someCurrentIndices != null
        ? walletStateApi.getLockByIndex(someCurrentIndices)
        : null;

    Indices? someNextIndices;
    if (someChangeFellowship != null &&
        someChangeContract != null &&
        someChangeState != null) {
      someNextIndices = walletStateApi.getCurrentIndicesForFunds(
          someChangeFellowship, someChangeContract, someChangeState);
    } else {
      someNextIndices =
          walletStateApi.getNextIndicesForFunds(fromFellowship, fromContract);
    }

    Lock? changeLock;
    if (someNextIndices != null) {
      changeLock = walletStateApi.getLock(
          fromFellowship, fromContract, someNextIndices.z);
    }

    final fromAddress = predicateFundsToUnlock != null
        ? await transactionBuilderApi
            .lockAddress(Lock(predicate: predicateFundsToUnlock))
        : null;

    List<Txo>? txos;
    try {
      txos = await utxoAlgebra.queryUtxo(fromAddress: fromAddress!);
    } catch (_) {
      throw CreateTxError('Problem contacting network');
    }

    txos = txos
        .where((x) =>
            !x.transactionOutput.value.hasTopl() &&
            !x.transactionOutput.value.hasUpdateProposal())
        .toList();

    Either<EncodingError, LockAddress> toAddressOpt;
    if (someToAddress != null) {
      toAddressOpt = Either.right(someToAddress);
    } else if (someToFellowship != null && someToContract != null) {
      final addrStr =
          walletStateApi.getAddress(someToFellowship, someToContract, null);
      toAddressOpt = AddressCodecs.decode(addrStr!);
    } else {
      toAddressOpt = Either.left(InvalidInputString());
    }

    if (txos.isEmpty) {
      throw CreateTxError('No LVL txos found');
    } else if (changeLock != null && toAddressOpt.isRight) {
      return (await buildTransaction(
        txos,
        someChangeFellowship,
        someChangeContract,
        someChangeState,
        predicateFundsToUnlock!,
        changeLock,
        toAddressOpt.get(),
        amount,
        fee,
        someNextIndices,
        keyPair.get(),
        tokenType,
      ))
          .withResult((p0) => Either.right(p0));
    } else if (changeLock == null) {
      throw CreateTxError('Unable to generate change lock');
    } else {
      throw CreateTxError('Unable to derive recipient address');
    }
  } catch (e) {
    if (e is SimpleTransactionAlgebraError) {
      return Either.left(e);
    } else {
      return Either.left(UnexpectedError(e.toString()));
    }
  }
}