Skip to content

Commit cca14fa

Browse files
committed
Port take_offer to @marshal
1 parent a1211c4 commit cca14fa

File tree

7 files changed

+81
-73
lines changed

7 files changed

+81
-73
lines changed

chia/_tests/cmds/wallet/test_wallet.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
SendTransactionResponse,
7676
SpendClawbackCoins,
7777
SpendClawbackCoinsResponse,
78+
TakeOffer,
7879
TakeOfferResponse,
7980
TransactionRecordWithMetadata,
8081
WalletInfoResponse,
@@ -1055,29 +1056,30 @@ def test_take_offer(capsys: object, get_test_cli_clients: tuple[TestRpcClients,
10551056
class TakeOfferRpcClient(TestWalletRpcClient):
10561057
async def take_offer(
10571058
self,
1058-
offer: Offer,
1059+
request: TakeOffer,
10591060
tx_config: TXConfig,
1060-
solver: Optional[dict[str, Any]] = None,
1061-
fee: uint64 = uint64(0),
1062-
push: bool = True,
1061+
extra_conditions: tuple[Condition, ...] = tuple(),
10631062
timelock_info: ConditionValidTimes = ConditionValidTimes(),
10641063
) -> TakeOfferResponse:
1065-
self.add_to_log("take_offer", (offer, tx_config, solver, fee, push, timelock_info))
1064+
self.add_to_log(
1065+
"take_offer",
1066+
(request.parsed_offer, tx_config, request.solver, request.fee, request.push, timelock_info),
1067+
)
10661068
return TakeOfferResponse(
10671069
[STD_UTX],
10681070
[STD_TX],
1069-
offer,
1071+
request.parsed_offer,
10701072
TradeRecord(
10711073
confirmed_at_index=uint32(0),
10721074
accepted_at_time=uint64(123456789),
10731075
created_at_time=uint64(12345678),
10741076
is_my_offer=False,
10751077
sent=uint32(1),
10761078
sent_to=[("aaaaa", uint8(1), None)],
1077-
offer=bytes(offer),
1079+
offer=bytes(request.parsed_offer),
10781080
taken_offer=None,
1079-
coins_of_interest=offer.get_involved_coins(),
1080-
trade_id=offer.name(),
1081+
coins_of_interest=request.parsed_offer.get_involved_coins(),
1082+
trade_id=request.parsed_offer.name(),
10811083
status=uint32(TradeStatus.PENDING_ACCEPT.value),
10821084
valid_times=ConditionValidTimes(),
10831085
),

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
SetWalletResyncOnStartup,
160160
SpendClawbackCoins,
161161
SplitCoins,
162+
TakeOffer,
162163
VerifySignature,
163164
VerifySignatureResponse,
164165
)
@@ -1607,7 +1608,16 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
16071608
assert offer_count.my_offers_count == 1
16081609
assert offer_count.taken_offers_count == 0
16091610

1610-
trade_record = (await env_2.rpc_client.take_offer(offer, wallet_environments.tx_config, fee=uint64(1))).trade_record
1611+
trade_record = (
1612+
await env_2.rpc_client.take_offer(
1613+
TakeOffer(
1614+
offer=offer.to_bech32(),
1615+
fee=uint64(1),
1616+
push=True,
1617+
),
1618+
wallet_environments.tx_config,
1619+
)
1620+
).trade_record
16111621
assert TradeStatus(trade_record.status) == TradeStatus.PENDING_CONFIRM
16121622

16131623
await env_1.rpc_client.cancel_offer(offer.name(), wallet_environments.tx_config, secure=False)

chia/cmds/wallet_funcs.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
SignMessageByID,
8989
SignMessageByIDResponse,
9090
SpendClawbackCoins,
91+
TakeOffer,
9192
VCAddProofs,
9293
VCGet,
9394
VCGetList,
@@ -892,11 +893,9 @@ async def take_offer(
892893
print()
893894
cli_confirm("Would you like to take this offer? (y/n): ")
894895
res = await wallet_client.take_offer(
895-
offer,
896-
fee=fee,
897-
tx_config=CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
898-
push=push,
896+
TakeOffer(offer.to_bech32(), fee=fee, push=push),
899897
timelock_info=condition_valid_times,
898+
tx_config=CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
900899
)
901900
if push:
902901
print(f"Accepted offer with ID {res.trade_record.trade_id}")

chia/data_layer/data_layer.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
DLUpdateRoot,
8484
LauncherRootPair,
8585
LogIn,
86+
TakeOffer,
8687
)
8788
from chia.wallet.wallet_rpc_client import WalletRpcClient
8889

@@ -1231,22 +1232,26 @@ async def take_offer(
12311232
)
12321233
)
12331234

1234-
solver: dict[str, Any] = {
1235-
"proofs_of_inclusion": proofs_of_inclusion,
1236-
**{
1237-
"0x" + our_offer_store.store_id.hex(): {
1238-
"new_root": "0x" + root.hex(),
1239-
"dependencies": [
1240-
{
1241-
"launcher_id": "0x" + their_offer_store.store_id.hex(),
1242-
"values_to_prove": ["0x" + entry.node_hash.hex() for entry in their_offer_store.proofs],
1243-
}
1244-
for their_offer_store in maker
1245-
],
1246-
}
1247-
for our_offer_store in taker
1248-
},
1249-
}
1235+
solver = Solver(
1236+
{
1237+
"proofs_of_inclusion": proofs_of_inclusion,
1238+
**{
1239+
"0x" + our_offer_store.store_id.hex(): {
1240+
"new_root": "0x" + root.hex(),
1241+
"dependencies": [
1242+
{
1243+
"launcher_id": "0x" + their_offer_store.store_id.hex(),
1244+
"values_to_prove": [
1245+
"0x" + entry.node_hash.hex() for entry in their_offer_store.proofs
1246+
],
1247+
}
1248+
for their_offer_store in maker
1249+
],
1250+
}
1251+
for our_offer_store in taker
1252+
},
1253+
}
1254+
)
12501255

12511256
await self.data_store.clean_node_table()
12521257

@@ -1256,9 +1261,7 @@ async def take_offer(
12561261

12571262
trade_record = (
12581263
await self.wallet_rpc.take_offer(
1259-
offer=offer,
1260-
solver=solver,
1261-
fee=fee,
1264+
TakeOffer(offer=offer.to_bech32(), solver=solver, fee=fee, push=True),
12621265
# TODO: probably shouldn't be default but due to peculiarities in the RPC, we're using a stop gap.
12631266
# This is not a change in behavior, the default was already implicit.
12641267
tx_config=DEFAULT_TX_CONFIG,

chia/wallet/wallet_request_types.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,17 @@ class CreateOfferForIDsResponse(_OfferEndpointResponse):
19381938
pass
19391939

19401940

1941+
@streamable
1942+
@dataclass(frozen=True)
1943+
class TakeOffer(TransactionEndpointRequest):
1944+
offer: str
1945+
solver: Optional[Solver] = None
1946+
1947+
@cached_property
1948+
def parsed_offer(self) -> Offer:
1949+
return Offer.from_bech32(self.offer)
1950+
1951+
19411952
@streamable
19421953
@dataclass(frozen=True)
19431954
class TakeOfferResponse(_OfferEndpointResponse): # Inheriting for de-dup sake

chia/wallet/wallet_rpc_api.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
from chia.wallet.nft_wallet.uncurry_nft import UncurriedNFT
7272
from chia.wallet.notification_store import Notification
7373
from chia.wallet.outer_puzzles import AssetType
74-
from chia.wallet.puzzle_drivers import PuzzleInfo, Solver
74+
from chia.wallet.puzzle_drivers import PuzzleInfo
7575
from chia.wallet.puzzles import p2_delegated_conditions
7676
from chia.wallet.puzzles.clawback.metadata import AutoClaimSettings, ClawbackMetadata
7777
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import puzzle_hash_for_synthetic_public_key
@@ -284,6 +284,8 @@
284284
StrayCAT,
285285
SubmitTransactions,
286286
SubmitTransactionsResponse,
287+
TakeOffer,
288+
TakeOfferResponse,
287289
TransactionRecordWithMetadata,
288290
VCAddProofs,
289291
VCGet,
@@ -2321,44 +2323,34 @@ async def check_offer_validity(self, request: CheckOfferValidity) -> CheckOfferV
23212323
)
23222324

23232325
@tx_endpoint(push=True)
2326+
@marshal
23242327
async def take_offer(
23252328
self,
2326-
request: dict[str, Any],
2329+
request: TakeOffer,
23272330
action_scope: WalletActionScope,
23282331
extra_conditions: tuple[Condition, ...] = tuple(),
2329-
) -> EndpointResult:
2330-
offer_hex: str = request["offer"]
2331-
2332-
offer = Offer.from_bech32(offer_hex)
2333-
fee: uint64 = uint64(request.get("fee", 0))
2334-
maybe_marshalled_solver: Optional[dict[str, Any]] = request.get("solver")
2335-
solver: Optional[Solver]
2336-
if maybe_marshalled_solver is None:
2337-
solver = None
2338-
else:
2339-
solver = Solver(info=maybe_marshalled_solver)
2340-
2332+
) -> TakeOfferResponse:
23412333
peer = self.service.get_full_node_peer()
23422334
trade_record = await self.service.wallet_state_manager.trade_manager.respond_to_offer(
2343-
offer,
2335+
request.parsed_offer,
23442336
peer,
23452337
action_scope,
2346-
fee=fee,
2347-
solver=solver,
2338+
fee=request.fee,
2339+
solver=request.solver,
23482340
extra_conditions=extra_conditions,
23492341
)
23502342

23512343
async with action_scope.use() as interface:
23522344
interface.side_effects.signing_responses.append(
2353-
SigningResponse(bytes(offer._bundle.aggregated_signature), trade_record.trade_id)
2345+
SigningResponse(bytes(request.parsed_offer._bundle.aggregated_signature), trade_record.trade_id)
23542346
)
23552347

2356-
return {
2357-
"trade_record": trade_record.to_json_dict_convenience(),
2358-
"offer": Offer.from_bytes(trade_record.offer).to_bech32(),
2359-
"transactions": None, # tx_endpoint wrapper will take care of this
2360-
"signing_responses": None, # tx_endpoint wrapper will take care of this
2361-
}
2348+
return TakeOfferResponse(
2349+
[], # tx_endpoint will fill in this default value
2350+
[], # tx_endpoint will fill in this default value
2351+
Offer.from_bytes(trade_record.offer),
2352+
trade_record,
2353+
)
23622354

23632355
async def get_offer(self, request: dict[str, Any]) -> EndpointResult:
23642356
trade_mgr = self.service.wallet_state_manager.trade_manager

chia/wallet/wallet_rpc_client.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
SplitCoinsResponse,
193193
SubmitTransactions,
194194
SubmitTransactionsResponse,
195+
TakeOffer,
195196
TakeOfferResponse,
196197
VCAddProofs,
197198
VCGet,
@@ -690,26 +691,16 @@ async def check_offer_validity(self, request: CheckOfferValidity) -> CheckOfferV
690691

691692
async def take_offer(
692693
self,
693-
offer: Offer,
694+
request: TakeOffer,
694695
tx_config: TXConfig,
695-
solver: Optional[dict[str, Any]] = None,
696-
fee: int = 0,
697696
extra_conditions: tuple[Condition, ...] = tuple(),
698697
timelock_info: ConditionValidTimes = ConditionValidTimes(),
699-
push: bool = True,
700698
) -> TakeOfferResponse:
701-
req = {
702-
"offer": offer.to_bech32(),
703-
"fee": fee,
704-
"extra_conditions": conditions_to_json_dicts(extra_conditions),
705-
"push": push,
706-
**tx_config.to_json_dict(),
707-
**timelock_info.to_json_dict(),
708-
}
709-
if solver is not None:
710-
req["solver"] = solver
711-
res = await self.fetch("take_offer", req)
712-
return json_deserialize_with_clvm_streamable(res, TakeOfferResponse)
699+
return TakeOfferResponse.from_json_dict(
700+
await self.fetch(
701+
"take_offer", request.json_serialize_for_transport(tx_config, extra_conditions, timelock_info)
702+
)
703+
)
713704

714705
async def get_offer(self, trade_id: bytes32, file_contents: bool = False) -> TradeRecord:
715706
res = await self.fetch("get_offer", {"trade_id": trade_id.hex(), "file_contents": file_contents})

0 commit comments

Comments
 (0)