private async void RefundTimeReachedEventHandler( Swap swap, CancellationToken cancellationToken = default) { Log.Debug("Refund time reached for swap {@swapId}", swap.Id); try { var isRefundedResult = await NYXSwapRefundedHelper.IsRefundedAsync( swap : swap, currency : NYX, tezos : Xtz, attempts : MaxRefundCheckAttempts, attemptIntervalInSec : RefundCheckAttemptIntervalInSec, cancellationToken : cancellationToken) .ConfigureAwait(false); if (!isRefundedResult.HasError) { if (isRefundedResult.Value) { RefundConfirmedEventHandler(swap, swap.RefundTx, cancellationToken); } else { await RefundAsync(swap, cancellationToken) .ConfigureAwait(false); } } } catch (Exception e) { Log.Error(e, "Error in refund time reached handler"); } }
public override async Task RedeemAsync( Swap swap, CancellationToken cancellationToken = default) { var nyx = NYX; var secretResult = await NYXSwapRedeemedHelper .IsRedeemedAsync( swap : swap, currency : nyx, tezos : Xtz, attempts : MaxRedeemCheckAttempts, attemptIntervalInSec : RedeemCheckAttemptIntervalInSec, cancellationToken : cancellationToken) .ConfigureAwait(false); if (!secretResult.HasError && secretResult.Value != null) { RedeemConfirmedEventHandler(swap, null, cancellationToken); return; } if (swap.StateFlags.HasFlag(SwapStateFlags.IsRedeemBroadcast) && swap.RedeemTx != null && swap.RedeemTx.CreationTime != null && swap.RedeemTx.CreationTime.Value.ToUniversalTime() + TimeSpan.FromMinutes(5) > DateTime.UtcNow) { // redeem already broadcast TrackTransactionConfirmationAsync( swap: swap, currency: nyx, txId: swap.RedeemTx.Id, confirmationHandler: RedeemConfirmedEventHandler, cancellationToken: cancellationToken) .FireAndForget(); return; } // check already refunded by initiator if (swap.IsAcceptor && swap.TimeStamp.ToUniversalTime().AddSeconds(DefaultInitiatorLockTimeInSeconds) < DateTime.UtcNow) { var isRefundedByParty = await NYXSwapRefundedHelper .IsRefundedAsync(swap, nyx, Xtz, cancellationToken) .ConfigureAwait(false); if (isRefundedByParty != null && !isRefundedByParty.HasError && isRefundedByParty.Value) { swap.StateFlags |= SwapStateFlags.IsUnsettled; RaiseSwapUpdated(swap, SwapStateFlags.IsUnsettled); return; } } if (swap.IsInitiator) { var redeemDeadline = swap.TimeStamp.ToUniversalTime().AddSeconds(DefaultAcceptorLockTimeInSeconds) - RedeemTimeReserve; if (DateTime.UtcNow > redeemDeadline) { Log.Error("Redeem dedline reached for swap {@swap}", swap.Id); return; } } Log.Debug("Create redeem for swap {@swapId}", swap.Id); var walletAddress = (await NYXAccount .GetUnspentAddressesAsync( toAddress: swap.ToAddress, amount: 0, fee: 0, feePrice: 0, feeUsagePolicy: FeeUsagePolicy.EstimatedFee, addressUsagePolicy: AddressUsagePolicy.UseOnlyOneAddress, transactionType: BlockchainTransactionType.SwapRedeem, cancellationToken: cancellationToken) .ConfigureAwait(false)) .FirstOrDefault(); if (walletAddress == null) { Log.Error("Insufficient funds for redeem"); return; } var redeemTx = new TezosTransaction { Currency = nyx, CreationTime = DateTime.UtcNow, From = walletAddress.Address, To = nyx.SwapContractAddress, Amount = 0, Fee = nyx.RedeemFee + nyx.RevealFee, GasLimit = nyx.RedeemGasLimit, StorageLimit = nyx.RedeemStorageLimit, Params = RedeemParams(swap), UseDefaultFee = true, Type = BlockchainTransactionType.Output | BlockchainTransactionType.SwapRedeem }; var signResult = await SignTransactionAsync(redeemTx, cancellationToken) .ConfigureAwait(false); if (!signResult) { Log.Error("Transaction signing error"); return; } swap.RedeemTx = redeemTx; swap.StateFlags |= SwapStateFlags.IsRedeemSigned; RaiseSwapUpdated(swap, SwapStateFlags.IsRedeemSigned); await BroadcastTxAsync(swap, redeemTx, cancellationToken) .ConfigureAwait(false); swap.RedeemTx = redeemTx; swap.StateFlags |= SwapStateFlags.IsRedeemBroadcast; RaiseSwapUpdated(swap, SwapStateFlags.IsRedeemBroadcast); TrackTransactionConfirmationAsync( swap: swap, currency: nyx, txId: redeemTx.Id, confirmationHandler: RedeemConfirmedEventHandler, cancellationToken: cancellationToken) .FireAndForget(); }