Last active
March 12, 2024 16:22
-
-
Save jchapuis/5b0edd14dff871fa2e83af00b13aa19c to your computer and use it in GitHub Desktop.
Prepare transfer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prepare(transferID: TransferID, transfer: Transfer): F[Branch.Vote[TransferFailure]] = { | |
if (accountID === transfer.origin) | |
Logger[F].debug(show"Preparing outgoing transfer $transferID: $transfer for account $accountID") >> | |
account.prepareOutgoingTransfer(transferID, transfer) | |
.onErrorRetryWithBackoff(Logger[F].warn(_)(show"Error preparing outgoing transfer $transferID, retrying in a bit")) | |
.onLeftRetryWithBackoff { case Account.PendingOutgoingTransfer => | |
Logger[F].warn(show"Account $accountID has a pending outgoing transfer, retrying in a bit") | |
}(retryParameters.onPendingTransfer) | |
.flatMap { | |
case Left(Account.Unknown) => Branch.Vote.Abort(TransferFailure.AccountNotFound(accountID)).pure[F] | |
case Left(InsufficientFunds(missing)) => Branch.Vote.Abort(TransferFailure.InsufficientFunds(missing)).pure[F] | |
case Left(Account.PendingOutgoingTransfer) => Branch.Vote.Abort(TransferFailure.OtherPendingTransfer).pure[F] | |
case Right(_) => Branch.Vote.Commit.pure[F] | |
} | |
else | |
Logger[F].debug(show"Preparing incoming $transferID: $transfer for account $accountID") >> | |
account.prepareIncomingTransfer(transferID, transfer) | |
.onErrorRetryWithBackoff(Logger[F].warn(_)(show"Error preparing incoming transfer $transferID, retrying in a bit")) | |
.map { | |
case Left(Account.Unknown) => Branch.Vote.Abort(TransferFailure.AccountNotFound(accountID)) | |
case Right(_) => Branch.Vote.Commit | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment