Merge pull request #8091 from zouppen/convert_currency_cmd
Add command for currency conversions
This commit is contained in:
@@ -36,7 +36,7 @@ import inspect
|
||||
from collections import defaultdict
|
||||
from functools import wraps, partial
|
||||
from itertools import repeat
|
||||
from decimal import Decimal
|
||||
from decimal import Decimal, InvalidOperation
|
||||
from typing import Optional, TYPE_CHECKING, Dict, List
|
||||
import os
|
||||
|
||||
@@ -1322,6 +1322,43 @@ class Commands:
|
||||
'onchain_amount': format_satoshis(onchain_amount_sat),
|
||||
}
|
||||
|
||||
@command('n')
|
||||
async def convert_currency(self, from_amount=1, from_ccy = '', to_ccy = ''):
|
||||
"""Converts the given amount of currency to another using the
|
||||
configured exchange rate source.
|
||||
"""
|
||||
if not self.daemon.fx.is_enabled():
|
||||
raise Exception("FX is disabled. To enable, run: 'electrum setconfig use_exchange_rate true'")
|
||||
# Currency codes are uppercase
|
||||
from_ccy = from_ccy.upper()
|
||||
to_ccy = to_ccy.upper()
|
||||
# Default currencies
|
||||
if from_ccy == '':
|
||||
from_ccy = "BTC" if to_ccy != "BTC" else self.daemon.fx.ccy
|
||||
if to_ccy == '':
|
||||
to_ccy = "BTC" if from_ccy != "BTC" else self.daemon.fx.ccy
|
||||
# Get current rates
|
||||
rate_from = self.daemon.fx.exchange.get_cached_spot_quote(from_ccy)
|
||||
rate_to = self.daemon.fx.exchange.get_cached_spot_quote(to_ccy)
|
||||
# Test if currencies exist
|
||||
if rate_from.is_nan():
|
||||
raise Exception(f'Currency to convert from ({from_ccy}) is unknown or rate is unavailable')
|
||||
if rate_to.is_nan():
|
||||
raise Exception(f'Currency to convert to ({to_ccy}) is unknown or rate is unavailable')
|
||||
# Conversion
|
||||
try:
|
||||
from_amount = Decimal(from_amount)
|
||||
to_amount = from_amount / rate_from * rate_to
|
||||
except InvalidOperation:
|
||||
raise Exception("from_amount is not a number")
|
||||
return {
|
||||
"from_amount": self.daemon.fx.ccy_amount_str(from_amount, False, from_ccy),
|
||||
"to_amount": self.daemon.fx.ccy_amount_str(to_amount, False, to_ccy),
|
||||
"from_ccy": from_ccy,
|
||||
"to_ccy": to_ccy,
|
||||
"source": self.daemon.fx.exchange.name(),
|
||||
}
|
||||
|
||||
|
||||
def eval_bool(x: str) -> bool:
|
||||
if x == 'false': return False
|
||||
@@ -1400,6 +1437,9 @@ command_options = {
|
||||
'connection_string': (None, "Lightning network node ID or network address"),
|
||||
'new_fee_rate': (None, "The Updated/Increased Transaction fee rate (in sat/byte)"),
|
||||
'strategies': (None, "Select RBF any one or multiple RBF strategies in any order, separated by ','; Options : 'CoinChooser','DecreaseChange','DecreasePayment' "),
|
||||
'from_amount': (None, "Amount to convert (default: 1)"),
|
||||
'from_ccy': (None, "Currency to convert from"),
|
||||
'to_ccy': (None, "Currency to convert to"),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user