public void Withdraw() { if (Program.currentLoggedInAddress != null) { Console.Clear(); Console.WriteLine("Withdraw money at the BlockChain system."); Console.WriteLine("Please enter the amount to withdraw: "); var amount = double.Parse(Console.ReadLine()); if (amount <= 0) { Console.WriteLine("Invalid amount. Please try again!"); return; } var transaction = new TransactionBlockChain { TransactionId = Guid.NewGuid().ToString(), SenderAddress = Program.currentLoggedInAddress.Address, ReceiveAddress = Program.currentLoggedInAddress.Address, Type = TransactionBlockChain.TransactionType.Withdraw, Amount = amount, CreatedAtMls = DateTime.Now.Ticks, UpdatedAtMls = DateTime.Now.Ticks, Status = (TransactionBlockChain.TransactionStatus) 1 }; if (bcAddressModel.UpdateBalance(Program.currentLoggedInAddress, transaction)) { Console.Clear(); Console.WriteLine("Withdraw success!"); Console.WriteLine("Continue with BlockChain? (y/n)"); if (Console.ReadLine().ToLower().Equals("n")) { Program.currentLoggedInAddress = null; } } else { Console.WriteLine("Transaction failed. Please try again later!\nEnter to continue!"); Console.ReadLine(); } } else { Console.WriteLine("Please login to use this function!"); } }
public void Transfer() { if (Program.currentLoggedInAccount != null) { Console.Clear(); Console.WriteLine("Transfer money at the SB system."); Console.WriteLine("Please enter the amount to transfer: "); var amount = double.Parse(Console.ReadLine()); Console.WriteLine("Enter ReceiverAccountNumber:"); var receiverAddress = Console.ReadLine(); var transaction = new TransactionBlockChain() { TransactionId = Guid.NewGuid().ToString(), SenderAddress = Program.currentLoggedInAddress.Address, ReceiveAddress = receiverAddress, Type = TransactionBlockChain.TransactionType.Transfer, Amount = amount, CreatedAtMls = DateTime.Now.Ticks, UpdatedAtMls = DateTime.Now.Ticks, Status = (TransactionBlockChain.TransactionStatus) 1 }; if (bcAddressModel.Transfer(Program.currentLoggedInAddress, transaction)) { Console.WriteLine("Transfers success!"); Console.WriteLine("Continue with BlockChain? (y/n)"); if (Console.ReadLine().ToLower().Equals("n")) { Program.currentLoggedInAddress = null; } } else { Console.WriteLine("Transaction failed. Please try again later!\nEnter to continue!"); Console.ReadLine(); } } else { Console.WriteLine("Please login to use this function!"); } }
public bool UpdateBalance(BlockChainAddress currentLoggedInAddress, TransactionBlockChain transactionBc) { var trans = ConnectionHelper.GetConnection().BeginTransaction(); try { var cmd = new MySqlCommand("select balance from address where address = @address", ConnectionHelper.GetConnection()); cmd.Parameters.AddWithValue("@address", currentLoggedInAddress.Address); var reader = cmd.ExecuteReader(); double currentBalance = 0; if (reader.Read()) { currentBalance = reader.GetDouble("balance"); } reader.Close(); if (transactionBc.Type == TransactionBlockChain.TransactionType.Withdraw && currentBalance < transactionBc.Amount) { throw new Exception("Not enough money in the account."); } if (transactionBc.Type == TransactionBlockChain.TransactionType.Withdraw) { currentBalance -= transactionBc.Amount; } if (transactionBc.Type == TransactionBlockChain.TransactionType.Deposit) { currentBalance += transactionBc.Amount; } var cmd1 = new MySqlCommand( "update address set balance = @balance WHERE address = @address", ConnectionHelper.GetConnection()); cmd1.Parameters.AddWithValue("@balance", currentBalance); cmd1.Parameters.AddWithValue("@address", currentLoggedInAddress.Address); var updateResult = cmd1.ExecuteNonQuery(); var cmd2 = new MySqlCommand( "insert into blockchaintransactions (transactionId, senderAddress, receiverAddress, type, amount, createdAt, updatedAt, status) values ( @transactionId, @senderAddress, @receiverAddress, @type, @amount, @createdAt, @updatedAt, @status) ", ConnectionHelper.GetConnection()); cmd2.Parameters.AddWithValue("@transactionId", transactionBc.TransactionId); cmd2.Parameters.AddWithValue("@senderAddress", transactionBc.SenderAddress); cmd2.Parameters.AddWithValue("@receiverAddress", transactionBc.ReceiveAddress); cmd2.Parameters.AddWithValue("@type", transactionBc.Type); cmd2.Parameters.AddWithValue("@amount", transactionBc.Amount); cmd2.Parameters.AddWithValue("@createdAt", transactionBc.CreatedAtMls); cmd2.Parameters.AddWithValue("@updatedAt", transactionBc.UpdatedAtMls); cmd2.Parameters.AddWithValue("@status", transactionBc.Status); var transactionResult = cmd2.ExecuteNonQuery(); if (updateResult != 1 || transactionResult != 1) { throw new Exception("Cannot add transactions or update accounts."); } trans.Commit(); return(true); } catch (Exception e) { trans.Rollback(); Console.WriteLine(e); return(false); } finally { ConnectionHelper.CloseConnection(); } }