Last active
April 17, 2021 15:50
-
-
Save spikeekips/914dff42bda5ad108fbfc283d5f54948 to your computer and use it in GitHub Desktop.
The script name explains everything; just simply run `boscoin-operation-checker.py -h`
This file contains 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
import argparse | |
import os | |
import pprint # noqa | |
import requests | |
import sys | |
try: | |
_, TERMINAL_COLUMNS = os.popen('stty size', 'r').read().split() | |
except ValueError: | |
TERMINAL_COLUMNS = 0 | |
else: | |
TERMINAL_COLUMNS = int(TERMINAL_COLUMNS) | |
BASE_FEE = 0.001 | |
def line(c='-'): | |
print(c * TERMINAL_COLUMNS) | |
return | |
def add_accounts_alias(addr, main_account): | |
if addr in ACCOUNTS: | |
return | |
if addr == main_account: | |
ACCOUNTS[addr] = 'self' | |
return | |
n = 1 | |
key = addr[:4] | |
while key in ACCOUNTS.values(): | |
key = addr[:4] + ('*' * n) | |
n += 1 | |
ACCOUNTS[addr] = key | |
return | |
ACCOUNTS = dict() | |
HORIZON = 'https://horizon-tokennet.dev.boscoin.io' | |
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('-horizon', default=HORIZON, help='horizon server address') | |
parser.add_argument('account_address', help='account address you want to trace') | |
if __name__ == '__main__': | |
options = parser.parse_args() | |
records = list() | |
url = options.horizon + '/accounts/' + options.account_address + '/operations?limit=50' | |
while True: | |
response = requests.get(url) | |
o = response.json() | |
next_url = o['_links']['next']['href'] | |
if url == next_url: | |
break | |
url = next_url | |
if len(o['_embedded']['records']) < 1: | |
break | |
for i in o['_embedded']['records']: | |
from_account = None | |
to_account = None | |
amount = None | |
if i['type'] in ('payment',): | |
amount = i['amount'] | |
from_account = i['from'] | |
to_account = i['to'] | |
elif i['type'] in ('create_account',): | |
amount = i['starting_balance'] | |
from_account = i['funder'] | |
to_account = i['account'] | |
add_accounts_alias(from_account, options.account_address) | |
add_accounts_alias(to_account, options.account_address) | |
records.append( | |
( | |
i['created_at'], | |
dict( | |
created_at=i['created_at'], | |
from_account=from_account, | |
to_account=to_account, | |
type=i['type'], | |
amount=amount, | |
), | |
), | |
) | |
print('# alias of all related addresses') | |
max_len_addr = max(map(lambda x: len(x), ACCOUNTS.values())) | |
line('=') | |
print(('%%%ds | %%s' % max_len_addr) % ('alias', 'full address')) | |
line('=') | |
for i, (addr, alias) in enumerate(ACCOUNTS.items()): | |
print((' %%s | %%%ds' % max_len_addr) % (alias, addr)) | |
line('-' if i < len(ACCOUNTS) - 1 else '=') | |
print() | |
print('# operations: %d records found' % len(records)) | |
line('=') | |
print(' %(created_at)20s | %(type)-17s | %(_from_account)4s -> %(_to_account)-4s | %(amount)20s' % dict( | |
created_at='created at', | |
type='type', | |
_from_account='from', | |
_to_account='to', | |
amount='amount', | |
)) | |
line('=') | |
balance = None | |
for i, (_, o) in enumerate(sorted(records)): | |
o['_from_account'] = ACCOUNTS[o['from_account']] | |
o['_to_account'] = ACCOUNTS[o['to_account']] | |
print(' %(created_at)s | %(type)-17s | %(_from_account)s -> %(_to_account)s | %(amount)20s' % o) | |
line('-' if i < len(records) - 1 else '=') | |
if o['type'] == 'create_account' and o['to_account'] == options.account_address: | |
balance = float(o['amount']) | |
continue | |
if o['from_account'] == options.account_address: | |
fee = BASE_FEE | |
if o['type'] == 'create_account': | |
fee = BASE_FEE * 2 | |
balance -= float(o['amount']) + fee | |
else: | |
balance += float(o['amount']) | |
print() | |
response = requests.get(options.horizon + '/accounts/' + options.account_address) | |
latest_balance = response.json()['balances'][0]['balance'] | |
line('=') | |
print(' > checked the latest balance from horizon | %30s' % latest_balance) | |
print(' < calculate from upper operation recores | %30.7f' % balance) | |
line('=') | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment