Skip to content

Commit db14856

Browse files
committed
Port cancel_offer to @marshal
1 parent 9ad6880 commit db14856

File tree

7 files changed

+46
-39
lines changed

7 files changed

+46
-39
lines changed

chia/_tests/cmds/wallet/test_wallet.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from chia.wallet.wallet_coin_store import GetCoinRecords
4343
from chia.wallet.wallet_request_types import (
4444
BalanceResponse,
45+
CancelOffer,
4546
CancelOfferResponse,
4647
CATAssetIDToName,
4748
CATAssetIDToNameResponse,
@@ -1180,14 +1181,14 @@ async def get_offer(self, request: GetOffer) -> GetOfferResponse:
11801181

11811182
async def cancel_offer(
11821183
self,
1183-
trade_id: bytes32,
1184+
request: CancelOffer,
11841185
tx_config: TXConfig,
1185-
fee: uint64 = uint64(0),
1186-
secure: bool = True,
1187-
push: bool = True,
1186+
extra_conditions: tuple[Condition, ...] = tuple(),
11881187
timelock_info: ConditionValidTimes = ConditionValidTimes(),
11891188
) -> CancelOfferResponse:
1190-
self.add_to_log("cancel_offer", (trade_id, tx_config, fee, secure, push, timelock_info))
1189+
self.add_to_log(
1190+
"cancel_offer", (request.trade_id, tx_config, request.fee, request.secure, request.push, timelock_info)
1191+
)
11911192
return CancelOfferResponse([STD_UTX], [STD_TX])
11921193

11931194
inst_rpc_client = CancelOfferRpcClient()

chia/_tests/wallet/rpc/test_wallet_rpc.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
from chia.wallet.wallet_protocol import WalletProtocol
107107
from chia.wallet.wallet_request_types import (
108108
AddKey,
109+
CancelOffer,
109110
CATAssetIDToName,
110111
CATGetAssetID,
111112
CATGetName,
@@ -1622,14 +1623,22 @@ async def test_offer_endpoints(wallet_environments: WalletTestFramework, wallet_
16221623
).trade_record
16231624
assert TradeStatus(trade_record.status) == TradeStatus.PENDING_CONFIRM
16241625

1625-
await env_1.rpc_client.cancel_offer(offer.name(), wallet_environments.tx_config, secure=False)
1626+
await env_1.rpc_client.cancel_offer(
1627+
CancelOffer(
1628+
trade_id=offer.name(),
1629+
secure=False,
1630+
push=True,
1631+
),
1632+
tx_config=wallet_environments.tx_config,
1633+
)
16261634

16271635
trade_record = (await env_1.rpc_client.get_offer(GetOffer(offer.name(), file_contents=True))).trade_record
16281636
assert trade_record.offer == bytes(offer)
16291637
assert TradeStatus(trade_record.status) == TradeStatus.CANCELLED
16301638

16311639
failed_cancel_res = await env_1.rpc_client.cancel_offer(
1632-
offer.name(), wallet_environments.tx_config, fee=uint64(1), secure=True
1640+
CancelOffer(trade_id=offer.name(), fee=uint64(1), secure=True, push=True),
1641+
tx_config=wallet_environments.tx_config,
16331642
)
16341643

16351644
trade_record = (await env_1.rpc_client.get_offer(GetOffer(offer.name()))).trade_record

chia/cmds/wallet_funcs.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from chia.wallet.vc_wallet.vc_store import VCProofs
4646
from chia.wallet.wallet_coin_store import GetCoinRecords
4747
from chia.wallet.wallet_request_types import (
48+
CancelOffer,
4849
CATAssetIDToName,
4950
CATAssetIDToNameResponse,
5051
CATGetName,
@@ -930,11 +931,8 @@ async def cancel_offer(
930931

931932
cli_confirm(f"Are you sure you wish to cancel offer with ID: {trade_record.trade_id}? (y/n): ")
932933
res = await wallet_client.cancel_offer(
933-
offer_id,
934-
CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
935-
secure=secure,
936-
fee=fee,
937-
push=push,
934+
CancelOffer(trade_id=offer_id, secure=secure, fee=fee, push=push),
935+
tx_config=CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
938936
timelock_info=condition_valid_times,
939937
)
940938
if push or not secure:

chia/data_layer/data_layer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
from chia.wallet.transaction_record import TransactionRecord
7070
from chia.wallet.util.tx_config import DEFAULT_TX_CONFIG
7171
from chia.wallet.wallet_request_types import (
72+
CancelOffer,
7273
CreateNewDL,
7374
CreateOfferForIDs,
7475
DLDeleteMirror,
@@ -1283,9 +1284,7 @@ async def cancel_offer(self, trade_id: bytes32, secure: bool, fee: uint64) -> No
12831284
store_ids = [offered.launcher_id for offered in summary.offered]
12841285

12851286
await self.wallet_rpc.cancel_offer(
1286-
trade_id=trade_id,
1287-
secure=secure,
1288-
fee=fee,
1287+
CancelOffer(trade_id=trade_id, secure=secure, fee=fee, push=True),
12891288
# TODO: probably shouldn't be default but due to peculiarities in the RPC, we're using a stop gap.
12901289
# This is not a change in behavior, the default was already implicit.
12911290
tx_config=DEFAULT_TX_CONFIG,

chia/wallet/wallet_request_types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,13 @@ def from_json_dict(cls, json_dict: dict[str, Any]) -> Self:
20182018
)
20192019

20202020

2021+
@streamable
2022+
@dataclass(frozen=True)
2023+
class CancelOffer(TransactionEndpointRequest):
2024+
trade_id: bytes32 = field(default_factory=default_raise)
2025+
secure: bool = field(default_factory=default_raise)
2026+
2027+
20212028
@streamable
20222029
@dataclass(frozen=True)
20232030
class CancelOfferResponse(TransactionEndpointResponse):

chia/wallet/wallet_rpc_api.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
ApplySignatures,
113113
ApplySignaturesResponse,
114114
BalanceResponse,
115+
CancelOffer,
116+
CancelOfferResponse,
115117
CATAssetIDToName,
116118
CATAssetIDToNameResponse,
117119
CATGetAssetID,
@@ -2412,22 +2414,24 @@ async def get_offers_count(self, request: Empty) -> GetOffersCountResponse:
24122414
)
24132415

24142416
@tx_endpoint(push=True)
2417+
@marshal
24152418
async def cancel_offer(
24162419
self,
2417-
request: dict[str, Any],
2420+
request: CancelOffer,
24182421
action_scope: WalletActionScope,
24192422
extra_conditions: tuple[Condition, ...] = tuple(),
2420-
) -> EndpointResult:
2423+
) -> CancelOfferResponse:
24212424
wsm = self.service.wallet_state_manager
2422-
secure = request["secure"]
2423-
trade_id = bytes32.from_hexstr(request["trade_id"])
2424-
fee: uint64 = uint64(request.get("fee", 0))
24252425
async with self.service.wallet_state_manager.lock:
24262426
await wsm.trade_manager.cancel_pending_offers(
2427-
[trade_id], action_scope, fee=fee, secure=secure, extra_conditions=extra_conditions
2427+
[request.trade_id],
2428+
action_scope,
2429+
fee=request.fee,
2430+
secure=request.secure,
2431+
extra_conditions=extra_conditions,
24282432
)
24292433

2430-
return {"transactions": None} # tx_endpoint wrapper will take care of this
2434+
return CancelOfferResponse([], []) # tx_endpoint will fill in default values here
24312435

24322436
@tx_endpoint(push=True, merge_spends=False)
24332437
async def cancel_offers(

chia/wallet/wallet_rpc_client.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
AddKeyResponse,
2020
ApplySignatures,
2121
ApplySignaturesResponse,
22+
CancelOffer,
2223
CancelOfferResponse,
2324
CancelOffersResponse,
2425
CATAssetIDToName,
@@ -715,29 +716,17 @@ async def get_offers_count(self) -> GetOffersCountResponse:
715716

716717
async def cancel_offer(
717718
self,
718-
trade_id: bytes32,
719+
request: CancelOffer,
719720
tx_config: TXConfig,
720-
fee: int = 0,
721-
secure: bool = True,
722721
extra_conditions: tuple[Condition, ...] = tuple(),
723722
timelock_info: ConditionValidTimes = ConditionValidTimes(),
724-
push: bool = True,
725723
) -> CancelOfferResponse:
726-
res = await self.fetch(
727-
"cancel_offer",
728-
{
729-
"trade_id": trade_id.hex(),
730-
"secure": secure,
731-
"fee": fee,
732-
"extra_conditions": conditions_to_json_dicts(extra_conditions),
733-
"push": push,
734-
**tx_config.to_json_dict(),
735-
**timelock_info.to_json_dict(),
736-
},
724+
return CancelOfferResponse.from_json_dict(
725+
await self.fetch(
726+
"cancel_offer", request.json_serialize_for_transport(tx_config, extra_conditions, timelock_info)
727+
)
737728
)
738729

739-
return json_deserialize_with_clvm_streamable(res, CancelOfferResponse)
740-
741730
async def cancel_offers(
742731
self,
743732
tx_config: TXConfig,

0 commit comments

Comments
 (0)