Skip to content

Commit

Permalink
Add @tx_out_cmd decorator (#17842)
Browse files Browse the repository at this point in the history
This adds a new command decorator (unused for now but more PRs to come)
that automatically adds the functionality to export transactions rather
than automatically push them.
  • Loading branch information
Quexington committed May 9, 2024
2 parents ff0b92f + fa800d5 commit d352c11
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions chia/_tests/cmds/wallet/test_tx_decorators.py
@@ -0,0 +1,27 @@
from __future__ import annotations

from typing import Any, List

import click
from click.testing import CliRunner

from chia._tests.cmds.wallet.test_consts import STD_TX
from chia.cmds.cmds_util import TransactionBundle, tx_out_cmd
from chia.wallet.transaction_record import TransactionRecord


def test_tx_out_cmd() -> None:
@click.command()
@tx_out_cmd
def test_cmd(**kwargs: Any) -> List[TransactionRecord]:
with open("./temp.push", "w") as file:
file.write(str(kwargs["push"]))
return [STD_TX, STD_TX]

runner: CliRunner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(test_cmd, ["--transaction-file", "./temp.transaction"])
with open("./temp.transaction", "rb") as file:
assert TransactionBundle.from_bytes(file.read()) == TransactionBundle([STD_TX, STD_TX])
with open("./temp.push") as file2:
assert file2.read() == "True"
27 changes: 27 additions & 0 deletions chia/cmds/cmds_util.py
Expand Up @@ -327,6 +327,33 @@ def timelock_args(func: Callable[..., None]) -> Callable[..., None]:
)


@streamable
@dataclasses.dataclass(frozen=True)
class TransactionBundle(Streamable):
txs: List[TransactionRecord]


def tx_out_cmd(func: Callable[..., List[TransactionRecord]]) -> Callable[..., None]:
def original_cmd(transaction_file: Optional[str] = None, **kwargs: Any) -> None:
txs: List[TransactionRecord] = func(**kwargs)
if transaction_file is not None:
print(f"Writing transactions to file {transaction_file}:")
with open(Path(transaction_file), "wb") as file:
file.write(bytes(TransactionBundle(txs)))

return click.option(
"--push/--no-push", help="Push the transaction to the network", type=bool, is_flag=True, default=True
)(
click.option(
"--transaction-file",
help="A file to write relevant transactions to",
type=str,
required=False,
default=None,
)(original_cmd)
)


@streamable
@dataclasses.dataclass(frozen=True)
class CMDCoinSelectionConfigLoader(Streamable):
Expand Down

0 comments on commit d352c11

Please sign in to comment.