Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions chia/_tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
FungibleAsset,
GetAllOffers,
GetCoinRecordsByNames,
GetFarmedAmount,
GetFarmedAmountResponse,
GetNextAddress,
GetNotifications,
GetOffer,
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions chia/cmds/farm_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]]:
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions chia/wallet/wallet_request_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 15 additions & 14 deletions chia/wallet/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@
GetCoinRecordsByNames,
GetCoinRecordsByNamesResponse,
GetCurrentDerivationIndexResponse,
GetFarmedAmount,
GetFarmedAmountResponse,
GetHeightInfoResponse,
GetLoggedInFingerprintResponse,
GetNextAddress,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions chia/wallet/wallet_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
GetCoinRecordsByNames,
GetCoinRecordsByNamesResponse,
GetCurrentDerivationIndexResponse,
GetFarmedAmount,
GetFarmedAmountResponse,
GetHeightInfoResponse,
GetLoggedInFingerprintResponse,
GetNextAddress,
Expand Down Expand Up @@ -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,
Expand Down
Loading