Skip to content

Commit 986d326

Browse files
committed
remove base
1 parent 36c2683 commit 986d326

File tree

2 files changed

+133
-139
lines changed

2 files changed

+133
-139
lines changed

tronapi/base.py

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

tronapi/tron.py

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,102 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import binascii
1718
from _sha256 import sha256
1819
import base58
1920
import math
2021

2122
from Crypto.Hash import keccak
22-
23-
from tronapi.base import BaseTron
23+
from tronapi.constants import DEFAULT_SOLIDITY_NODE, DEFAULT_TRON_NODE
2424
from tronapi.exceptions import InvalidTronError
2525
from tronapi import utils
26+
from tronapi.provider import HttpProvider
27+
28+
29+
class Tron(object):
30+
31+
def __init__(self, full_node, solidity_node=None, event_server=None, private_key=None):
32+
"""A Python API for interacting with the Tron (TRX)
33+
34+
Args:
35+
full_node (:obj:`str`): A provider connected to a valid full node
36+
solidity_node (:obj:`str`): A provider connected to a valid solidity node
37+
event_server (:obj:`str`, optional): Optional for smart contract events. Expects a valid event server URL
38+
private_key (str): Optional default private key used when signing transactions
39+
40+
"""
41+
42+
if not solidity_node:
43+
solidity_node = DEFAULT_SOLIDITY_NODE
44+
45+
if isinstance(full_node, str):
46+
full_node = HttpProvider(full_node)
47+
48+
if isinstance(solidity_node, str):
49+
solidity_node = HttpProvider(solidity_node)
50+
51+
if isinstance(event_server, str):
52+
event_server = HttpProvider(event_server)
53+
54+
self.__set_full_node(full_node)
55+
self.__set_solidity_node(solidity_node)
56+
self.__set_event_server(event_server)
57+
58+
self.tron_node = HttpProvider(DEFAULT_TRON_NODE)
59+
60+
if private_key:
61+
self.private_key = private_key
62+
63+
def __set_full_node(self, provider):
64+
"""Check specified "full node"
65+
66+
Args:
67+
provider (HttpProvider): full node
68+
69+
"""
70+
if not self.is_valid_provider(provider):
71+
raise Exception('Invalid full node provided')
72+
73+
self.full_node = provider
74+
self.full_node.status_page = '/wallet/getnowblock'
75+
76+
def __set_solidity_node(self, provider):
77+
"""Check specified "solidity node"
78+
79+
Args:
80+
provider (HttpProvider): solidity node
81+
82+
"""
83+
if not self.is_valid_provider(provider):
84+
raise Exception('Invalid solidity node provided')
85+
86+
self.solidity_node = provider
87+
self.solidity_node.status_page = '/walletsolidity/getnowblock'
88+
89+
def __set_event_server(self, server):
90+
"""Check specified "event server"
91+
92+
Args:
93+
server (HttpProvider): event server
94+
95+
"""
96+
if server and not self.is_valid_provider(server):
97+
raise Exception('Invalid event provided')
98+
99+
self.event_server = server
100+
101+
def is_event_connected(self):
102+
"""
103+
Checks if is connected to the event server.
26104
105+
Returns:
106+
bool: True if successful, False otherwise.
107+
108+
"""
109+
if not self.event_server:
110+
return False
27111

28-
class Tron(BaseTron):
112+
return self.event_server.request('/healthcheck') == 'OK'
29113

30114
def get_event_result(self, contract_address=None, since=0, event_name=None, block_number=None):
31115
"""Will return all events matching the filters.
@@ -1005,3 +1089,49 @@ def sha3(string, prefix=False):
10051089
return '0x' + keccak_hash.hexdigest()
10061090

10071091
return keccak_hash.hexdigest()
1092+
1093+
@staticmethod
1094+
def is_valid_provider(provider):
1095+
"""Check connected provider
1096+
1097+
Args:
1098+
provider(HttpProvider): Provider
1099+
1100+
Returns:
1101+
True if successful, False otherwise.
1102+
1103+
"""
1104+
return isinstance(provider, HttpProvider)
1105+
1106+
@staticmethod
1107+
def to_ascii(s):
1108+
return binascii.a2b_hex(s)
1109+
1110+
@staticmethod
1111+
def from_ascii(string):
1112+
return binascii.b2a_hex(bytes(string, encoding="utf8"))
1113+
1114+
@staticmethod
1115+
def to_utf8(hex_string):
1116+
return binascii.unhexlify(hex_string).decode('utf8')
1117+
1118+
@staticmethod
1119+
def from_utf8(string):
1120+
return binascii.hexlify(bytes(string, encoding="utf8"))
1121+
1122+
@staticmethod
1123+
def from_decimal(value):
1124+
return hex(value)
1125+
1126+
@staticmethod
1127+
def to_decimal(value):
1128+
return int((str(value)), 10)
1129+
1130+
def is_connected(self):
1131+
"""Check all connected nodes"""
1132+
1133+
return {
1134+
'full_node': self.full_node.is_connected(),
1135+
'solidity_node': self.solidity_node.is_connected(),
1136+
'event_server': self.is_event_connected()
1137+
}

0 commit comments

Comments
 (0)