Skip to content

Commit 0a26924

Browse files
authored
[CHIA-3606] Port create_offer_for_ids to @marshal (#20056)
* Port `create_offer_for_ids` to `@marshal` * Self from typing_extensions * Fix support for "" in PuzzleInfo/Solver
1 parent 18c1010 commit 0a26924

File tree

10 files changed

+241
-169
lines changed

10 files changed

+241
-169
lines changed

chia/_tests/cmds/wallet/test_wallet.py

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from chia.types.signing_mode import SigningMode
3030
from chia.util.bech32m import encode_puzzle_hash
3131
from chia.wallet.conditions import Condition, ConditionValidTimes
32+
from chia.wallet.puzzle_drivers import PuzzleInfo
3233
from chia.wallet.trade_record import TradeRecord
3334
from chia.wallet.trading.offer import Offer
3435
from chia.wallet.trading.trade_status import TradeStatus
@@ -49,6 +50,7 @@
4950
CATSpend,
5051
CATSpendResponse,
5152
ClawbackPuzzleDecoratorOverride,
53+
CreateOfferForIDs,
5254
CreateOfferForIDsResponse,
5355
DeleteUnconfirmedTransactions,
5456
ExtendDerivationIndex,
@@ -79,6 +81,29 @@
7981
)
8082
from chia.wallet.wallet_spend_bundle import WalletSpendBundle
8183

