From e65e19e2936bbcae304000411ba3fea9609afc52 Mon Sep 17 00:00:00 2001 From: spekulaas Date: Fri, 10 Nov 2023 16:16:28 +0100 Subject: [PATCH] splitted transactions aswell from taskhelper --- goodchain/src/helpers/TransactionHelper.py | 223 +++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 goodchain/src/helpers/TransactionHelper.py diff --git a/goodchain/src/helpers/TransactionHelper.py b/goodchain/src/helpers/TransactionHelper.py new file mode 100644 index 0000000..1d1ed9e --- /dev/null +++ b/goodchain/src/helpers/TransactionHelper.py @@ -0,0 +1,223 @@ +from classes.Transaction import Tx +from classes.TxBlock import TxBlock + +from helpers import UtilityHelper as utilityHelper +from helpers import TaskHelper as taskHelper + +def transaction(self): + receiver = input("Enter the username of the receiver:") + if receiver == "": + return False + + receiver_data = self.db.fetchUserByUsername(receiver) + if not receiver_data: + print("Username not found") + return False + + if receiver_data[1] == self.user.public_ser: + utilityHelper.clearScreen() + print("You cant send money to yourself") + return False + + amount = input("Enter the amount excluding fees:") + if amount == "": + return False + + try: + amount = float(amount) + except: + print("Wrong input, try again") + return False + + fee = input("Enter fee:") + if fee == "": + return False + + try: + fee = float(fee) + except: + print("Wrong input, try again") + return False + + utilityHelper.clearScreen() + + print(f"Processing transaction of {amount + fee} coins, {receiver} will receive {amount} coins") + + if amount + fee > taskHelper.getBalanceWithOutgoingPool(self, taskHelper.getBalance(self)): + print("You dont have enough money") + return False + + new_tx = Tx() + new_tx.createTransaction(self.user.public_ser, self.user.private_ser, amount, fee, receiver_data[1]) + return new_tx + +def pendingTransactions(self): + transactions = utilityHelper.loadFile("../data/transaction_pool.dat") + total = 0 + total_amount = 0 + user_transactions = {} + for transaction in transactions: + + # continue if transaction is a reward + if transaction.type != 0: + continue + if transaction.inputs[0][0] == self.user.public_ser: + total += 1 + print(f"---------------------------------------{total}-------------------------------------------") + print(f"Transaction input: {transaction.inputs[0][1]}") + print(f"Transaction output: {transaction.outputs[0][1]}") + print(f"Transaction fees: {transaction.inputs[0][1] - transaction.outputs[0][1]}") + print(f"Transaction sender: \n{transaction.inputs[0][0]}") + print(f"Transaction recipient: \n{transaction.outputs[0][0]}") + total_amount += transaction.inputs[0][1] + user_transactions[total] = transaction + + if total == 0: + print("No transactions found") + return False + + print("-----------------------------------------------------------------------------------") + print(f"Total transactions: {total}") + print(f"Total spent: {total_amount}") + print("-----------------------------------------------------------------------------------") + + print("0 -- Go back") + print("1 -- Cancel/Modify transaction") + choice = input(">>: ") + + match choice: + case "0": + utilityHelper.clearScreen() + return + case "1": + print("Select a transaction to modify/cancel") + user_input = input(">>: ") + try: + user_input = int(user_input) + except: + utilityHelper.clearScreen() + print("Wrong input, try again") + return + + if user_input > total or user_input < 1: + utilityHelper.clearScreen() + print("Wrong input, try again") + return + + if user_transactions[user_input].type == 1: + utilityHelper.clearScreen() + print("You cant modify/cancel a reward transaction") + return + + # print info of that transaction + utilityHelper.clearScreen() + print(f"---------------------------------------{str(user_input)}-------------------------------------------") + print(f"Transaction input: {user_transactions.get(user_input).inputs[0][1]}") + print(f"Transaction output: {user_transactions.get(user_input).outputs[0][1]}") + print(f"Transaction fees: {user_transactions.get(user_input).inputs[0][1] - user_transactions.get(user_input).outputs[0][1]}") + print(f"Transaction sender: \n{user_transactions.get(user_input).inputs[0][0]}") + print(f"Transaction recipient: \n{user_transactions.get(user_input).outputs[0][0]}") + print(f"---------------------------------------END-------------------------------------------") + print("Modifying a transaction will bring you back in line, your transaction will lose the place in the pool.") + print("0 -- Go back") + print("1 -- Change amount") + print("2 -- Change Recipient") + print("3 -- Cancel transaction") + choice = input(">>: ") + + match choice: + case "0": + utilityHelper.clearScreen() + return + case "1": + utilityHelper.clearScreen() + print("Enter a new amount") + amount = input(">>: ") + try: + amount = float(amount) + except: + utilityHelper.clearScreen() + print("Wrong input, try again") + return + + print("Enter a new fee") + fee = input(">>: ") + + try: + fee = float(fee) + except: + utilityHelper.clearScreen() + print("Wrong input, try again") + return + + if amount + fee > taskHelper.getBalanceWithOutgoingPool(self, taskHelper.getBalance(self)): + utilityHelper.clearScreen() + print("You dont have enough money") + print("Nothing happened to the old transaction") + return False + + new_tx = Tx() + new_tx.createTransaction(self.user.public_ser, self.user.private_ser, amount, fee, user_transactions[user_input].outputs[0][0]) + + # remove old transaction from array + transactions.remove(user_transactions[user_input]) + utilityHelper.resetFile("../data/transaction_pool.dat") + + # add old transactions to pool + for transaction in transactions: + utilityHelper.saveFile("../data/transaction_pool.dat", transaction) + utilityHelper.saveFile("../data/transaction_pool.dat", new_tx) + + utilityHelper.clearScreen() + print("Transaction modified") + return + case "2": + print("Select a new recipient") + recipient = input(">>: ") + recipient_data = self.db.fetchUserByUsername(recipient) + if not recipient_data: + utilityHelper.clearScreen() + print("Username not found") + return False + + if recipient_data[1] == self.user.public_ser: + utilityHelper.clearScreen() + print("You cant send money to yourself") + return False + + new_tx = Tx() + new_tx.createTransaction(self.user.public_ser, self.user.private_ser, user_transactions[user_input].inputs[0][1], user_transactions[user_input].inputs[0][1] - user_transactions[user_input].outputs[0][1], recipient_data[1]) + + # remove old transaction from array + transactions.remove(user_transactions[user_input]) + utilityHelper.resetFile("../data/transaction_pool.dat") + + # add old transactions to pool + for transaction in transactions: + print(transaction) + utilityHelper.saveFile("../data/transaction_pool.dat", transaction) + utilityHelper.saveFile("../data/transaction_pool.dat", new_tx) + + utilityHelper.clearScreen() + print("Transaction modified") + return + + case "3": + # remove old transaction from array + transactions.remove(user_transactions[user_input]) + utilityHelper.resetFile("../data/transaction_pool.dat") + + # add old transactions to pool + for transaction in transactions: + utilityHelper.saveFile("../data/transaction_pool.dat", transaction) + utilityHelper.clearScreen() + print("Transaction cancelled") + return + case _: + utilityHelper.clearScreen() + print("Wrong input, try again") + return + case _: + utilityHelper.clearScreen() + print("Wrong input, try again") + return \ No newline at end of file