-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
173 lines (148 loc) · 5.86 KB
/
run.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys
import re
import os
import codecs # UniCode support
from pymongo import MongoClient
from math import ceil
import requests
from time import sleep
import logging
import argparse
from datetime import datetime
from pprint import pprint
import getpass
import terminal_banner
from colorama import init
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint
from pyfiglet import figlet_format
# start loggin
logging.basicConfig(format='%(asctime)s %(message)s',level=logging.ERROR)
# MongoDB connection
client = MongoClient()
print("Connected to MongoDB successfully!")
# Database
db = client['tron-airdrop']
API_URL = os.environ.get('API_URL')
if API_URL is None:
API_URL = 'https://api.tronscan.org'
parser = argparse.ArgumentParser()
parser.add_argument('--address',
dest='address', help='Adress from')
parser.add_argument('--token',
dest='token', help='Token to send')
parser.add_argument('--amount',
dest='amount', help='Amount To Token')
cprint(figlet_format(' TRON ', font='starwars'),
'white', 'on_red', attrs=['bold'])
cprint(figlet_format('AIRDROP', font='starwars'),
'white', 'on_red', attrs=['bold'])
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█'):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
# Print New Line on Complete
if iteration == total:
print()
if __name__ == "__main__":
args = parser.parse_args()
if (args.address is None or args.token is None):
logging.error('Please try: python run.py --address=ADDRESS --token=TOKENNAME')
logging.error('App closed...')
sys.exit(0)
if (args.amount is None):
args.amount=1
else:
args.amount = int(args.amount)
# get list of witness
resultWitnesses = requests.get(API_URL+"/api/witness").json()
# ToSend DB
#db['tosend'].remove()
tosend = db['tosend']
# Get voters and send
for W in resultWitnesses:
start = 0
limit = 100
print('Get voters from: '+W['address'])
voters = requests.get(API_URL+"/api/vote", params={'limit': 0, 'candidate': W['address']}).json()
totalVotes = voters['total']
pages = ceil(totalVotes/limit)
for pos in range(0, pages):
voters = requests.get(API_URL+"/api/vote", params={'limit': limit, 'start': pos*limit, 'candidate': W['address']}).json()
for item in voters["data"]:
tosend.insert_one(item)
sleep(1)
# get account info
resultBalance = requests.get(API_URL+"/api/account/"+args.address).json()
bw = resultBalance['bandwidth']['freeNetRemaining']+resultBalance['bandwidth']['netRemaining']
for b in resultBalance['balances']:
if b['name']==args.token:
balance = b['balance']
break
if balance==0:
logging.error('No balance found!')
logging.error('App closed...')
sys.exit(0)
# total sending
sendto = tosend.find().distinct("voterAddress")
totalSending = len(sendto)*args.amount
info_text = "Your have {:,.0f} {},\nand is sending {:,.0f} {}.\nYour BW is {:,.0f},\nAnd it is estimated {:,.0f}BW".format(
balance,args.token,totalSending,args.token,bw,len(sendto)*200)
print(terminal_banner.Banner(info_text))
if totalSending>balance:
print('Only the first {} will receive.'.format(ceil(balance/args.amount)))
wd = input("Do you confirm this operation? (Y/n)")
if wd!= "y":
logging.error('User did not confirm!')
logging.error('App closed...')
sys.exit(0)
PK=getpass.getpass(prompt='Please enter your private key:')
# get
sentto = db['sentto']
l = len(sendto)
if totalSending>balance:
l = ceil(balance/args.amount)
printProgressBar(0, l, prefix = 'Sending...:', suffix = 'Complete', length = 50)
i=1
cerror=0
for s in sendto:
data = {"broadcast": True, "key": PK, "contract": {"amount": args.amount,
"ownerAddress": args.address, "toAddress": s, "assetName": args.token}
}
try:
result = requests.post(API_URL+"/api/transaction-builder/contract/transferasset", json=data).json()
if (result["result"]["code"]=="SUCCESS"):
record = {
"from": args.address,
"to": s,
"amount": args.amount,
"token": args.token,
"hash": result["transaction"]["hash"],
"update": datetime.utcnow()
}
sentto.insert(record)
sendto.remove(s)
except ValueError:
cerror += 1
logging.error("Oops! Error sending. Try again...")
printProgressBar(i, l, prefix = 'Sending...:', suffix = 'Complete', length = 50)
i = i +1
if i>l:
break
print("All done!")
if cerror>0:
print("System fail to send {} transactions, try again.".format(cerror))
logging.info('App closed...')