-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTether.py
More file actions
executable file
·156 lines (142 loc) · 7.73 KB
/
Tether.py
File metadata and controls
executable file
·156 lines (142 loc) · 7.73 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import json
from tronpy.tron import Contract
from Tron import Tron
from Ethereum import Ethereum
from data.contracts import usdt_trc20_bytecode
from data.contracts import usdt_trc20_address
from data.contracts import usdt_trc20_abi
from tronpy.tron import PrivateKey
class Tether:
tether_contract = None
usdt_erc20_address = "0xdAC17F958D2ee523a2206206994597C13D831ec7"
blockchain_instance = None
def __init__(self, ):
pass
def create_account(self, private_key: str = None, parent_for_tron=None):
if isinstance(self.blockchain_instance, Ethereum):
if private_key is None:
self.blockchain_instance.create_account()
else:
self.blockchain_instance.set_current_account(private_key)
self.tether_contract = \
self.blockchain_instance.web3_instance.eth.contract(address=self.blockchain_instance.
get_current_account_address(), abi=self.abi)
elif isinstance(self.blockchain_instance, Tron):
if private_key is None and self.blockchain_instance.account is not None:
private_key = self.blockchain_instance.get_prik_of_current()
elif private_key is not None:
self.blockchain_instance.set_current_account(private_key)
else:
return {"error": "At least one private key must be set into tron or into tether... use create-account..."}
try:
self.tether_contract = self.blockchain_instance.tron_instance.get_contract("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t")
trn = (self.blockchain_instance.tron_instance.trx
.deploy_contract(self.blockchain_instance.get_current_account_address(), self.tether_contract)
.fee_limit(5_000_000)
.build()
.sign(PrivateKey(bytes.fromhex(private_key)))
)
print(f"Transaction is ready data {trn}")
print("Creating account, deploying now...")
res = trn.broadcast()
print("Already broadcasted awaiting now...")
res = res.wait()
except Exception as ex:
return {"error":
f"Cannot successfully deploy the contract into address {self.blockchain_instance.get_current_account_address()}\
are you sure the address owns enough tron for deployment and you are on mainnet?" }
return res
def transfer(self, to, amount):
if isinstance(self.blockchain_instance, Tron):
transaction = (
Contract(self.tether_contract).functions.transfer(to, amount).with_owner(
self.blockchain_instance.get_current_account_address()).fee_limit(5_000_000).build()
.sign(self.blockchain_instance.get_prik_of_current())
)
return transaction.broadcast().wait()
elif isinstance(self.blockchain_instance, Ethereum):
self.tether_contract.functions.transfer(to, amount)
return {"development": "in development yet"}
def set_current_account(self, prik):
if isinstance(self.blockchain_instance, Tron):
return self.blockchain_instance.set_current_account(prik)
elif isinstance(self.blockchain_instance, Ethereum):
return self.blockchain_instance.set_current_account(prik)
else:
return None
def get_current_account_address(self):
if isinstance(self.blockchain_instance, Tron):
return self.blockchain_instance.get_current_account_address()
elif isinstance(self.blockchain_instance, Ethereum):
return self.blockchain_instance.get_current_account_address()
else:
return None
def get_balance_of(self, target=None):
if isinstance(self.blockchain_instance, Tron):
from data.contracts import usdt_trc20_address
functions = self.blockchain_instance.tron_instance.get_contract(usdt_trc20_address).functions
if target is None:
target = self.get_current_account_address()
return functions.balanceOf(target)/(10**functions.decimals())
elif isinstance(self.blockchain_instance, Ethereum):
return self.tether_contract.functions.get_balance()
else:
return None
def get_prik_of_current(self):
if isinstance(self.blockchain_instance, Tron):
return self.blockchain_instance.get_prik_of_current()
elif isinstance(self.blockchain_instance, Ethereum):
return self.blockchain_instance.get_current_account_pri_key()
else:
return None
def set_provider(self, url, for_bc=None,api_key=None):
if self.blockchain_instance is None or for_bc is not None:
self.blockchain_instance = for_bc
else:
raise ValueError("Please call set-token-type first and after the error, call this, or \
use the \"for\" parameter with tron or ethereum...")
if isinstance(self.blockchain_instance, Tron):
return self.blockchain_instance.set_provider(api_key=api_key,endpoint_uri=url)
elif isinstance(self.blockchain_instance, Ethereum):
return self.blockchain_instance.set_provider(url)
else:
raise ValueError("Not valid blockchain given...")
def get_transaction_status(self, txid):
if isinstance(self.blockchain_instance, Tron):
return self.blockchain_instance.get_transaction_status(txid)
elif isinstance(self.blockchain_instance, Ethereum):
return self.blockchain_instance.get_transaction_status(txid)
else:
return None
def set_token_type(self, token_type, ether_instance=None, tron_instance: Tron=None):
#client = Tron(network="nile")
if ether_instance is None and tron_instance is None:
raise Exception("Not valid parameters at least one blockchain must be given...")
if token_type == "usdt-erc20":
if self.blockchain_instance is None or not isinstance(self.blockchain_instance, Ethereum):
self.blockchain_instance = ether_instance
self.tether_contract = self.blockchain_instance \
.get_smart_contract(address=self.address, abi=self.abi, bytecode=self.bytecode)
elif token_type == "usdt-trc20":
if self.blockchain_instance is None or not isinstance(self.blockchain_instance, Tron):
self.blockchain_instance = tron_instance
self.tether_contract = Contract(abi=usdt_trc20_abi, bytecode=usdt_trc20_bytecode,)
if self.blockchain_instance.get_current_account_address() is None:
return {"result": "Cannot deploy contract without an account, please first use create-account or set-account.."}
txn = (self.blockchain_instance.tron_instance.trx.deploy_contract\
(self.blockchain_instance.get_current_account_address(), self.tether_contract)
.fee_limit(5_000_000)
.build()
.sign(PrivateKey(bytes.fromhex(self.blockchain_instance.\
account["private_key"]))))
try:
print("Transaction created for deployment, loading now...")
print("Successfully loaded contract at address: {} ".format(txn.broadcast().wait()["contract_address"]))
except Exception as ex:
print(ex)
return {"result": "Cannot load or create the contract successfully..."}
else:
return {"result": "Cannot recognize {} as a valid tether token type".format(token_type)}
return {"result": "Successfully working with {}".format(token_type)}
def get_fee(self):
pass