Skip to content

Commit b15381b

Browse files
committed
Proxies module additions.
1 parent 0c45767 commit b15381b

File tree

10 files changed

+141
-12
lines changed

10 files changed

+141
-12
lines changed

etherscan/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Client(object):
4848
TO = '&to='
4949
VALUE = '&value='
5050
DATA = '&data='
51-
POSITION = '&='
51+
POSITION = '&position='
5252
HEX = '&hex='
5353
GAS_PRICE = '&gasPrice='
5454
GAS = '&gas='

etherscan/client.ropsten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Client(object):
4747
TO = '&to='
4848
VALUE = '&value='
4949
DATA = '&data='
50-
POSITION = '&='
50+
POSITION = '&position='
5151
HEX = '&hex='
5252
GAS_PRICE = '&gasPrice='
5353
GAS = '&gas='

etherscan/proxies.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,27 @@ def get_transaction_receipt(self, tx_hash: str):
7474
self.build_url()
7575
req = self.connect()
7676
return req['result']
77+
78+
def get_code(self, address: str):
79+
self.url_dict[self.ACTION] = 'eth_getCode'
80+
self.url_dict[self.ADDRESS] = address
81+
self.url_dict[self.TAG] = 'latest'
82+
self.build_url()
83+
req = self.connect()
84+
return req['result']
85+
86+
def get_storage_at(self, address: str, position: Union[str, int]):
87+
self.url_dict[self.ACTION] = 'eth_getStorageAt'
88+
self.url_dict[self.ADDRESS] = address
89+
self.url_dict[self.POSITION] = position if type(
90+
position) is str else hex(position)
91+
self.url_dict[self.TAG] = 'latest'
92+
self.build_url()
93+
req = self.connect()
94+
return req['result']
95+
96+
def gas_price(self):
97+
self.url_dict[self.ACTION] = 'eth_gasPrice'
98+
self.build_url()
99+
req = self.connect()
100+
return req['result']

examples/proxies/gas_price.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from etherscan.proxies import Proxies
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+
api = Proxies(api_key=key)
8+
price = api.gas_price()
9+
print(price)

examples/proxies/get_code.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from etherscan.proxies import Proxies
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+
api = Proxies(api_key=key)
8+
code = api.get_code('0xf75e354c5edc8efed9b59ee9f67a80845ade7d0c')
9+
print(code)

examples/proxies/get_storage_at.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from etherscan.proxies import Proxies
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+
api = Proxies(api_key=key)
8+
value = api.get_storage_at('0x6e03d9cce9d60f3e9f2597e13cd4c54c55330cfd', 0x0)
9+
print(value)

tests/test_accounts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
1010
]
1111
MULTI_BALANCE = [
12-
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
12+
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
1313
'balance': '40807178566070000000000'},
14-
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
15-
'balance': '40807178566070000000000'}
14+
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
15+
'balance': '40807178566070000000000'}
1616
]
1717
API_KEY = 'YourAPIkey'
1818

tests/test_blocks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ def test_get_block_reward(self):
1414
api = Blocks(api_key=(API_KEY))
1515
reward_object = api.get_block_reward(BLOCK)
1616
self.assertEqual(reward_object['blockReward'], BLOCK_REWARD)
17-
self.assertEqual(reward_object['uncleInclusionReward'], UNCLE_INCLUSION_REWARD)
17+
self.assertEqual(reward_object['uncleInclusionReward'],
18+
UNCLE_INCLUSION_REWARD)

tests/test_proxies.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,91 @@
44
from etherscan.proxies import Proxies
55

66
API_KEY = 'YourAPIkey'
7+
BLOCK_NUMBER = 2165403
8+
BLOCK_DIFFICULTY = 67858873048710
9+
UNCLE_INDEX = 0
10+
UNCLE_DIFFICULTY = 67858872000134
11+
TX_COUNT = 4
12+
TX_HASH = "0xed57e2434ddab54526620cbb4dcdaa0c6965027e2cb8556ef4750ed1eafa48c2"
13+
TX_INDEX = 0
14+
TX_ADDRESS = "0x2910543af39aba0cd09dbb2d50200b3e800a63d2"
15+
STORAGE_ADDRESS = "0x6e03d9cce9d60f3e9f2597e13cd4c54c55330cfd"
16+
STORAGE_POS = 0
17+
STORAGE_CONTENTS = "0x0000000000000000000000003d0768d" \
18+
"a09ce77d25e2d998e6a7b6ed4b9116c2d"
19+
CODE_ADDRESS = "0xf75e354c5edc8efed9b59ee9f67a80845ade7d0c"
20+
CODE_CONTENTS = "0x3660008037602060003660003473273930d21e01ee25e4c219b6" \
21+
"3259d214872220a261235a5a03f21560015760206000f3"
722

823

924
class ProxiesTestCase(unittest.TestCase):
1025

