This repository was archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAPI.py
More file actions
83 lines (70 loc) · 3.37 KB
/
API.py
File metadata and controls
83 lines (70 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
from APIResponse import APIResponse
from Errors import *
import json
import binascii
class API:
def __init__(self):
self.api_endpoint = "http://localhost:8545/"
def http_request(self, method, params):
data = {"jsonrpc": "2.0",
"method": method,
"params": params,
"id": 1543} # TODO make nonce
r = requests.post(self.api_endpoint, json=data)
if r.status_code == requests.codes.ok:
return r.text
else:
raise BadResponseError(r.status_code)
def api_request(self, method, params):
try:
response = self.http_request(method, params)
json_response = json.loads(response)
if json_response.get('error', False):
return APIResponse({}, json_response['error'].get('code', '-1'),
json_response['error'].get('message', 'Internal error'))
return APIResponse(json_response, 0)
except BadResponseError:
return APIResponse({}, 3, "Internal error")
def getGasPrice(self):
response = self.api_request("eth_gasPrice", [])
response.response_dict["result"] = int(response.response_dict["result"], 16)
return response
def getAccountByAddress(self, address):
response = self.api_request("eth_getBalance", [address, "latest"])
ans = APIResponse({"address": address,
"balance": int(response.response_dict["result"], 16)})
return ans
def getLatestBlock(self):
response = self.api_request("eth_getBlockByNumber", ["latest", False])
ans = APIResponse({"number": int(response.response_dict["result"]["number"], 16),
"hash": response.response_dict["result"]["hash"]})
return ans
def getSHA256(self, string):
b = bytearray()
b.extend(map(ord, string))
bytes_string = str(binascii.b2a_hex(b), 'ascii')
byte_string = "0x" + bytes_string
response = self.api_request("web3_sha3", [byte_string])
return response.response_dict["result"]
def getStorageAt(self, data, quantity, block="latest"):
response = self.api_request("eth_getStorageAt", [data, quantity, block])
return response
def getInfo(self, from_address, to_address, data):
response = self.api_request("eth_call", [{"from": from_address,
"to": to_address,
"data": data}, "latest"])
return APIResponse({"data": response.response_dict["result"]})
#tested on samples
def getMethodId(self, definition):
hash = self.getSHA256(definition)
return hash[2:10]
def send_transaction(self, from_address, to_address, gas, gasPrice, value, data):
response = self.api_request("eth_sendTransaction", [{"from": from_address,
"to": to_address,
"gas": gas,
"gasPrice": gasPrice,
"value": value,
"data": data}])
ans = APIResponse({"hash": response.response_dict["result"]})
return ans