Exemple #1
0
        public void AddFromCurrency(FromRelation relation)
        {
            var currency = _context.Currencies.Where(c => c.CurrencyId == relation.CurrencyId).FirstOrDefault();
            var fromCur  = new FromCurrency()
            {
                FromCurrencyId = relation.CurrencyId,
                FromRelation   = relation,
                FromRelationId = relation.CurrencyRelationId,
                FullName       = currency.FullName,
                IsoCode        = currency.IsoCode
            };

            _context.FromCurrencies.Add(fromCur);
        }
Exemple #2
0
 protected override void Execute(CodeActivityContext context)
 {
     strFCur = FromCurrency.Get(context);
     strTCur = ToCurrency.Get(context);
     Rate.Set(context, GetExchangeRate());
 }
Exemple #3
0
        private async Task <Error> ConvertAsync()
        {
            try
            {
                var account         = App.Account;
                var currencyAccount = account
                                      .GetCurrencyAccount <ILegacyCurrencyAccount>(FromCurrency.Name);

                var fromWallets = (await currencyAccount
                                   .GetUnspentAddressesAsync(
                                       toAddress: null,
                                       amount: Amount,
                                       fee: 0,
                                       feePrice: await FromCurrency.GetDefaultFeePriceAsync(),
                                       feeUsagePolicy: FeeUsagePolicy.EstimatedFee,
                                       addressUsagePolicy: AddressUsagePolicy.UseMinimalBalanceFirst,
                                       transactionType: BlockchainTransactionType.SwapPayment))
                                  .ToList();

                foreach (var fromWallet in fromWallets)
                {
                    if (fromWallet.Currency != FromCurrency.Name)
                    {
                        fromWallet.Currency = FromCurrency.Name;
                    }
                }

                // check balances
                var errors = await BalanceChecker.CheckBalancesAsync(App.Account, fromWallets);

                if (errors.Any())
                {
                    return(new Error(Errors.SwapError, GetErrorsDescription(errors)));
                }

                if (Amount == 0)
                {
                    return(new Error(Errors.SwapError, Resources.CvZeroAmount));
                }

                if (Amount > 0 && !fromWallets.Any())
                {
                    return(new Error(Errors.SwapError, Resources.CvInsufficientFunds));
                }

                var symbol = App.SymbolsProvider
                             .GetSymbols(App.Account.Network)
                             .SymbolByCurrencies(FromCurrency, ToCurrency);

                var baseCurrency = App.Account.Currencies.GetByName(symbol.Base);
                var side         = symbol.OrderSideForBuyCurrency(ToCurrency);
                var terminal     = App.Terminal;
                var price        = EstimatedPrice;
                var orderPrice   = EstimatedOrderPrice;

                if (price == 0)
                {
                    return(new Error(Errors.NoLiquidity, Resources.CvNoLiquidity));
                }

                var qty = AmountHelper.AmountToQty(side, Amount, price, baseCurrency.DigitsMultiplier);

                if (qty < symbol.MinimumQty)
                {
                    var minimumAmount = AmountHelper.QtyToAmount(side, symbol.MinimumQty, price, FromCurrency.DigitsMultiplier);
                    var message       = string.Format(CultureInfo.InvariantCulture, Resources.CvMinimumAllowedQtyWarning, minimumAmount, FromCurrency.Name);

                    return(new Error(Errors.SwapError, message));
                }

                var order = new Order
                {
                    Symbol          = symbol.Name,
                    TimeStamp       = DateTime.UtcNow,
                    Price           = orderPrice,
                    Qty             = qty,
                    Side            = side,
                    Type            = OrderType.FillOrKill,
                    FromWallets     = fromWallets.ToList(),
                    MakerNetworkFee = EstimatedMakerNetworkFee
                };

                await order.CreateProofOfPossessionAsync(account);

                terminal.OrderSendAsync(order);

                // wait for swap confirmation
                var timeStamp = DateTime.UtcNow;

                while (DateTime.UtcNow < timeStamp + SwapTimeout)
                {
                    await Task.Delay(SwapCheckInterval);

                    var currentOrder = terminal.Account.GetOrderById(order.ClientOrderId);

                    if (currentOrder == null)
                    {
                        continue;
                    }

                    if (currentOrder.Status == OrderStatus.Pending)
                    {
                        continue;
                    }

                    if (currentOrder.Status == OrderStatus.PartiallyFilled || currentOrder.Status == OrderStatus.Filled)
                    {
                        var swap = (await terminal.Account
                                    .GetSwapsAsync())
                                   .FirstOrDefault(s => s.OrderId == currentOrder.Id);

                        if (swap == null)
                        {
                            continue;
                        }

                        return(null);
                    }

                    if (currentOrder.Status == OrderStatus.Canceled)
                    {
                        return(new Error(Errors.PriceHasChanged, Resources.SvPriceHasChanged));
                    }

                    if (currentOrder.Status == OrderStatus.Rejected)
                    {
                        return(new Error(Errors.OrderRejected, Resources.SvOrderRejected));
                    }
                }

                return(new Error(Errors.TimeoutReached, Resources.SvTimeoutReached));
            }
            catch (Exception e)
            {
                Log.Error(e, "Conversion error");

                return(new Error(Errors.SwapError, Resources.CvConversionError));
            }
        }
Exemple #4
0
 public override string ToString()
 {
     return($" The predicted currency exchange from {FromCurrency.ToUpper()} "
            + $" to {ToCurrency.ToUpper()} for {Date} is {Rate}. ");
 }