public async Task <IActionResult> GetByCustomerAsync()
        {
            var customerId = _requestContext.UserId;

            var walletResult = await _walletOperationsService.GetCustomerWalletAsync(customerId);

            switch (walletResult.Error)
            {
            case WalletsErrorCodes.None:

                var linkedWalletTask = _publicWalletLinkingService.GetLinkedWalletAddressAsync(customerId);

                var tokensStatisticsTask =
                    _operationsHistoryClient.StatisticsApi.GetTokensStatisticsForCustomerAsync(
                        new TokensStatisticsForCustomerRequest {
                    CustomerId = customerId
                });

                await Task.WhenAll(linkedWalletTask, tokensStatisticsTask);

                Money18?linkedWalletBalance = null;

                if (linkedWalletTask.Result.Status == PublicAddressStatus.Linked)
                {
                    var balance =
                        await _ethereumBridgeClient.Wallets.GetBalanceAsync(linkedWalletTask.Result.PublicAddress);

                    linkedWalletBalance = balance.Amount;
                }

                var response = new WalletResponseModel
                {
                    Balance                    = walletResult.Balance.ToDisplayString(),
                    ExternalBalance            = linkedWalletBalance?.ToDisplayString(),
                    AssetSymbol                = walletResult.AssetSymbol,
                    TotalEarned                = tokensStatisticsTask.Result.EarnedAmount.ToDisplayString(),
                    TotalSpent                 = tokensStatisticsTask.Result.BurnedAmount.ToDisplayString(),
                    IsWalletBlocked            = walletResult.IsWalletBlocked,
                    PrivateWalletAddress       = walletResult.Address,
                    PublicWalletAddress        = linkedWalletTask.Result.PublicAddress,
                    PublicAddressLinkingStatus = linkedWalletTask.Result.Status.ToLinkingStatus(),
                    StakedBalance              = walletResult.StakedBalance.ToDisplayString(),
                    TransitAccountAddress      = _settingsService.GetTransitAccountAddress(),
                };
                return(Ok(new List <WalletResponseModel> {
                    response
                }));

            case WalletsErrorCodes.InvalidCustomerId:
                throw LykkeApiErrorException.BadRequest(ApiErrorCodes.Service.InvalidCustomerId);

            case WalletsErrorCodes.CustomerWalletMissing:
                throw LykkeApiErrorException.BadRequest(ApiErrorCodes.Service.CustomerWalletMissing);

            default:
                throw new InvalidOperationException(
                          $"Unexpected error during Transfer for {_requestContext.UserId} - {walletResult.Error}");
            }
        }
Example #2
0
        public async Task <ActionResult <MessageModel> > SimulatePurchase(WalletRequestModel requestModel, ClaimsPrincipal currentUser, string username)
        {
            var userAuthenticate = await dbContext.Users.FirstOrDefaultAsync(x => x.Username == username);

            var    product      = requestModel.Product;
            var    reciever     = requestModel.Reciever;
            var    amount       = requestModel.Amount;
            Wallet wallet       = requestModel.Wallet;
            Wallet walletExists = null;
            WalletResponseModel  walletResponseModel = new WalletResponseModel();
            BCryptPasswordHasher _BCrypt             = new BCryptPasswordHasher();

            if (currentUser.HasClaim(c => c.Type == "Roles"))
            {
                if (userAuthenticate != null)
                {
                    try
                    {
                        walletExists = await dbContext.Wallets.FirstOrDefaultAsync(x => x.Iban == wallet.Iban);

                        if (walletExists != null && (wallet.CardNumber == walletExists.CardNumber && wallet.CardExpirationDate == walletExists.CardExpirationDate && _BCrypt.AuthenticateWalletCVV(wallet, walletExists)))
                        {
                            if (walletExists.CardExpirationDate < DateTime.Now)
                            {
                                responseMessage.Message = "Wallet Card is expired";
                                return(StatusCode(406, responseMessage));
                            }

                            return(await ValidatePurchaseAmountAndBankAccount(userAuthenticate, currentUser, walletExists, product, reciever, amount, _transactionsService));
                        }
                        else
                        {
                            responseMessage.Message = "Wallet not found! Invalid Credentials!";
                            return(StatusCode(404, responseMessage));
                        }
                    }
                    catch (NullReferenceException)
                    {
                        responseMessage.Message = "Wallet not found! Invalid Credentials!";
                        return(StatusCode(404, responseMessage));
                    }
                }
                else
                {
                    responseMessage.Message = "User not found!";
                    return(StatusCode(404, responseMessage));
                }
            }
            responseMessage.Message = "You are not autorized to do such actions!";
            return(StatusCode(403, responseMessage));
        }
Example #3
0
        public async Task <ActionResult <ICollection <WalletResponseModel> > > GetWalletsInfo(ClaimsPrincipal currentUser, string username)
        {
            if (currentUser.HasClaim(c => c.Type == "Roles"))
            {
                var userAuthenticate = await dbContext.Users.FirstOrDefaultAsync(x => x.Username == username);

                UserAccResponseModel userWallets = new UserAccResponseModel();

                if (userAuthenticate == null)
                {
                    responseMessage.Message = "User not found!";
                    return(StatusCode(404, responseMessage));
                }
                else
                {
                    foreach (var walletRef in dbContext.Wallets.Where(x => x.UserId == userAuthenticate.Id))
                    {
                        WalletResponseModel walletResponseModel = new WalletResponseModel();
                        var wallet = walletRef;
                        walletResponseModel.IBAN       = wallet.Iban;
                        walletResponseModel.Amount     = Math.Round(wallet.Amount, 2);
                        walletResponseModel.CardNumber = wallet.CardNumber;

                        if (walletResponseModel.CardNumber.StartsWith('5'))
                        {
                            walletResponseModel.CardBrand = "Master Card";
                        }
                        else
                        {
                            walletResponseModel.CardBrand = "Visa";
                        }

                        userWallets.UserWallets.Add(walletResponseModel);
                    }

                    if (userWallets.UserWallets.Count > 0)
                    {
                        return(StatusCode(200, userWallets.UserWallets.OrderBy(x => x.IBAN)));
                    }

                    responseMessage.Message = "You don't have a Wallet!";
                    return(StatusCode(400, responseMessage));
                }
            }
            else
            {
                responseMessage.Message = "You are not authorized to do such actions";
                return(StatusCode(403, responseMessage));
            }
        }