Exemplo n.º 1
0
        public static async Task <BitcornResponse> CreateCornaddy(BitcornContext dbContext, UserWallet userWallet, IConfiguration configuration)
        {
            var cornResponse = new BitcornResponse();

            cornResponse.WalletAvailable = true;
            try
            {
                var index = await GetWalletIndexAsync(dbContext);

                var server = await dbContext.GetWalletServer(index);

                if (server == null)
                {
                    cornResponse.WalletAvailable = false;
                    return(cornResponse);
                }

                //wallet server has been disabled, find the first server that has been enabled
                if (!server.Enabled)
                {
                    server = await dbContext.WalletServer.FirstOrDefaultAsync(s => s.Enabled);

                    //server is still null, return
                    if (server == null)
                    {
                        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");
                }
                await CreateCornaddyInternal(cornResponse, dbContext, server, userWallet, accessToken);
            }
            catch (Exception e)
            {
                throw e;
            }
            return(cornResponse);
        }
Exemplo n.º 2
0
        static async Task CreateCornaddyInternal(BitcornResponse cornResponse, BitcornContext dbContext, WalletServer walletServer, UserWallet userWallet, string accessToken)
        {
            using (var client = new WalletClient(walletServer.Endpoint, accessToken))
            {
                var response = await client.GetNewAddressAsync("main");

                if (!response.IsError)
                {
                    var address = response.GetParsedContent();
                    userWallet.CornAddy     = address;
                    userWallet.WalletServer = walletServer.Index;

                    cornResponse.WalletObject = address;
                    await dbContext.SaveAsync();
                }
                //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.WalletAvailable = false;
                }
            }
        }
Exemplo n.º 3
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);
                }

                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(txId, user, server, amount, dbContext, platform);

                        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();

                        //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;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(cornResponse);
        }