def do_transaction(self, arg): """Send coins to address. Usage: transaction <destination> <amount>""" global USER_WALLET try: source_address = USER_WALLET.get_address() except: print("Wallet not configured") self.typecmd("help wallet") return args = arg.split() if len(args) == 2: address = args[0] amount = args[1] else: address = input("enter recipient address : ") amount = input("enter amount : ") try: amount = float(amount) except ValueError: print("Incorrect amount") return magic = api.get_magic() t = transaction.Transaction(source_address, address, amount, magic=magic) USER_WALLET.sign_transaction(t) if self.api.push_transaction(t): print("OK") else: print("KO")
def do_mine(args): """Mine a block.""" w = Wallet.load_keys(args.wallet) magic = args.api.get_magic() coinbase = transaction.Transaction("0", w.get_address(), 1, magic=magic) print("Mining block...") b = mine(coinbase, args.api) print("Pushing block...") args.api.push_block(b) print("Successfully mined block.")
def do_transaction(args): """Perform a transaction.""" w = Wallet.load_keys(args.wallet) magic = args.api.get_magic() tx = transaction.Transaction(w.get_address(), args.destination, args.amount, magic=magic) w.sign_transaction(tx) if args.api.push_transaction(tx): print("Transaction successfully broadcasted.") else: print("[Error] Failed to broadcast transaction.")
def from_json(data): """Loads a JSON representation, and returns an object""" try: if type(data) != dict: dic = json.loads(data) else: dic = data if dic["_child_1"] is not None: c1 = TreeNode.from_json(dic["_child_1"]) dic.update({"_child_1": c1}) if dic["_child_2"] is not None: c2 = TreeNode.from_json(dic["_child_2"]) dic.update({"_child_2": c2}) if dic["_data"] is not None: t = transaction.Transaction(**dic["_data"]) dic.update({"_data": t}) n = TreeNode(**dic) return n except: raise (ValueError("JSON could not be loaded"))
def do_mine(self, arg): """Miner control. Usage: mine <start|stop>""" if arg == "start": global USER_WALLET try: source_address = USER_WALLET.get_address() except: print("Wallet not configured") self.typecmd("help wallet") return global MINING_POOL magic = api.get_magic() t = transaction.Transaction("0", USER_WALLET.get_address(), 1, magic=magic) print("Starting miner") sys.stdout.flush() MINING_POOL.apply_async(mine, (t, self.api), callback=miner_push_block, error_callback=mine_error) elif arg == "stop": pass else: print("Invalid subcommand") self.typecmd("help mine") return