From 467aba59db0737aeb61a283a6de4e2a06aeb568a Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 17 Jun 2024 08:15:35 -0700 Subject: [PATCH 1/2] Add offer expiration to CLI --- chia/_tests/cmds/wallet/test_wallet.py | 27 +++++++++++++++++++++++++- chia/cmds/wallet_funcs.py | 18 ++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/chia/_tests/cmds/wallet/test_wallet.py b/chia/_tests/cmds/wallet/test_wallet.py index 751c55f1965e..bd94ac6da0b7 100644 --- a/chia/_tests/cmds/wallet/test_wallet.py +++ b/chia/_tests/cmds/wallet/test_wallet.py @@ -1,5 +1,6 @@ from __future__ import annotations +import datetime import os from pathlib import Path from typing import Any, Dict, List, Optional, Tuple, Union, cast @@ -855,7 +856,12 @@ async def get_all_offers( ], trade_id=bytes32([1 + i] * 32), status=uint32(TradeStatus.PENDING_ACCEPT.value), - valid_times=ConditionValidTimes(), + valid_times=ConditionValidTimes( + min_time=uint64(0), + max_time=uint64(100), + min_height=uint32(0), + max_height=uint32(100), + ), ) records.append(trade_offer) return records @@ -888,6 +894,25 @@ async def get_all_offers( ] run_cli_command_and_assert(capsys, root_dir, command_args, assert_list) expected_calls: logType = {"get_all_offers": [(0, 10, None, True, False, True, True, True)]} + command_args = [ + "wallet", + "get_offers", + FINGERPRINT_ARG, + "--summaries", + ] + # these are various things that should be in the output + assert_list = [ + "Timelock information:", + " - Not valid until ", + " - Expires at ", + f"{datetime.date.fromtimestamp(0).strftime('%Y-%m-%d %H:%M:%S')}", + f"{datetime.date.fromtimestamp(100).strftime('%Y-%m-%d %H:%M:%S')}", + "height 0", + "height 100", + ] + run_cli_command_and_assert(capsys, root_dir, command_args, assert_list) + assert expected_calls["get_all_offers"] is not None + expected_calls["get_all_offers"].append((0, 10, None, False, True, False, False, False)) test_rpc_clients.wallet_rpc_client.check_log(expected_calls) diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index f286c7b2a492..9b4bca3b4abc 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -6,7 +6,7 @@ import pathlib import sys import time -from datetime import datetime +from datetime import date, datetime from decimal import Decimal from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Tuple, Union @@ -629,6 +629,22 @@ async def print_trade_record(record: TradeRecord, wallet_client: WalletRpcClient print("Pending Outbound Balances:") await print_offer_summary(cat_name_resolver, outbound_balances, has_fee=(fees > 0)) print(f"Included Fees: {fees / units['chia']} XCH, {fees} mojos") + print("Timelock information:") + if record.valid_times.min_time is not None: + print( + " - Not valid until " + f"{date.fromtimestamp(record.valid_times.min_time).strftime('%Y-%m-%d %H:%M:%S')}" + ) + if record.valid_times.min_height is not None: + print(f" - Not valid until height {record.valid_times.min_height}") + if record.valid_times.max_time is not None: + print( + " - Expires at " + f"{date.fromtimestamp(record.valid_times.max_time).strftime('%Y-%m-%d %H:%M:%S')} " + "(+/- 10 min)" + ) + if record.valid_times.max_height is not None: + print(f" - Expires at height {record.valid_times.max_height} (wait ~10 blocks after to be reorg safe)") print("---------------") From e75aeb6909d4cb5e69834888fefe5267340fc0e6 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 20 Jun 2024 13:24:35 -0700 Subject: [PATCH 2/2] Add timezone information --- chia/_tests/cmds/wallet/test_wallet.py | 5 +++-- chia/cmds/wallet_funcs.py | 18 ++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/chia/_tests/cmds/wallet/test_wallet.py b/chia/_tests/cmds/wallet/test_wallet.py index bd94ac6da0b7..2fbae9253c10 100644 --- a/chia/_tests/cmds/wallet/test_wallet.py +++ b/chia/_tests/cmds/wallet/test_wallet.py @@ -900,13 +900,14 @@ async def get_all_offers( FINGERPRINT_ARG, "--summaries", ] + tzinfo = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo # these are various things that should be in the output assert_list = [ "Timelock information:", " - Not valid until ", " - Expires at ", - f"{datetime.date.fromtimestamp(0).strftime('%Y-%m-%d %H:%M:%S')}", - f"{datetime.date.fromtimestamp(100).strftime('%Y-%m-%d %H:%M:%S')}", + f"{datetime.datetime.fromtimestamp(0, tz=tzinfo).strftime('%Y-%m-%d %H:%M %Z')}", + f"{datetime.datetime.fromtimestamp(100, tz=tzinfo).strftime('%Y-%m-%d %H:%M %Z')}", "height 0", "height 100", ] diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index 9b4bca3b4abc..ab762b9ae60e 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -6,7 +6,7 @@ import pathlib import sys import time -from datetime import date, datetime +from datetime import datetime, timezone from decimal import Decimal from typing import Any, Awaitable, Callable, Dict, List, Optional, Sequence, Tuple, Union @@ -607,6 +607,11 @@ async def print_offer_summary( print(output) +def format_timestamp_with_timezone(timestamp: int) -> str: + tzinfo = datetime.now(timezone.utc).astimezone().tzinfo + return datetime.fromtimestamp(timestamp, tz=tzinfo).strftime("%Y-%m-%d %H:%M %Z") + + async def print_trade_record(record: TradeRecord, wallet_client: WalletRpcClient, summaries: bool = False) -> None: print() print(f"Record with id: {record.trade_id}") @@ -631,18 +636,11 @@ async def print_trade_record(record: TradeRecord, wallet_client: WalletRpcClient print(f"Included Fees: {fees / units['chia']} XCH, {fees} mojos") print("Timelock information:") if record.valid_times.min_time is not None: - print( - " - Not valid until " - f"{date.fromtimestamp(record.valid_times.min_time).strftime('%Y-%m-%d %H:%M:%S')}" - ) + print(" - Not valid until " f"{format_timestamp_with_timezone(record.valid_times.min_time)}") if record.valid_times.min_height is not None: print(f" - Not valid until height {record.valid_times.min_height}") if record.valid_times.max_time is not None: - print( - " - Expires at " - f"{date.fromtimestamp(record.valid_times.max_time).strftime('%Y-%m-%d %H:%M:%S')} " - "(+/- 10 min)" - ) + print(" - Expires at " f"{format_timestamp_with_timezone(record.valid_times.max_time)} " "(+/- 10 min)") if record.valid_times.max_height is not None: print(f" - Expires at height {record.valid_times.max_height} (wait ~10 blocks after to be reorg safe)") print("---------------")