2020-03-03 20:49:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-03-09 18:54:41 +00:00
|
|
|
"""
|
2020-09-04 14:12:20 +00:00
|
|
|
funds-checker runs simple checks on a portal node using the siad API and
|
2020-03-09 18:54:41 +00:00
|
|
|
dispatches messages to a Discord channel.
|
|
|
|
"""
|
|
|
|
|
2020-08-31 11:38:45 +00:00
|
|
|
import discord, traceback, asyncio, os
|
2020-03-09 15:38:23 +00:00
|
|
|
from bot_utils import setup, send_msg, siad, sc_precision
|
2020-03-05 17:07:37 +00:00
|
|
|
|
2020-03-09 15:38:23 +00:00
|
|
|
bot_token = setup()
|
2020-03-03 23:26:51 +00:00
|
|
|
client = discord.Client()
|
|
|
|
|
2020-08-18 15:25:54 +00:00
|
|
|
|
2020-08-19 07:25:23 +00:00
|
|
|
async def exit_after(delay):
|
|
|
|
await asyncio.sleep(delay)
|
2020-08-31 11:38:45 +00:00
|
|
|
os._exit(0)
|
2020-08-19 07:25:23 +00:00
|
|
|
|
|
|
|
|
2020-03-03 23:26:51 +00:00
|
|
|
@client.event
|
|
|
|
async def on_ready():
|
|
|
|
await run_checks()
|
2020-08-31 11:39:07 +00:00
|
|
|
asyncio.create_task(exit_after(3))
|
2020-03-03 23:26:51 +00:00
|
|
|
|
2020-03-03 20:49:09 +00:00
|
|
|
|
2020-03-09 15:38:23 +00:00
|
|
|
async def run_checks():
|
2020-09-04 14:12:20 +00:00
|
|
|
print("Running Skynet portal funds checks")
|
2020-03-09 15:38:23 +00:00
|
|
|
try:
|
2020-09-04 14:12:20 +00:00
|
|
|
await check_funds()
|
2020-09-29 10:32:45 +00:00
|
|
|
except: # catch all exceptions
|
2020-03-09 15:38:23 +00:00
|
|
|
trace = traceback.format_exc()
|
|
|
|
await send_msg(client, "```\n{}\n```".format(trace), force_notify=True)
|
2020-03-03 20:49:09 +00:00
|
|
|
|
|
|
|
|
2020-09-04 14:12:20 +00:00
|
|
|
# check_funds checks that the wallet is unlocked, that it has at least 1
|
|
|
|
# allowance worth of money left, and if less than half the allowance is spent.
|
|
|
|
# If all checks pass it sends an informational message.
|
|
|
|
async def check_funds():
|
2020-03-09 19:59:21 +00:00
|
|
|
print("\nChecking wallet/funds health...")
|
2020-03-09 15:38:23 +00:00
|
|
|
wallet_get = siad.get_wallet()
|
|
|
|
renter_get = siad.get_renter()
|
2020-03-03 20:49:09 +00:00
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
if not wallet_get["unlocked"]:
|
2020-03-09 15:38:23 +00:00
|
|
|
await send_msg(client, "Wallet locked", force_notify=True)
|
2020-03-03 23:26:51 +00:00
|
|
|
return
|
2020-03-03 20:49:09 +00:00
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
confirmed_coins = int(wallet_get["confirmedsiacoinbalance"])
|
|
|
|
unconfirmed_coins = int(wallet_get["unconfirmedincomingsiacoins"])
|
|
|
|
unconfirmed_outgoing_coins = int(wallet_get["unconfirmedoutgoingsiacoins"])
|
2020-03-03 20:49:09 +00:00
|
|
|
balance = confirmed_coins + unconfirmed_coins - unconfirmed_outgoing_coins
|
|
|
|
print("Balance: ", balance / sc_precision)
|
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
allowance = renter_get["settings"]["allowance"]
|
|
|
|
allowance_funds = int(allowance["funds"])
|
|
|
|
allocated_funds = int(renter_get["financialmetrics"]["totalallocated"])
|
2020-03-06 15:11:42 +00:00
|
|
|
unallocated_funds = allowance_funds - allocated_funds
|
2020-03-03 23:26:51 +00:00
|
|
|
|
2020-09-29 10:32:45 +00:00
|
|
|
balance_msg = "Balance: {} SC, Allowance Funds: {} SC".format(
|
|
|
|
round(balance / sc_precision), round(allowance_funds / sc_precision)
|
|
|
|
)
|
|
|
|
alloc_msg = "Unallocated: {} SC, Allocated: {} SC".format(
|
|
|
|
round(unallocated_funds / sc_precision), round(allocated_funds / sc_precision)
|
|
|
|
)
|
2020-03-09 15:38:23 +00:00
|
|
|
|
2020-03-03 23:26:51 +00:00
|
|
|
# Send an alert if there is less than 1 allowance worth of money left.
|
|
|
|
if balance < allowance_funds:
|
2020-11-27 15:58:37 +00:00
|
|
|
wallet_address_res = siad.get("/wallet/address")
|
|
|
|
wallet_msg = "Address: {}".format(wallet_address_res["address"])
|
|
|
|
message = "__Wallet balance running low!__ {} {}".format(balance_msg, wallet_msg)
|
2020-09-29 10:32:45 +00:00
|
|
|
return await send_msg(client, message, force_notify=True)
|
2020-03-03 23:26:51 +00:00
|
|
|
|
2020-04-14 18:50:01 +00:00
|
|
|
# Alert devs when only a fraction of the allowance is remaining.
|
2020-04-14 22:22:37 +00:00
|
|
|
SPEND_THRESHOLD = 0.8
|
2020-09-29 10:32:45 +00:00
|
|
|
if allocated_funds >= SPEND_THRESHOLD * allowance_funds:
|
|
|
|
message = "__More than {:.0%} of allowance spent!__ {}".format(
|
|
|
|
SPEND_THRESHOLD, alloc_msg
|
|
|
|
)
|
|
|
|
return await send_msg(client, message, force_notify=True)
|
2020-03-03 23:26:51 +00:00
|
|
|
|
|
|
|
# Send an informational heartbeat if all checks passed.
|
2020-09-29 10:32:45 +00:00
|
|
|
await send_msg(client, "Funds checks passed. {} {}".format(balance_msg, alloc_msg))
|
2020-03-03 20:49:09 +00:00
|
|
|
|
2020-08-31 11:38:45 +00:00
|
|
|
|
2020-03-03 23:26:51 +00:00
|
|
|
client.run(bot_token)
|