Return existing CAT wallet instead of raising (#14101)

* Return existing CAT wallet instead of raising

There's no reason to raise here, all the caller is looking for is an instance of the CATWallet that they're looking for and to create it if it doesn't exist.

* rename create_wallet_for_cat -> get_or_...

* Move wallet created noti inside of function

* remove redundant call
This commit is contained in:
Matt Hauff 2022-12-13 21:46:16 -07:00 committed by GitHub
parent c2529e1f9e
commit ffd02b2f80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 17 additions and 17 deletions

View File

@ -588,10 +588,9 @@ class WalletRpcApi:
elif request["mode"] == "existing":
async with self.service.wallet_state_manager.lock:
cat_wallet = await CATWallet.create_wallet_for_cat(
cat_wallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_state_manager, main_wallet, request["asset_id"], name
)
self.service.wallet_state_manager.state_changed("wallet_created")
return {"type": cat_wallet.type(), "asset_id": request["asset_id"], "wallet_id": cat_wallet.id()}
else: # undefined mode

View File

@ -174,7 +174,7 @@ class CATWallet:
return self
@staticmethod
async def create_wallet_for_cat(
async def get_or_create_wallet_for_cat(
wallet_state_manager: WalletStateManager,
wallet: Wallet,
limitations_program_hash_hex: str,
@ -192,7 +192,7 @@ class CATWallet:
assert isinstance(w, CATWallet)
if w.get_asset_id() == limitations_program_hash_hex:
self.log.warning("Not creating wallet for already existing CAT wallet")
raise ValueError("Wallet already exists")
return w
self.wallet_state_manager = wallet_state_manager
if limitations_program_hash_hex in DEFAULT_CATS:
@ -218,7 +218,7 @@ class CATWallet:
puzzle_driver: PuzzleInfo,
name: Optional[str] = None,
) -> CATWallet:
return await cls.create_wallet_for_cat(
return await cls.get_or_create_wallet_for_cat(
wallet_state_manager,
wallet,
puzzle_driver["tail"].hex(),

View File

@ -739,10 +739,11 @@ class WalletStateManager:
if bytes(tail_hash).hex()[2:] in self.default_cats or self.config.get(
"automatically_add_unknown_cats", False
):
cat_wallet = await CATWallet.create_wallet_for_cat(self, self.main_wallet, bytes(tail_hash).hex()[2:])
cat_wallet = await CATWallet.get_or_create_wallet_for_cat(
self, self.main_wallet, bytes(tail_hash).hex()[2:]
)
wallet_id = cat_wallet.id()
wallet_type = WalletType(cat_wallet.type())
self.state_changed("wallet_created")
else:
# Found unacknowledged CAT, save it in the database.
await self.interested_store.add_unacknowledged_token(

View File

@ -184,7 +184,7 @@ class TestCATWallet:
assert cat_wallet.cat_info.limitations_program_hash is not None
asset_id = cat_wallet.get_asset_id()
cat_wallet_2: CATWallet = await CATWallet.create_wallet_for_cat(
cat_wallet_2: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_2.wallet_state_manager, wallet2, asset_id
)
@ -276,7 +276,7 @@ class TestCATWallet:
# Test that the a default CAT will initialize correctly
asset = DEFAULT_CATS[next(iter(DEFAULT_CATS))]
asset_id = asset["asset_id"]
cat_wallet_2 = await CATWallet.create_wallet_for_cat(wallet_node.wallet_state_manager, wallet, asset_id)
cat_wallet_2 = await CATWallet.get_or_create_wallet_for_cat(wallet_node.wallet_state_manager, wallet, asset_id)
assert await cat_wallet_2.get_name() == asset["name"]
await cat_wallet_2.set_name("Test Name")
assert await cat_wallet_2.get_name() == "Test Name"
@ -332,7 +332,7 @@ class TestCATWallet:
assert cat_wallet.cat_info.limitations_program_hash is not None
asset_id = cat_wallet.get_asset_id()
cat_wallet_2: CATWallet = await CATWallet.create_wallet_for_cat(
cat_wallet_2: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_2.wallet_state_manager, wallet2, asset_id
)
@ -422,11 +422,11 @@ class TestCATWallet:
assert cat_wallet_0.cat_info.limitations_program_hash is not None
asset_id = cat_wallet_0.get_asset_id()
cat_wallet_1: CATWallet = await CATWallet.create_wallet_for_cat(
cat_wallet_1: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_1.wallet_state_manager, wallet_1, asset_id
)
cat_wallet_2: CATWallet = await CATWallet.create_wallet_for_cat(
cat_wallet_2: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_2.wallet_state_manager, wallet_2, asset_id
)

View File

@ -63,7 +63,7 @@ class TestCATTrades:
# Add the taker's CAT to the maker's wallet
assert cat_wallet_maker.cat_info.my_tail is not None
assert new_cat_wallet_taker.cat_info.my_tail is not None
new_cat_wallet_maker: CATWallet = await CATWallet.create_wallet_for_cat(
new_cat_wallet_maker: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_maker.wallet_state_manager, wallet_maker, new_cat_wallet_taker.get_asset_id()
)

View File

@ -669,7 +669,7 @@ async def test_nft_offer_sell_nft_for_cat(self_hostname: str, two_wallet_nodes:
await time_out_assert(20, cat_wallet_maker.get_confirmed_balance, cats_to_mint)
await time_out_assert(20, cat_wallet_maker.get_unconfirmed_balance, cats_to_mint)
cat_wallet_taker: CATWallet = await CATWallet.create_wallet_for_cat(
cat_wallet_taker: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_taker.wallet_state_manager, wallet_taker, cat_wallet_maker.get_asset_id()
)
@ -857,7 +857,7 @@ async def test_nft_offer_request_nft_for_cat(
await time_out_assert(20, cat_wallet_maker.get_confirmed_balance, cats_to_mint)
await time_out_assert(20, cat_wallet_maker.get_unconfirmed_balance, cats_to_mint)
cat_wallet_taker: CATWallet = await CATWallet.create_wallet_for_cat(
cat_wallet_taker: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_taker.wallet_state_manager, wallet_taker, cat_wallet_maker.get_asset_id()
)
if test_change:

View File

@ -544,11 +544,11 @@ async def test_nft_offer_nft_for_cat(self_hostname: str, two_wallet_nodes: Any,
await time_out_assert(20, cat_wallet_taker.get_confirmed_balance, cats_to_mint)
await time_out_assert(20, cat_wallet_taker.get_unconfirmed_balance, cats_to_mint)
wallet_maker_for_taker_cat: CATWallet = await CATWallet.create_wallet_for_cat(
wallet_maker_for_taker_cat: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_0.wallet_state_manager, wallet_maker, cat_wallet_taker.get_asset_id()
)
wallet_taker_for_maker_cat: CATWallet = await CATWallet.create_wallet_for_cat(
wallet_taker_for_maker_cat: CATWallet = await CATWallet.get_or_create_wallet_for_cat(
wallet_node_1.wallet_state_manager, wallet_taker, cat_wallet_maker.get_asset_id()
)