Skip to content

Commit

Permalink
[CHIA-423] Port chia wallet send to @tx_out_cmd (#18002)
Browse files Browse the repository at this point in the history
This adds the new @tx_out_cmd decorator to the `chia wallet send`
command. This allows the command to output a transaction file instead of
pushing the transaction automatically.
  • Loading branch information
Quexington authored May 15, 2024
2 parents d352c11 + 9fd0c86 commit 96dd9e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
17 changes: 14 additions & 3 deletions chia/_tests/cmds/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pkg_resources
import pytest
from chia_rs import Coin, G2Element
from click.testing import CliRunner

from chia._tests.cmds.cmd_test_utils import TestRpcClients, TestWalletRpcClient, logType, run_cli_command_and_assert
from chia._tests.cmds.wallet.test_consts import (
Expand All @@ -19,6 +20,7 @@
bytes32_hexstr,
get_bytes32,
)
from chia.cmds.cmds_util import TransactionBundle
from chia.rpc.wallet_request_types import (
CancelOfferResponse,
CATSpendResponse,
Expand Down Expand Up @@ -405,9 +407,18 @@ async def cat_spend(
"Transaction submitted to nodes: [{'peer_id': 'aaaaa'",
f"-f 789101 -tx 0x{get_bytes32(2).hex()}",
]

run_cli_command_and_assert(capsys, root_dir, command_args + [FINGERPRINT_ARG], assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + [CAT_FINGERPRINT_ARG], cat_assert_list)
with CliRunner().isolated_filesystem():
run_cli_command_and_assert(
capsys, root_dir, command_args + [FINGERPRINT_ARG] + ["--transaction-file=temp"], assert_list
)
run_cli_command_and_assert(
capsys, root_dir, command_args + [CAT_FINGERPRINT_ARG] + ["--transaction-file=temp2"], cat_assert_list
)

with open("temp", "rb") as file:
assert TransactionBundle.from_bytes(file.read()) == TransactionBundle([STD_TX])
with open("temp2", "rb") as file:
assert TransactionBundle.from_bytes(file.read()) == TransactionBundle([STD_TX])

# these are various things that should be in the output
expected_calls: logType = {
Expand Down
9 changes: 7 additions & 2 deletions chia/cmds/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

from chia.cmds import options
from chia.cmds.check_wallet_db import help_text as check_help_text
from chia.cmds.cmds_util import tx_out_cmd
from chia.cmds.coins import coins_cmd
from chia.cmds.plotnft import validate_fee
from chia.wallet.transaction_record import TransactionRecord
from chia.wallet.transaction_sorting import SortKey
from chia.wallet.util.address_type import AddressType
from chia.wallet.util.wallet_types import WalletType
Expand Down Expand Up @@ -192,6 +194,7 @@ def get_transactions_cmd(
type=int,
default=0,
)
@tx_out_cmd
def send_cmd(
wallet_rpc_port: Optional[int],
fingerprint: int,
Expand All @@ -206,10 +209,11 @@ def send_cmd(
coins_to_exclude: Sequence[str],
reuse: bool,
clawback_time: int,
) -> None: # pragma: no cover
push: bool,
) -> List[TransactionRecord]:
from .wallet_funcs import send

asyncio.run(
return asyncio.run(
send(
wallet_rpc_port=wallet_rpc_port,
fp=fingerprint,
Expand All @@ -224,6 +228,7 @@ def send_cmd(
excluded_coin_ids=coins_to_exclude,
reuse_puzhash=True if reuse else None,
clawback_time_lock=clawback_time,
push=push,
)
)

Expand Down
37 changes: 22 additions & 15 deletions chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ async def send(
excluded_coin_ids: Sequence[str],
reuse_puzhash: Optional[bool],
clawback_time_lock: int,
) -> None:
push: bool,
) -> List[TransactionRecord]:
async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
if memo is None:
memos = None
Expand All @@ -284,19 +285,19 @@ async def send(
f"A transaction of amount {amount} and fee {fee} is unusual.\n"
f"Pass in --override if you are sure you mean to do this."
)
return
return []
if amount == 0:
print("You can not send an empty transaction")
return
return []
if clawback_time_lock < 0:
print("Clawback time lock seconds cannot be negative.")
return
return []
try:
typ = await get_wallet_type(wallet_id=wallet_id, wallet_client=wallet_client)
mojo_per_unit = get_mojo_per_unit(typ)
except LookupError:
print(f"Wallet id: {wallet_id} not found.")
return
return []

final_fee: uint64 = uint64(int(fee * units["chia"])) # fees are always in XCH mojos
final_amount: uint64 = uint64(int(amount * mojo_per_unit))
Expand All @@ -319,6 +320,7 @@ async def send(
if clawback_time_lock > 0
else None
),
push=push,
)
elif typ in {WalletType.CAT, WalletType.CRCAT}:
print("Submitting transaction...")
Expand All @@ -334,23 +336,28 @@ async def send(
address,
final_fee,
memos,
push=push,
)
else:
print("Only standard wallet and CAT wallets are supported")
return
return []

tx_id = res.transaction.name
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, tx_id))
return None
if push:
start = time.time()
while time.time() - start < 10:
await asyncio.sleep(0.1)
tx = await wallet_client.get_transaction(tx_id)
if len(tx.sent_to) > 0:
print(transaction_submitted_msg(tx))
print(transaction_status_msg(fingerprint, tx_id))
return res.transactions

print("Transaction not yet submitted to nodes")
print(f"To get status, use command: chia wallet get_transaction -f {fingerprint} -tx 0x{tx_id}")
if push: # pragma: no cover
print(f"To get status, use command: chia wallet get_transaction -f {fingerprint} -tx 0x{tx_id}")

return res.transactions # pragma: no cover


async def get_address(wallet_rpc_port: Optional[int], fp: Optional[int], wallet_id: int, new_address: bool) -> None:
Expand Down

0 comments on commit 96dd9e4

Please sign in to comment.