1
0
Commit Graph

68 Commits

Author SHA1 Message Date
SomberNight
09b9fb8374 exchange_rate: try harder to refresh quote when cache is expiring
Previously we polled every 2.5 minutes to get the fx spot price,
and had a 10 minute cache expiry during which the latest spot price
was valid.

On Android, this often resulted in having no price available (showing
"No data" in GUI) when putting the app in the foreground after e.g.
a half-hour sleep in the background: often there would be no fx price
until the next tick, which could take 2.5 minutes. (btw in some cases
I saw the application trying to get new quotes from the network as
soon as the app was put in the foreground but it seems those happened
so fast that the network was not ready yet and DNS lookups failed)

Now we make the behaviour a bit more complex: we still fetch the price
every 2.5 mins, and the cache is still valid for 10 mins, however if
the last price is >7.5 mins old, we become more aggressive and go into
an exponential backoff, initially trying a request every few seconds.
For the Android scenario, this means there might be "No data" for fx
for a few seconds after a long sleep, however if there is a working
network, it should soon get a fresh fx spot price quote.
2023-06-14 15:42:28 +00:00
SomberNight
23f2412da7 qt: follow-up "rm thousand sep when copying numbers to clipboard"
follow-up https://github.com/spesmilo/electrum/pull/8479
2023-06-13 15:59:18 +00:00
SomberNight
24980feab7 config: introduce ConfigVars
A new config API is introduced, and ~all of the codebase is adapted to it.
The old API is kept but mainly only for dynamic usage where its extra flexibility is needed.

Using examples, the old config API looked this:
```
>>> config.get("request_expiry", 86400)
604800
>>> config.set_key("request_expiry", 86400)
>>>
```

The new config API instead:
```
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS
604800
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS = 86400
>>>
```

The old API operated on arbitrary string keys, the new one uses
a static ~enum-like list of variables.

With the new API:
- there is a single centralised list of config variables, as opposed to
  these being scattered all over
- no more duplication of default values (in the getters)
- there is now some (minimal for now) type-validation/conversion for
  the config values

closes https://github.com/spesmilo/electrum/pull/5640
closes https://github.com/spesmilo/electrum/pull/5649

Note: there is yet a third API added here, for certain niche/abstract use-cases,
where we need a reference to the config variable itself.
It should only be used when needed:
```
>>> var = config.cv.WALLET_PAYREQ_EXPIRY_SECONDS
>>> var
<ConfigVarWithConfig key='request_expiry'>
>>> var.get()
604800
>>> var.set(3600)
>>> var.get_default_value()
86400
>>> var.is_set()
True
>>> var.is_modifiable()
True
```
2023-05-25 17:39:48 +00:00
SomberNight
03ab33f4b2 SimpleConfig: change API of set_key(): "save" is now kwarg-only 2023-05-25 17:37:16 +00:00
Ali Sherief
a7d3240bc2 Remove Localbitcoins provider 2023-05-16 10:06:55 +02:00
Sander van Grieken
4df6052567 qml: add validators for BtcField and FiatField controls 2023-05-09 15:43:52 +02:00
SomberNight
3bdda3a861 config: force 'history_rates' config var to bool
fixes https://github.com/spesmilo/electrum/issues/8367

probably regression from 503776c0de
(note that before that commit, we were casting to bool)
2023-05-03 12:18:34 +00:00
SomberNight
312f2641e7 don't use bare except
use "except Exception", or if really needed explicitly "except BaseException"
2023-04-24 12:58:01 +00:00
ThomasV
8774e59347 exchange rate: if default unit is sat, display value of 1000 sats 2023-04-13 12:03:22 +02:00
SomberNight
512b63c424 exchange_rate: FxThread does not need network 2023-03-29 16:41:09 +00:00
SomberNight
7834f6c427 commands: fix satoshis decimal conversion in payto cmd and others
When called via jsonrpc (but not via cli) with non-string amounts,
there could be a rounding error resulting in sending 1 sat less.

