Exemple #1
0
        private async Task InitiateSwapAsync(Swap swap)
        {
            Log.Debug("Initiate swap {@swapId}", swap.Id);

            var soldCurrency = _account.Currencies.GetByName(swap.SoldCurrency);

            if (swap.Secret == null)
            {
                var secret = _account.Wallet
                             .GetDeterministicSecret(soldCurrency, swap.TimeStamp);

                swap.Secret = secret.SubArray(0, CurrencySwap.DefaultSecretSize);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecret);

                secret.Clear();
            }

            if (swap.SecretHash == null)
            {
                swap.SecretHash = CurrencySwap.CreateSwapSecretHash(swap.Secret);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecretHash);
            }

            WalletAddress walletAddress;

            if (swap.ToAddress == null)
            {
                walletAddress = await _account
                                .GetRedeemAddressAsync(swap.PurchasedCurrency)
                                .ConfigureAwait(false);

                swap.ToAddress = walletAddress.Address;

                RaiseSwapUpdated(swap, SwapStateFlags.Empty);
            }
            else
            {
                walletAddress = await _account
                                .GetAddressAsync(swap.PurchasedCurrency, swap.ToAddress)
                                .ConfigureAwait(false);
            }

            swap.RewardForRedeem = await GetRewardForRedeemAsync(walletAddress)
                                   .ConfigureAwait(false);

            RaiseSwapUpdated(swap, SwapStateFlags.Empty);

            _swapClient.SwapInitiateAsync(swap);
        }
Exemple #2
0
        private async Task InitiateSwapAsync(ClientSwap swap)
        {
            Log.Debug("Initiate swap {@swapId}", swap.Id);

            if (swap.Secret == null)
            {
                var secret = _account.Wallet
                             .GetDeterministicSecret(swap.SoldCurrency, swap.TimeStamp);

                swap.Secret = secret.SubArray(0, CurrencySwap.DefaultSecretSize);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecret);

                secret.Clear();
            }

            if (swap.SecretHash == null)
            {
                swap.SecretHash = CurrencySwap.CreateSwapSecretHash(swap.Secret);
                RaiseSwapUpdated(swap, SwapStateFlags.HasSecretHash);
            }

            if (swap.ToAddress == null)
            {
                swap.ToAddress = (await _account
                                  .GetRedeemAddressAsync(swap.PurchasedCurrency)
                                  .ConfigureAwait(false))
                                 .Address;
                RaiseSwapUpdated(swap, SwapStateFlags.Empty);
            }

            var purchasedCurrencyBalance = await _account
                                           .GetBalanceAsync(swap.PurchasedCurrency)
                                           .ConfigureAwait(false);

            swap.RewardForRedeem = purchasedCurrencyBalance.Available < swap.PurchasedCurrency.GetDefaultRedeemFee() &&
                                   !(swap.PurchasedCurrency is BitcoinBasedCurrency)
                ? swap.PurchasedCurrency.GetDefaultRedeemFee() * 2
                : 0;
            RaiseSwapUpdated(swap, SwapStateFlags.Empty);

            _swapClient.SwapInitiateAsync(swap);
        }
Exemple #3
0
        private async Task InitiateSwapAsync(
            Swap swap,
            CancellationToken cancellationToken = default)
        {
            Log.Debug("Initiate swap {@swapId}", swap.Id);

            var soldCurrency = _account.Currencies.GetByName(swap.SoldCurrency);

            if (swap.Secret == null)
            {
                var secret = _account.Wallet
                             .GetDeterministicSecret(soldCurrency, swap.TimeStamp);

                swap.Secret = secret.SubArray(0, CurrencySwap.DefaultSecretSize);

                await UpdateSwapAsync(swap, SwapStateFlags.HasSecret, cancellationToken)
                .ConfigureAwait(false);

                secret.Clear();
            }

            if (swap.SecretHash == null)
            {
                swap.SecretHash = CurrencySwap.CreateSwapSecretHash(swap.Secret);

                await UpdateSwapAsync(swap, SwapStateFlags.HasSecretHash, cancellationToken)
                .ConfigureAwait(false);
            }

            var redeemFromWalletAddress = swap.RedeemFromAddress != null
                ? await _account
                                          .GetAddressAsync(swap.PurchasedCurrency, swap.RedeemFromAddress, cancellationToken)
                                          .ConfigureAwait(false)
                : null;

            var purchasedCurrency = _account.Currencies.GetByName(swap.PurchasedCurrency);

            swap.RewardForRedeem = await RewardForRedeemHelper
                                   .EstimateAsync(
                account : _account,
                quotesProvider : _quotesProvider,
                feeCurrencyQuotesProvider : symbol => _marketDataRepository
                ?.OrderBookBySymbol(symbol)
                ?.TopOfBook(),
                redeemableCurrency : purchasedCurrency,
                redeemFromAddress : redeemFromWalletAddress,
                cancellationToken : cancellationToken)
                                   .ConfigureAwait(false);

            // select refund address for bitcoin based currency
            if (soldCurrency is BitcoinBasedConfig && swap.RefundAddress == null)
            {
                swap.RefundAddress = (await _account
                                      .GetCurrencyAccount <BitcoinBasedAccount>(soldCurrency.Name)
                                      .GetRefundAddressAsync(cancellationToken)
                                      .ConfigureAwait(false))
                                     ?.Address;
            }

            await UpdateSwapAsync(swap, SwapStateFlags.Empty, cancellationToken)
            .ConfigureAwait(false);

            _swapClient.SwapInitiateAsync(swap);
        }