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
92 changes: 63 additions & 29 deletions chia/_tests/cmds/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from chia.types.signing_mode import SigningMode
from chia.util.bech32m import encode_puzzle_hash
from chia.wallet.conditions import Condition, ConditionValidTimes
from chia.wallet.puzzle_drivers import PuzzleInfo
from chia.wallet.trade_record import TradeRecord
from chia.wallet.trading.offer import Offer
from chia.wallet.trading.trade_status import TradeStatus
Expand All @@ -49,6 +50,7 @@
CATSpend,
CATSpendResponse,
ClawbackPuzzleDecoratorOverride,
CreateOfferForIDs,
CreateOfferForIDsResponse,
DeleteUnconfirmedTransactions,
ExtendDerivationIndex,
Expand Down Expand Up @@ -79,6 +81,29 @@
)
from chia.wallet.wallet_spend_bundle import WalletSpendBundle

TEMP = PuzzleInfo(
{
"type": "singleton",
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
"launcher_ph": "0xeff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9",
"also": {
"type": "metadata",
"metadata": "",
"updater_hash": "0x0707070707070707070707070707070707070707070707070707070707070707",
"also": {
"type": "ownership",
"owner": "()",
"transfer_program": {
"type": "royalty transfer program",
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
"royalty_address": "0x0303030303030303030303030303030303030303030303030303030303030303",
"royalty_percentage": "1000",
},
},
},
}
)

