From 2c41f960b9f4d71f0ee551c241d2c45276e3d1a1 Mon Sep 17 00:00:00 2001 From: spekulaas Date: Fri, 10 Nov 2023 11:15:35 +0100 Subject: [PATCH] added change modify pending tx, fixed same user tx --- goodchain/src/helpers/TaskHelper.py | 181 +++++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 3 deletions(-) diff --git a/goodchain/src/helpers/TaskHelper.py b/goodchain/src/helpers/TaskHelper.py index 96b94a1..29d9690 100644 --- a/goodchain/src/helpers/TaskHelper.py +++ b/goodchain/src/helpers/TaskHelper.py @@ -16,6 +16,11 @@ def transaction(self): 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 == "": @@ -56,7 +61,6 @@ def createBlock(self): except: lastBlock = [] - print(lastBlock) if lastBlock != []: lastBlock = lastBlock[-1] @@ -171,7 +175,8 @@ def createBlock(self): transaction_count = 0 for transaction in transactions: if transaction_count not in selected_transactions: - utilityHelper.saveFile("../data/transaction_pool.dat", transactions[i]) + utilityHelper.saveFile("../data/transaction_pool.dat", transaction) + transaction_count += 1 return True @@ -310,4 +315,174 @@ def getPersonalTransactions(self): print(f"Total received: {total_output}") print(f"Total balance: {total_output - total_input}") print("-----------------------------------------------------------------------------------") - \ No newline at end of file + +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 > getBalanceWithOutgoingPool(self, 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