Skip to content

Instantly share code, notes, and snippets.

@niftynei
Created August 18, 2023 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftynei/9fd159ca9fe1cee1b98694402d45f816 to your computer and use it in GitHub Desktop.
Save niftynei/9fd159ca9fe1cee1b98694402d45f816 to your computer and use it in GitHub Desktop.
hodlinvoice.py
#!/usr/bin/env python3
from pyln.client import Plugin
plugin = Plugin()
# TODO/FIXME:
# - on restart, all the state in wait_on;
# - datastore: rpcs on CLN
# - how long to hodl for?
# build in smarts about
# dictionary: key is the payment_hash
# value: list of htlcs for that payment_hash
wait_on = {}
@plugin.subscribe('new_block')
def new_block(...):
# todo: check if too close and fail?
pass
@plugin.async_hook("htlc_accepted")
def on_htlc_accepted(request, htlc, onion, plugin, **kwargs):
if htlc['payment_hash'] in wait_on:
hash_list = wait_on[htlc['payment_hash']]
hash_list.append(request)
else:
request.set_result({'result': 'continue'})
@plugin.method("hodlinvoice")
def hodl(plugin, payment_hash):
if payment_hash not in wait_on:
wait_on[payment_hash] = []
return {'hodl': True, 'payment_hash': payment_hash}
@plugin.method("inspecthodl")
def inspect():
return {'hodled': wait_on}
@plugin.method("unhodlinvoice")
def unhodl(plugin, payment_hash):
count = 0
hodled = False
if payment_hash in wait_on:
hodled = True
for req in wait_on[payment_hash]:
count += 1
req.set_result({'result': 'continue'})
return {'hodl': hodled,
'payment_hash': payment_hash,
'unhodled_count': count}
plugin.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment