From 104667ec98ac9dc376e630899da7118cfd21d496 Mon Sep 17 00:00:00 2001 From: Eugene Yurtaev Date: Tue, 29 Mar 2016 18:08:00 +0300 Subject: [PATCH] initial commit --- API.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ server.py | 1 + 2 files changed, 50 insertions(+) create mode 100644 API.py create mode 100644 server.py diff --git a/API.py b/API.py new file mode 100644 index 0000000..da1cb86 --- /dev/null +++ b/API.py @@ -0,0 +1,49 @@ +import requests +from APIResponse import APIResponse +from Errors import * +import json + +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 str(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 str(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 str(ans) diff --git a/server.py b/server.py new file mode 100644 index 0000000..74d1b74 --- /dev/null +++ b/server.py @@ -0,0 +1 @@ +Flask goes here