84+
TEMP = PuzzleInfo(
85+
{
86+
"type": "singleton",
87+
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
88+
"launcher_ph": "0xeff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9",
89+
"also": {
90+
"type": "metadata",
91+
"metadata": "",
92+
"updater_hash": "0x0707070707070707070707070707070707070707070707070707070707070707",
93+
"also": {
94+
"type": "ownership",
95+
"owner": "()",
96+
"transfer_program": {
97+
"type": "royalty transfer program",
98+
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
99+
"royalty_address": "0x0303030303030303030303030303030303030303030303030303030303030303",
100+
"royalty_percentage": "1000",
101+
},
102+
},
103+
},
104+
}
105+
)
106+
82107
test_offer_file_path = importlib_resources.files(__name__.rpartition(".")[0]).joinpath("test_offer.toffer")
83108
test_offer_file_bech32 = test_offer_file_path.read_text(encoding="utf-8")
84109
test_offer_id: str = "0xdfb7e8643376820ec995b0bcdb3fc1f764c16b814df5e074631263fcf1e00839"
@@ -763,17 +788,22 @@ def test_make_offer(capsys: object, get_test_cli_clients: tuple[TestRpcClients,
763788
class MakeOfferRpcClient(TestWalletRpcClient):
764789
async def create_offer_for_ids(
765790
self,
766-
offer_dict: dict[uint32, int],
791+
request: CreateOfferForIDs,
767792
tx_config: TXConfig,
768-
driver_dict: Optional[dict[str, Any]] = None,
769-
solver: Optional[dict[str, Any]] = None,
770-
fee: uint64 = uint64(0),
771-
validate_only: bool = False,
793+
extra_conditions: tuple[Condition, ...] = tuple(),
772794
timelock_info: ConditionValidTimes = ConditionValidTimes(),
773795
) -> CreateOfferForIDsResponse:
774796
self.add_to_log(
775797
"create_offer_for_ids",
776-
(offer_dict, tx_config, driver_dict, solver, fee, validate_only, timelock_info),
798+
(
799+
request.offer,
800+
tx_config,
801+
request.driver_dict,
802+
request.solver,
803+
request.fee,
804+
request.validate_only,
805+
timelock_info,
806+
),
777807
)
778808

779809
created_offer = Offer({}, WalletSpendBundle([], G2Element()), {})
@@ -866,35 +896,39 @@ async def create_offer_for_ids(
866896
"create_offer_for_ids": [
867897
(
868898
{
869-
1: -10000000000000,
870-
3: -100000,
871-
"0404040404040404040404040404040404040404040404040404040404040404": -100000,
872-
"0202020202020202020202020202020202020202020202020202020202020202": 10000,
873-
"0101010101010101010101010101010101010101010101010101010101010101": 1,
899+
"1": "-10000000000000",
900+
"3": "-100000",
901+
"0404040404040404040404040404040404040404040404040404040404040404": "-100000",
902+
"0202020202020202020202020202020202020202020202020202020202020202": "10000",
903+
"0101010101010101010101010101010101010101010101010101010101010101": "1",
874904
},
875905
DEFAULT_TX_CONFIG.override(reuse_puzhash=True),
876906
{
877-
"0101010101010101010101010101010101010101010101010101010101010101": {
878-
"type": "singleton",
879-
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
880-
"launcher_ph": "0xeff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9",
881-
"also": {
882-
"type": "metadata",
883-
"metadata": "",
884-
"updater_hash": "0x0707070707070707070707070707070707070707070707070707070707070707",
907+
bytes32([1] * 32): PuzzleInfo(
908+
{
909+
"type": "singleton",
910+
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
911+
"launcher_ph": "0xeff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9",
885912
"also": {
886-
"type": "ownership",
887-
"owner": "()",
888-
"transfer_program": {
889-
"type": "royalty transfer program",
890-
"launcher_id": "0x0101010101010101010101010101010101010101010101010101010101010101",
891-
"royalty_address": "0x0303030303030303030303030303030303030303030"
892-
"303030303030303030303",
893-
"royalty_percentage": "1000",
913+
"type": "metadata",
914+
"metadata": "",
915+
"updater_hash": "0x0707070707070707070707070707070707070707070707070707070707070707",
916+
"also": {
917+
"type": "ownership",
918+
"owner": "()",
919+
"transfer_program": {
920+
"type": "royalty transfer program",
921+
"launcher_id": (
922+
"0x0101010101010101010101010101010101010101010101010101010101010101"
923+
),
924+
"royalty_address": "0x0303030303030303030303030303030303030303030"
925+
"303030303030303030303",
926+
"royalty_percentage": "1000",
927+
},
894928
},
895929
},
896-
},
897-
}
930+
}
931+
)
898932
},
899933
None,
900934
500000000000,

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
from chia.wallet.derive_keys import master_sk_to_wallet_sk, master_sk_to_wallet_sk_unhardened
8282
from chia.wallet.did_wallet.did_wallet import DIDWallet
8383
from chia.wallet.nft_wallet.nft_wallet import NFTWallet
84+
from chia.wallet.puzzle_drivers import PuzzleInfo
8485
from chia.wallet.puzzles.clawback.metadata import AutoClaimSettings
8586
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import puzzle_hash_for_pk
8687
from chia.wallet.signer_protocol import UnsignedTransaction
@@ -114,6 +115,7 @@
114115
CheckOfferValidity,
115116
ClawbackPuzzleDecoratorOverride,
116117
CombineCoins,
118+
CreateOfferForIDs,
117119
DefaultCAT,
118120
DeleteKey,
119121
DeleteNotifications,
@@ -1586,28 +1588,29 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
15861588
)
15871589
# Create an offer of 5 chia for one CAT
15881590
await env_1.rpc_client.create_offer_for_ids(
1589-
{uint32(1): -5, cat_asset_id.hex(): 1}, wallet_environments.tx_config, validate_only=True
1591+
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, validate_only=True),
1592+
tx_config=wallet_environments.tx_config,
15901593
)
15911594
all_offers = await env_1.rpc_client.get_all_offers()
15921595
assert len(all_offers) == 0
15931596

1594-
driver_dict: dict[str, Any] = {
1595-
cat_asset_id.hex(): {
1596-
"type": "CAT",
1597-
"tail": "0x" + cat_asset_id.hex(),
1598-
**(
1599-
{}
1600-
if wallet_type is CATWallet
1601-
else {"also": {"type": "revocation layer", "hidden_puzzle_hash": "0x" + bytes32.zeros.hex()}}
1602-
),
1603-
}
1597+
driver_dict = {
1598+
cat_asset_id: PuzzleInfo(
1599+
{
1600+
"type": "CAT",
1601+
"tail": "0x" + cat_asset_id.hex(),
1602+
**(
1603+
{}
1604+
if wallet_type is CATWallet
1605+
else {"also": {"type": "revocation layer", "hidden_puzzle_hash": "0x" + bytes32.zeros.hex()}}
1606+
),
1607+
}
1608+
)
16041609
}
16051610

16061611
create_res = await env_1.rpc_client.create_offer_for_ids(
1607-
{uint32(1): -5, cat_asset_id.hex(): 1},
1608-
wallet_environments.tx_config,
1609-
driver_dict=driver_dict,
1610-
fee=uint64(1),
1612+
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, driver_dict=driver_dict, fee=uint64(1)),
1613+
tx_config=wallet_environments.tx_config,
16111614
)
16121615
offer = create_res.offer
16131616

@@ -1618,7 +1621,7 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
16181621
assert summary == {
16191622
"offered": {"xch": 5},
16201623
"requested": {cat_asset_id.hex(): 1},
1621-
"infos": driver_dict,
1624+
"infos": {key.hex(): info.info for key, info in driver_dict.items()},
16221625
"fees": 1,
16231626
"additions": [c.name().hex() for c in offer.additions()],
16241627
"removals": [c.name().hex() for c in offer.removals()],
@@ -1662,7 +1665,8 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
16621665
assert TradeStatus(trade_record.status) == TradeStatus.PENDING_CANCEL
16631666

16641667
create_res = await env_1.rpc_client.create_offer_for_ids(
1665-
{uint32(1): -5, cat_wallet_id: 1}, wallet_environments.tx_config, fee=uint64(1)
1668+
CreateOfferForIDs(offer={str(1): "-5", str(cat_wallet_id): "1"}, fee=uint64(1)),
1669+
tx_config=wallet_environments.tx_config,
16661670
)
16671671
all_offers = await env_1.rpc_client.get_all_offers()
16681672
assert len(all_offers) == 2
@@ -1776,9 +1780,8 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:
17761780
assert len(all_offers) == 2
17771781

17781782
await env_1.rpc_client.create_offer_for_ids(
1779-
{uint32(1): -5, cat_asset_id.hex(): 1},
1780-
wallet_environments.tx_config,
1781-
driver_dict=driver_dict,
1783+
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, driver_dict=driver_dict),
1784+
tx_config=wallet_environments.tx_config,
17821785
)
17831786
assert (
17841787
len([o for o in await env_1.rpc_client.get_all_offers() if o.status == TradeStatus.PENDING_ACCEPT.value]) == 2
@@ -1814,14 +1817,12 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:
18141817
)
18151818

18161819
await env_1.rpc_client.create_offer_for_ids(
1817-
{uint32(1): -5, cat_asset_id.hex(): 1},
1818-
wallet_environments.tx_config,
1819-
driver_dict=driver_dict,
1820+
CreateOfferForIDs(offer={str(1): "-5", cat_asset_id.hex(): "1"}, driver_dict=driver_dict),
1821+
tx_config=wallet_environments.tx_config,
18201822
)
18211823
await env_1.rpc_client.create_offer_for_ids(
1822-
{uint32(1): 5, cat_asset_id.hex(): -1},
1823-
wallet_environments.tx_config,
1824-
driver_dict=driver_dict,
1824+
CreateOfferForIDs(offer={str(1): "5", cat_asset_id.hex(): "-1"}, driver_dict=driver_dict),
1825+
tx_config=wallet_environments.tx_config,
18251826
)
18261827
assert (
18271828
len([o for o in await env_1.rpc_client.get_all_offers() if o.status == TradeStatus.PENDING_ACCEPT.value]) == 2
@@ -1868,9 +1869,8 @@ def only_ids(trades: list[TradeRecord]) -> list[bytes32]:
18681869
)
18691870

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

18871887
with pytest.raises(ValueError, match="not currently supported"):
18881888
await env_1.rpc_client.create_offer_for_ids(
1889-
{uint32(1): -5, cat_asset_id.hex(): 1},
1889+
CreateOfferForIDs(
1890+
offer={str(1): "-5", cat_asset_id.hex(): "1"},
1891+
driver_dict=driver_dict,
1892+
),
18901893
wallet_environments.tx_config,
1891-
driver_dict=driver_dict,
18921894
timelock_info=ConditionValidTimes(min_secs_since_created=uint64(1)),
18931895
)
18941896

chia/cmds/wallet_funcs.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
CATSpend,
5353
CATSpendResponse,
5454
ClawbackPuzzleDecoratorOverride,
55+
CreateOfferForIDs,
5556
DeleteNotifications,
5657
DeleteUnconfirmedTransactions,
5758
DIDFindLostDID,
@@ -507,16 +508,16 @@ async def make_offer(
507508
if offers == [] or requests == []:
508509
print("Not creating offer: Must be offering and requesting at least one asset")
509510
else:
510-
offer_dict: dict[Union[uint32, str], int] = {}
511-
driver_dict: dict[str, Any] = {}
511+
offer_dict: dict[str, str] = {}
512+
driver_dict: dict[bytes32, PuzzleInfo] = {}
512513
printable_dict: dict[str, tuple[str, int, int]] = {} # dict[asset_name, tuple[amount, unit, multiplier]]
513514
royalty_assets: list[RoyaltyAsset] = []
514515
fungible_assets: list[FungibleAsset] = []
515516
for item in [*offers, *requests]:
516517
name, amount = tuple(item.split(":")[0:2])
517518
try:
518519
b32_id = bytes32.from_hexstr(name)
519-
id: Union[uint32, str] = b32_id.hex()
520+
id: str = b32_id.hex()
520521
result = await wallet_client.cat_asset_id_to_name(CATAssetIDToName(b32_id))
521522
if result.name is not None:
522523
name = result.name
@@ -535,7 +536,7 @@ async def make_offer(
535536
id = info.launcher_id.hex()
536537
assert isinstance(id, str)
537538
if item in requests:
538-
driver_dict[id] = {
539+
puzzle_info_dict: dict[str, Any] = {
539540
"type": "singleton",
540541
"launcher_id": "0x" + id,
541542
"launcher_ph": "0x" + info.launcher_puzhash.hex(),
@@ -548,7 +549,7 @@ async def make_offer(
548549
if info.supports_did:
549550
assert info.royalty_puzzle_hash is not None
550551
assert info.royalty_percentage is not None
551-
driver_dict[id]["also"]["also"] = {
552+
puzzle_info_dict["also"]["also"] = {
552553
"type": "ownership",
553554
"owner": "()",
554555
"transfer_program": {
@@ -565,17 +566,18 @@ async def make_offer(
565566
info.royalty_percentage,
566567
)
567568
)
569+
driver_dict[info.launcher_id] = PuzzleInfo(puzzle_info_dict)
568570
else:
569571
id = decode_puzzle_hash(name).hex()
570572
assert hrp is not None
571573
unit = units[hrp]
572574
except ValueError:
573-
id = uint32(name)
574-
if id == 1:
575+
id = str(uint32(name))
576+
if id == "1":
575577
name = "XCH"
576578
unit = units["chia"]
577579
else:
578-
name = (await wallet_client.get_cat_name(CATGetName(id))).name
580+
name = (await wallet_client.get_cat_name(CATGetName(uint32(name)))).name
579581
unit = units["cat"]
580582
if item in offers:
581583
fungible_assets.append(FungibleAsset(name, uint64(abs(int(Decimal(amount) * unit)))))
@@ -585,7 +587,7 @@ async def make_offer(
585587
print("Not creating offer: Cannot offer and request the same asset in a trade")
586588
break
587589
else:
588-
offer_dict[id] = int(Decimal(amount) * unit) * multiplier
590+
offer_dict[id] = str(int(Decimal(amount) * unit) * multiplier)
589591
else:
590592
print("Creating Offer")
591593
print("--------------")
@@ -642,9 +644,11 @@ async def make_offer(
642644

643645
with filepath.open(mode="w") as file:
644646
res = await wallet_client.create_offer_for_ids(
645-
offer_dict,
646-
driver_dict=driver_dict,
647-
fee=fee,
647+
CreateOfferForIDs(
648+
offer=offer_dict,
649+
driver_dict=driver_dict,
650+
fee=fee,
651+
),
648652
tx_config=CMDTXConfigLoader(
649653
reuse_puzhash=reuse_puzhash,
650654
).to_tx_config(units["chia"], config, fingerprint),

0 commit comments

Comments
 (0)