public void giaodich() { Console.WriteLine("Vui lòng nhập số tài khoản chuyển tiền: "); var accountNumber = Console.ReadLine(); var receiverAccount = ModelBlockchain.FindByAccountaddress(accountNumber); if (receiverAccount == null) { Console.WriteLine("Tài khoản nhận tiền không tồn tại hoặc đã bị khoá."); return; } Console.WriteLine("Địa chỉ ài khoản nhận tiền: " + receiverAccount.Accountaddress); Console.WriteLine("Nhập số tiền chuyển khoản: "); var amount = double.Parse(Console.ReadLine()); Console.WriteLine("Vui lòng nhập mã tài khoản."); string privatekey = Console.ReadLine(); if (ModelBlockchain.FindByPrivateKey(privatekey) != null) { Program.currentLoggedInAccountblock = ModelBlockchain.FindByAccountaddress(Program.currentLoggedInAccountblock.Accountaddress); if (amount > Program.currentLoggedInAccountblock.Balane) { Console.WriteLine("Số dư tài khoản không đủ thực hiện giao dịch."); return; } var transactionHistory = new TransactionBlockchain() { Id = Guid.NewGuid().ToString(), Type = TransactionNganhang.TrasactionType.Stranfer, Amount = amount, SenderId = Program.currentLoggedInAccountblock.Accountaddress, ReceiverId = accountNumber }; if (Program.currentLoggedInAccountblock.Accountaddress == accountNumber) { return; } if (ModelBlockchain.Transfer(Program.currentLoggedInAccountblock, transactionHistory)) { Console.WriteLine("Giao dịch thành công."); } else { Console.WriteLine("Giao dịch thất bại, vui lòng thử lại."); } } else { Console.WriteLine(" mã tài khoản không đúng"); } }
public bool Transfer(BlockchainAccount currentLoggedInAccount, TransactionBlockchain transactionHistory) { var mySqlTransaction = ConnectionHepper.GetConnection().BeginTransaction(); try { // Kiểm tra số dư tài khoản. var selectBalance = "select Balance from AccountBlockchain where Accountaddress = @Accountaddress"; var cmdSelect = new MySqlCommand(selectBalance, ConnectionHepper.GetConnection()); cmdSelect.Parameters.AddWithValue("@Accountaddress", currentLoggedInAccount.Accountaddress); var reader = cmdSelect.ExecuteReader(); double currentAccountBalance = 0; if (reader.Read()) { currentAccountBalance = reader.GetDouble("Balance"); } reader.Close(); // important. if (currentAccountBalance < transactionHistory.Amount) { throw new Exception("Không đủ tiền trong tài khoản."); } currentAccountBalance -= transactionHistory.Amount; // Update tài khoản. var updateQuery = "update `AccountBlockchain` set `Balance` = @Balance where Accountaddress = @Accountaddress "; var sqlCmd = new MySqlCommand(updateQuery, ConnectionHepper.GetConnection()); sqlCmd.Parameters.AddWithValue("@Balance", currentAccountBalance); sqlCmd.Parameters.AddWithValue("@Accountaddress", currentLoggedInAccount.Accountaddress); var updateResult = sqlCmd.ExecuteNonQuery(); // Kiểm tra số dư tài khoản. var selectBalanceReceiver = "select Balance from `AccountBlockchain` where Accountaddress = @Accountaddress "; var cmdSelectReceiver = new MySqlCommand(selectBalanceReceiver, ConnectionHepper.GetConnection()); cmdSelectReceiver.Parameters.AddWithValue("@Accountaddress", transactionHistory.ReceiverId); var readerReceiver = cmdSelectReceiver.ExecuteReader() ?? throw new ArgumentNullException("cmdSelectReceiver.ExecuteReader()"); double receiverBalance = 0; if (readerReceiver.Read()) { receiverBalance = readerReceiver.GetDouble("Balance"); } readerReceiver.Close(); // important. receiverBalance += transactionHistory.Amount; // Update tài khoản. var updateQueryReceiver = "update `AccountBlockchain` set `Balance` = @Balance where Accountaddress = @Accountaddress"; var sqlCmdReceiver = new MySqlCommand(updateQueryReceiver, ConnectionHepper.GetConnection()); sqlCmdReceiver.Parameters.AddWithValue("@Balance", receiverBalance); sqlCmdReceiver.Parameters.AddWithValue("@Accountaddress", transactionHistory.ReceiverId); var updateResultReceiver = sqlCmdReceiver.ExecuteNonQuery(); // Lưu lịch sử giao dịch. var historyTransactionQuery = "insert into TransactionBlockchain(Id, Type, Senderid, ReceiverId, Amount) " + "values (@Id, @Type, @Senderid, @ReceiverId, @Amount)"; var historyTransactionCmd = new MySqlCommand(historyTransactionQuery, ConnectionHepper.GetConnection()); historyTransactionCmd.Parameters.AddWithValue("@Id", transactionHistory.Id); historyTransactionCmd.Parameters.AddWithValue("@Amount", transactionHistory.Amount); historyTransactionCmd.Parameters.AddWithValue("@Type", transactionHistory.Type); historyTransactionCmd.Parameters.AddWithValue("@Senderid", transactionHistory.SenderId); historyTransactionCmd.Parameters.AddWithValue("@Receiverid", transactionHistory.ReceiverId); var historyResult = historyTransactionCmd.ExecuteNonQuery(); if (updateResult != 1 || historyResult != 1 || updateResultReceiver != 1) { throw new Exception("Không thể thêm giao dịch hoặc update tài khoản."); } mySqlTransaction.Commit(); return(true); } catch (Exception e) { Console.WriteLine(e.StackTrace); mySqlTransaction.Rollback(); return(false); } finally { ConnectionHepper.CloseConnection(); } }