Exemplo n.º 1
0
        public async Task <IActionResult> GetBalancesByAssetId(string assetId)
        {
            var result = new List <WalletAssetBalanceModel>();

            var wallets = await _clientAccountService.GetWalletsByClientIdAsync(_requestContext.ClientId);

            var clientKeys = await _hftInternalService.GetKeysAsync(_requestContext.ClientId);

            foreach (var wallet in wallets)
            {
                var balance = await _balancesClient.GetClientBalanceByAssetId(
                    new ClientBalanceByAssetIdModel
                {
                    ClientId = wallet.Type == TradingWalletType ? _requestContext.ClientId : wallet.Id,
                    AssetId  = assetId
                });

                result.Add(new WalletAssetBalanceModel
                {
                    Id          = wallet.Id,
                    Type        = wallet.Type,
                    Name        = wallet.Name,
                    Description = wallet.Description,
                    Balances    = balance != null ? ClientBalanceResponseModel.Create(balance) : new ClientBalanceResponseModel {
                        AssetId = assetId, Balance = 0, Reserved = 0
                    },
                    ApiKey = clientKeys.FirstOrDefault(x => x.Wallet == wallet.Id)?.Key
                });
            }

            return(Ok(result));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetBalancesByAssetId(string assetId)
        {
            var result = new List <WalletAssetBalanceModel>();

            var walletsTask         = _clientAccountService.Wallets.GetClientWalletsFilteredAsync(_requestContext.ClientId, owner: OwnerType.Spot);
            var clientKeysTask      = _hftInternalService.Keys.GetKeys(_requestContext.ClientId);
            var availableAssetsTask = _assetsHelper.GetSetOfAssetsAvailableToClientAsync(_requestContext.ClientId, _requestContext.PartnerId);
            await Task.WhenAll(walletsTask, clientKeysTask);

            var wallets         = walletsTask.Result;
            var clientKeys      = clientKeysTask.Result;
            var availableAssets = availableAssetsTask.Result;

            if (!availableAssets.Contains(assetId))
            {
                return(NotFound());
            }

            foreach (var wallet in wallets)
            {
                var balance = await _balancesClient.GetClientBalanceByAssetId(
                    new ClientBalanceByAssetIdModel
                {
                    ClientId = wallet.Type == WalletType.Trading ? _requestContext.ClientId : wallet.Id,
                    AssetId  = assetId
                });

                result.Add(new WalletAssetBalanceModel
                {
                    Id          = wallet.Id,
                    Type        = wallet.Type.ToString(),
                    Name        = wallet.Name,
                    Description = wallet.Description,
                    Balances    = balance != null ? ClientBalanceResponseModel.Create(balance) : new ClientBalanceResponseModel {
                        AssetId = assetId, Balance = 0, Reserved = 0
                    },
                    ApiKey = clientKeys.FirstOrDefault(x => x.WalletId == wallet.Id)?.ApiKey
                });
            }

            return(Ok(result));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Cashout([FromBody] CreateCashoutRequest cmd, [FromQuery] Guid?id)
        {
            if (string.IsNullOrWhiteSpace(cmd.DestinationAddress) || string.IsNullOrWhiteSpace(cmd.AssetId) || cmd.Volume == 0m)
            {
                throw LykkeApiErrorException.BadRequest(LykkeApiErrorCodes.Service.InvalidInput);
            }

            var asset = await _assetsServiceWithCache.TryGetAssetAsync(cmd.AssetId);

            if (asset == null)
            {
                return(NotFound($"Asset '{cmd.AssetId}' not found."));
            }

            var balance = await _balancesClient.GetClientBalanceByAssetId(new ClientBalanceByAssetIdModel(cmd.AssetId, _requestContext.ClientId));

            var cashoutSettings = await _clientAccountClient.ClientSettings.GetCashOutBlockSettingsAsync(_requestContext.ClientId);

            var kycStatus = await _kycStatusService.GetKycStatusAsync(_requestContext.ClientId);

            if (_baseSettings.EnableTwoFactor)
            {
                try
                {
                    if ((await _confirmationCodesClient.Google2FaIsClientBlacklistedAsync(_requestContext.ClientId)).IsClientBlacklisted)
                    {
                        throw LykkeApiErrorException.Forbidden(LykkeApiErrorCodes.Service.SecondFactorCheckForbiden);
                    }
                }
                catch (ApiException e)
                {
                    if (e.StatusCode == HttpStatusCode.BadRequest)
                    {
                        throw LykkeApiErrorException.Forbidden(LykkeApiErrorCodes.Service.TwoFactorRequired);
                    }
                }
            }

            var operationId = id ?? Guid.NewGuid();

            var cashoutCommand = new CreateCashoutCommand
            {
                OperationId                 = operationId,
                DestinationAddress          = cmd.DestinationAddress,
                DestinationAddressExtension = cmd.DestinationAddressExtension,
                Volume = cmd.Volume,
                Asset  = new AssetCashoutModel
                {
                    Id              = asset.Id,
                    DisplayId       = asset.DisplayId,
                    MultiplierPower = asset.MultiplierPower,
                    AssetAddress    = asset.AssetAddress,
                    Accuracy        = asset.Accuracy,
                    BlockchainIntegrationLayerId = asset.BlockchainIntegrationLayerId,
                    Blockchain           = asset.Blockchain.ToString(),
                    Type                 = asset.Type?.ToString(),
                    IsTradable           = asset.IsTradable,
                    IsTrusted            = asset.IsTrusted,
                    KycNeeded            = asset.KycNeeded,
                    BlockchainWithdrawal = asset.BlockchainWithdrawal,
                    CashoutMinimalAmount = (decimal)asset.CashoutMinimalAmount,
                    LowVolumeAmount      = (decimal?)asset.LowVolumeAmount ?? 0,
                    LykkeEntityId        = asset.LykkeEntityId
                },
                Client = new ClientCashoutModel
                {
                    Id               = new Guid(_requestContext.ClientId),
                    Balance          = balance?.Balance ?? 0,
                    CashOutBlocked   = cashoutSettings.CashOutBlocked,
                    KycStatus        = kycStatus.ToString(),
                    ConfirmationType = "google"
                },
                GlobalSettings = new GlobalSettingsCashoutModel
                {
                    MaxConfirmationAttempts = _baseSettings.MaxTwoFactorConfirmationAttempts,
                    TwoFactorEnabled        = _baseSettings.EnableTwoFactor,
                    CashOutBlocked          = false, // TODO
                    FeeSettings             = new FeeSettingsCashoutModel
                    {
                        TargetClients = new Dictionary <string, string>
                        {
                            { "Cashout", _feeSettings.TargetClientId.Cashout }
                        }
                    }
                }
            };

            _cqrsEngine.SendCommand(cashoutCommand, "apiv2", OperationsBoundedContext.Name);

            return(Created(Url.Action("Get", new { operationId }), operationId));
        }
Exemplo n.º 4
0
        public async Task <ValidationResult> ValidateLimitOrderAsync(string walletId, string assetPairId, OrderAction side, decimal price, decimal volume)
        {
            if (price <= 0)
            {
                return(new ValidationResult
                {
                    Message = ErrorMessages.LessThanZero(nameof(price)),
                    FieldName = nameof(price)
                });
            }

            if (volume <= 0)
            {
                return(new ValidationResult
                {
                    Message = ErrorMessages.LessThanZero(nameof(volume)),
                    FieldName = nameof(volume)
                });
            }

            var assetPair = await _assetsService.TryGetAssetPairAsync(assetPairId);

            if (assetPair == null)
            {
                return(new ValidationResult
                {
                    Message = ErrorMessages.AssetPairNotFound,
                    FieldName = nameof(assetPairId)
                });
            }

            if (volume < (decimal)assetPair.MinVolume)
            {
                return(new ValidationResult
                {
                    Message = ErrorMessages.MustBeGreaterThan(nameof(volume), assetPair.MinVolume.ToString(CultureInfo.InvariantCulture)),
                    FieldName = nameof(volume)
                });
            }

            decimal totalVolume;
            string  asset;

            if (side == OrderAction.Buy)
            {
                asset       = assetPair.QuotingAssetId;
                totalVolume = price * volume;
            }
            else
            {
                asset       = assetPair.BaseAssetId;
                totalVolume = volume;
            }

            var assetBalance = await _balancesClient.GetClientBalanceByAssetId(
                new ClientBalanceByAssetIdModel
            {
                ClientId = walletId,
                AssetId  = asset
            });

            if (assetBalance == null || assetBalance.Balance - assetBalance.Reserved < totalVolume)
            {
                return(new ValidationResult
                {
                    Message = ErrorMessages.NotEnoughFunds,
                    FieldName = nameof(volume)
                });
            }

            return(null);
        }