-
Notifications
You must be signed in to change notification settings - Fork 0
/
tron_transaction.py
82 lines (73 loc) · 2.53 KB
/
tron_transaction.py
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
import requests
from config import config
import json
from decimal import Decimal
headers = {"Accept": "application/json", "Content-Type": "application/json", "TRON-PRO-API-KEY": "251db50f-89f1-4200-8878-6285a46c6d14"}
def tronTransaction(from_address, from_address_private, to_address, value, server):
value = int(Decimal(value) * Decimal(1000000))
try:
return broadcastTransaction(transactionSign(transaction(from_address, to_address, value, server), from_address_private, server), server)
except Exception as e:
return {
"status": "fail",
"message": str(e)
}
def transaction(from_address, to_address, value, server):
global headers
try:
url = config('NODE', server) + "/wallet/createtransaction"
payload = {
"to_address": to_address,
"owner_address": from_address,
"visible": True,
"amount": value
}
response = requests.post(url, json=payload, headers=headers)
if 'Error' in response.json():
return {
"status": "fail",
"message": str(response.json()['Error'])
}
return response.text
except Exception as e:
return {
"status": "fail",
"message": str(e)
}
def transactionSign(transaction, from_address_private, server):
global headers
if 'status' in transaction and transaction['status'] == 'fail':
return transaction
try:
url = config('NODE', 'TRX_PUBLIC_NODE') + "/wallet/gettransactionsign"
response = requests.post(url, json={
"transaction": transaction,
"privateKey": from_address_private
}, headers=headers)
if 'Error' in response.json():
return {
"status": "fail",
"message": str(response.json()['Error'])
}
return response.text
except Exception as e:
return {
"status": "fail",
"message": str(e)
}
def broadcastTransaction(payload, server):
global headers
if 'status' in payload and payload['status'] == 'fail':
return payload
try:
url = config('NODE', server) + "/wallet/broadcasttransaction"
response = requests.post(url, json=json.loads(payload), headers=headers).json()
return {
'status': 'success',
'txh': str(response['txid']),
}
except Exception as e:
return {
"status": "fail",
"message": str(e)
}