2021-10-08 10:15:40 -07:00
#!/usr/bin/env python3
2021-06-16 08:06:47 -07:00
import argparse
import os
import os.path
import sys
from decimal import Decimal
from solana.publickey import PublicKey
sys.path.insert(0, os.path.abspath(
2021-06-25 02:33:40 -07:00
os.path.join(os.path.dirname(__file__), "..")))
2021-06-16 08:06:47 -07:00
import mango # nopep8
parser = argparse.ArgumentParser(
description="Sends a notification if an account's SOL balance is below the '--minimum-sol-balance' parameter threshold.")
2021-07-23 02:20:44 -07:00
mango.ContextBuilder.add_command_line_parameters(parser)
2021-06-16 08:06:47 -07:00
parser.add_argument("--address", type=PublicKey, required=True,
help="address of the account")
parser.add_argument("--minimum-sol-balance", type=Decimal, default=Decimal("0.1"),
help="the minimum SOL balance required for the alert. A SOL balance less than this value will trigger a nifitication.")
parser.add_argument("--notify", type=mango.parse_subscription_target, action="append", default=[],
help="The notification target for low balance events")
2021-09-13 06:05:19 -07:00
args: argparse.Namespace = mango.parse_args(parser)
2021-06-16 08:06:47 -07:00
2021-07-23 02:20:44 -07:00
context = mango.ContextBuilder.from_command_line_parameters(args)
2021-06-16 08:06:47 -07:00
account_info = mango.AccountInfo.load(context, args.address)
if account_info is None:
raise Exception(f"No account at '{args.address}'")
else:
2021-08-27 12:37:23 -07:00
if account_info.sols < args.minimum_sol_balance:
report = f"Account \"{args.name} [{args.address}]\" on {context.client.cluster_name} has only {account_info.sols} SOL, which is below the minimum required balance of {args.minimum_sol_balance} SOL."
2021-06-16 08:06:47 -07:00
for notify in args.notify:
notify.send(report)
print(f"Notification sent: {report}")