Skip to content

Commit d22bc98

Browse files
committed
Port cat_get_name
1 parent 0ab252e commit d22bc98

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

chia/_tests/cmds/cmd_test_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from chia.wallet.util.tx_config import TXConfig
3434
from chia.wallet.util.wallet_types import WalletType
3535
from chia.wallet.wallet_request_types import (
36+
CATGetName,
37+
CATGetNameResponse,
3638
GetSyncStatusResponse,
3739
GetTransaction,
3840
GetTransactionResponse,
@@ -146,9 +148,9 @@ async def get_transaction(self, request: GetTransaction) -> GetTransactionRespon
146148
bytes32([2] * 32),
147149
)
148150

149-
async def get_cat_name(self, wallet_id: int) -> str:
150-
self.add_to_log("get_cat_name", (wallet_id,))
151-
return "test" + str(wallet_id)
151+
async def get_cat_name(self, request: CATGetName) -> CATGetNameResponse:
152+
self.add_to_log("get_cat_name", (request.wallet_id,))
153+
return CATGetNameResponse(request.wallet_id, "test" + str(request.wallet_id))
152154

153155
async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMessageByAddressResponse:
154156
self.add_to_log("sign_message_by_address", (request.address, request.message))

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
from chia.wallet.wallet_protocol import WalletProtocol
105105
from chia.wallet.wallet_request_types import (
106106
AddKey,
107+
CATGetName,
107108
CATSetName,
108109
CheckDeleteKey,
109110
ClawbackPuzzleDecoratorOverride,
@@ -1205,11 +1206,11 @@ async def test_cat_endpoints(wallet_environments: WalletTestFramework, wallet_ty
12051206
<= (await env_0.rpc_client.get_wallet_balance(GetWalletBalance(cat_0_id))).wallet_balance.to_json_dict().items()
12061207
)
12071208
asset_id = await env_0.rpc_client.get_cat_asset_id(cat_0_id)
1208-
assert (await env_0.rpc_client.get_cat_name(cat_0_id)) == wallet_type.default_wallet_name_for_unknown_cat(
1209-
asset_id.hex()
1210-
)
1209+
assert (
1210+
await env_0.rpc_client.get_cat_name(CATGetName(cat_0_id))
1211+
).name == wallet_type.default_wallet_name_for_unknown_cat(asset_id.hex())
12111212
await env_0.rpc_client.set_cat_name(CATSetName(cat_0_id, "My cat"))
1212-
assert (await env_0.rpc_client.get_cat_name(cat_0_id)) == "My cat"
1213+
assert (await env_0.rpc_client.get_cat_name(CATGetName(cat_0_id))).name == "My cat"
12131214
result = await env_0.rpc_client.cat_asset_id_to_name(asset_id)
12141215
assert result is not None
12151216
wid, name = result

chia/cmds/wallet_funcs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from chia.wallet.vc_wallet.vc_store import VCProofs
4646
from chia.wallet.wallet_coin_store import GetCoinRecords
4747
from chia.wallet.wallet_request_types import (
48+
CATGetName,
4849
CATSetName,
4950
CATSpendResponse,
5051
ClawbackPuzzleDecoratorOverride,
@@ -185,7 +186,7 @@ async def get_unit_name_for_wallet_id(
185186
}:
186187
name: str = config["network_overrides"]["config"][config["selected_network"]]["address_prefix"].upper()
187188
elif wallet_type in {WalletType.CAT, WalletType.CRCAT, WalletType.RCAT}:
188-
name = await wallet_client.get_cat_name(wallet_id=wallet_id)
189+
name = (await wallet_client.get_cat_name(CATGetName(wallet_id=uint32(wallet_id)))).name
189190
else:
190191
raise LookupError(f"Operation is not supported for Wallet type {wallet_type.name}")
191192

@@ -571,7 +572,7 @@ async def make_offer(
571572
name = "XCH"
572573
unit = units["chia"]
573574
else:
574-
name = await wallet_client.get_cat_name(id)
575+
name = (await wallet_client.get_cat_name(CATGetName(id))).name
575576
unit = units["cat"]
576577
if item in offers:
577578
fungible_assets.append(FungibleAsset(name, uint64(abs(int(Decimal(amount) * unit)))))

chia/wallet/wallet_request_types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,19 @@ class CATSetNameResponse(Streamable):
607607
wallet_id: uint32
608608

609609

610+
@streamable
611+
@dataclass(frozen=True)
612+
class CATGetName(Streamable):
613+
wallet_id: uint32
614+
615+
616+
@streamable
617+
@dataclass(frozen=True)
618+
class CATGetNameResponse(Streamable):
619+
wallet_id: uint32
620+
name: str
621+
622+
610623
@streamable
611624
@dataclass(frozen=True)
612625
class DIDSetWalletName(Streamable):

chia/wallet/wallet_rpc_api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
ApplySignatures,
113113
ApplySignaturesResponse,
114114
BalanceResponse,
115+
CATGetName,
116+
CATGetNameResponse,
115117
CATSetName,
116118
CATSetNameResponse,
117119
CheckDeleteKey,
@@ -2106,11 +2108,11 @@ async def cat_set_name(self, request: CATSetName) -> CATSetNameResponse:
21062108
await wallet.set_name(request.name)
21072109
return CATSetNameResponse(wallet_id=request.wallet_id)
21082110

2109-
async def cat_get_name(self, request: dict[str, Any]) -> EndpointResult:
2110-
wallet_id = uint32(request["wallet_id"])
2111-
wallet = self.service.wallet_state_manager.get_wallet(id=wallet_id, required_type=CATWallet)
2111+
@marshal
2112+
async def cat_get_name(self, request: CATGetName) -> CATGetNameResponse:
2113+
wallet = self.service.wallet_state_manager.get_wallet(id=request.wallet_id, required_type=CATWallet)
21122114
name: str = wallet.get_name()
2113-
return {"wallet_id": wallet_id, "name": name}
2115+
return CATGetNameResponse(wallet_id=request.wallet_id, name=name)
21142116

21152117
async def get_stray_cats(self, request: dict[str, Any]) -> EndpointResult:
21162118
"""

chia/wallet/wallet_rpc_client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
ApplySignaturesResponse,
2525
CancelOfferResponse,
2626
CancelOffersResponse,
27+
CATGetName,
28+
CATGetNameResponse,
2729
CATSetName,
2830
CATSetNameResponse,
2931
CATSpendResponse,
@@ -645,11 +647,8 @@ async def cat_asset_id_to_name(self, asset_id: bytes32) -> Optional[tuple[Option
645647
wallet_id: Optional[uint32] = None if res["wallet_id"] is None else uint32(res["wallet_id"])
646648
return wallet_id, res["name"]
647649

648-
async def get_cat_name(self, wallet_id: int) -> str:
649-
request = {"wallet_id": wallet_id}
650-
response = await self.fetch("cat_get_name", request)
651-
# TODO: casting due to lack of type checked deserialization
652-
return cast(str, response["name"])
650+
async def get_cat_name(self, request: CATGetName) -> CATGetNameResponse:
651+
return CATGetNameResponse.from_json_dict(await self.fetch("cat_get_name", request.to_json_dict()))
653652

654653
async def set_cat_name(self, request: CATSetName) -> CATSetNameResponse:
655654
return CATSetNameResponse.from_json_dict(await self.fetch("cat_set_name", request.to_json_dict()))

0 commit comments

Comments
 (0)