Skip to content

Commit a483e32

Browse files
committed
added erc20 and sourcecode functionality, added some tests and examples
1 parent cf147e6 commit a483e32

File tree

8 files changed

+475
-39
lines changed

8 files changed

+475
-39
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/usr/bin/python3.6"
3+
}

changelog.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

etherscan/accounts.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_balance_multiple(self):
2525
return req['result']
2626

2727
def get_transaction_page(self, page=1, offset=10000, sort='asc',
28-
internal=False) -> list:
28+
internal=False, erc20=False) -> list:
2929
"""
3030
Get a page of transactions, each transaction
3131
returns list of dict with keys:
@@ -52,12 +52,20 @@ def get_transaction_page(self, page=1, offset=10000, sort='asc',
5252
'asc' -> ascending order
5353
'desc' -> descending order
5454
55-
internal options:
56-
True -> Gets the internal transactions of a smart contract
55+
internal options: (currently marked at Beta for etherscan.io)
56+
True -> Gets the internal transactions of the address
57+
False -> (default) get normal external transactions
58+
59+
erc20 options: (currently marked at Beta for etherscan.io)
60+
True -> Gets the erc20 token transcations of the address
5761
False -> (default) get normal external transactions
62+
63+
NOTE: not sure if this works for contract addresses, requires testing
5864
"""
5965
if internal:
6066
self.url_dict[self.ACTION] = 'txlistinternal'
67+
elif erc20:
68+
self.url_dict[self.ACTION] = 'tokentx'
6169
else:
6270
self.url_dict[self.ACTION] = 'txlist'
6371
self.url_dict[self.PAGE] = str(page)

etherscan/contracts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ def get_abi(self):
1111
self.build_url()
1212
req = self.connect()
1313
return req['result']
14+
15+
def get_sourcecode(self):
16+
self.url_dict[self.ACTION] = 'getsourcecode'
17+
self.build_url()
18+
req = self.connect()
19+
return req['result']

examples/accounts/Accounts Examples Notebook.ipynb

Lines changed: 407 additions & 25 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from etherscan.accounts import Account
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
address = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
8+
9+
api = Account(address=address, api_key=key)
10+
transactions = api.get_transaction_page(page=1, offset=10000, sort='des', erc20=True)
11+
print(transactions)

examples/contracts/get_sourcecode.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from etherscan.contracts import Contract
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
address = '0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359'
8+
9+
api = Contract(address=address, api_key=key)
10+
sourcecode = api.get_sourcecode()
11+
# TODO: make this return something pretty
12+
print(sourcecode[0]['SourceCode'])

tests/test_accounts.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
3+
from etherscan.accounts import Account
4+
5+
SINGLE_BALANCE = '40807168566070000000000'
6+
SINGLE_ACCOUNT = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
7+
MULTI_ACCOUNT = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
8+
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
9+
MULTI_BALANCE = [
10+
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
11+
'balance': '40807168566070000000000'},
12+
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
13+
'balance': '40807168566070000000000'}
14+
]
15+
API_KEY = 'YourAPIkey'
16+
17+
class AccountsTestCase(unittest.TestCase):
18+
19+
def test_get_balance(self):
20+
api = Account(address=SINGLE_ACCOUNT, api_key=API_KEY)
21+
self.assertEqual(api.get_balance(), SINGLE_BALANCE)
22+
23+
def test_get_balance_multi(self):
24+
api = Account(address=MULTI_ACCOUNT, api_key=API_KEY)
25+
self.assertEqual(api.get_balance_multiple(), MULTI_BALANCE)

0 commit comments

Comments
 (0)