public static bool CheckWalletExist(SqliteDatabaseService sqliteDatabaseService, ulong userId, out WalletAccount walletAccount)
 {
     using (var context = sqliteDatabaseService.GetContext(true))
     {
         walletAccount = context.WalletAccountTable.FirstOrDefault(a => a.UserId == userId);
         return(walletAccount != default);
     }
 }
Exemple #2
0
        public async Task RegisterWalletAsync([Summary("Wallet address.")][Remainder]
                                              string address)
        {
            try
            {
                if (!WalletUtilities.CheckWalletAddress(address))
                {
                    await ReplyAsync($"Address is not a valid {ConfigService.CoinName} address!").ConfigureAwait(false);
                    await AddReactionAsync("❌").ConfigureAwait(false);

                    return;
                }

                if (WalletUtilities.CheckWalletExist(SqliteDatabaseService, Context.User.Id, out var walletAccount))
                {
                    using (var sqliteContext = SqliteDatabaseService.GetContext())
                    {
                        sqliteContext.WalletAccountTable.Attach(walletAccount);
                        walletAccount.RegisteredWalletAddress = address;
                        sqliteContext.WalletAccountTable.Update(walletAccount);
                        await sqliteContext.SaveChangesAsync().ConfigureAwait(false);
                    }

                    await ReplyDMAsync("Successfully updated your wallet!").ConfigureAwait(false);
                    await AddReactionAsync("✅").ConfigureAwait(false);
                }
                else
                {
                    var newAccount = await RpcService.WalletRpcClient.CreateAccountAsync(Context.User.Id.ToString()).ConfigureAwait(false);

                    if (newAccount == null)
                    {
                        throw new Exception("Unable to create account.");
                    }

                    using (var sqliteContext = SqliteDatabaseService.GetContext())
                    {
                        var wallet = new WalletAccount
                        {
                            UserId                  = Context.User.Id,
                            AccountIndex            = newAccount.AccountIndex,
                            RegisteredWalletAddress = address,
                            TipWalletAddress        = newAccount.Address
                        };

                        sqliteContext.WalletAccountTable.Add(wallet);
                        await sqliteContext.SaveChangesAsync().ConfigureAwait(false);
                    }

                    var embed = new EmbedBuilder()
                                .WithColor(Color.Orange)
                                .WithTitle("Successfully registered your wallet!")
                                .WithDescription($"Deposit {ConfigService.CoinSymbol} to start tipping!\n\nAddress: `{newAccount.Address}`");

                    await ReplyDMAsync(embed.Build()).ConfigureAwait(false);
                    await AddReactionAsync("✅").ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                await CatchError(ex).ConfigureAwait(false);
            }
        }