-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTokens.py
204 lines (172 loc) · 6.29 KB
/
getTokens.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
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException
import PySimpleGUI as sg
import time
fnt = "Arial 11 underline"
def tokenNames(driver):
# Gets list of token names in BEP20 wallet exluding BNB
table = driver.find_elements_by_xpath('//table[@class="table table-align-middle table-hover"]\
//tbody//tr//td//div[@class="media-body"]//a[@class="hash-tag text-truncate font-weight-bold"]')
cleanNames = []
for token in table:
token = token.text
cleanNames.append(token)
return cleanNames
def tokenNames_PP(driver, walletAdd):
# Gets list of token names in BEP20 wallet and is used to ask user for
# purchase prices in GUI
driver.get("https://bscscan.com/tokenholdings?a=" + walletAdd)
time.sleep(0.5)
table = driver.find_elements_by_xpath('//table[@class="table table-align-middle table-hover"]\
//tbody//tr//td//div[@class="media-body"]//a[@class="hash-tag text-truncate font-weight-bold"]')
cleanNames = []
for token in table:
token = token.text
cleanNames.append(token)
return cleanNames
def tokenHoldingAmount(driver, tokenNames):
# Get token holding amounts for each token in BEP20 wallet excluding BNB.
# Token amounts begin at columns[11].text & increase by index of 8
columns = driver.find_elements_by_xpath('//table[@class="table table-align-middle table-hover"]\
//tbody//tr//td')
tokenAmounts = []
i = 11
for token in tokenNames:
tokenAmounts.append(columns[i].text)
i = i + 8
return tokenAmounts
def tokenAddresses(driver):
# Retrieve token addresses for pulling up Poocoin page for each token
addresses = driver.find_elements_by_xpath('//table[@class="table table-align-middle table-hover"]\
//tbody//tr//td//div[@class="media-body"]//a[@class="hash-tag text-truncate d-block font-size-1"]')
return addresses
def tokenPrices(driver, addresses):
# Use token addresses to loop thru Poocoin pages and extract price
links =[]
for add in addresses:
links.append("https://poocoin.app/tokens/" + add.text)
prices = []
j = 0
for link in links:
driver.get(link)
time.sleep(1.4)
price = driver.find_element_by_xpath('//div[@class="mb-1 d-flex flex-column lh-1"]//span')
prices.append(price.text)
return prices
def existingLoad(driver, walletAdd, pPrices):
# Function loads all token data and displays in GUI
# Takes three arguments being driver, BEP20 wallet address andlist of token purchase prices
driver.get("https://bscscan.com/tokenholdings?a=" + walletAdd)
time.sleep(0.5)
table = tokenNames(driver)
tokenAmounts = tokenHoldingAmount(driver, table)
address = tokenAddresses(driver)
print("Current prices:")
prices = tokenPrices(driver, address)
print(prices)
cleanPrices = []
for price in prices:
price = price.replace('$', '')
price = price.replace(',', '')
print(price)
price = float(price)
cleanPrices.append(price)
cleanAmounts = []
for amount in tokenAmounts:
amount = amount.replace(',', '')
amount = float(amount)
cleanAmounts.append(amount)
purchasePrices = []
for purchaseP in pPrices:
purchasePrices.append(purchaseP)
# Calculating PL for tokens w/ non-zero and non-one purchase prices
pLGain = []
i = 0
for p in purchasePrices:
if p == 0 or p == 1:
i = i + 1
p = '---'
pLGain.append(p)
continue
p = round(((cleanPrices[i] - p) / p) * 100, 2)
p = str(p) + "%"
pLGain.append(p)
i = i + 1
# Calculating value of token holding for non-zero purchase prices
values = []
totalVals = []
i = 0
for v in purchasePrices:
if purchasePrices[i] == 0:
i = i + 1
val = '---'
values.append(val)
continue
val = round(cleanPrices[i] * cleanAmounts[i], 2)
totalVals.append(val)
val = "{:,}".format(val)
val = "$" + str(val)
values.append(val)
i = i + 1
totalValue = round(sum(totalVals), 2)
totalValue = "{:,}".format(totalValue)
# Generating columns for the GUI
tokenColumn = [[sg.Text("Token", font=fnt)]]
i = 0
for token in table:
if totalVals[i] >= 25:
tokenColumn.append([sg.Text(token)])
i = i + 1
print('added')
else:
i = i + 1
print('skipped')
i = 0
amountColumn = [[sg.Text("Amount", font=fnt)]]
for amount in tokenAmounts:
if totalVals[i] >= 25:
amountColumn.append([sg.Text(amount)])
i = i + 1
else:
i = i + 1
i = 0
priceColumn = [[sg.Text("Price", font=fnt)]]
for price in cleanPrices:
if totalVals[i] >= 25:
priceColumn.append([sg.Text(price)])
i = i + 1
else:
i = i + 1
i = 0
pLColumn = [[sg.Text("P/L", font=fnt)]]
for pl in pLGain:
if totalVals[i] >= 25:
pLColumn.append([sg.Text(pl)])
i = i + 1
else:
i = i + 1
i = 0
valColumn = [[sg.Text("Value (USD)", font=fnt)]]
for val in values:
if totalVals[i] >= 25:
valColumn.append([sg.Text(val)])
i = i + 1
else:
i = i + 1
totColumn = [[sg.Text("= $" + str(totalValue))]]
# Layout for GUI w/ each column
layout = [
[sg.Text('Wallet Address: ' + walletAdd, pad=(10,10))],
[sg.Frame(layout=tokenColumn, title=''), sg.Frame(layout=amountColumn, title=''),\
sg.Frame(layout=priceColumn, title=''), sg.Frame(layout=pLColumn, title=''),
sg.Frame(layout=valColumn, title='')],
[sg.Sizer(402,10), sg.Frame(layout=totColumn, title='')],
[sg.Button('Back', pad=(5,12)), sg.Sizer(323,10), sg.Button("Update Prices", pad=(5,12)),\
sg.Button("Close", pad=(5,12))]
]
return layout