[LABS-291] Port get_farmed_amount to @marshal (#20187)

Port `get_farmed_amount` to `@marshal`
This commit is contained in:
Matt Hauff
2025-12-10 09:57:27 -08:00
committed by GitHub
parent d25dbbe652
commit 88e9f78bea
5 changed files with 63 additions and 40 deletions
+14 -13
View File
@@ -130,6 +130,8 @@ from chia.wallet.wallet_request_types import (
FungibleAsset,
GetAllOffers,
GetCoinRecordsByNames,
GetFarmedAmount,
GetFarmedAmountResponse,
GetNextAddress,
GetNotifications,
GetOffer,
@@ -468,21 +470,20 @@ async def test_get_farmed_amount(wallet_environments: WalletTestFramework) -> No
env = wallet_environments.environments[0]
wallet_rpc_client = env.rpc_client
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
@@ -514,8 +515,8 @@ async def test_get_farmed_amount_with_fee(wallet_environments: WalletTestFramewo
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.parametrize(
+12 -11
View File
@@ -14,6 +14,7 @@ from chia.full_node.full_node_rpc_client import FullNodeRpcClient
from chia.util.config import lock_and_load_config, save_config
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
@@ -54,9 +55,9 @@ async def get_wallets_stats(
wallet_rpc_port: int | None,
root_path: Path,
include_pool_rewards: bool,
) -> dict[str, Any] | None:
) -> GetFarmedAmountResponse | None:
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: int | None) -> list[dict[str, Any]] | None:
@@ -124,23 +125,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
+18
View File
@@ -2514,3 +2514,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
+15 -14
View File
@@ -202,6 +202,8 @@ from chia.wallet.wallet_request_types import (
GetCoinRecordsByNames,
GetCoinRecordsByNamesResponse,
GetCurrentDerivationIndexResponse,
GetFarmedAmount,
GetFarmedAmountResponse,
GetHeightInfoResponse,
GetLoggedInFingerprintResponse,
GetNextAddress,
@@ -3185,7 +3187,8 @@ class WalletRpcApi:
"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
@@ -3194,14 +3197,12 @@ class WalletRpcApi:
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
@@ -3221,19 +3222,19 @@ class WalletRpcApi:
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
+4 -2
View File
@@ -104,6 +104,8 @@ from chia.wallet.wallet_request_types import (
GetCoinRecordsByNames,
GetCoinRecordsByNamesResponse,
GetCurrentDerivationIndexResponse,
GetFarmedAmount,
GetFarmedAmountResponse,
GetHeightInfoResponse,
GetLoggedInFingerprintResponse,
GetNextAddress,
@@ -391,8 +393,8 @@ class WalletRpcClient(RpcClient):
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,