From 567a7440eb4b84b93679fd68d31f50c528347c70 Mon Sep 17 00:00:00 2001 From: ytx1991 Date: Sat, 10 Jun 2023 11:00:41 -0700 Subject: [PATCH 1/5] Add CLI for Clawback --- chia/cmds/wallet.py | 51 ++++++++++++++++++++++++++++++++++++++ chia/cmds/wallet_funcs.py | 37 ++++++++++++++++++++++++--- chia/rpc/wallet_rpc_api.py | 4 +-- 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/chia/cmds/wallet.py b/chia/cmds/wallet.py index 2acd0eae837e..d302f7143623 100644 --- a/chia/cmds/wallet.py +++ b/chia/cmds/wallet.py @@ -96,6 +96,12 @@ def get_transaction_cmd(wallet_rpc_port: Optional[int], fingerprint: int, id: in default=False, help="Reverse the transaction ordering", ) +@click.option( + "--clawback", + is_flag=True, + default=False, + help="Only show clawback transactions", +) def get_transactions_cmd( wallet_rpc_port: Optional[int], fingerprint: int, @@ -106,6 +112,7 @@ def get_transactions_cmd( paginate: Optional[bool], sort_key: SortKey, reverse: bool, + clawback: bool, ) -> None: extra_params = { "id": id, @@ -115,6 +122,7 @@ def get_transactions_cmd( "limit": limit, "sort_key": sort_key, "reverse": reverse, + "clawback": clawback, } import asyncio @@ -185,6 +193,13 @@ def get_transactions_cmd( is_flag=True, default=False, ) +@click.option( + "--clawback_time", + help="The seconds that the recipient needs to wait to claim the fund." + " A positive number will enable the Clawback features.", + type=int, + default=0, +) def send_cmd( wallet_rpc_port: Optional[int], fingerprint: int, @@ -198,6 +213,7 @@ def send_cmd( max_coin_amount: str, coins_to_exclude: Tuple[str], reuse: bool, + clawback_time: int, ) -> None: extra_params = { "id": id, @@ -210,6 +226,7 @@ def send_cmd( "max_coin_amount": max_coin_amount, "exclude_coin_ids": list(coins_to_exclude), "reuse_puzhash": True if reuse else None, + "clawback_time": clawback_time, } import asyncio @@ -274,6 +291,40 @@ def get_address_cmd(wallet_rpc_port: Optional[int], id, fingerprint: int, new_ad asyncio.run(execute_with_wallet(wallet_rpc_port, fingerprint, extra_params, get_address)) +@wallet_cmd.command( + "clawback", + help="Claim or revert a Clawback transaction." + " The wallet will automatically detect if you are able to revert or claim.", +) +@click.option( + "-wp", + "--wallet-rpc-port", + help="Set the port where the Wallet is hosting the RPC interface. See the rpc_port under wallet in config.yaml", + type=int, + default=None, +) +@click.option("-i", "--id", help="Id of the wallet to use", type=int, default=1, show_default=True, required=True) +@click.option("-f", "--fingerprint", help="Set the fingerprint to specify which key to use", type=int) +@click.option( + "-ids", + "--tx_ids", + help="IDs of the Clawback transactions you want to revert or claim. Separate multiple IDs by comma (,).", + type=str, + default="", + required=True, +) +@click.option( + "-m", "--fee", help="A fee to add to the offer when it gets taken, in XCH", default="0", show_default=True +) +def clawback(wallet_rpc_port: Optional[int], id, fingerprint: int, tx_ids: str, fee: str) -> None: + extra_params = {"id": id, "tx_ids": tx_ids, "fee": fee} + import asyncio + + from .wallet_funcs import spend_clawback + + asyncio.run(execute_with_wallet(wallet_rpc_port, fingerprint, extra_params, spend_clawback)) + + @wallet_cmd.command("delete_unconfirmed_transactions", help="Deletes all unconfirmed transactions for this wallet ID") @click.option( "-wp", diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index 83b4f6ccbcef..8e31446f152d 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -28,6 +28,8 @@ from chia.wallet.trading.trade_status import TradeStatus from chia.wallet.transaction_record import TransactionRecord from chia.wallet.util.address_type import AddressType, ensure_valid_address +from chia.wallet.util.puzzle_decorator_type import PuzzleDecoratorType +from chia.wallet.util.query_filter import TransactionTypeFilter from chia.wallet.util.transaction_type import TransactionType from chia.wallet.util.wallet_types import WalletType from chia.wallet.vc_wallet.vc_store import VCProofs @@ -151,9 +153,15 @@ async def get_transactions(args: dict, wallet_client: WalletRpcClient, fingerpri limit = args["limit"] sort_key = args["sort_key"] reverse = args["reverse"] - + type_filter = ( + None + if not args["clawback"] + else TransactionTypeFilter.include( + [TransactionType.INCOMING_CLAWBACK_RECEIVE, TransactionType.INCOMING_CLAWBACK_SEND] + ) + ) txs: List[TransactionRecord] = await wallet_client.get_transactions( - wallet_id, start=offset, end=(offset + limit), sort_key=sort_key, reverse=reverse + wallet_id, start=offset, end=(offset + limit), sort_key=sort_key, reverse=reverse, type_filter=type_filter ) config = load_config(DEFAULT_ROOT_PATH, "config.yaml", SERVICE_NAME) @@ -212,6 +220,7 @@ async def send(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> exclude_coin_ids: List[str] = args["exclude_coin_ids"] memo = args["memo"] reuse_puzhash = args["reuse_puzhash"] + clawback_time_lock = args["clawback_time"] if memo is None: memos = None else: @@ -226,7 +235,9 @@ async def send(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> if amount == 0: print("You can not send an empty transaction") return - + if clawback_time_lock < 0: + print("Clawback time lock seconds cannot be negative.") + return try: typ = await get_wallet_type(wallet_id=wallet_id, wallet_client=wallet_client) mojo_per_unit = get_mojo_per_unit(typ) @@ -250,6 +261,11 @@ async def send(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> final_max_coin_amount, exclude_coin_ids=exclude_coin_ids, reuse_puzhash=reuse_puzhash, + puzzle_decorator_override=[ + {"decorator": PuzzleDecoratorType.CLAWBACK.name, "clawback_timelock": clawback_time_lock} + ] + if clawback_time_lock > 0 + else None, ) elif typ == WalletType.CAT: print("Submitting transaction...") @@ -1225,6 +1241,21 @@ async def sign_message(args: Dict, wallet_client: WalletRpcClient, fingerprint: print(f"Signing Mode: {signing_mode}") +async def spend_clawback(args: Dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: + tx_ids = [] + for tid in args["tx_ids"].split(","): + tx_ids.append(bytes32.from_hexstr(tid)) + if len(tx_ids) == 0: + print("Transaction ID is required.") + return + fee = Decimal(args["fee"]) + if fee < 0: + print("Batch fee cannot be negative.") + return + response = await wallet_client.spend_clawback_coins(tx_ids, int(fee * units["chia"])) + print(str(response)) + + async def mint_vc(args: Dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: # pragma: no cover config = load_config(DEFAULT_ROOT_PATH, "config.yaml", SERVICE_NAME) vc_record, txs = await wallet_client.vc_mint( diff --git a/chia/rpc/wallet_rpc_api.py b/chia/rpc/wallet_rpc_api.py index 209bde21f519..ffd06ad1bea6 100644 --- a/chia/rpc/wallet_rpc_api.py +++ b/chia/rpc/wallet_rpc_api.py @@ -914,8 +914,8 @@ async def get_transactions(self, request: Dict) -> EndpointResult: tx["metadata"] = record.parsed_metadata().to_json_dict() tx["metadata"]["coin_id"] = coin.name().hex() tx["metadata"]["spent"] = record.spent - except Exception as e: - log.error(f"Failed to get transaction {tr.name}: {e}") + except Exception: + log.exception(f"Failed to get transaction {tr.name}.") return { "transactions": tx_list, "wallet_id": wallet_id, From d4f0b154bcc9741a0b5df1f62e83fc668fa27c71 Mon Sep 17 00:00:00 2001 From: ytx1991 Date: Mon, 12 Jun 2023 09:41:45 -0700 Subject: [PATCH 2/5] Fix coverage & Disable auto claim --- chia/cmds/wallet.py | 6 +++--- chia/cmds/wallet_funcs.py | 6 +++--- chia/wallet/puzzles/clawback/metadata.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/chia/cmds/wallet.py b/chia/cmds/wallet.py index d302f7143623..dd6a2ac3423a 100644 --- a/chia/cmds/wallet.py +++ b/chia/cmds/wallet.py @@ -113,7 +113,7 @@ def get_transactions_cmd( sort_key: SortKey, reverse: bool, clawback: bool, -) -> None: +) -> None: # pragma: no cover extra_params = { "id": id, "verbose": verbose, @@ -214,7 +214,7 @@ def send_cmd( coins_to_exclude: Tuple[str], reuse: bool, clawback_time: int, -) -> None: +) -> None: # pragma: no cover extra_params = { "id": id, "amount": amount, @@ -316,7 +316,7 @@ def get_address_cmd(wallet_rpc_port: Optional[int], id, fingerprint: int, new_ad @click.option( "-m", "--fee", help="A fee to add to the offer when it gets taken, in XCH", default="0", show_default=True ) -def clawback(wallet_rpc_port: Optional[int], id, fingerprint: int, tx_ids: str, fee: str) -> None: +def clawback(wallet_rpc_port: Optional[int], id, fingerprint: int, tx_ids: str, fee: str) -> None: # pragma: no cover extra_params = {"id": id, "tx_ids": tx_ids, "fee": fee} import asyncio diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index 8e31446f152d..045602d3acfb 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -144,7 +144,7 @@ async def get_transaction(args: dict, wallet_client: WalletRpcClient, fingerprin ) -async def get_transactions(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: +async def get_transactions(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: # pragma: no cover wallet_id = args["id"] paginate = args["paginate"] if paginate is None: @@ -209,7 +209,7 @@ def check_unusual_transaction(amount: Decimal, fee: Decimal): return fee >= amount -async def send(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: +async def send(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: # pragma: no cover wallet_id: int = args["id"] amount = Decimal(args["amount"]) fee = Decimal(args["fee"]) @@ -1241,7 +1241,7 @@ async def sign_message(args: Dict, wallet_client: WalletRpcClient, fingerprint: print(f"Signing Mode: {signing_mode}") -async def spend_clawback(args: Dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: +async def spend_clawback(args: Dict, wallet_client: WalletRpcClient, fingerprint: int) -> None: # pragma: no cover tx_ids = [] for tid in args["tx_ids"].split(","): tx_ids.append(bytes32.from_hexstr(tid)) diff --git a/chia/wallet/puzzles/clawback/metadata.py b/chia/wallet/puzzles/clawback/metadata.py index d16bb04268a5..96557b66a3eb 100644 --- a/chia/wallet/puzzles/clawback/metadata.py +++ b/chia/wallet/puzzles/clawback/metadata.py @@ -32,7 +32,7 @@ class ClawbackVersion(IntEnum): @streamable @dataclass(frozen=True) class AutoClaimSettings(Streamable): - enabled: bool = True + enabled: bool = False tx_fee: uint64 = uint64(0) min_amount: uint64 = uint64(0) batch_size: uint16 = uint16(50) From b642b6a1b0c3a05e72cf6cc677e439a089bcfc0f Mon Sep 17 00:00:00 2001 From: ytx1991 Date: Mon, 12 Jun 2023 16:35:10 -0700 Subject: [PATCH 3/5] Display Clawback TX in CLI --- chia/cmds/wallet_funcs.py | 33 +++++++++++++++++++++++++--- chia/rpc/wallet_rpc_api.py | 5 ++--- chia/wallet/util/transaction_type.py | 6 +++++ chia/wallet/wallet_state_manager.py | 7 ++---- tests/wallet/test_wallet.py | 15 +++++++++++++ 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index 045602d3acfb..bd77cdc1f2cc 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -29,10 +29,11 @@ from chia.wallet.transaction_record import TransactionRecord from chia.wallet.util.address_type import AddressType, ensure_valid_address from chia.wallet.util.puzzle_decorator_type import PuzzleDecoratorType -from chia.wallet.util.query_filter import TransactionTypeFilter -from chia.wallet.util.transaction_type import TransactionType +from chia.wallet.util.query_filter import HashFilter, TransactionTypeFilter +from chia.wallet.util.transaction_type import CLAWBACK_TRANSACTION_TYPES, TransactionType from chia.wallet.util.wallet_types import WalletType from chia.wallet.vc_wallet.vc_store import VCProofs +from chia.wallet.wallet_coin_store import GetCoinRecords CATNameResolver = Callable[[bytes32], Awaitable[Optional[Tuple[Optional[uint32], str]]]] @@ -50,7 +51,14 @@ def transaction_description_from_type(tx: TransactionRecord) -> str: return transaction_type_descriptions.get(TransactionType(tx.type), "(unknown reason)") -def print_transaction(tx: TransactionRecord, verbose: bool, name, address_prefix: str, mojo_per_unit: int) -> None: +def print_transaction( + tx: TransactionRecord, + verbose: bool, + name, + address_prefix: str, + mojo_per_unit: int, + coin_record: Optional[Dict[str, Any]] = None, +) -> None: if verbose: print(tx) else: @@ -62,6 +70,17 @@ def print_transaction(tx: TransactionRecord, verbose: bool, name, address_prefix print(f"Amount {description}: {chia_amount} {name}") print(f"To address: {to_address}") print("Created at:", datetime.fromtimestamp(tx.created_at_time).strftime("%Y-%m-%d %H:%M:%S")) + if coin_record is not None: + if tx.type == TransactionType.INCOMING_CLAWBACK_RECEIVE.value: + print("Clawback: Recipient") + print( + "Claimable after:", + datetime.fromtimestamp(tx.created_at_time + coin_record["metadata"]["time_lock"]).strftime( + "%Y-%m-%d %H:%M:%S" + ), + ) + else: + print("Clawback: Sender") print("") @@ -187,12 +206,20 @@ async def get_transactions(args: dict, wallet_client: WalletRpcClient, fingerpri for j in range(0, num_per_screen): if i + j >= len(txs): break + coin_record: Optional[Dict[str, Any]] = None + if txs[i + j].type in CLAWBACK_TRANSACTION_TYPES: + coin_record = ( + await wallet_client.get_coin_records( + GetCoinRecords(coin_id_filter=HashFilter.include([txs[i + j].additions[0].name()])) + ) + )["coin_records"][0] print_transaction( txs[i + j], verbose=(args["verbose"] > 0), name=name, address_prefix=address_prefix, mojo_per_unit=mojo_per_unit, + coin_record=coin_record, ) if i + num_per_screen >= len(txs): return None diff --git a/chia/rpc/wallet_rpc_api.py b/chia/rpc/wallet_rpc_api.py index ffd06ad1bea6..ec8a79b87cc2 100644 --- a/chia/rpc/wallet_rpc_api.py +++ b/chia/rpc/wallet_rpc_api.py @@ -75,7 +75,7 @@ from chia.wallet.util.compute_hints import compute_coin_hints from chia.wallet.util.compute_memos import compute_memos from chia.wallet.util.query_filter import HashFilter, TransactionTypeFilter -from chia.wallet.util.transaction_type import TransactionType +from chia.wallet.util.transaction_type import CLAWBACK_TRANSACTION_TYPES, TransactionType from chia.wallet.util.wallet_sync_utils import fetch_coin_spend_for_coin_state from chia.wallet.util.wallet_types import CoinType, WalletType from chia.wallet.vc_wallet.vc_store import VCProofs @@ -899,12 +899,11 @@ async def get_transactions(self, request: Dict) -> EndpointResult: ) tx_list = [] # Format for clawback transactions - clawback_types = {TransactionType.INCOMING_CLAWBACK_RECEIVE.value, TransactionType.INCOMING_CLAWBACK_SEND.value} for tr in transactions: try: tx = (await self._convert_tx_puzzle_hash(tr)).to_json_dict_convenience(self.service.config) tx_list.append(tx) - if tx["type"] not in clawback_types: + if tx["type"] not in CLAWBACK_TRANSACTION_TYPES: continue coin: Coin = tr.additions[0] record: Optional[WalletCoinRecord] = await self.service.wallet_state_manager.coin_store.get_coin_record( diff --git a/chia/wallet/util/transaction_type.py b/chia/wallet/util/transaction_type.py index b5d1ae927906..eff5090c0c8f 100644 --- a/chia/wallet/util/transaction_type.py +++ b/chia/wallet/util/transaction_type.py @@ -13,3 +13,9 @@ class TransactionType(IntEnum): INCOMING_CLAWBACK_RECEIVE = 6 INCOMING_CLAWBACK_SEND = 7 OUTGOING_CLAWBACK = 8 + + +CLAWBACK_TRANSACTION_TYPES = { + TransactionType.INCOMING_CLAWBACK_SEND.value, + TransactionType.INCOMING_CLAWBACK_RECEIVE.value, +} diff --git a/chia/wallet/wallet_state_manager.py b/chia/wallet/wallet_state_manager.py index 96477cfbba04..293f8fd5fc8a 100644 --- a/chia/wallet/wallet_state_manager.py +++ b/chia/wallet/wallet_state_manager.py @@ -82,7 +82,7 @@ from chia.wallet.util.compute_memos import compute_memos from chia.wallet.util.puzzle_decorator import PuzzleDecoratorManager from chia.wallet.util.query_filter import HashFilter -from chia.wallet.util.transaction_type import TransactionType +from chia.wallet.util.transaction_type import CLAWBACK_TRANSACTION_TYPES, TransactionType from chia.wallet.util.wallet_sync_utils import ( PeerRequestException, fetch_coin_spend_for_coin_state, @@ -1442,10 +1442,7 @@ async def _add_coin_states( await self.interested_store.remove_interested_coin_id(coin_state.coin.name()) confirmed_tx_records: List[TransactionRecord] = [] for tx_record in all_unconfirmed: - if tx_record.type in { - TransactionType.INCOMING_CLAWBACK_SEND.value, - TransactionType.INCOMING_CLAWBACK_RECEIVE.value, - }: + if tx_record.type in CLAWBACK_TRANSACTION_TYPES: for add_coin in tx_record.additions: if add_coin == coin_state.coin: confirmed_tx_records.append(tx_record) diff --git a/tests/wallet/test_wallet.py b/tests/wallet/test_wallet.py index 8f236add4abc..767b16320f38 100644 --- a/tests/wallet/test_wallet.py +++ b/tests/wallet/test_wallet.py @@ -427,6 +427,7 @@ async def test_wallet_clawback_claim_manual( wallet_node_2, server_3 = wallets[1] wallet = wallet_node.wallet_state_manager.main_wallet wallet_1 = wallet_node_2.wallet_state_manager.main_wallet + api_0 = WalletRpcApi(wallet_node) api_1 = WalletRpcApi(wallet_node_2) if trusted: wallet_node.config["trusted_peers"] = {server_1.node_id.hex(): server_1.node_id.hex()} @@ -486,6 +487,20 @@ async def test_wallet_clawback_claim_manual( await time_out_assert(10, wallet.get_confirmed_balance, 3999999999500) await time_out_assert(10, wallet_1.get_confirmed_balance, 4000000000500) + txs = await api_0.get_transactions( + dict( + type_filter={ + "values": [ + TransactionType.INCOMING_CLAWBACK_SEND.value, + ], + "mode": 1, + }, + wallet_id=1, + ) + ) + assert len(txs["transactions"]) == 1 + assert txs["transactions"][0]["confirmed"] + @pytest.mark.parametrize( "trusted", [True, False], From df948b073507d658507469c0ff19c050b236bf41 Mon Sep 17 00:00:00 2001 From: ytx1991 Date: Mon, 12 Jun 2023 23:42:53 -0700 Subject: [PATCH 4/5] Fix coverage --- chia/cmds/wallet_funcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index bd77cdc1f2cc..a19765e01751 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -58,7 +58,7 @@ def print_transaction( address_prefix: str, mojo_per_unit: int, coin_record: Optional[Dict[str, Any]] = None, -) -> None: +) -> None: # pragma: no cover if verbose: print(tx) else: From 7348ba3ba8cacbe605d773aff3162c1ff630eb16 Mon Sep 17 00:00:00 2001 From: ytx1991 Date: Tue, 13 Jun 2023 10:38:48 -0700 Subject: [PATCH 5/5] Fix get transaction amount --- chia/cmds/wallet_funcs.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index a19765e01751..0be6db8522cc 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -44,6 +44,9 @@ TransactionType.FEE_REWARD: "rewarded", TransactionType.INCOMING_TRADE: "received in trade", TransactionType.OUTGOING_TRADE: "sent in trade", + TransactionType.INCOMING_CLAWBACK_RECEIVE: "received in clawback as recipient", + TransactionType.INCOMING_CLAWBACK_SEND: "received in clawback as sender", + TransactionType.OUTGOING_CLAWBACK: "claim/clawback", } @@ -71,16 +74,12 @@ def print_transaction( print(f"To address: {to_address}") print("Created at:", datetime.fromtimestamp(tx.created_at_time).strftime("%Y-%m-%d %H:%M:%S")) if coin_record is not None: - if tx.type == TransactionType.INCOMING_CLAWBACK_RECEIVE.value: - print("Clawback: Recipient") - print( - "Claimable after:", - datetime.fromtimestamp(tx.created_at_time + coin_record["metadata"]["time_lock"]).strftime( - "%Y-%m-%d %H:%M:%S" - ), - ) - else: - print("Clawback: Sender") + print( + "Recipient claimable time:", + datetime.fromtimestamp(tx.created_at_time + coin_record["metadata"]["time_lock"]).strftime( + "%Y-%m-%d %H:%M:%S" + ), + ) print("")