Added gas price data feed from gas-oracle.zoltu.io

pull/2445/head
David Núñez 2020-11-20 13:27:59 +01:00
parent 5500de9a59
commit 659c8db947
2 changed files with 81 additions and 2 deletions

View File

@ -132,3 +132,25 @@ class UpvestGasPriceDatafeed(EthereumGasPriceDatafeed):
self._probe_feed()
self.gas_prices = {self.get_canonical_speed(k): int(Web3.toWei(v, 'gwei'))
for k, v in self._raw_data['estimates'].items()}
class ZoltuGasPriceDatafeed(EthereumGasPriceDatafeed):
"""Gas price datafeed from gas-oracle.zoltu.io"""
name = "gas-oracle.zoltu.io datafeed"
api_url = "https://gas-oracle.zoltu.io"
_speed_names = {
SLOW: 'percentile_40',
MEDIUM: 'percentile_75',
FAST: 'percentile_95',
FASTEST: 'percentile_98'
}
_default_speed = 'fast'
def _parse_gas_prices(self):
self._probe_feed()
self.gas_prices = dict()
for canonical_speed_name, zoltu_speed in self._speed_names.items():
gwei_price = self._raw_data[zoltu_speed].split(" ")[0]
wei_price = int(Web3.toWei(gwei_price, 'gwei'))
self.gas_prices[canonical_speed_name] = wei_price

View File

@ -23,10 +23,12 @@ from requests.exceptions import ConnectionError
from web3 import Web3
from nucypher.utilities.datafeeds import (
EtherchainGasPriceDatafeed,
Datafeed,
EtherchainGasPriceDatafeed,
EthereumGasPriceDatafeed,
UpvestGasPriceDatafeed,
EthereumGasPriceDatafeed)
ZoltuGasPriceDatafeed
)
from nucypher.utilities.gas_strategies import construct_datafeed_fallback_strategy
etherchain_json = {
@ -47,6 +49,39 @@ upvest_json = {
}
}
zoltu_json = {
"number_of_blocks": 200,
"latest_block_number": 11294588,
"percentile_1": "1 nanoeth",
"percentile_2": "1 nanoeth",
"percentile_3": "3.452271231 nanoeth",
"percentile_4": "10 nanoeth",
"percentile_5": "10 nanoeth",
"percentile_10": "18.15 nanoeth",
"percentile_15": "25 nanoeth",
"percentile_20": "28 nanoeth",
"percentile_25": "30 nanoeth",
"percentile_30": "32 nanoeth",
"percentile_35": "37 nanoeth",
"percentile_40": "41 nanoeth",
"percentile_45": "44 nanoeth",
"percentile_50": "47.000001459 nanoeth",
"percentile_55": "50 nanoeth",
"percentile_60": "52.5 nanoeth",
"percentile_65": "55.20000175 nanoeth",
"percentile_70": "56.1 nanoeth",
"percentile_75": "58 nanoeth",
"percentile_80": "60.20000175 nanoeth",
"percentile_85": "63 nanoeth",
"percentile_90": "64 nanoeth",
"percentile_95": "67 nanoeth",
"percentile_96": "67.32 nanoeth",
"percentile_97": "68 nanoeth",
"percentile_98": "70 nanoeth",
"percentile_99": "71 nanoeth",
"percentile_100": "74 nanoeth"
}
def test_probe_datafeed(mocker):
@ -156,6 +191,28 @@ def test_upvest():
assert gas_strategy("web3", "tx") == Web3.toWei(105.2745, 'gwei')
def test_zoltu():
feed = ZoltuGasPriceDatafeed()
assert set(feed._speed_names.values()).issubset(zoltu_json.keys())
# assert feed._default_speed in zoltu_json.keys()
with patch.object(feed, '_probe_feed'):
feed._raw_data = zoltu_json
assert feed.get_gas_price('slow') == Web3.toWei(41, 'gwei')
assert feed.get_gas_price('medium') == Web3.toWei(58, 'gwei')
assert feed.get_gas_price('fast') == Web3.toWei(67, 'gwei')
assert feed.get_gas_price('fastest') == Web3.toWei(70, 'gwei')
assert feed.get_gas_price() == feed.get_gas_price('fast') # Default
parsed_gas_prices = feed.gas_prices
with patch('nucypher.utilities.datafeeds.ZoltuGasPriceDatafeed._parse_gas_prices', autospec=True):
ZoltuGasPriceDatafeed.gas_prices = dict()
with patch.dict(ZoltuGasPriceDatafeed.gas_prices, values=parsed_gas_prices):
gas_strategy = feed.construct_gas_strategy()
assert gas_strategy("web3", "tx") == Web3.toWei(67, 'gwei')
def test_datafeed_fallback_gas_price_strategy():
mocked_gas_price = 0xFABADA