Ejemplo n.º 1
0
        public static async Task <BitcornResponse> Withdraw(BitcornContext dbContext, IConfiguration configuration, User user, string cornAddy, decimal amount, string platform)
        {
            var cornResponse = new BitcornResponse();

            cornResponse.WalletAvailable = true;
            try
            {
                if (user.IsBanned)
                {
                    return(cornResponse);
                }
                if (user.UserWallet.Balance < amount)
                {
                    return(cornResponse);
                }

                if (user.UserWallet.IsLocked != null && user.UserWallet.IsLocked.Value)
                {
                    return(cornResponse);
                }

                if ((await TxUtils.ShouldLockWallet(dbContext, user, amount)))
                {
                    return(cornResponse);
                }


                var server = await dbContext.GetWalletServer(user.UserWallet);

                if (!server.Enabled || !server.WithdrawEnabled)
                {
                    cornResponse.WalletAvailable = false;
                    return(cornResponse);
                }

                string accessToken = await GetWalletServerAccessToken(configuration);

                //failed to fetch access token
                if (!CheckAccessTokenExists(accessToken))
                {
                    throw new UnauthorizedAccessException("Failed to fetch wallet server access token");
                }

                using (var client = new WalletClient(server.Endpoint, accessToken))
                {
                    var response = await client.SendToAddressAsync(cornAddy, amount);

                    if (!response.IsError)
                    {
                        string txId = response.GetParsedContent();
                        await DebitWithdrawTx(cornAddy, txId, user, server, amount, dbContext, platform, int.Parse(configuration["Config:EmptyUserId"]));

                        cornResponse.WalletObject = txId;
                    }
                    //we got an error, fetch the internal wallet error code and figure out what to do
                    else
                    {
                        //get wallet error response
                        var error = response.GetError();
                        cornResponse.ErrorCode      = error.Code;
                        cornResponse.DepositAddress = server.DepositAddress;
                        //invalid withdrawal address
                        if (error.Code == WalletErrorCodes.RPC_INVALID_ADDRESS_OR_KEY)
                        {
                            cornResponse.UserError = true;
                        }
                        //too much immature corn to complete this transaction at this time
                        else if (error.Code == WalletErrorCodes.RPC_WALLET_INSUFFICIENT_FUNDS)
                        {
                            cornResponse.WalletAvailable = false;
                        }
                        //wallet server was not reached
                        else if (error.Code == WalletErrorCodes.HTTP_ERROR)
                        {
                            cornResponse.WalletAvailable = false;
                        }
                        try
                        {
                            await BITCORNLogger.LogError(dbContext, new Exception("wallet withdraw failed"), JsonConvert.SerializeObject(new { error = error, amount, cornAddy }));
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(cornResponse);
        }