public static async Task <CornTx> DebitWithdrawTx(string cornaddy, string txId, User user, WalletServer server, decimal amount, BitcornContext dbContext, string platform, int emptyUser) { if (user.UserWallet.Balance >= amount) { var sql = new StringBuilder(); sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), amount, '-', nameof(UserWallet.UserId), user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(WalletServer), nameof(WalletServer.ServerBalance), amount, '-', nameof(WalletServer.Id), server.Id)); await DbOperations.ExecuteSqlRawAsync(dbContext, sql.ToString()); var log = new CornTx(); log.BlockchainTxId = txId; log.Amount = amount; log.Timestamp = DateTime.Now; log.TxType = "$withdraw"; log.TxGroupId = Guid.NewGuid().ToString(); log.Platform = platform; log.ReceiverId = emptyUser; log.SenderId = user.UserId; log.CornAddy = cornaddy; var price = log.UsdtPrice = await ProbitApi.GetCornPriceAsync(dbContext); log.TotalUsdtValue = price * amount; dbContext.CornTx.Add(log); await dbContext.SaveAsync(); return(log); } return(null); }
public static CornTx VerifyTx(User from, User to, decimal amount, string platform, string txType, string txId) { if (amount <= 0) { return(null); } if (from == null || to == null) { return(null); } if (from.IsBanned || to.IsBanned) { return(null); } if (from.UserId == to.UserId) { return(null); } if (from.UserWallet.Balance >= amount) { var tx = new CornTx(); tx.TxType = txType; tx.TxGroupId = txId; tx.Amount = amount; tx.ReceiverId = to.UserId; tx.SenderId = from.UserId; tx.Timestamp = DateTime.Now; tx.Platform = platform; return(tx); } return(null); }
public static async Task <CornTx> DebitWithdrawTx(string txId, User user, WalletServer server, decimal amount, BitcornContext dbContext, string platform) { if (user.UserWallet.Balance >= amount) { var sql = new StringBuilder(); sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), amount, '-', nameof(UserWallet.UserId), user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(WalletServer), nameof(WalletServer.ServerBalance), amount, '-', nameof(WalletServer.Id), server.Id)); await dbContext.Database.ExecuteSqlRawAsync(sql.ToString()); var log = new CornTx(); log.BlockchainTxId = txId; log.Amount = amount; log.Timestamp = DateTime.Now; log.TxType = "$withdraw"; log.TxGroupId = Guid.NewGuid().ToString(); log.Platform = platform; log.ReceiverId = user.UserId; dbContext.CornTx.Add(log); await dbContext.SaveAsync(); return(log); } return(null); }
public static async Task <CornTx[]> Deposit(BitcornContext dbContext, WalletDepositRequest request) { var receipts = new List <CornTx>(); try { var server = dbContext.WalletServer.FirstOrDefault((s) => s.Index == request.Index); int newDeposits = 0; var sql = new StringBuilder(); foreach (var payment in request.Payments) { decimal amount = payment.Amount; string address = payment.Address; string txid = payment.TxId; bool isLogged = await dbContext.IsDepositRegistered(txid); if (!isLogged) { newDeposits++; var wallet = await dbContext.WalletByAddress(address); if (wallet != null) { var cornTx = new CornTx(); cornTx.Amount = amount; cornTx.BlockchainTxId = txid; cornTx.ReceiverId = wallet.UserId; cornTx.SenderId = null; cornTx.Timestamp = DateTime.Now; cornTx.TxType = TransactionType.receive.ToString(); cornTx.Platform = "wallet-server"; cornTx.TxGroupId = Guid.NewGuid().ToString(); var deposit = new CornDeposit(); deposit.TxId = txid; deposit.UserId = wallet.UserId; sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), amount, '+', nameof(UserWallet.UserId), wallet.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(WalletServer), nameof(WalletServer.ServerBalance), amount, '+', nameof(WalletServer.Id), server.Id)); dbContext.CornTx.Add(cornTx); dbContext.CornDeposit.Add(deposit); receipts.Add(cornTx); } } } if (newDeposits > 0) { server.LastBalanceUpdateBlock = request.Block; int count = await dbContext.Database.ExecuteSqlRawAsync(sql.ToString()); await dbContext.SaveAsync(); } } catch (Exception e) { await BITCORNLogger.LogError(dbContext, e); } return(receipts.ToArray()); }
public static async Task <int> TryClaimTx(PlatformId platformId, User user, BitcornContext dbContext) { if (user == null) { user = await BitcornUtils.GetUserForPlatform(platformId, dbContext).FirstOrDefaultAsync(); } if (user == null) { return(0); } bool lockApplied = false; try { if (UserLockCollection.Lock(user)) { lockApplied = true; } else { return(0); } var now = DateTime.Now; var txs = await dbContext.UnclaimedTx.Where(u => u.Platform == platformId.Platform && u.ReceiverPlatformId == platformId.Id && !u.Claimed && !u.Refunded && u.Expiration > now).ToArrayAsync(); var sql = new StringBuilder(); var pk = nameof(UserWallet.UserId); foreach (var tx in txs) { tx.Claimed = true; var log = new CornTx(); log.Amount = tx.Amount; log.Platform = tx.Platform; log.ReceiverId = user.UserId; log.SenderId = tx.SenderUserId; log.Timestamp = tx.Timestamp; log.TxGroupId = Guid.NewGuid().ToString(); log.TxType = tx.TxType; log.UnclaimedTx.Add(tx); dbContext.CornTx.Add(log); sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), tx.Amount, '+', pk, user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.AmountOfTipsReceived), 1, '+', pk, user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.TotalReceivedBitcornTips), tx.Amount, '+', pk, user.UserId)); sql.Append(TxUtils.UpdateNumberIfTop(nameof(UserStat), nameof(UserStat.LargestReceivedBitcornTip), tx.Amount, pk, user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.AmountOfTipsSent), 1, '+', pk, tx.SenderUserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.TotalSentBitcornViaTips), tx.Amount, '+', pk, tx.SenderUserId)); sql.Append(TxUtils.UpdateNumberIfTop(nameof(UserStat), nameof(UserStat.LargestSentBitcornTip), tx.Amount, pk, tx.SenderUserId)); } if (txs.Length > 0) { await DbOperations.ExecuteSqlRawAsync(dbContext, sql.ToString()); await dbContext.SaveAsync(); } return(txs.Length); } catch (Exception e) { await BITCORNLogger.LogError(dbContext, e, JsonConvert.SerializeObject(platformId)); return(0); } finally { if (lockApplied) { UserLockCollection.Release(user); } } }
public static async Task <int> TryClaimTx(PlatformId platformId, User user, BitcornContext dbContext) { if (user == null) { user = await BitcornUtils.GetUserForPlatform(platformId, dbContext).FirstOrDefaultAsync(); } if (user == null) { return(0); } bool lockApplied = false; try { lock (LockUserAttribute.LockedUsers) { if (LockUserAttribute.LockedUsers.Contains(user.UserId)) { return(0); } lockApplied = LockUserAttribute.LockedUsers.Add(user.UserId); } var now = DateTime.Now; var txs = await dbContext.UnclaimedTx.Where(u => u.Platform == platformId.Platform && u.ReceiverPlatformId == platformId.Id && !u.Claimed && !u.Refunded && u.Expiration > now).ToArrayAsync(); var sql = new StringBuilder(); var pk = nameof(UserWallet.UserId); foreach (var tx in txs) { tx.Claimed = true; var log = new CornTx(); log.Amount = tx.Amount; log.Platform = tx.Platform; log.ReceiverId = user.UserId; log.SenderId = tx.SenderUserId; log.Timestamp = tx.Timestamp; log.TxGroupId = Guid.NewGuid().ToString(); log.TxType = tx.TxType; log.UnclaimedTx.Add(tx); dbContext.CornTx.Add(log); sql.Append(TxUtils.ModifyNumber(nameof(UserWallet), nameof(UserWallet.Balance), tx.Amount, '+', pk, user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.Tipped), 1, '+', pk, user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.TippedTotal), tx.Amount, '+', pk, user.UserId)); sql.Append(TxUtils.UpdateNumberIfTop(nameof(UserStat), nameof(UserStat.TopTipped), tx.Amount, pk, user.UserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.Tip), 1, '+', pk, tx.SenderUserId)); sql.Append(TxUtils.ModifyNumber(nameof(UserStat), nameof(UserStat.TipTotal), tx.Amount, '+', pk, tx.SenderUserId)); sql.Append(TxUtils.UpdateNumberIfTop(nameof(UserStat), nameof(UserStat.TopTip), tx.Amount, pk, tx.SenderUserId)); } if (txs.Length > 0) { await dbContext.Database.ExecuteSqlRawAsync(sql.ToString()); await dbContext.SaveAsync(); } return(txs.Length); } catch (Exception e) { await BITCORNLogger.LogError(dbContext, e); return(0); } finally { if (lockApplied) { lock (LockUserAttribute.LockedUsers) { LockUserAttribute.LockedUsers.Remove(user.UserId); } } } }