From e8394ef976804eda2c4ef245e48133f2617926ed Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 22 Oct 2025 11:51:42 -0700 Subject: [PATCH] Port `get_farmed_amount` to `@marshal` --- chia/_tests/wallet/rpc/test_wallet_rpc.py | 27 +++++++++++---------- chia/cmds/farm_funcs.py | 23 +++++++++--------- chia/wallet/wallet_request_types.py | 18 ++++++++++++++ chia/wallet/wallet_rpc_api.py | 29 ++++++++++++----------- chia/wallet/wallet_rpc_client.py | 6 +++-- 5 files changed, 63 insertions(+), 40 deletions(-) diff --git a/chia/_tests/wallet/rpc/test_wallet_rpc.py b/chia/_tests/wallet/rpc/test_wallet_rpc.py index e4adc32f512a..f6d95e83eb56 100644 --- a/chia/_tests/wallet/rpc/test_wallet_rpc.py +++ b/chia/_tests/wallet/rpc/test_wallet_rpc.py @@ -139,6 +139,8 @@ FungibleAsset, GetAllOffers, GetCoinRecordsByNames, + GetFarmedAmount, + GetFarmedAmountResponse, GetNextAddress, GetNotifications, GetOffer, @@ -549,21 +551,20 @@ async def test_get_farmed_amount(wallet_rpc_environment: WalletRpcTestEnvironmen wallet_rpc_client = env.wallet_1.rpc_client await full_node_api.farm_blocks_to_wallet(2, wallet) - get_farmed_amount_result = await wallet_rpc_client.get_farmed_amount() + get_farmed_amount_result = await wallet_rpc_client.get_farmed_amount(GetFarmedAmount()) get_timestamp_for_height_result = await wallet_rpc_client.get_timestamp_for_height( GetTimestampForHeight(uint32(3)) ) # genesis + 2 - expected_result = { - "blocks_won": 2, - "farmed_amount": 4_000_000_000_000, - "farmer_reward_amount": 500_000_000_000, - "fee_amount": 0, - "last_height_farmed": 3, - "last_time_farmed": get_timestamp_for_height_result.timestamp, - "pool_reward_amount": 3_500_000_000_000, - "success": True, - } + expected_result = GetFarmedAmountResponse( + blocks_won=uint32(2), + farmed_amount=uint64(4_000_000_000_000), + farmer_reward_amount=uint64(500_000_000_000), + fee_amount=uint64(0), + last_height_farmed=uint32(3), + last_time_farmed=uint64(get_timestamp_for_height_result.timestamp), + pool_reward_amount=uint64(3_500_000_000_000), + ) assert get_farmed_amount_result == expected_result @@ -591,8 +592,8 @@ async def test_get_farmed_amount_with_fee(wallet_rpc_environment: WalletRpcTestE await full_node_api.farm_blocks_to_puzzlehash(count=2, farm_to=our_ph, guarantee_transaction_blocks=True) await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20) - result = await wallet_rpc_client.get_farmed_amount() - assert result["fee_amount"] == fee_amount + result = await wallet_rpc_client.get_farmed_amount(GetFarmedAmount()) + assert result.fee_amount == fee_amount @pytest.mark.anyio diff --git a/chia/cmds/farm_funcs.py b/chia/cmds/farm_funcs.py index 560a65b39b76..c26f2111d00f 100644 --- a/chia/cmds/farm_funcs.py +++ b/chia/cmds/farm_funcs.py @@ -13,6 +13,7 @@ from chia.full_node.full_node_rpc_client import FullNodeRpcClient from chia.util.errors import CliRpcConnectionError from chia.util.network import is_localhost +from chia.wallet.wallet_request_types import GetFarmedAmount, GetFarmedAmountResponse from chia.wallet.wallet_rpc_client import WalletRpcClient SECONDS_PER_BLOCK = (24 * 3600) / 4608 @@ -53,9 +54,9 @@ async def get_wallets_stats( wallet_rpc_port: Optional[int], root_path: Path, include_pool_rewards: bool, -) -> Optional[dict[str, Any]]: +) -> Optional[GetFarmedAmountResponse]: async with get_any_service_client(WalletRpcClient, root_path, wallet_rpc_port) as (wallet_client, _): - return await wallet_client.get_farmed_amount(include_pool_rewards) + return await wallet_client.get_farmed_amount(GetFarmedAmount(include_pool_rewards)) async def get_challenges(root_path: Path, farmer_rpc_port: Optional[int]) -> Optional[list[dict[str, Any]]]: @@ -123,23 +124,23 @@ async def summary( print("Farming") if amounts is not None: - print(f"Total chia farmed: {amounts['farmed_amount'] / units['chia']}") - print(f"User transaction fees: {amounts['fee_amount'] / units['chia']}") + print(f"Total chia farmed: {amounts.farmed_amount / units['chia']}") + print(f"User transaction fees: {amounts.fee_amount / units['chia']}") if include_pool_rewards: - print(f"Farmer rewards: {amounts['farmer_reward_amount'] / units['chia']}") - print(f"Pool rewards: {amounts['pool_reward_amount'] / units['chia']}") - print(f"Total rewards: {(amounts['farmer_reward_amount'] + amounts['pool_reward_amount']) / units['chia']}") + print(f"Farmer rewards: {amounts.farmer_reward_amount / units['chia']}") + print(f"Pool rewards: {amounts.pool_reward_amount / units['chia']}") + print(f"Total rewards: {(amounts.farmer_reward_amount + amounts.pool_reward_amount) / units['chia']}") if blockchain_state is not None and blockchain_state["peak"] is not None: peak_height = blockchain_state["peak"].height - blocks_since_last_farm = peak_height - amounts["last_height_farmed"] - print(f"Current/Last height farmed: {peak_height}/{amounts['last_height_farmed']}") + blocks_since_last_farm = peak_height - amounts.last_height_farmed + print(f"Current/Last height farmed: {peak_height}/{amounts.last_height_farmed}") print(f"Blocks since last farmed: {blocks_since_last_farm}") print( f"Time since last farmed: {format_minutes(int((blocks_since_last_farm * SECONDS_PER_BLOCK) / 60))}" ) else: - print(f"Block rewards: {(amounts['farmer_reward_amount'] + amounts['pool_reward_amount']) / units['chia']}") - print(f"Last height farmed: {amounts['last_height_farmed']}") + print(f"Block rewards: {(amounts.farmer_reward_amount + amounts.pool_reward_amount) / units['chia']}") + print(f"Last height farmed: {amounts.last_height_farmed}") class PlotStats: total_plot_size = 0 diff --git a/chia/wallet/wallet_request_types.py b/chia/wallet/wallet_request_types.py index 700155fa1c5c..ff9578975cfa 100644 --- a/chia/wallet/wallet_request_types.py +++ b/chia/wallet/wallet_request_types.py @@ -2448,3 +2448,21 @@ class CRCATApprovePending(TransactionEndpointRequest): @dataclass(frozen=True) class CRCATApprovePendingResponse(TransactionEndpointResponse): pass + + +@streamable +@dataclass(frozen=True) +class GetFarmedAmount(Streamable): + include_pool_rewards: bool = False + + +@streamable +@dataclass(frozen=True) +class GetFarmedAmountResponse(Streamable): + farmed_amount: uint64 + pool_reward_amount: uint64 + farmer_reward_amount: uint64 + fee_amount: uint64 + last_height_farmed: uint32 + last_time_farmed: uint64 + blocks_won: uint32 diff --git a/chia/wallet/wallet_rpc_api.py b/chia/wallet/wallet_rpc_api.py index cc0836757ef3..b2869e5acc69 100644 --- a/chia/wallet/wallet_rpc_api.py +++ b/chia/wallet/wallet_rpc_api.py @@ -202,6 +202,8 @@ GetCoinRecordsByNames, GetCoinRecordsByNamesResponse, GetCurrentDerivationIndexResponse, + GetFarmedAmount, + GetFarmedAmountResponse, GetHeightInfoResponse, GetLoggedInFingerprintResponse, GetNextAddress, @@ -3453,7 +3455,8 @@ async def get_coin_records(self, request: dict[str, Any]) -> EndpointResult: "total_count": result.total_count, } - async def get_farmed_amount(self, request: dict[str, Any]) -> EndpointResult: + @marshal + async def get_farmed_amount(self, request: GetFarmedAmount) -> GetFarmedAmountResponse: tx_records: list[TransactionRecord] = await self.service.wallet_state_manager.tx_store.get_farming_rewards() amount = 0 pool_reward_amount = 0 @@ -3462,14 +3465,12 @@ async def get_farmed_amount(self, request: dict[str, Any]) -> EndpointResult: blocks_won = 0 last_height_farmed = uint32(0) - include_pool_rewards = request.get("include_pool_rewards", False) - for record in tx_records: if record.wallet_id not in self.service.wallet_state_manager.wallets: continue if record.type == TransactionType.COINBASE_REWARD.value: if ( - not include_pool_rewards + not request.include_pool_rewards and self.service.wallet_state_manager.wallets[record.wallet_id].type() == WalletType.POOLING_WALLET ): # Don't add pool rewards for pool wallets unless explicitly requested @@ -3489,19 +3490,19 @@ async def get_farmed_amount(self, request: dict[str, Any]) -> EndpointResult: last_height_farmed = max(last_height_farmed, height) amount += record.amount - last_time_farmed = uint64( + last_time_farmed = ( await self.service.get_timestamp_for_height(last_height_farmed) if last_height_farmed > 0 else 0 ) assert amount == pool_reward_amount + farmer_reward_amount + fee_amount - return { - "farmed_amount": amount, - "pool_reward_amount": pool_reward_amount, - "farmer_reward_amount": farmer_reward_amount, - "fee_amount": fee_amount, - "last_height_farmed": last_height_farmed, - "last_time_farmed": last_time_farmed, - "blocks_won": blocks_won, - } + return GetFarmedAmountResponse( + farmed_amount=uint64(amount), + pool_reward_amount=uint64(pool_reward_amount), + farmer_reward_amount=uint64(farmer_reward_amount), + fee_amount=uint64(fee_amount), + last_height_farmed=uint32(last_height_farmed), + last_time_farmed=uint64(last_time_farmed), + blocks_won=uint32(blocks_won), + ) @tx_endpoint(push=False) @marshal diff --git a/chia/wallet/wallet_rpc_client.py b/chia/wallet/wallet_rpc_client.py index 0e3265c2d6ae..7e67106024d9 100644 --- a/chia/wallet/wallet_rpc_client.py +++ b/chia/wallet/wallet_rpc_client.py @@ -104,6 +104,8 @@ GetCoinRecordsByNames, GetCoinRecordsByNamesResponse, GetCurrentDerivationIndexResponse, + GetFarmedAmount, + GetFarmedAmountResponse, GetHeightInfoResponse, GetLoggedInFingerprintResponse, GetNextAddress, @@ -391,8 +393,8 @@ async def extend_derivation_index(self, request: ExtendDerivationIndex) -> Exten await self.fetch("extend_derivation_index", request.to_json_dict()) ) - async def get_farmed_amount(self, include_pool_rewards: bool = False) -> dict[str, Any]: - return await self.fetch("get_farmed_amount", {"include_pool_rewards": include_pool_rewards}) + async def get_farmed_amount(self, request: GetFarmedAmount) -> GetFarmedAmountResponse: + return GetFarmedAmountResponse.from_json_dict(await self.fetch("get_farmed_amount", request.to_json_dict())) async def create_signed_transactions( self,