forked from Manolis-Tasiopoulos/Whale-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.py
122 lines (96 loc) · 4.27 KB
/
tracker.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
import os
from dotenv import load_dotenv
from blockcypher import get_address_full
from decimal import Decimal
address = '1P5ZEDWTKTFGxQjZphgWPQUpe554WKDfHQ'
load_dotenv()
blockCypher_token = os.getenv('BLOCKCYPHER_TOKEN')
if blockCypher_token is None:
raise EnvironmentError('BLOCKCYPHER_TOKEN is not set in .env file')
def sent_tx(tx_item, address):
for tx_key, tx_value in tx_item.items():
if tx_key == 'inputs':
for input_dict in tx_value:
if address in input_dict['addresses']:
return True
else:
return False
def calculate_total(tx_item, address, is_sent_tx=None):
total_satoshi = 0
for tx_key, tx_value in tx_item.items():
if tx_key == 'inputs':
for input_dict in tx_value:
if address in input_dict['addresses']:
total_satoshi += input_dict['output_value']
if tx_key == 'outputs':
for outputs_dict in tx_value:
if address in outputs_dict['addresses']:
if is_sent_tx is True:
total_satoshi -= outputs_dict['value']
else:
total_satoshi += outputs_dict['value']
convert_to_decimal = Decimal(total_satoshi / pow(10, 8))
return convert_to_decimal
# -------------------------FULL DETAILS OF TRANSACTIONS-------------------------
def main(verbose):
txs = get_address_full(api_key=blockCypher_token, txn_limit=4, address=address)
balance_btc = txs['final_balance'] / 100
position_in_transactions = 0
transactions = []
is_next_block_same = False
for item in txs['txs']:
total_btc = calculate_total(item, address, is_sent_tx=sent_tx(item, address))
if position_in_transactions < len(txs['txs']) - 1 and txs['txs'][position_in_transactions]['block_height'] == \
txs['txs'][position_in_transactions + 1]['block_height']:
if verbose:
print('-----------------------------Skipping Double Block-----------------------------')
is_next_block_same = True
temp_btc_value = total_btc
if verbose:
print(float(temp_btc_value))
if is_next_block_same is False:
for key, value in item.items():
if key == 'inputs' or key == 'outputs':
if verbose:
print('-----------------------------', key, '-----------------------------')
for item_in_out in value:
for key_in_out, value_in_out in item_in_out.items():
if verbose:
print('\t', key_in_out, ' : ', value_in_out)
if verbose:
print()
else:
if verbose:
print(key, ' : ', value)
if total_btc != 0 and txs['txs'][position_in_transactions]['confirmations'] != 0:
if sent_tx(item, address) is True:
tx_type = 'SELL'
if verbose:
print('SELL')
print('Sent BTC: ', total_btc)
else:
tx_type = 'BUY'
if verbose:
print('BUY')
print('Sent BTC: ', total_btc)
tx_date = txs['txs'][position_in_transactions]['confirmed']
if total_btc > 1:
transactions.append({'block': txs['txs'][position_in_transactions]['block_height'],
'time': tx_date,
'type': tx_type,
'amount (BTC)': total_btc})
position_in_transactions += 1
is_next_block_same = False
if verbose:
print(
"\n=========================================================================================================================================================\n")
transactions.append(balance_btc)
for tx in transactions[:-1]:
for key, value in tx.items():
if verbose:
print('\t', key, ' : ', value)
if verbose:
print()
return transactions
if __name__ == '__main__':
main(verbose=True)