Skip to content

Commit

Permalink
[CHIA-427] Port chia wallet take/cancel_offer to @tx_out_cmd (#18038)
Browse files Browse the repository at this point in the history
This brings another set of commands to the @tx_out_cmd decorator which
gives it the capability to optionally push a transaction and export the
transactions to a local file.
  • Loading branch information
Quexington committed May 20, 2024
2 parents 6a51fdc + c598f70 commit 0afa088
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
16 changes: 11 additions & 5 deletions chia/_tests/cmds/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,9 @@ async def take_offer(
tx_config: TXConfig,
solver: Optional[Dict[str, Any]] = None,
fee: uint64 = uint64(0),
push: bool = True,
) -> TakeOfferResponse:
self.add_to_log("take_offer", (offer, tx_config, solver, fee))
self.add_to_log("take_offer", (offer, tx_config, solver, fee, push))
return TakeOfferResponse(
[STD_UTX],
[STD_TX],
Expand Down Expand Up @@ -981,7 +982,7 @@ async def take_offer(
(cat2,),
(bytes32.from_hexstr("accce8e1c71b56624f2ecaeff5af57eac41365080449904d0717bd333c04806d"),),
],
"take_offer": [(Offer.from_bech32(test_offer_file_bech32), DEFAULT_TX_CONFIG, None, 1000000000000)],
"take_offer": [(Offer.from_bech32(test_offer_file_bech32), DEFAULT_TX_CONFIG, None, 1000000000000, True)],
}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)

Expand Down Expand Up @@ -1010,9 +1011,14 @@ async def get_offer(self, trade_id: bytes32, file_contents: bool = False) -> Tra
)

async def cancel_offer(
self, trade_id: bytes32, tx_config: TXConfig, fee: uint64 = uint64(0), secure: bool = True
self,
trade_id: bytes32,
tx_config: TXConfig,
fee: uint64 = uint64(0),
secure: bool = True,
push: bool = True,
) -> CancelOfferResponse:
self.add_to_log("cancel_offer", (trade_id, tx_config, fee, secure))
self.add_to_log("cancel_offer", (trade_id, tx_config, fee, secure, push))
return CancelOfferResponse([STD_UTX], [STD_TX])

