core-lightning/tests/plugins/balance_snaps.py
niftynei 70a73928cb balance-snaps: add a balance_snapshot event; fires after first catchup
Fire off a snapshot of current account balances (node wallet + every
'active' channel) after we've caught up to the chain tip for the *first*
time (in other words, on start).
2021-12-28 04:42:42 +10:30

31 lines
724 B
Python
Executable File

#!/usr/bin/env python3
from pyln.client import Plugin
import json
import os.path
plugin = Plugin()
@plugin.subscribe("balance_snapshot")
def notify_balance_snapshot(plugin, balance_snapshot, **kwargs):
# we save to disk so that we don't get borked if the node restarts
# assumes notification calls are synchronous (not thread safe)
with open('snaps.json', 'a') as f:
f.write(json.dumps(balance_snapshot) + ',')
@plugin.method('listsnapshots')
def return_moves(plugin):
result = []
if os.path.exists('snaps.json'):
with open('snaps.json', 'r') as f:
jd = f.read()
result = json.loads('[' + jd[:-1] + ']')
return {'balance_snapshots': result}
plugin.run()