1126
def test_get_most_recent_block(self):
1227
api = Proxies(api_key=API_KEY)
13-
# currently raises an exception even though it should not, see:
14-
# https://github.com/corpetty/py-etherscan-api/issues/32
1528
most_recent = int(api.get_most_recent_block(), 16)
1629
print(most_recent)
1730
p = re.compile('^[0-9]{7}$')
1831
self.assertTrue(p.match(str(most_recent)))
32+
33+
def test_get_block_by_number(self):
34+
api = Proxies(api_key=API_KEY)
35+
block = api.get_block_by_number(BLOCK_NUMBER)
36+
print(block)
37+
self.assertEqual(block['difficulty'], hex(BLOCK_DIFFICULTY))
38+
39+
def test_get_uncle_by_blocknumber_index(self):
40+
api = Proxies(api_key=API_KEY)
41+
uncle = api.get_uncle_by_blocknumber_index(BLOCK_NUMBER, UNCLE_INDEX)
42+
print(uncle)
43+
self.assertEqual(uncle['difficulty'], hex(UNCLE_DIFFICULTY))
44+
45+
def test_get_block_transaction_count_by_number(self):
46+
api = Proxies(api_key=API_KEY)
47+
tx_count = api.get_block_transaction_count_by_number(BLOCK_NUMBER)
48+
print(tx_count)
49+
self.assertEqual(tx_count, hex(TX_COUNT))
50+
51+
def test_get_transaction_by_hash(self):
52+
api = Proxies(api_key=API_KEY)
53+
tx = api.get_transaction_by_hash(TX_HASH)
54+
print(tx)
55+
self.assertEqual(tx['blockNumber'], hex(BLOCK_NUMBER))
56+
57+
def test_get_transaction_by_blocknumber_index(self):
58+
api = Proxies(api_key=API_KEY)
59+
tx = api.get_transaction_by_blocknumber_index(BLOCK_NUMBER,
60+
TX_INDEX)
61+
print(tx)
62+
self.assertEqual(tx['hash'], TX_HASH)
63+
64+
def test_get_transaction_count(self):
65+
api = Proxies(api_key=API_KEY)
66+
tx_count = int(api.get_transaction_count(TX_ADDRESS), 16)
67+
print(tx_count)
68+
p = re.compile('^[0-9]*$')
69+
self.assertTrue(p.match(str(tx_count)))
70+
71+
def test_get_transaction_receipt(self):
72+
api = Proxies(api_key=API_KEY)
73+
tx_receipt = api.get_transaction_receipt(TX_HASH)
74+
print(tx_receipt)
75+
self.assertEqual(tx_receipt['blockNumber'], hex(BLOCK_NUMBER))
76+
77+
def test_get_code(self):
78+
api = Proxies(api_key=API_KEY)
79+
code_contents = api.get_code(CODE_ADDRESS)
80+
print(code_contents)
81+
self.assertEqual(code_contents, CODE_CONTENTS)
82+
83+
def test_get_storage_at(self):
84+
api = Proxies(api_key=API_KEY)
85+
storage_contents = api.get_storage_at(STORAGE_ADDRESS, STORAGE_POS)
86+
print(storage_contents)
87+
self.assertEqual(storage_contents, STORAGE_CONTENTS)
88+
89+
def test_gas_price(self):
90+
api = Proxies(api_key=API_KEY)
91+
price = int(api.gas_price(), 16)
92+
print(price)
93+
p = re.compile('^[0-9]*$')
94+
self.assertTrue(p.match(str(price)))

tests/test_transactions.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
from etherscan.transactions import Transactions
44

55
API_KEY = 'YourAPIkey'
6-
TX_HASH_1 = '0x15f8e5ea1079d9a0bb04a4c58ae5fe7654b5b2b4463375ff7ffb490aa0032f3a'
7-
TX_HASH_2 = '0x513c1ba0bebf66436b5fed86ab668452b7805593c05073eb2d51d3a52f480a76'
6+
TX_1 = '0x15f8e5ea1079d9a0bb04a4c58ae5fe7654b5b2b4463375ff7ffb490aa0032f3a'
7+
TX_2 = '0x513c1ba0bebf66436b5fed86ab668452b7805593c05073eb2d51d3a52f480a76'
88
ERROR_STRING = 'Bad jump destination'
99

10+
1011
class TransactionsTestCase(unittest.TestCase):
1112

1213
def test_get_status(self):
1314
api = Transactions(api_key=(API_KEY))
14-
status = api.get_status(TX_HASH_1)
15+
status = api.get_status(TX_1)
1516
self.assertEqual(status['isError'], '1')
1617
self.assertEqual(status['errDescription'], ERROR_STRING)
1718

1819
def test_get_tx_receipt_status(self):
1920
api = Transactions(api_key=(API_KEY))
20-
receipt_status = api.get_tx_receipt_status(TX_HASH_2)
21+
receipt_status = api.get_tx_receipt_status(TX_2)
2122
self.assertEqual(receipt_status['status'], '1')

0 commit comments

Comments
 (0)