Skip to content

Commit

Permalink
[CHIA-1204] Add missing client endpoints for corresponding Wallet RPC…
Browse files Browse the repository at this point in the history
… endpoints (#18578)

* Add client endpoints for `get/set_auto_claim`

* Add client endpoint for `verify_signature`

* Add client endpoint for `get_transaction_memo`

* Add client endpoint for `get_offers_count`

* Add client endpoint for `get_cat_list`

* Add client endpoint for `did_get_pubkey`

* Port `test_creation_from_backup_file` to `WalletTestFramework`

* Add client endpoint for `did_get_information_needed_for_recovery`

* Add client endpoint for `did_get_current_coin_info`

* Add client endpoint for `nft_get_by_did`

* Add client endpoint for `nft_set_nft_status`

* Add client endpoint for `nft_get_wallets_with_dids`

* Add client endpoint for `nft_set_did_bulk`

* Add client endpoint for `nft_transfer_bulk`

* Make sure we have a proper tx_id in test_wallet_make_transaction_with_memo.

* Reuse request's address after checking it.

* Minor readability improvement.

---------

Co-authored-by: Amine Khaldi <[email protected]>
  • Loading branch information
Quexington and AmineKhaldi authored Sep 12, 2024
1 parent c256c8c commit 810f0bf
Show file tree
Hide file tree
Showing 8 changed files with 662 additions and 173 deletions.
19 changes: 9 additions & 10 deletions chia/_tests/wallet/cat_wallet/test_cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from chia._tests.environments.wallet import WalletEnvironment, WalletStateTransition, WalletTestFramework
from chia._tests.util.time_out_assert import time_out_assert, time_out_assert_not_none
from chia.protocols.wallet_protocol import CoinState
from chia.rpc.wallet_request_types import GetTransactionMemo
from chia.simulator.simulator_protocol import ReorgProtocol
from chia.types.blockchain_format.coin import Coin, coin_as_list
from chia.types.blockchain_format.program import Program
Expand Down Expand Up @@ -242,8 +243,6 @@ async def test_cat_spend(wallet_environments: WalletTestFramework) -> None:
env_2: WalletEnvironment = wallet_environments.environments[1]
wallet_node = env_1.node
wallet_node_2 = env_2.node
api_0 = env_1.rpc_api
api_1 = env_2.rpc_api
wallet = env_1.xch_wallet
wallet2 = env_2.xch_wallet
full_node_api = wallet_environments.full_node
Expand Down Expand Up @@ -327,11 +326,11 @@ async def test_cat_spend(wallet_environments: WalletTestFramework) -> None:
if tx_record.wallet_id == cat_wallet.id():
assert tx_record.to_puzzle_hash == cat_2_hash
if tx_record.spend_bundle is not None:
tx_id = tx_record.name.hex()
tx_id = tx_record.name
assert tx_id is not None
memos = await api_0.get_transaction_memo({"transaction_id": tx_id})
assert len(memos[tx_id]) == 2 # One for tx, one for change
assert list(memos[tx_id].values())[0][0] == cat_2_hash.hex()
memos = await env_1.rpc_client.get_transaction_memo(GetTransactionMemo(transaction_id=tx_id))
assert len(memos.coins_with_memos) == 2
assert memos.coins_with_memos[1].memos[0] == cat_2_hash

await wallet_environments.process_pending_states(
[
Expand Down Expand Up @@ -406,10 +405,10 @@ async def test_cat_spend(wallet_environments: WalletTestFramework) -> None:
coins = await cat_wallet_2.select_coins(uint64(60), action_scope)
assert len(coins) == 1
coin = coins.pop()
tx_id = coin.name().hex()
memos = await api_1.get_transaction_memo(dict(transaction_id=tx_id))
assert len(memos[tx_id]) == 2
assert list(memos[tx_id].values())[0][0] == cat_2_hash.hex()
tx_id = coin.name()
memos = await env_2.rpc_client.get_transaction_memo(GetTransactionMemo(transaction_id=tx_id))
assert len(memos.coins_with_memos) == 2
assert memos.coins_with_memos[1].memos[0] == cat_2_hash
cat_hash = await cat_wallet.get_new_inner_hash()
async with cat_wallet_2.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
await cat_wallet_2.generate_signed_transaction([uint64(15)], [cat_hash], action_scope)
Expand Down
341 changes: 263 additions & 78 deletions chia/_tests/wallet/did_wallet/test_did.py

Large diffs are not rendered by default.

77 changes: 37 additions & 40 deletions chia/_tests/wallet/nft_wallet/test_nft_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
from chia._tests.environments.wallet import WalletStateTransition, WalletTestFramework
from chia._tests.util.time_out_assert import time_out_assert
from chia.rpc.rpc_client import ResponseFailureError
from chia.rpc.wallet_request_types import (
NFTCoin,
NFTGetByDID,
NFTSetDIDBulk,
NFTSetNFTStatus,
NFTTransferBulk,
NFTWalletWithDID,
)
from chia.simulator.simulator_protocol import ReorgProtocol
from chia.types.blockchain_format.program import Program
from chia.types.blockchain_format.sized_bytes import bytes32
Expand All @@ -26,7 +34,6 @@
from chia.wallet.util.address_type import AddressType
from chia.wallet.util.compute_memos import compute_memos
from chia.wallet.util.wallet_types import WalletType
from chia.wallet.wallet_spend_bundle import WalletSpendBundle
from chia.wallet.wallet_state_manager import WalletStateManager


Expand Down Expand Up @@ -818,8 +825,8 @@ async def test_nft_with_did_wallet_creation(wallet_environments: WalletTestFrame
assert res["wallet_id"] != nft_wallet.id()
nft_wallet_p2_puzzle = res["wallet_id"]

res = await env.rpc_client.fetch("nft_get_by_did", {"did_id": hmr_did_id})
assert nft_wallet.id() == res["wallet_id"]
wallet_by_did_response = await env.rpc_client.get_nft_wallet_by_did(NFTGetByDID(did_id=hmr_did_id))
assert nft_wallet.id() == wallet_by_did_response.wallet_id

await wallet_environments.process_pending_states(
[
Expand All @@ -830,10 +837,8 @@ async def test_nft_with_did_wallet_creation(wallet_environments: WalletTestFrame
]
)

res = await env.rpc_client.fetch("nft_get_wallets_with_dids", {})
assert res.get("success")
assert res.get("nft_wallets") == [
{"wallet_id": nft_wallet.id(), "did_id": hmr_did_id, "did_wallet_id": did_wallet.id()}
assert (await env.rpc_client.get_nft_wallets_with_dids()).nft_wallets == [
NFTWalletWithDID(wallet_id=nft_wallet.id(), did_id=hmr_did_id, did_wallet_id=did_wallet.id())
]

res = await env.rpc_client.get_nft_wallet_did(wallet_id=nft_wallet.id())
Expand Down Expand Up @@ -1273,9 +1278,8 @@ async def test_nft_transfer_nft_with_did(wallet_environments: WalletTestFramewor
)

# Check if the NFT owner DID is reset
res = await env_1.rpc_client.fetch("nft_get_by_did", {})
assert res.get("success")
assert env_1.wallet_aliases["nft"] == res["wallet_id"]
wallet_by_did_response = await env_1.rpc_client.get_nft_wallet_by_did(NFTGetByDID())
assert env_1.wallet_aliases["nft"] == wallet_by_did_response.wallet_id
coins = (await env_1.rpc_client.list_nfts(env_1.wallet_aliases["nft"], start_index=0, num=1))["nft_list"]
assert len(coins) == 1
coin = NFTInfo.from_json_dict(coins[0])
Expand Down Expand Up @@ -1332,9 +1336,8 @@ async def test_nft_transfer_nft_with_did(wallet_environments: WalletTestFramewor
]
)

res = await env_1.rpc_client.fetch("nft_get_by_did", {"did_id": hmr_did_id})
assert res.get("success")
assert env_1.wallet_aliases["nft_w_did"] == res["wallet_id"]
wallet_by_did_response = await env_1.rpc_client.get_nft_wallet_by_did(NFTGetByDID(did_id=hmr_did_id))
assert env_1.wallet_aliases["nft_w_did"] == wallet_by_did_response.wallet_id
# Check NFT DID is set now
coins = (await env_1.rpc_client.list_nfts(env_1.wallet_aliases["nft_w_did"], start_index=0, num=1))["nft_list"]
assert len(coins) == 1
Expand Down Expand Up @@ -1725,19 +1728,16 @@ async def test_nft_bulk_set_did(wallet_environments: WalletTestFramework) -> Non
nft2 = NFTInfo.from_json_dict(coins[0])
assert nft2.owner_did is None
nft_coin_list = [
{"wallet_id": env.wallet_aliases["nft_w_did"], "nft_coin_id": nft1.nft_coin_id.hex()},
{"wallet_id": env.wallet_aliases["nft_w_did"], "nft_coin_id": nft12.nft_coin_id.hex()},
{"wallet_id": env.wallet_aliases["nft_no_did"], "nft_coin_id": nft2.nft_coin_id.hex()},
{"wallet_id": env.wallet_aliases["nft_no_did"]},
{"nft_coin_id": nft2.nft_coin_id.hex()},
NFTCoin(wallet_id=uint32(env.wallet_aliases["nft_w_did"]), nft_coin_id=nft1.nft_coin_id.hex()),
NFTCoin(wallet_id=uint32(env.wallet_aliases["nft_w_did"]), nft_coin_id=nft12.nft_coin_id.hex()),
NFTCoin(wallet_id=uint32(env.wallet_aliases["nft_no_did"]), nft_coin_id=nft2.nft_coin_id.hex()),
]
fee = 1000
res = await env.rpc_client.fetch("nft_set_did_bulk", dict(did_id=hmr_did_id, nft_coin_list=nft_coin_list, fee=fee))
sb = WalletSpendBundle.from_json_dict(res["spend_bundle"])
assert len(sb.coin_spends) == 5
tx_num = res["tx_num"]
assert isinstance(tx_num, int)
assert tx_num == 5 # 1 for each NFT being spent (3), 1 for fee tx, 1 for did tx
fee = uint64(1000)
set_did_bulk_resp = await env.rpc_client.set_nft_did_bulk(
NFTSetDIDBulk(did_id=hmr_did_id, nft_coin_list=nft_coin_list, fee=fee, push=True)
)
assert len(set_did_bulk_resp.spend_bundle.coin_spends) == 5
assert set_did_bulk_resp.tx_num == 5 # 1 for each NFT being spent (3), 1 for fee tx, 1 for did tx
coins = (await env.rpc_client.list_nfts(env.wallet_aliases["nft_w_did"], start_index=0, num=2))["nft_list"]
assert len(coins) == 2
nft1 = NFTInfo.from_json_dict(coins[0])
Expand Down Expand Up @@ -1784,9 +1784,8 @@ async def test_nft_bulk_set_did(wallet_environments: WalletTestFramework) -> Non
]
)

res = await env.rpc_client.fetch("nft_get_by_did", {"did_id": hmr_did_id})
assert res.get("success")
assert env.wallet_aliases["nft_w_did"] == res.get("wallet_id")
wallet_by_did_response = await env.rpc_client.get_nft_wallet_by_did(NFTGetByDID(did_id=hmr_did_id))
assert env.wallet_aliases["nft_w_did"] == wallet_by_did_response.wallet_id
coins = (await env.rpc_client.list_nfts(env.wallet_aliases["nft_w_did"], start_index=0, num=3))["nft_list"]
assert len(coins) == 3
nft1 = NFTInfo.from_json_dict(coins[0])
Expand Down Expand Up @@ -2024,20 +2023,18 @@ async def test_nft_bulk_transfer(wallet_environments: WalletTestFramework) -> No
nft2 = NFTInfo.from_json_dict(coins[0])
assert nft2.owner_did is None
nft_coin_list = [
{"wallet_id": env_0.wallet_aliases["nft_w_did"], "nft_coin_id": nft1.nft_coin_id.hex()},
{"wallet_id": env_0.wallet_aliases["nft_w_did"], "nft_coin_id": nft12.nft_coin_id.hex()},
{"wallet_id": env_0.wallet_aliases["nft_no_did"], "nft_coin_id": nft2.nft_coin_id.hex()},
{"wallet_id": env_0.wallet_aliases["nft_no_did"]},
{"nft_coin_id": nft2.nft_coin_id.hex()},
NFTCoin(wallet_id=uint32(env_0.wallet_aliases["nft_w_did"]), nft_coin_id=nft1.nft_coin_id.hex()),
NFTCoin(wallet_id=uint32(env_0.wallet_aliases["nft_w_did"]), nft_coin_id=nft12.nft_coin_id.hex()),
NFTCoin(wallet_id=uint32(env_0.wallet_aliases["nft_no_did"]), nft_coin_id=nft2.nft_coin_id.hex()),
]

fee = 1000
fee = uint64(1000)
address = encode_puzzle_hash(await wallet_1.get_puzzle_hash(new=False), AddressType.XCH.hrp(env_1.node.config))
res = await env_0.rpc_client.fetch(
"nft_transfer_bulk", dict(target_address=address, nft_coin_list=nft_coin_list, fee=fee)
bulk_transfer_resp = await env_0.rpc_client.transfer_nft_bulk(
NFTTransferBulk(target_address=address, nft_coin_list=nft_coin_list, fee=fee, push=True)
)
assert len(res["spend_bundle"]["coin_spends"]) == 4
assert res["tx_num"] == 4
assert len(bulk_transfer_resp.spend_bundle.coin_spends) == 4
assert bulk_transfer_resp.tx_num == 4

await wallet_environments.process_pending_states(
[
Expand Down Expand Up @@ -2419,8 +2416,8 @@ async def test_set_nft_status(wallet_environments: WalletTestFramework) -> None:
assert not coin.pending_transaction
nft_coin_id = coin.nft_coin_id
# Set status
res = await env.rpc_client.fetch(
"nft_set_nft_status", dict(wallet_id=env.wallet_aliases["nft"], coin_id=nft_coin_id.hex(), in_transaction=True)
await env.rpc_client.set_nft_status(
NFTSetNFTStatus(wallet_id=uint32(env.wallet_aliases["nft"]), coin_id=nft_coin_id, in_transaction=True)
)
coins = (await env.rpc_client.list_nfts(env.wallet_aliases["nft"], start_index=0, num=1))["nft_list"]
assert len(coins) == 1
Expand Down
Loading

0 comments on commit 810f0bf

Please sign in to comment.