Skip to content

Commit b908965

Browse files
committed
feat: add script to get ADA amounts locked on testnet
- Introduced `testnet_cleanup_info.py` to fetch ADA amounts still locked. - Added `info` and `info_addresses` functions in `testnet_cleanup.py` to calculate total balance and rewards of all addresses in a given location.
1 parent 9786cca commit b908965

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Print the Lovelace balance and rewards of all addresses in the given location."""
3+
4+
import argparse
5+
import logging
6+
import os
7+
import pathlib as pl
8+
import sys
9+
10+
from cardano_clusterlib import clusterlib
11+
12+
from cardano_node_tests.utils import helpers
13+
from cardano_node_tests.utils import testnet_cleanup
14+
15+
LOGGER = logging.getLogger(__name__)
16+
17+
18+
def get_args() -> argparse.Namespace:
19+
"""Get command line arguments."""
20+
parser = argparse.ArgumentParser(description=__doc__.split("\n", maxsplit=1)[0])
21+
parser.add_argument(
22+
"-a",
23+
"--artifacts-base-dir",
24+
required=True,
25+
type=helpers.check_dir_arg,
26+
help="Path to a directory with testing artifacts",
27+
)
28+
return parser.parse_args()
29+
30+
31+
def main() -> int:
32+
logging.basicConfig(
33+
format="%(name)s:%(levelname)s:%(message)s",
34+
level=logging.INFO,
35+
)
36+
args = get_args()
37+
38+
socket_env = os.environ.get("CARDANO_NODE_SOCKET_PATH")
39+
if not socket_env:
40+
LOGGER.error("The `CARDANO_NODE_SOCKET_PATH` environment variable is not set.")
41+
return 1
42+
43+
state_dir = pl.Path(socket_env).parent
44+
cluster_obj = clusterlib.ClusterLib(state_dir=state_dir)
45+
location = pl.Path(args.artifacts_base_dir).expanduser().resolve()
46+
balance, rewards = testnet_cleanup.addresses_info(cluster_obj=cluster_obj, location=location)
47+
LOGGER.info(f"Uncleaned balance: {balance}")
48+
LOGGER.info(f"Uncleaned rewards: {rewards}")
49+
50+
return 0
51+
52+
53+
if __name__ == "__main__":
54+
sys.exit(main())

cardano_node_tests/utils/testnet_cleanup.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,43 @@ def _get_faucet_payment_rec(
381381
return faucet_payment
382382

383383

384+
def addresses_info(cluster_obj: clusterlib.ClusterLib, location: pl.Path) -> tp.Tuple[int, int]:
385+
"""Return the total balance and rewards of all addresses in the given location."""
386+
balance = 0
387+
rewards = 0
388+
files_found = group_addr_files(find_addr_files(location))
389+
390+
for files in files_found:
391+
for fpath in files:
392+
f_name = fpath.name
393+
if f_name == "faucet.addr":
394+
continue
395+
396+
# Add sleep to prevent
397+
# "Network.Socket.connect: <socket: 11>: resource exhausted"
398+
time.sleep(0.1)
399+
400+
if f_name.endswith("_stake.addr"):
401+
address = clusterlib.read_address_from_file(fpath)
402+
stake_addr_info = cluster_obj.g_query.get_stake_addr_info(address)
403+
if not stake_addr_info:
404+
continue
405+
f_rewards = stake_addr_info.reward_account_balance
406+
if f_rewards:
407+
rewards += f_rewards
408+
LOGGER.info(f"{f_rewards} on '{fpath}'")
409+
else:
410+
address = clusterlib.read_address_from_file(fpath)
411+
f_balance = cluster_obj.g_query.get_address_balance(
412+
address=address, coin=clusterlib.DEFAULT_COIN
413+
)
414+
if f_balance:
415+
LOGGER.info(f"{f_balance} on '{fpath}'")
416+
balance += f_balance
417+
418+
return balance, rewards
419+
420+
384421
def cleanup(
385422
cluster_obj: clusterlib.ClusterLib,
386423
location: clusterlib.FileType,

0 commit comments

Comments
 (0)