Last active
August 19, 2023 03:01
-
-
Save andelf/65121c2c7f81e773f5f879d9992843f8 to your computer and use it in GitHub Desktop.
TRON estimate energy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import collections | |
import statistics | |
import sys | |
import requests | |
import base58 | |
CNTR = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" | |
PAGE = 1 # max 5 | |
PRICE = 140 | |
if len(sys.argv) >= 2: | |
CNTR = sys.argv[1] | |
if len(sys.argv) >= 3: | |
PAGE = int(sys.argv[2]) | |
url = f"https://api.trongrid.io/v1/accounts/{CNTR}/transactions?only_confirmed=true&only_to=true&limit=200&search_internal=false" | |
resp = requests.get(url) | |
payload = resp.json() | |
data = payload['data'] | |
for i in range(1, PAGE): | |
print(f"paging ... {i}/{PAGE}") | |
url = payload['meta']['links']['next'] | |
resp = requests.get(url) | |
payload = resp.json() | |
data += payload['data'] | |
stat = collections.defaultdict(list) | |
txns = 0 | |
for txn in data: | |
if ( | |
txn.get('energy_usage_total', 0) > 0 | |
and txn['raw_data']['contract'][0]['parameter']['value']['contract_address'] | |
== base58.b58decode_check(CNTR).hex() | |
): | |
txns += 1 | |
stat[txn['ret'][0]['contractRet']].append(txn['energy_usage_total']) | |
print("TXNs:", txns) | |
print("RESULT_CODE\tMAX\tMIN\tMEAN\tMEDIAN\tRate") | |
for state, values in stat.items(): | |
print( | |
"%15s" % state, | |
max(values), | |
min(values), | |
int(statistics.mean(values)), | |
int(statistics.median(values)), | |
"%.1f%%" % (len(values) / txns * 100), | |
sep='\t', | |
) | |
print('Use fee_limit >', (max(stat['SUCCESS']) * PRICE) / 1_000_000, 'TRX') |
Author
andelf
commented
Mar 5, 2021
@andelf Thank you so much, So can I now convert the fee_limit to USD and be sure that the Tron network will deduct this amount from me for this transaction?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment