private void AccountMiningConfirmed(int cryptoId, MiningConfirmedModel model, WalletFactory _walletFactory, WalletStatementFactory _walletStatementFactory) { var wallet = _walletFactory.GetByAccountId(model.AccountId, cryptoId); if (wallet == null) { MiningConfirmedFaild(model, "Wallet is null"); return; } if (wallet.FrozenBalance < model.Amount) { MiningConfirmedFaild(model, "Insufficient frozenbalance"); return; } try { _walletFactory.Unfreeze(wallet.Id, model.Amount); _walletStatementFactory.Insert(new WalletStatement { Action = UserWalletStatementAction.AwardDeposit, Amount = model.Amount, Balance = wallet.Balance + model.Amount, FrozenAmount = -model.Amount, FrozenBalance = wallet.FrozenBalance - model.Amount, Remark = null, Timestamp = DateTime.UtcNow, WalletId = wallet.Id }); } catch (Exception ex) { MiningConfirmedFaild(model, (ex.InnerException == null ? ex.Message : ex.InnerException.Message)); } }
private void MiningReceived(WalletFactory _walletFactory, DepositFactory _depositFactory, WalletStatementFactory _walletStatementFactory, TransactionFactory _transactionFactory, long requestId, string transactionId, Guid accountId, decimal amount) { if (RedisHelper.KeyExists(miningReceivedKey + requestId))//防止没写进数据库之前,相同的请求调用 { throw new CommonException(20001, "Error: Repeated requests"); } var excistDeposit = _depositFactory.GetByRequestId(accountId, requestId); if (excistDeposit != null) { throw new CommonException(20001, "Error: Repeated requests"); } RedisHelper.StringSet(miningReceivedKey + requestId, "1", new TimeSpan(0, 0, 30)); var crypto = new CryptocurrencyDAC().GetByCode("FIII"); if (crypto == null) { throw new CommonException(20000, "Error: Invalid cryptocurrency"); } var wallet = _walletFactory.GetByAccountId(accountId, crypto.Id); var timestamp = DateTime.UtcNow; using (var scope = new TransactionScope()) { if (wallet == null) { wallet = GenerateWallet(_walletFactory, accountId, crypto.Id, crypto.Code); } var deposit = new Deposit { AccountId = accountId, WalletId = wallet.Id, FromType = DepositFromType.PosMining, ToAddress = wallet.Address, Amount = amount, Status = TransactionStatus.Confirmed, Timestamp = timestamp, RequestId = requestId, OrderNo = NumberGenerator.GenerateUnixOrderNo(), TransactionId = transactionId, CryptoCode = crypto.Code }; _depositFactory.Insert(deposit); _transactionFactory.Insert(new UserTransaction { Id = Guid.NewGuid(), AccountId = accountId, CryptoId = crypto.Id, CryptoCode = crypto.Code, Type = UserTransactionType.Deposit, DetailId = deposit.Id.ToString(), Status = (byte)deposit.Status, Timestamp = deposit.Timestamp, Amount = amount, OrderNo = deposit.OrderNo }); _walletFactory.Increase(wallet.Id, amount); //_walletFactory.DecreaseFreeze(wallet.Id, -amount); _walletStatementFactory.Insert(new WalletStatement { Action = Entities.UserWalletStatementAction.AwardDeposit, Amount = amount, Balance = wallet.Balance + amount, FrozenAmount = 0, FrozenBalance = wallet.FrozenBalance, Remark = null, Timestamp = timestamp, WalletId = wallet.Id }); scope.Complete(); } }