-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
139 lines (122 loc) · 5.14 KB
/
stats.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
#!/usr/bin/python3
import datetime
import time
from erdpy.accounts import Account
from erdpy.transactions import BunchOfTransactions, Transaction #, TransactionWatcher
from erdpy.proxy import ElrondProxy
from operator import itemgetter
def current_milli_time():
return round(time.time() * 1000)
def milli_to_time(milliseconds):
date = datetime.datetime.fromtimestamp(milliseconds/1000.0)
date = date.strftime('%Y-%m-%d %H:%M:%S')
return date
def milli_to_sec_round(milliseconds):
return int((milliseconds/1000))%60
def milli_to_min_round(milliseconds):
return int((milliseconds/(1000*60)))%60
def milli_to_sec(milliseconds):
return (milliseconds/1000)%60
def milli_to_min(milliseconds):
return (milliseconds/(1000*60))%60
def milli_elrond_to_time(milliseconds):
date = datetime.datetime.fromtimestamp(milliseconds)
date = date.strftime('%Y-%m-%d %H:%M:%S')
return date
def send_transactions(proxy, num_trans, value, contract_address, gas_price, gas_limit, data , chain_id, version):
transactions: BunchOfTransactions = BunchOfTransactions()
for i in range(num_trans):
tx = Transaction()
tx.nonce = sender.nonce + i
tx.value = str(value)
tx.sender = sender.address.bech32()
tx.receiver = contract_address
tx.gasPrice = gas_price
tx.gasLimit = gas_limit
tx.data = data
tx.chainID = chain_id
tx.version = version
tx.sign(sender)
transactions.add_prepared(tx)
start_time = current_milli_time()
num, hashes = transactions.send(proxy)
return start_time, num, hashes
def process_found_data(found_data):
start_time = 9999999999999
end_time = 0
for item in found_data:
# start time is always the same with this implementation (BunchOfTransactions)
# I used it before sending transactions one by one
if start_time > int(item['start_time']):
start_time = int(item['start_time'])
if end_time < int(item['end_time']):
end_time = int(item['end_time'])
print(f"----------------------------------------")
print(f"Hash: {item['hash']} - Status: {item['status']}")
spent_time = item['end_time']-item['start_time']
print(f"Elrond age: {milli_to_time(item['start_time'])} {milli_elrond_to_time(item['timestamp'])} - Start time: {milli_to_time(item['start_time'])} - End time: {milli_to_time(item['end_time'])} - Diff (end t. - start t.): {spent_time}")
print(f"Epoch: {item['epoch']} - Round: {item['round']} - Source shard: {item['sourceShard']} - Destination shard: {item['destinationShard']}")
result = end_time - start_time
print(f"{len(found_data)} transactions executed successfully in {milli_to_min_round(result)} minutes {milli_to_sec_round(result)} seconds ({result} ms).")
print(f"They were sent from this computer at {milli_to_time(start_time)}.")
print(f"They arrived to an Elrond devnet node at {milli_to_time(start_time)}.")
print(f"This computer detected querying the Elrond API that they ended at {milli_to_time(end_time)}.")
def waiting_for_results(proxy, start_time, hashes):
i = 0
found = []
found_data = []
while len(found) < NUM_TRANS:
try:
data = proxy.get_transaction(hashes[str(i)], with_results=True)
dictio = data.to_dictionary()
print(i, end = "\r")
if dictio['status'] == 'success' and not found.__contains__(hashes[str(i)]):
ti, st, ep, ro, so, de = itemgetter('timestamp', 'status', 'epoch', 'round', 'sourceShard', 'destinationShard')(dictio)
obj = {
'hash': hashes[str(i)],
'start_time': start_time,
'end_time': current_milli_time(),
'timestamp': ti,
'status': st,
'epoch': ep,
'round': ro,
'sourceShard': so,
'destinationShard': de
}
found_data.append(obj)
found.append(hashes[str(i)])
except:
pass
finally:
i = (i+1)%NUM_TRANS
return found_data
NUM_TRANS = 5
TIMEOUT = 1
TOKEN = f"4142432d313039373339" # ABC-109739
FUNCTION = "addLiquidityEgld"
AMOUNT_EGLD = 10000000000000000 # 0.01
ONE_EGLD = 1000000000000000000
CONTRACT_ADDRESS = "erd1qqqqqqqqqqqqqpgqs3mnl4qnqrenzmaezf72vzeg5snpe9p0sn2qveucs5";
PROXY = "https://devnet-gateway.elrond.com"
CHAIN_ID = "D"
PEM = "~/wallet/wallet3.pem"
proxy = ElrondProxy(PROXY)
# network settings
network_config = proxy.get_network_config()
# account = Account(pem_file="~/wallet/wallet1.pem")
# account = Account(key_file="myWallet.json", pass_file="myPass.txt")
sender = Account(pem_file=PEM)
sender.sync_nonce(proxy)
# gas_price = proxy.estimate_gas_price()
gas_price = network_config.min_gas_price
gas_limit = 5000000
print(f"Starting parallel {milli_to_time(current_milli_time())}")
print(f"Funding ABC-xEGLD liquidity pool with {AMOUNT_EGLD*NUM_TRANS/ONE_EGLD} xEGLD among {NUM_TRANS} transactions")
print(f"Please wait a few seconds")
# sending a bunch of transactions together
start_time, num, hashes = send_transactions(proxy, NUM_TRANS, AMOUNT_EGLD, CONTRACT_ADDRESS, gas_price , gas_limit, FUNCTION + "@" + TOKEN, CHAIN_ID, 1)
print(f"{num} transactions were sent")
# waiting for the transaction to end, only with success status
found_data = waiting_for_results(proxy, start_time, hashes)
# process results
process_found_data(found_data)