Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit 645ce9d

Browse files
committed
Builds the Etherscan getLogs url, refs #16
Currently py-etherscan-api doesn't provide support for event logs, see: corpetty/py-etherscan-api#26
1 parent 4648b40 commit 645ce9d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/pyetheroll.py

+15
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,18 @@ def get_last_bets(self, address=None, page=1, offset=100):
434434
}
435435
bets.append(bet)
436436
return bets
437+
438+
def get_logs_url(
439+
self, address, from_block, to_block='latest', topic0=None):
440+
"""
441+
Builds the Etherscan API URL call for the `getLogs` action.
442+
"""
443+
url = self.ChainEtherscanAccount.PREFIX
444+
url += 'module=logs&action=getLogs&'
445+
url += 'apikey={}&'.format(self.etherscan_api_key)
446+
url += 'address={}&'.format(address)
447+
url += 'fromBlock={}&'.format(from_block)
448+
url += 'toBlock={}&'.format(to_block)
449+
if topic0 is not None:
450+
url += 'topic0={}&'.format(topic0)
451+
return url

src/tests/test_pyetheroll.py

+45
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,48 @@ def test_get_last_bets(self):
511511
expected_calls = [expected_call]
512512
self.assertEqual(
513513
m_get_transaction_page.call_args_list, expected_calls)
514+
515+
def test_get_logs_url(self):
516+
with \
517+
mock.patch('etherscan.contracts.Contract.get_abi') \
518+
as m_get_abi, \
519+
mock.patch('pyetheroll.get_etherscan_api_key') \
520+
as m_get_etherscan_api_key:
521+
m_get_abi.return_value = '[]'
522+
m_get_etherscan_api_key.return_value = 'apikey'
523+
etheroll = Etheroll()
524+
address = '0x46044beaa1e985c67767e04de58181de5daaa00f'
525+
from_block = 1
526+
logs_url = etheroll.get_logs_url(
527+
address=address, from_block=from_block)
528+
self.assertEqual(
529+
logs_url,
530+
(
531+
'https://api.etherscan.io/api?'
532+
'module=logs&action=getLogs&apikey=apikey'
533+
'&address=0x46044beaa1e985c67767e04de58181de5daaa00f&'
534+
'fromBlock=1&toBlock=latest&'
535+
)
536+
)
537+
# makes sure Testnet is also supported
538+
with \
539+
mock.patch('etherscan.contracts.Contract.get_abi') \
540+
as m_get_abi, \
541+
mock.patch('pyetheroll.get_etherscan_api_key') \
542+
as m_get_etherscan_api_key:
543+
m_get_abi.return_value = '[]'
544+
m_get_etherscan_api_key.return_value = 'apikey'
545+
etheroll = Etheroll(chain_id=ChainID.ROPSTEN)
546+
address = '0x46044beaa1e985c67767e04de58181de5daaa00f'
547+
from_block = 1
548+
logs_url = etheroll.get_logs_url(
549+
address=address, from_block=from_block)
550+
self.assertEqual(
551+
logs_url,
552+
(
553+
'https://api-ropsten.etherscan.io/api?'
554+
'module=logs&action=getLogs&apikey=apikey&'
555+
'address=0x46044beaa1e985c67767e04de58181de5daaa00f&'
556+
'fromBlock=1&toBlock=latest&'
557+
)
558+
)

0 commit comments

Comments
 (0)