example:
```
$ ./run_electrum --testnet -w ~/.electrum/testnet/wallets/test_segwit_2 paytomany '[["tb1q6k5h4cz6ra8nzhg90xm9wldvadgh0fpttfthcg", 0.00033389]]' --fee 0
02000000000101b9e6018acb16952e3c9618b069df404dc85544eda8120e5f6e7cd7e94ce5ae8d0100000000fdffffff02fd8100000000000016001410c5b97085ec1637a9f702852f5a81f650fae1566d82000000000000160014d5a97ae05a1f4f315d0579b6577daceb5177a42b024730440220251d2ce83f6e69273de8e9be8602fbcf72b9157e1c0116161fa52f7e04db6e4302202d84045cc6b7056a215d1db3f59884e28dadd5257e1a3960068f90df90b452d1012102b0eff3bf364a2ab5effe952cba33521ebede81dac88c71951a5ed598cb48347b3a022500

$ curl --data-binary '{"id":"curltext","method":"paytomany","params":{"outputs":[["tb1q6k5h4cz6ra8nzhg90xm9wldvadgh0fpttfthcg", 0.00033389]], "fee": 0, "wallet": "/home/user/.electrum/testnet/wallets/test_segwit_2"}}' http://user:pass@127.0.0.1:7777
{"id": "curltext", "jsonrpc": "2.0", "result": "02000000000101b9e6018acb16952e3c9618b069df404dc85544eda8120e5f6e7cd7e94ce5ae8d0100000000fdffffff02fe8100000000000016001410c5b97085ec1637a9f702852f5a81f650fae1566c82000000000000160014d5a97ae05a1f4f315d0579b6577daceb5177a42b0247304402206ef66b845ca298c14dc6e8049cba9ed19db1671132194518ce5d521de6f5df8802205ca4b1aee703e3b98331fb9b88210917b385560020c8b2a8a88da38996b101c4012102b0eff3bf364a2ab5effe952cba33521ebede81dac88c71951a5ed598cb48347b39022500"}
```
^ note that first tx has output for 0.00033389, second tx has output for 0.00033388

fixes https://github.com/spesmilo/electrum/issues/8274
2023-03-22 12:22:36 +00:00
SomberNight
0a5d18634c exchange_rate: guard against garbage hist data coming from exchange
See discussion at 583089d57b (r104678577)
CoinGecko for PLN gives "None" str as rate (instead of null) for two months mid-2014:
```
  2.29 | D | exchange_rate.CoinGecko | found corrupted historical_rate: rate='None'. for ccy='PLN' at 2014-05-10
```
Thanks to @lukasz1992 for reporting.
2023-03-16 16:11:02 +00:00
ThomasV
7207f13e97 Qt: set history_rates both through settings_dialog and history_list
follow-up 503776c0de
2023-03-16 16:48:12 +01:00
ThomasV
503776c0de move fiat columns show/hide settings from settings_dialog to tab toolbars 2023-03-12 13:30:11 +01:00
SomberNight
2a9909c252 locale amounts: consistently use "." as dec point, and " " as thou sep
Always use "." as decimal point, and " " as thousands separator.

Previously,
- for decimal point, we were using
  - "." in some places (e.g. AmountEdit, most fiat amounts), and
  - `locale.localeconv()['decimal_point']` in others.
- for thousands separator, we were using
  - "," in some places (most fiat amounts), and
  - " " in others (format_satoshis)

I think it is better to be consistent even if whatever we pick differs from the locale.
Using whitespace for thousands separator (vs comma) is probably less confusing for people
whose locale would user "." for ts and "," for dp (as in e.g. German).

The alternative option would be to always use the locale. Even if we decide to do that later,
this refactoring should be useful.

closes https://github.com/spesmilo/electrum/issues/2629
2023-01-10 14:45:35 +00:00
Joel Lehtonen
0b540956fb exchange_rate: "BTC" to "BTC" rate is not guaranteed to be present
Which ccy rates are available depends on the configured
exchange (config key use_exchange) and the configured currency (config
key currency). Only for some exchanges, the fx.ccy-BTC fx rate is
available (depends on the ExchangeBase.get_rates implementation).

As they say, for hodlers 1 BTC = 1 BTC.
2022-12-06 04:19:23 +02:00
Joel Lehtonen
01d31dd61d exchange_rate: Add precisions of some cryptocurrencies
There are many cryptocurrencies available in CoinGecko and some other
exchange rate providers. If the user wants to use a cryptocurrency as
a display currency, the precisions used to be 2. This patch adds
precisions of some cryptocurrencies.
2022-12-05 23:08:12 +02:00
Joel Lehtonen
9ac58d0bc0 exchange_rate: Allow formatting amount with custom currency 2022-12-05 23:08:12 +02:00
Arian Ventura Rodríguez
7de1c604c6 Add yadio.io api to exchange rate 2022-09-26 17:11:08 +02:00
SomberNight
03b514863e exchange_rate: more robust spot price against temporary network issues
We poll the fx rate provider every 2.5 minutes (unchanged).
Previously if there was any error during a tick, there was no fx rate
available in the client until the next tick.
Now, instead, we keep the last rates received with a 10 minute expiry.

