-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the test case bugs for now, not the robust way.
Add test set up to ignore the annoying requests test warning. Add logs modules and test case. Add module gas tracker and test case. Add api key error. Modified README file. Modified setup.py file version.
- Loading branch information
1 parent
afa8129
commit f31036b
Showing
12 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from .client import Client | ||
|
||
|
||
class GasTrackerException(Exception): | ||
"""Base class for exceptions in this module.""" | ||
pass | ||
|
||
|
||
class GasTracker(Client): | ||
def __init__(self, api_key='YourApiKeyToken'): | ||
Client.__init__(self, address='', api_key=api_key) | ||
self.url_dict[self.MODULE] = 'gastracker' | ||
|
||
def get_estimation_of_confirmation_time(self, gas_price: str) -> str: | ||
""" | ||
Returns the estimated time, in seconds, for a transaction to be confirmed on the blockchain. | ||
Args: | ||
gas_price (str): the price paid per unit of gas, in wei | ||
Returns: | ||
str: The result is returned in seconds. | ||
""" | ||
self.url_dict[self.ACTION] = 'gasestimate' | ||
self.url_dict[self.GAS_PRICE] = gas_price | ||
self.build_url() | ||
req = self.connect() | ||
return req['result'] | ||
|
||
def get_gas_oracle(self) -> dict: | ||
""" | ||
Returns the current Safe, Proposed and Fast gas prices. | ||
Returns: | ||
dict: The gas prices are returned in Gwei. | ||
""" | ||
self.url_dict[self.ACTION] = 'gasoracle' | ||
self.build_url() | ||
req = self.connect() | ||
return req['result'] | ||
|
||
def get_daily_average_gas_limit(self, start_date, end_date) -> list: | ||
# TODO API Pro | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from .client import Client | ||
|
||
|
||
class LogsException(Exception): | ||
"""Base class for exceptions in this module.""" | ||
pass | ||
|
||
|
||
class Logs(Client): | ||
""" | ||
The Event Log API was designed to provide an alternative to the native eth_getLogs. | ||
""" | ||
def __init__(self, api_key='YourApiKeyToken'): | ||
Client.__init__(self, address='', api_key=api_key) | ||
self.url_dict[self.MODULE] = 'logs' | ||
|
||
def get_logs(self, from_block: str, to_block='latest', | ||
topic0='', topic1='', topic0_1_opr='and',) -> list: | ||
""" | ||
Get Event Logs from block number [from_block] to block [to_block] , | ||
where log address = [address], topic[0] = [topic0] 'AND' topic[1] = [topic1] | ||
Args: | ||
from_block (str): start block number | ||
to_block (str, optional): end block number. Defaults to 'latest'. | ||
topic0 (str, optional): Defaults to ''. | ||
topic1 (str, optional): Defaults to ''. | ||
topic0_1_opr (str, optional): and|or between topic0 & topic1. Defaults to 'and'. | ||
Returns: | ||
list: [description] | ||
""" | ||
# TODO: support multi topics | ||
if not topic0 and topic1: | ||
raise(LogsException('can not only set topic1 while topic0 is empty')) | ||
self.url_dict[self.ACTION] = 'getLogs' | ||
self.url_dict[self.FROM_BLOCK] = from_block if type( | ||
from_block) is str else str(from_block) | ||
self.url_dict[self.TO_BLOCK] = to_block if type( | ||
to_block) is str else str(to_block) | ||
self.url_dict[self.TOPIC0] = topic0 if type( | ||
topic0) is str else hex(topic0) | ||
self.url_dict[self.TOPIC1] = topic1 if type( | ||
topic1) is str else hex(topic1) | ||
self.url_dict[self.TOPIC0_1_OPR] = topic0_1_opr | ||
self.build_url() | ||
req = self.connect() | ||
return req['result'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import unittest | ||
import warnings | ||
|
||
from etherscan.gas_tracker import GasTracker | ||
|
||
GAS_PRICE = '2000000000' | ||
PRICE_ORACLE_RESULT_DICT_KEYS = ("SafeGasPrice", | ||
"ProposeGasPrice", | ||
"FastGasPrice", | ||
"suggestBaseFee") | ||
API_KEY = 'YourAPIkey' | ||
|
||
|
||
class BlocksTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
warnings.simplefilter('ignore', ResourceWarning) | ||
self.api = GasTracker(api_key=API_KEY) | ||
|
||
def test_get_estimation_of_confirmation_time(self): | ||
estimated_time = self.api.get_estimation_of_confirmation_time(GAS_PRICE) | ||
self.assertTrue(int(estimated_time) > 0) | ||
|
||
def test_get_gas_oracle(self): | ||
oracle_price = self.api.get_gas_oracle() | ||
for key in PRICE_ORACLE_RESULT_DICT_KEYS: | ||
self.assertTrue(key in oracle_price and float(oracle_price[key]) > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import unittest | ||
import warnings | ||
|
||
from etherscan.logs import Logs, LogsException | ||
from etherscan.client import InvalidAPIKey | ||
|
||
FROM_BLOCK = 379224 | ||
TO_BLOCK = 400000 | ||
ADDRESS = '0x33990122638b9132ca29c723bdf037f1a891a70c' | ||
This comment has been minimized.
Sorry, something went wrong. |
||
TOPIC0 = '0xf63780e752c6a54a94fc52715dbc5518a3b4c3c2833d301a204226548a2a8545' | ||
TOPIC1 = '0x72657075746174696f6e00000000000000000000000000000000000000000000' | ||
TOPIC0_1_OPR = 'and' | ||
API_KEY = 'YourAPIkey' | ||
|
||
|
||
class BlocksTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
warnings.simplefilter('ignore', ResourceWarning) | ||
self.api = Logs(api_key=(API_KEY)) | ||
|
||
def test_invalid_api_key(self): | ||
with self.assertRaises(InvalidAPIKey): | ||
api = Logs(api_key=('invalid' + API_KEY)) | ||
api.get_logs(from_block=FROM_BLOCK, topic0=TOPIC0) | ||
|
||
def test_get_logs_error(self): | ||
with self.assertRaises(LogsException): | ||
self.api.get_logs(from_block=FROM_BLOCK, topic1=TOPIC1) | ||
|
||
def test_get_logs_one_topic(self): | ||
topics = self.api.get_logs(from_block=FROM_BLOCK, topic0=TOPIC0) | ||
for topic in topics: | ||
self.assertTrue(TOPIC0 in topic.get('topics', '')) | ||
|
||
def test_get_logs_two_topics(self): | ||
topics = self.api.get_logs(from_block=FROM_BLOCK, topic0=TOPIC0, topic1=TOPIC1) | ||
for topic in topics: | ||
self.assertTrue(TOPIC0 in topic.get('topics', '') | ||
and TOPIC1 in topic.get('topics', '')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0x1b2445C6fb59cC883fe0bdDa67337fd347f95DC8