inst_rpc_client = CancelOfferRpcClient() # pylint: disable=no-value-for-parameter
Expand All @@ -1033,7 +1039,7 @@ async def cancel_offer(
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"get_offer": [(test_offer_id_bytes, True)],
"cancel_offer": [(test_offer_id_bytes, DEFAULT_TX_CONFIG, 1000000000000, True)],
"cancel_offer": [(test_offer_id_bytes, DEFAULT_TX_CONFIG, 1000000000000, True, True)],
"cat_asset_id_to_name": [
(cat1,),
(cat2,),
Expand Down
17 changes: 12 additions & 5 deletions chia/cmds/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ def add_token_cmd(wallet_rpc_port: Optional[int], asset_id: str, token_name: str
default=False,
)
@click.option("--override", help="Creates offer without checking for unusual values", is_flag=True, default=False)
# This command looks like a good candidate for @tx_out_cmd however, pushing an incomplete tx is nonsensical and
# we already have a canonical offer file format which the idea of exporting a different transaction conflicts with
def make_offer_cmd(
wallet_rpc_port: Optional[int],
fingerprint: int,
Expand Down Expand Up @@ -564,17 +566,19 @@ def get_offers_cmd(
is_flag=True,
default=False,
)
@tx_out_cmd
def take_offer_cmd(
path_or_hex: str,
wallet_rpc_port: Optional[int],
fingerprint: int,
examine_only: bool,
fee: str,
reuse: bool,
) -> None:
reuse: bool, # reuse is not used
push: bool,
) -> List[TransactionRecord]:
from .wallet_funcs import take_offer

asyncio.run(take_offer(wallet_rpc_port, fingerprint, Decimal(fee), path_or_hex, examine_only)) # reuse is not used
return asyncio.run(take_offer(wallet_rpc_port, fingerprint, Decimal(fee), path_or_hex, examine_only, push=push))


@wallet_cmd.command("cancel_offer", help="Cancel an existing offer")
Expand All @@ -591,10 +595,13 @@ def take_offer_cmd(
@click.option(
"-m", "--fee", help="The fee to use when cancelling the offer securely, in XCH", default="0", show_default=True
)
def cancel_offer_cmd(wallet_rpc_port: Optional[int], fingerprint: int, id: str, insecure: bool, fee: str) -> None:
@tx_out_cmd
def cancel_offer_cmd(
wallet_rpc_port: Optional[int], fingerprint: int, id: str, insecure: bool, fee: str, push: bool
) -> List[TransactionRecord]:
from .wallet_funcs import cancel_offer

asyncio.run(cancel_offer(wallet_rpc_port, fingerprint, Decimal(fee), id, not insecure))
return asyncio.run(cancel_offer(wallet_rpc_port, fingerprint, Decimal(fee), id, not insecure, push=push))


@wallet_cmd.command("check", short_help="Check wallet DB integrity", help=check_help_text)
Expand Down
45 changes: 30 additions & 15 deletions chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ async def take_offer(
d_fee: Decimal,
file: str,
examine_only: bool,
) -> None:
push: bool = True,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
if os.path.exists(file):
filepath = pathlib.Path(file)
Expand All @@ -711,7 +712,7 @@ async def take_offer(
offer = Offer.from_bech32(offer_hex)
except ValueError:
print("Please enter a valid offer file or hex blob")
return
return []

offered, requested, _, _ = offer.summary()
cat_name_resolver = wallet_client.cat_asset_id_to_name
Expand Down Expand Up @@ -776,15 +777,21 @@ async def take_offer(
if not examine_only:
print()
cli_confirm("Would you like to take this offer? (y/n): ")
trade_record = (
await wallet_client.take_offer(
offer,
fee=fee,
tx_config=CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
res = await wallet_client.take_offer(
offer,
fee=fee,
tx_config=CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
push=push,
)
if push:
print(f"Accepted offer with ID {res.trade_record.trade_id}")
print(
f"Use chia wallet get_offers --id {res.trade_record.trade_id} -f {fingerprint} to view its status"
)
).trade_record
print(f"Accepted offer with ID {trade_record.trade_id}")
print(f"Use chia wallet get_offers --id {trade_record.trade_id} -f {fingerprint} to view its status")

return res.transactions
else:
return []


async def cancel_offer(
Expand All @@ -793,7 +800,8 @@ async def cancel_offer(
d_fee: Decimal,
offer_id_hex: str,
secure: bool,
) -> None:
push: bool = True,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
offer_id = bytes32.from_hexstr(offer_id_hex)
fee: int = int(d_fee * units["chia"])
Expand All @@ -802,13 +810,20 @@ async def cancel_offer(
await print_trade_record(trade_record, wallet_client, summaries=True)

cli_confirm(f"Are you sure you wish to cancel offer with ID: {trade_record.trade_id}? (y/n): ")
await wallet_client.cancel_offer(
offer_id, CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint), secure=secure, fee=fee
res = await wallet_client.cancel_offer(
offer_id,
CMDTXConfigLoader().to_tx_config(units["chia"], config, fingerprint),
secure=secure,
fee=fee,
push=push,
)
print(f"Cancelled offer with ID {trade_record.trade_id}")
if secure:
if push or not secure:
print(f"Cancelled offer with ID {trade_record.trade_id}")
if secure and push:
print(f"Use chia wallet get_offers --id {trade_record.trade_id} -f {fingerprint} to view cancel status")

return res.transactions


def wallet_coin_unit(typ: WalletType, address_prefix: str) -> Tuple[str, int]: # pragma: no cover
if typ in {WalletType.CAT, WalletType.CRCAT}:
Expand Down

0 comments on commit 0afa088

Please sign in to comment.