One potential drawback is that previously there was instant feedback
to the user when e.g. changing proxy settings, and this is no longer
the case. E.g. consider a provider that bans Tor exit nodes. If a user
enables using a Tor proxy in the network settings, the fxrate used to
disappear immediately - but now the cached rate would still be
available.
2022-08-26 11:48:51 +00:00
SomberNight
5b000a871f EventListener follow-ups: adapt left-out classes and minor clean-ups 2022-06-22 02:09:26 +02:00
SomberNight
2c57c78ebe asyncio: stop using get_event_loop(). introduce ~singleton loop.
asyncio.get_event_loop() became deprecated in python3.10. (see https://github.com/python/cpython/issues/83710)
```
.../electrum/electrum/daemon.py:470: DeprecationWarning: There is no current event loop
  self.asyncio_loop = asyncio.get_event_loop()
.../electrum/electrum/network.py:276: DeprecationWarning: There is no current event loop
  self.asyncio_loop = asyncio.get_event_loop()
```
Also, according to that thread, "set_event_loop() [... is] not deprecated by oversight".
So, we stop using get_event_loop() and set_event_loop() in our own code.
Note that libraries we use (such as the stdlib for python <3.10), might call get_event_loop,
which then relies on us having called set_event_loop e.g. for the GUI thread. To work around
this, a custom event loop policy providing a get_event_loop implementation is used.

Previously, we have been using a single asyncio event loop, created with
util.create_and_start_event_loop, and code in many places got a reference to this loop
using asyncio.get_event_loop().
Now, we still use a single asyncio event loop, but it is now stored as a global in
util._asyncio_event_loop (access with util.get_asyncio_loop()).

I believe these changes also fix https://github.com/spesmilo/electrum/issues/5376
2022-04-29 18:49:07 +02:00
SomberNight
583089d57b exchange_rate: normalise some internal types, saner Decimal conversion
fix https://github.com/spesmilo/electrum/issues/7770
2022-04-20 19:01:10 +02:00
SomberNight
81d0928abd exchange_rate: add some type hints 2022-04-20 18:43:24 +02:00
SomberNight
4f102d7752 exchange_rate: don't log full exception trace for timeouts
```
20220222T134125.306163Z |    ERROR | exchange_rate.CoinGecko | failed fx quotes: TimeoutError()
Traceback (most recent call last):
  File "...\electrum\electrum\exchange_rate.py", line 81, in update_safe
    self.quotes = await self.get_rates(ccy)
  File "...\electrum\electrum\exchange_rate.py", line 306, in get_rates
    json = await self.get_json('api.coingecko.com', '/api/v3/exchange_rates')
  File "...\electrum\electrum\exchange_rate.py", line 65, in get_json
    async with session.get(url) as response:
  File "...\Python39\site-packages\aiohttp\client.py", line 1138, in __aenter__
    self._resp = await self._coro
  File "...\Python39\site-packages\aiohttp\client.py", line 634, in _request
    break
  File "...\Python39\site-packages\aiohttp\helpers.py", line 721, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
```
2022-02-22 15:04:37 +01:00
SomberNight
c9c094cfab requirements: bump min aiorpcx to 0.22.0
aiorpcx 0.20 changed the behaviour/API of TaskGroups.
When used as a context manager, TaskGroups no longer propagate
exceptions raised by their tasks. Instead, the calling code has
to explicitly check the results of tasks and decide whether to re-raise
any exceptions.
This is a significant change, and so this commit introduces "OldTaskGroup",
which should behave as the TaskGroup class of old aiorpcx. All existing
usages of TaskGroup are replaced with OldTaskGroup.

closes https://github.com/spesmilo/electrum/issues/7446
2022-02-15 18:22:44 +01:00
SomberNight
3f3212e94d some clean-ups now that we require python 3.8
In particular, asyncio.CancelledError no longer inherits Exception (it inherits BaseException directly)
2022-02-15 18:22:36 +01:00
Benoit Verret
f731c38293 Minor style changes 2021-03-21 00:36:23 -04:00
SomberNight
1ba5997238 qt lightning_tx_dialog: use historical fx rate for fiat amounts 2021-03-12 18:29:00 +01:00
SomberNight
1fb0c28d0a exchange_rate: (fix) rm need to restart app to disable FX rates
Previously if the user disabled FX rates in the settings, the UI
would keep showing the fiat amounts everywhere until the next time
the program was started. (and the rates would not even refresh anymore)
2021-01-29 19:53:04 +01:00
Aldrin P. S. Castro
c32ac41c36 Added new brazillian exchange to exchange rates plugin: Walltime. 2020-12-06 23:35:39 -03:00
SomberNight
13563697f5 exchange_rate: log full traceback in case of unexpected exception
related: #6748
2020-11-20 01:45:11 +01:00
ThomasV
f49e766b21 Merge pull request #6410 from cointradermonitor/cointradermonitor-new-brazilian-index
Brazilian Bitcoin index source included.
2020-07-29 18:01:22 +02:00
Cointrader Monitor
c67eef6e40 Brazilian Bitcoin index source included.
Added Brazilian Bitcoin Index from Cointrader Monitor (https://cointradermonitor.com/api/pbb/v1/ticker) as a "BRL" Fiat source.
The index is calculated from the last price and volume from 30 brazilian exchanges. It is a well-known price index used by bitcoin brazilian users.
More information at https://cointradermonitor.com/
2020-07-19 01:52:31 +00:00
Gustavo
396eabc623 removed scam exchange 2020-07-17 16:23:15 -03:00
Gustavo
edef0cd4b6 fix biscoint ticker path 2020-07-17 15:30:48 -03:00
Gustavo
a073ea6050 more price sources 2020-07-17 15:27:36 -03:00
SomberNight
52f8aafb60 kivy: fix fx history rates defaults.
In kivy, if the user enabled fx rates but did not touch the fx history settings,
the GUI would show that history rates are enabled but in fact they would be disabled:
the GUI called fx.get_history_config(default=True) when displaying the checkbox,
but exchange_rate.py would not fetch history rates.
(it would only get fixed if the user touched the fx history checkbox)

Note: FxThread.run() calls fx.show_history(), which calls fx.get_history_config() without arguments.
2020-07-16 01:00:51 +02:00
SomberNight
cf1f2ba4dc network: replace "server" strings with ServerAddr objects 2020-04-15 17:23:47 +02:00
ThomasV
9224404108 Move callback manager out of Network class 2020-04-14 18:29:51 +02:00
SomberNight
9e57a59615 network: handle main_taskgroup dying better. passthrough CancelledError
Previously if a task running in the main_taskgroup raised,
main_taskgroup might have never finished as fx.run (another task running
in main_taskgroup) could not be cancelled (it was swallowing the CancelledError).

Need to be careful with catching all Exceptions or BaseExceptions,
as that might result in a coroutine not being cancellable.
Note that from python 3.8 onwards, CancelledError will inherit from BaseException
instead of Exception, so catching all Exceptions is somewhat less horrible
but this will only really matter if we raise the min py version to 3.8...

Really, all "except BaseException" lines are suspect and at least should be
considered for replacement with "except Exception".

-----

regarding fx.run not being cancellable before, and relevant lines, see:

6197cfbb3b/electrum/network.py (L1171)
0decdffce2/aiorpcx/curio.py (L242)
0decdffce2/aiorpcx/curio.py (L199)
0decdffce2/aiorpcx/curio.py (L208)
0decdffce2/aiorpcx/curio.py (L218)
0decdffce2/aiorpcx/curio.py (L221)
6197cfbb3b/electrum/daemon.py (L194)
6197cfbb3b/electrum/daemon.py (L203)
6197cfbb3b/electrum/exchange_rate.py (L507)
6197cfbb3b/electrum/exchange_rate.py (L79)
2019-08-30 19:46:25 +02:00
ldz1
d17489e971 Removed dead exchange. 2019-07-21 13:15:06 +02:00
SomberNight
40e2b1d6e7 exchange_rate: fix #5495 2019-07-14 14:34:02 +02:00
SomberNight
93d68a4361 exchange_rate: fix #5487 2019-07-04 19:55:03 +02:00
SomberNight
3385a94753 logging: basics 2019-05-02 15:19:03 +02:00
SomberNight
4374b809fa fix-up prev 2019-03-27 19:01:38 +01:00
Francis Pouliot
a9c13d6641 exchange_rate: add Bylls BTC/CAD rates 2019-03-27 19:00:40 +01:00
SomberNight
6311aa24a8 follow-up prev 2019-03-22 18:52:57 +01:00
SomberNight
c23e949848 exchange_rate: change default provider to CoinGecko
closes #5188
2019-03-22 16:55:57 +01:00
SomberNight
5cbf54d23a exchange_rate: minor clean-up
based on mhsmith/electrum@7ed3f032dd
2019-03-22 16:48:47 +01:00