-
Notifications
You must be signed in to change notification settings - Fork 5
/
pcs_tracker.py
228 lines (192 loc) · 16.6 KB
/
pcs_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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from web3 import Web3
from bscscan import BscScan
import asyncio
import requests, json
import datetime, time, os, curses
import sqlite3
# Initialize screen
curses.initscr()
curses.start_color()
# Color scheme
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
# PancakeSwap information
pancakeswap_factory_abi = '[{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount0In","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1In","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount0Out","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount1Out","type":"uint256"},{"indexed":true,"internalType":"address","name":"to","type":"address"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint112","name":"reserve0","type":"uint112"},{"indexed":false,"internalType":"uint112","name":"reserve1","type":"uint112"}],"name":"Sync","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"MINIMUM_LIQUIDITY","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"PERMIT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"burn","outputs":[{"internalType":"uint256","name":"amount0","type":"uint256"},{"internalType":"uint256","name":"amount1","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getReserves","outputs":[{"internalType":"uint112","name":"_reserve0","type":"uint112"},{"internalType":"uint112","name":"_reserve1","type":"uint112"},{"internalType":"uint32","name":"_blockTimestampLast","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_token0","type":"address"},{"internalType":"address","name":"_token1","type":"address"}],"name":"initialize","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"kLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"mint","outputs":[{"internalType":"uint256","name":"liquidity","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"price0CumulativeLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"price1CumulativeLast","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"skim","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount0Out","type":"uint256"},{"internalType":"uint256","name":"amount1Out","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"swap","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"sync","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]'
# Open JSON file
try:
f = open('config.json')
config = json.load(f)
except:
print("No or missconfigured configuration file")
# Configuration parameter
for conf in config['config']:
blockchain_url = conf['blockchain_url']
bsc_api_key = conf['bsc_api_key']
currency_api_key = conf['currency_api_key']
currency = conf['currency']
pancakeswap_factory = conf['pancakeswap_factory']
bnb_busd_pair_address = conf['bnb_busd_pair_address']
refresh_interval = conf['refresh_interval']
# Initialize database
con = sqlite3.connect('pcs_tracker.db')
c = con.cursor()
# Connect to blockchain node
try:
blockchain = Web3(Web3.HTTPProvider(blockchain_url))
except:
print("Node not connected")
def init_database():
with con:
# Price table
c.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='price'")
if c.fetchone()[0] != 1:
c.execute("CREATE TABLE price (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, date DATE, price FLOAT, tokenamount FLOAT, balance INT, profit INT, token_id INT)")
# Rate table
c.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='rate'")
if c.fetchone()[0] != 1:
c.execute("CREATE TABLE rate (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, rate FLOAT, count INT)")
def set_history(token_id, price, tokenamount, balance, profit):
with con:
c.execute(f"SELECT date FROM price WHERE token_id='{token_id}'")
if not c.fetchone():
c.execute(f"INSERT INTO PRICE(date, price, tokenamount, balance, profit, token_id) VALUES ('{datetime.datetime.now()}', '{price}', '{tokenamount}', '{balance}', '{profit}', '{token_id}')")
else:
c.execute(f"SELECT * FROM price WHERE token_id='{token_id}'")
column = [d[0] for d in c.description]
data = [dict(zip(column, row)) for row in c.fetchall()]
return data
def calc_diff(value, history, percent=False):
if percent:
diff = ((value - history) / history) * 100
if (value < 0 and history < 0):
diff = (-diff)
else:
diff = value - history
return round(diff, 2)
def exchange_rate(currency):
count = 60 / refresh_interval
rate = 0
url = "https://free.currconv.com/api/v7/convert?q=USD_{0}&compact=ultra&apiKey={1}".format(currency, currency_api_key)
with con:
c.execute(f"SELECT count FROM rate WHERE count='{count}'")
rows = c.fetchone()
if rows is None:
c.execute(f"SELECT rate FROM rate")
rows = c.fetchone()
if rows is None:
rate = (float)(requests.get(url).json()["USD_{0}".format(currency)])
c.execute(f"INSERT INTO rate(rate, count) VALUES ('{rate}', '1')")
else:
c.execute(f"SELECT rate, count FROM rate")
column = [d[0] for d in c.description]
data = [dict(zip(column, row)) for row in c.fetchall()]
rate = data[0]['rate']
count = data[0]['count'] + 1
c.execute(f"UPDATE rate set rate='{rate}', count='{count}'")
else:
rate = (float)(requests.get(url).json()["USD_{0}".format(currency)])
c.execute(f"UPDATE rate set rate='{rate}', count='1'")
return rate
def token_price(pair_address, decimal_token0, decimal_token1, token_invert):
contract = blockchain.eth.contract(address=pair_address, abi=pancakeswap_factory_abi);
reserve = contract.functions.getReserves().call()
if (token_invert):
reserve_token0 = reserve[1]
reserve_token1 = reserve[0]
else:
reserve_token0 = reserve[0]
reserve_token1 = reserve[1]
token_price = (reserve_token1 / 10 ** decimal_token1) / (reserve_token0 / 10 ** decimal_token0)
return token_price
async def token_amount(token_address, wallet):
async with BscScan(bsc_api_key) as client:
return (
await client.get_acc_balance_by_token_contract_address(contract_address=token_address, address=wallet)
)
def create_table(description, price, tokencount, balance, profit, coord, token_history, price_history, balance_history, profit_history):
wname = curses.newwin(5, 15, (1+coord), 10)
wname.border(0)
wname.addstr(1, 2, "Token", curses.color_pair(1))
wname.addstr(3, 2, description['token_name'], curses.A_BOLD)
wname.refresh()
if (price_history < 0):
colorpair = curses.color_pair(2)
else:
colorpair = curses.color_pair(1)
wvalue = curses.newwin(5, 42, (1+coord), 25)
wvalue.border(0)
wvalue.addstr(1, 2, "Value", curses.color_pair(1))
wvalue.addstr(1, 30, "Change %", curses.color_pair(1))
wvalue.addstr(3, 2, str(price))
wvalue.addstr(3, 30, str(price_history), colorpair)
wvalue.refresh()
wamount = curses.newwin(5, 42, (1+coord), 67)
wamount.border(0)
wamount.addstr(1, 2, "Token amount", curses.color_pair(1))
wamount.addstr(1, 22, "Additional tokens", curses.color_pair(1))
wamount.addstr(3, 2, str('{:,.0f}'.format(tokencount)))
wamount.addstr(3, 22, str('{:,.0f}'.format(token_history)))
wamount.refresh()
if (balance_history < 0):
colorpair = curses.color_pair(2)
else:
colorpair = curses.color_pair(1)
wbalance = curses.newwin(5, 25, (1+coord), 109)
wbalance.border(0)
wbalance.addstr(1, 2, "Balance", curses.color_pair(1))
wbalance.addstr(1, 15, "Change %", curses.color_pair(1))
wbalance.addstr(3, 2, str(balance))
wbalance.addstr(3, 15, str(balance_history), colorpair)
wbalance.refresh()
if (profit_history < 0):
colorpair = curses.color_pair(2)
else:
colorpair = curses.color_pair(1)
wprofit = curses.newwin(5, 25, (1+coord), 134)
wprofit.border(0)
wprofit.addstr(1, 2, "Profit", curses.color_pair(1))
wprofit.addstr(1, 15, "Change %", curses.color_pair(1))
wprofit.addstr(3, 2, str(profit))
wprofit.addstr(3, 15, str(profit_history), colorpair)
wprofit.refresh()
def main():
token_history = 0
price_history = 0
balance_history = 0
profit_history = 0
init_database()
while True:
coord = 0
rate = exchange_rate(currency)
for i in config['tokens']:
coord = coord + 5
if (i['decimal_token0'] == 3):
tokencount = round(int(asyncio.run(token_amount(i['token_address'], i['wallet']))) / 1000)
elif (i['decimal_token0'] == 9):
tokencount = round(int(asyncio.run(token_amount(i['token_address'], i['wallet']))) / 1000000000)
else:
tokencount = round(int(asyncio.run(token_amount(i['token_address'], i['wallet']))) / 1000000000000000000)
if (i['token_currency'] == "BUSD"):
tprice = token_price(i["token_pair"], i["decimal_token0"], i["decimal_token1"], i["token_invert"])
price = '%.18f' % tprice
# convert WBNB to BUSD
else:
bprice = token_price(bnb_busd_pair_address, 18, 18, False)
tprice = token_price(i["token_pair"], i["decimal_token0"], i["decimal_token1"], i["token_invert"])
price = '%.18f' % (bprice * tprice)
balance = int( (float(price) * tokencount) * rate)
profit = int(balance - int(i['investment']))
# Write initial values to history database
history = set_history(i['id'], price, tokencount, balance, profit)
# Calculate differences since start
if history:
token_history = calc_diff(tokencount, history[0]['tokenamount'])
if (token_history < 0):
token_history = 0
price_history = calc_diff(float(price), history[0]['price'], percent=True)
balance_history = calc_diff(balance, history[0]['balance'], percent=True)
profit_history = calc_diff(profit, history[0]['profit'], percent=True)
create_table(i, price, tokencount, balance, profit, coord, token_history, price_history, balance_history, profit_history)
time.sleep(refresh_interval)
if __name__ == "__main__":
main()