test_offer_file_path = importlib_resources.files(__name__.rpartition(".")[0]).joinpath("test_offer.toffer")
test_offer_file_bech32 = test_offer_file_path.read_text(encoding="utf-8")
test_offer_id: str = "0xdfb7e8643376820ec995b0bcdb3fc1f764c16b814df5e074631263fcf1e00839"
Expand Down Expand Up @@ -763,17 +788,22 @@ def test_make_offer(capsys: object, get_test_cli_clients: tuple[TestRpcClients,
class MakeOfferRpcClient(TestWalletRpcClient):
async def create_offer_for_ids(
self,
offer_dict: dict[uint32, int],
request: CreateOfferForIDs,
tx_config: TXConfig,
driver_dict: Optional[dict[str, Any]] = None,
solver: Optional[dict[str, Any]] = None,
fee: uint64 = uint64(0),
validate_only: bool = False,
extra_conditions: tuple[Condition, ...] = tuple(),
timelock_info: ConditionValidTimes = ConditionValidTimes(),
) -> CreateOfferForIDsResponse:
self.add_to_log(
"create_offer_for_ids",
(offer_dict, tx_config, driver_dict, solver, fee, validate_only, timelock_info),
(
request.offer,
tx_config,
request.driver_dict,
request.solver,
request.fee,
request.validate_only,
timelock_info,
),
)

created_offer = Offer({}, WalletSpendBundle([], G2Element()), {})
Expand Down Expand Up @@ -866,35 +896,39 @@ async def create_offer_for_ids(
"create_offer_for_ids": [
(
{
1: -10000000000000,
3: -100000,
"0404040404040404040404040404040404040404040404040404040404040404": -100000,
"0202020202020202020202020202020202020202020202020202020202020202": 10000,
"0101010101010101010101010101010101010101010101010101010101010101": 1,
"1": "-10000000000000",
"3": "-100000",
"0404040404040404040404040404040404040404040404040404040404040404": "-100000",
"0202020202020202020202020202020202020202020202020202020202020202": "10000",
"0101010101010101010101010101010101010101010101010101010101010101": "1",
},
DEFAULT_TX_CONFIG.override(reuse_puzhash=True),
{
"0101010101010101010101010101010101010101010101010101010101010101": {
"type": "singleton",
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
"launcher_ph": "0xeff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9",
"also": {
"type": "metadata",
"metadata": "",
"updater_hash": "0x0707070707070707070707070707070707070707070707070707070707070707",
bytes32([1] * 32): PuzzleInfo(
{
"type": "singleton",
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
"launcher_ph": "0xeff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9",
"also": {
"type": "ownership",
"owner": "()",
"transfer_program": {
"type": "royalty transfer program",
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
"royalty_address": "0x0303030303030303030303030303030303030303030"
"303030303030303030303",
"royalty_percentage": "1000",
"type": "metadata",
"metadata": "",
"updater_hash": "0x0707070707070707070707070707070707070707070707070707070707070707",
"also": {
"type": "ownership",
"owner": "()",
"transfer_program": {
"type": "royalty transfer program",
"launcher_id": (
"0x0101010101010101010101010101010101010101010101010101010101010101"
),
"royalty_address": "0x0303030303030303030303030303030303030303030"
"303030303030303030303",
"royalty_percentage": "1000",
},
},
},
},
}
}
)
},
None,
500000000000,
Expand Down
64 changes: 33 additions & 31 deletions chia/_tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
from chia.wallet.derive_keys import master_sk_to_wallet_sk, master_sk_to_wallet_sk_unhardened
from chia.wallet.did_wallet.did_wallet import DIDWallet
from chia.wallet.nft_wallet.nft_wallet import NFTWallet
from chia.wallet.puzzle_drivers import PuzzleInfo
from chia.wallet.puzzles.clawback.metadata import AutoClaimSettings
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import puzzle_hash_for_pk
from chia.wallet.signer_protocol import UnsignedTransaction
Expand Down Expand Up @@ -113,6 +114,7 @@
CheckOfferValidity,
ClawbackPuzzleDecoratorOverride,
CombineCoins,
CreateOfferForIDs,
DefaultCAT,
DeleteKey,
DeleteNotifications,
Expand Down Expand Up @@ -1546,28 +1548,29 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
)
# Create an offer of 5 chia for one CAT
await env_1.rpc_client.create_offer_for_ids(
{uint32(1): -5, cat_asset_id.hex(): 1}, wallet_environments.tx_config, validate_only=True
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, validate_only=True),
tx_config=wallet_environments.tx_config,
)
all_offers = await env_1.rpc_client.get_all_offers()
assert len(all_offers) == 0

driver_dict: dict[str, Any] = {
cat_asset_id.hex(): {
"type": "CAT",
"tail": "0x" + cat_asset_id.hex(),
**(
{}
if wallet_type is CATWallet
else {"also": {"type": "revocation layer", "hidden_puzzle_hash": "0x" + bytes32.zeros.hex()}}
),
}
driver_dict = {
cat_asset_id: PuzzleInfo(
{
"type": "CAT",
"tail": "0x" + cat_asset_id.hex(),
**(
{}
if wallet_type is CATWallet
else {"also": {"type": "revocation layer", "hidden_puzzle_hash": "0x" + bytes32.zeros.hex()}}
),
}
)
}

create_res = await env_1.rpc_client.create_offer_for_ids(
{uint32(1): -5, cat_asset_id.hex(): 1},
wallet_environments.tx_config,
driver_dict=driver_dict,
fee=uint64(1),
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, driver_dict=driver_dict, fee=uint64(1)),
tx_config=wallet_environments.tx_config,
)
offer = create_res.offer

Expand All @@ -1578,7 +1581,7 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
assert summary == {
"offered": {"xch": 5},
"requested": {cat_asset_id.hex(): 1},
"infos": driver_dict,
"infos": {key.hex(): info.info for key, info in driver_dict.items()},
"fees": 1,
"additions": [c.name().hex() for c in offer.additions()],
"removals": [c.name().hex() for c in offer.removals()],
Expand Down Expand Up @@ -1622,7 +1625,8 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
assert TradeStatus(trade_record.status) == TradeStatus.PENDING_CANCEL

create_res = await env_1.rpc_client.create_offer_for_ids(
{uint32(1): -5, cat_wallet_id: 1}, wallet_environments.tx_config, fee=uint64(1)
CreateOfferForIDs(offer={str(1): "-5", str(cat_wallet_id): "1"}, fee=uint64(1)),
tx_config=wallet_environments.tx_config,
)
all_offers = await env_1.rpc_client.get_all_offers()
assert len(all_offers) == 2
Expand Down Expand Up @@ -1736,9 +1740,8 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:
assert len(all_offers) == 2

await env_1.rpc_client.create_offer_for_ids(
{uint32(1): -5, cat_asset_id.hex(): 1},
wallet_environments.tx_config,
driver_dict=driver_dict,
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, driver_dict=driver_dict),
tx_config=wallet_environments.tx_config,
)
assert (
len([o for o in await env_1.rpc_client.get_all_offers() if o.status == TradeStatus.PENDING_ACCEPT.value]) == 2
Expand Down Expand Up @@ -1774,14 +1777,12 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:
)

await env_1.rpc_client.create_offer_for_ids(
{uint32(1): -5, cat_asset_id.hex(): 1},
wallet_environments.tx_config,
driver_dict=driver_dict,
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, driver_dict=driver_dict),
tx_config=wallet_environments.tx_config,
)
await env_1.rpc_client.create_offer_for_ids(
{uint32(1): 5, cat_asset_id.hex(): -1},
wallet_environments.tx_config,
driver_dict=driver_dict,
CreateOfferForIDs(offer={str(1): "5", cat_asset_id.hex(): "-1"}, driver_dict=driver_dict),
tx_config=wallet_environments.tx_config,
)
assert (
len([o for o in await env_1.rpc_client.get_all_offers() if o.status == TradeStatus.PENDING_ACCEPT.value]) == 2
Expand Down Expand Up @@ -1828,9 +1829,8 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:
)

await env_1.rpc_client.create_offer_for_ids(
{uint32(1): 5, cat_asset_id.hex(): -1},
wallet_environments.tx_config,
driver_dict=driver_dict,
CreateOfferForIDs(offer={str(1): "5", cat_asset_id.hex(): "-1"}, driver_dict=driver_dict),
tx_config=wallet_environments.tx_config,
)
assert (
len([o for o in await env_1.rpc_client.get_all_offers() if o.status == TradeStatus.PENDING_ACCEPT.value]) == 1
Expand All @@ -1846,9 +1846,11 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:

with pytest.raises(ValueError, match="not currently supported"):
await env_1.rpc_client.create_offer_for_ids(
{uint32(1): -5, cat_asset_id.hex(): 1},
CreateOfferForIDs(
offer={str(1): "-5", cat_asset_id.hex(): "1"},
driver_dict=driver_dict,
),
wallet_environments.tx_config,
driver_dict=driver_dict,
timelock_info=ConditionValidTimes(min_secs_since_created=uint64(1)),
)

Expand Down
28 changes: 16 additions & 12 deletions chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
CATSpend,
CATSpendResponse,
ClawbackPuzzleDecoratorOverride,
CreateOfferForIDs,
DeleteNotifications,
DeleteUnconfirmedTransactions,
DIDFindLostDID,
Expand Down Expand Up @@ -507,16 +508,16 @@ async def make_offer(
if offers == [] or requests == []:
print("Not creating offer: Must be offering and requesting at least one asset")
else:
offer_dict: dict[Union[uint32, str], int] = {}
driver_dict: dict[str, Any] = {}
offer_dict: dict[str, str] = {}
driver_dict: dict[bytes32, PuzzleInfo] = {}
printable_dict: dict[str, tuple[str, int, int]] = {} # dict[asset_name, tuple[amount, unit, multiplier]]
royalty_assets: list[RoyaltyAsset] = []
fungible_assets: list[FungibleAsset] = []
for item in [*offers, *requests]:
name, amount = tuple(item.split(":")[0:2])
try:
b32_id = bytes32.from_hexstr(name)
id: Union[uint32, str] = b32_id.hex()
id: str = b32_id.hex()
result = await wallet_client.cat_asset_id_to_name(CATAssetIDToName(b32_id))
if result.name is not None:
name = result.name
Expand All @@ -535,7 +536,7 @@ async def make_offer(
id = info.launcher_id.hex()
assert isinstance(id, str)
if item in requests:
driver_dict[id] = {
puzzle_info_dict: dict[str, Any] = {
"type": "singleton",
"launcher_id": "0x" + id,
"launcher_ph": "0x" + info.launcher_puzhash.hex(),
Expand All @@ -548,7 +549,7 @@ async def make_offer(
if info.supports_did:
assert info.royalty_puzzle_hash is not None
assert info.royalty_percentage is not None
driver_dict[id]["also"]["also"] = {
puzzle_info_dict["also"]["also"] = {
"type": "ownership",
"owner": "()",
"transfer_program": {
Expand All @@ -565,17 +566,18 @@ async def make_offer(
info.royalty_percentage,
)
)
driver_dict[info.launcher_id] = PuzzleInfo(puzzle_info_dict)
else:
id = decode_puzzle_hash(name).hex()
assert hrp is not None
unit = units[hrp]
except ValueError:
id = uint32(name)
if id == 1:
id = str(uint32(name))
if id == "1":
name = "XCH"
unit = units["chia"]
else:
name = (await wallet_client.get_cat_name(CATGetName(id))).name
name = (await wallet_client.get_cat_name(CATGetName(uint32(name)))).name
unit = units["cat"]
if item in offers:
fungible_assets.append(FungibleAsset(name, uint64(abs(int(Decimal(amount) * unit)))))
Expand All @@ -585,7 +587,7 @@ async def make_offer(
print("Not creating offer: Cannot offer and request the same asset in a trade")
break
else:
offer_dict[id] = int(Decimal(amount) * unit) * multiplier
offer_dict[id] = str(int(Decimal(amount) * unit) * multiplier)
else:
print("Creating Offer")
print("--------------")
Expand Down Expand Up @@ -642,9 +644,11 @@ async def make_offer(

with filepath.open(mode="w") as file:
res = await wallet_client.create_offer_for_ids(
offer_dict,
driver_dict=driver_dict,
fee=fee,
CreateOfferForIDs(
offer=offer_dict,
driver_dict=driver_dict,
fee=fee,
),
tx_config=CMDTXConfigLoader(
reuse_puzhash=reuse_puzhash,
).to_tx_config(units["chia"], config, fingerprint),
Expand Down
Loading
Loading