buildTransaction method
Builds a transaction.
Throws a CannotSerializeProtobufFile if there is a problem serializing the transaction.
Implementation
Future<IoTransaction> buildTransaction(
List<Txo> txos,
String? someChangeFellowship,
String? someChangeContract,
int? someChangeState,
Lock_Predicate predicateFundsToUnlock,
Lock lockForChange,
LockAddress recipientLockAddress,
int amount,
int fee,
Indices? someNextIndices,
KeyPair keyPair,
ValueTypeIdentifier typeIdentifier,
) async {
try {
final lockChange = await transactionBuilderApi.lockAddress(lockForChange);
final eitherIoTransaction =
await transactionBuilderApi.buildTransferAmountTransaction(
typeIdentifier,
txos,
predicateFundsToUnlock,
amount,
recipientLockAddress,
lockChange,
fee,
);
final ioTransaction = eitherIoTransaction.fold((l) => throw l, (r) => r);
bool nextIndicesExist = false;
if (someChangeFellowship != null &&
someChangeContract != null &&
someChangeState != null) {
nextIndicesExist = !(walletStateApi.getCurrentIndicesForFunds(
someChangeFellowship,
someChangeContract,
someChangeState,
) ==
null);
}
if (ioTransaction.outputs.length >= 2 && !nextIndicesExist) {
final lockAddress =
await transactionBuilderApi.lockAddress(lockForChange);
final vk = someNextIndices != null
? walletApi.deriveChildKeys(keyPair, someNextIndices)
: null;
walletStateApi.updateWalletState(
Encoding()
.encodeToBase58Check(lockForChange.predicate.writeToBuffer()),
AddressCodecs.encode(lockAddress),
vk != null ? "ExtendedEd25519" : null,
vk != null ? Encoding().encodeToBase58(vk.writeToBuffer()) : null,
someNextIndices!, // TODO(ultimaterex): Figure out why nullable is allowed but we don't have a null path
);
}
return ioTransaction;
} catch (e) {
throw CannotSerializeProtobufFile('Cannot write to file');
}
}