Skip to content

Commit 4fb49f1

Browse files
committed
-prevent responding to own trade
-failures now have reasons attached
1 parent 1516811 commit 4fb49f1

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/wallet/trade_manager.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ async def respond_to_offer(self, file_path: Path) -> bool:
228228
] = await self.wallet_state_manager.get_wallet_for_colour(
229229
colour
230230
)
231+
unspent = await self.wallet_state_manager.get_spendable_coins_for_wallet(wallets[colour].wallet_info.id)
232+
if coinsol.coin in [record.coin for record in unspent]:
233+
return False, "can't respond to own offer"
231234
innerpuzzlereveal = solution.rest().rest().rest().first()
232235
innersol = solution.rest().rest().rest().rest().first()
233236
out_amount = cc_wallet_puzzles.get_output_amount_for_puzzle_and_solution(
@@ -267,6 +270,9 @@ async def respond_to_offer(self, file_path: Path) -> bool:
267270
coinsols.append(coinsol)
268271
else:
269272
# standard chia coin
273+
unspent = await self.wallet_state_manager.get_spendable_coins_for_wallet(1)
274+
if coinsol.coin in [record.coin for record in unspent]:
275+
return False, "can't respond to own offer"
270276
if chia_discrepancy is None:
271277
chia_discrepancy = cc_wallet_puzzles.get_output_discrepancy_for_puzzle_and_solution(
272278
coinsol.coin, puzzle, solution
@@ -309,7 +315,7 @@ async def respond_to_offer(self, file_path: Path) -> bool:
309315
my_cc_spends.add(add)
310316

311317
if my_cc_spends == set() or my_cc_spends is None:
312-
return False
318+
return False, "insufficient funds"
313319

314320
auditor = my_cc_spends.pop()
315321
auditor_inner_puzzle = await self.get_inner_puzzle_for_puzzle_hash(
@@ -570,4 +576,4 @@ async def respond_to_offer(self, file_path: Path) -> bool:
570576
for tx in my_tx_records:
571577
await self.wallet_state_manager.add_transaction(tx)
572578

573-
return True
579+
return True, None

src/wallet/websocket_server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,11 @@ async def create_offer_for_ids(self, websocket, request, response_api):
464464

465465
async def respond_to_offer(self, websocket, request, response_api):
466466
file_path = Path(request["filename"])
467-
success = await self.trade_manager.respond_to_offer(file_path)
468-
response = {"success": success}
467+
success, reason = await self.trade_manager.respond_to_offer(file_path)
468+
if success:
469+
response = {"success": success}
470+
else:
471+
response = {"success": success, "reason": reason}
469472
return await websocket.send(format_response(response_api, response))
470473

471474
async def safe_handle(self, websocket, path):

0 commit comments

Comments
 (0)