pyln-client and Python decorators
Tuesday, March 28 was the first live coding session, maybe you joined us maybe not. In both cases you can find the writeup of the session on LNROOM.
Do you know about Python decorators? This is a mechanism that allows
to modify a function's definition. For instance, if we want to modify
the function's definition func
where decorator
is a function that
takes a function as argument and return a function, we can use
decorators and write something like this using @
symbol
@decorator
def func():
pass
which is equivalent to
def func():
pass
func = decorator(func)
See https://peps.python.org/pep-0318/.
Why talking about Python decorators?
Because this is the mechanism pyln-client
library uses to declare
new JSON-RPC methods in plugins.
For instance if we want to add a method mymethod
via a plugin that
returns our node id we can write this plugin:
from pyln.client import Plugin
plugin = Plugin()
@plugin.method("mymethod")
def mymethod(plugin):
node_id = plugin.rpc.getinfo()["id"]
return { "id": node_id }
plugin.run()
If you want to know more about pyln-client
library come join us on
Thursday, March 13 at the next event.
Two weeks ago I attended Liquidity Ads: Everything You Need To Know event where Alex Myer insisted on the importance of building LN at the protocol level in order to have a better P2P network in the long run.
Maybe you want to give it a try and watch the recording.
He also recommended this article about liquidity ads written by Lysa Neigut in this follow-up.
Have an amazing day, Tony Aldon https://lnroom.live