Skip to content

Commit

Permalink
currencyrate: general update and fixes for upstream options compat
Browse files Browse the repository at this point in the history
  • Loading branch information
daywalker90 authored and chrisguida committed Jul 11, 2024
1 parent 90ec46e commit 13cec86
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
8 changes: 3 additions & 5 deletions currencyrate/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Currencyrate plugin

This plugin provides Bitcoin currency conversion functions using various
different backends and taking the median. It caches results for an hour.
different backends and taking the median. It caches results for an hour.

## Installation

Expand All @@ -17,8 +17,7 @@ For general plugin installation instructions see the repos main
would be "USD,last_trade".
* --disable-source: Disable the source with this name.

For c-lightning versions 0.9.3 and above, you can specify these
options multiple times to add or disable multiple sources.
You can specify these options multiple times to add or disable multiple sources.

## Commands

Expand All @@ -27,7 +26,7 @@ options multiple times to add or disable multiple sources.
```
$ lightning-cli currencyrates USD
{
"localbitcoins": "5347227msat",
"coindesk": "5347227msat",
"bitstamp": "5577515msat",
"coingecko": "5579273msat",
}
Expand All @@ -48,7 +47,6 @@ $ lightning-cli currencyconvert 100 USD
Thanks to those services who provide this information. Coindesk require
a blurb, so I did that for everyone (quoting from their front page):

* localbitcoins.com: "Buy and Sell Bitcoin Everywhere"
* www.bitstamp.net: "The original global crypto exchange."
* api.coingecko.com: "The world's most comprehensive cryptocurrency API"
* api.coindesk.com: "Powered by CoinDesk: https://www.coindesk.com/price/bitcoin"
Expand Down
47 changes: 28 additions & 19 deletions currencyrate/currencyrate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#!/usr/bin/env python3
from pyln.client import Plugin
from collections import namedtuple
from pyln.client import Millisatoshi
from cachetools import cached, TTLCache
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import requests
import statistics
try:
import statistics
from collections import namedtuple

import requests
from cachetools import TTLCache, cached
from pyln.client import Millisatoshi, Plugin
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
except ModuleNotFoundError as err:
# OK, something is not installed?
import json
import sys

getmanifest = json.loads(sys.stdin.readline())
print(
json.dumps({
"jsonrpc": "2.0",
"id": getmanifest["id"],
"result": {"disable": str(err)},
})
)
sys.exit(1)

plugin = Plugin()

Expand Down Expand Up @@ -141,18 +156,12 @@ def init(options, configuration, plugin):
set_proxies(plugin)

sourceopts = options["add-source"]
# Prior to 0.9.3, 'multi' was unsupported.
if type(sourceopts) is not list:
sourceopts = [sourceopts]
if sourceopts != [""]:
for s in sourceopts:
parts = s.split(",")
sources.append(Source(parts[0], parts[1], parts[2:]))

disableopts = options["disable-source"]
# Prior to 0.9.3, 'multi' was unsupported.
if type(disableopts) is not list:
disableopts = [disableopts]
if disableopts != [""]:
for s in sources[:]:
if s.name in disableopts:
Expand All @@ -164,13 +173,13 @@ def init(options, configuration, plugin):
name="add-source",
default="",
description="Add source name,urlformat,resultmembers...",
multi=True,
)
plugin.add_option(
name="disable-source", default="", description="Disable source by name"
name="disable-source",
default="",
description="Disable source by name",
multi=True,
)

# This has an effect only for recent pyln versions (0.9.3+).
plugin.options["add-source"]["multi"] = True
plugin.options["disable-source"]["multi"] = True

plugin.run()
9 changes: 5 additions & 4 deletions currencyrate/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pyln-client>=0.7.3
requests>=2.10.0
requests[socks]>=2.10.0
cachetools
pyln-client>=23
requests>=2.32.2
requests[socks]>=2.32.2
urllib3>=2.2.2
cachetools>=5.3.3
3 changes: 2 additions & 1 deletion currencyrate/test_currencyrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_currencyrate(node_factory):
opts = {
"plugin": plugin_path,
"allow-deprecated-apis": deprecated_apis,
"disable-source": "bitstamp",
"disable-source": ["bitstamp", "coinbase"],
}
l1 = node_factory.get_node(options=opts)
plugins = [os.path.basename(p["name"]) for p in l1.rpc.plugin("list")["plugins"]]
Expand All @@ -40,6 +40,7 @@ def test_currencyrate(node_factory):
rates = l1.rpc.call("currencyrates", ["USD"])
LOGGER.info(rates)
assert "bitstamp" not in rates
assert "coinbase" not in rates
assert "coingecko" in rates
assert extract_digits(rates["coingecko"]) > 0

Expand Down

0 comments on commit 13cec86

Please sign in to comment.