-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspore_price_utils.py
274 lines (230 loc) · 8.67 KB
/
spore_price_utils.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import json
import time
import math
import requests
from web3 import Web3
from web3.middleware import geth_poa_middleware
from decimal import Decimal, InvalidOperation # <-- Import InvalidOperation
import os
# Token configurations
avax_tokens = {
"wavax": {
"symbol": "AVAX",
"address": {
43114: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
43113: "",
},
"decimals": 18,
},
"usdt": {
"symbol": "USDT",
"address": {
43114: "0xc7198437980c041c805A1EDcbA50c1Ce5db95118",
43113: "",
},
"decimals": 18,
},
"spore": {
"symbol": "SPORE",
"address": {
43114: "0x6e7f5C0b9f4432716bDd0a77a3601291b9D9e985",
43113: "",
},
"decimals": 9,
}
}
bsc_tokens = {
"wbnb": {
"chainId": 56,
"symbol": "BNB",
"address": {
56: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
97: "",
},
"decimals": 18,
},
"usdt": {
"chainId": 56,
"symbol": "USDT",
"address": {
56: "0x55d398326f99059fF775485246999027B3197955",
97: "",
},
"decimals": 18,
},
"spore": {
"chainId": 56,
"symbol": "SPORE",
"address": {
56: "0x33A3d962955A3862C8093D1273344719f03cA17C",
97: "",
},
"decimals": 9,
}
}
# Load contract ABI
with open('abi/spore_abi.json') as abi_file:
spore_abi = json.load(abi_file)
# RPC URLs
avax_rpc_url = "https://api.avax.network/ext/bc/C/rpc"
bsc_rpc_url = "https://bsc-dataseed1.defibit.io/"
# Wallet addresses
avax_LP = "0x0a63179a8838b5729e79d239940d7e29e40a0116"
bsc_LP = "0x4aA8F0ef7dd950e260d5EeaF50A1D796D0cefd2f"
# Initialize Web3 providers
avax_web3 = Web3(Web3.HTTPProvider(avax_rpc_url))
bsc_web3 = Web3(Web3.HTTPProvider(bsc_rpc_url))
# verify connection
if not avax_web3.is_connected():
raise ConnectionError("Avalanche Web3 connection failed.")
if not bsc_web3.is_connected():
raise ConnectionError("Binance Smart Chain Web3 connection failed.")
avax_web3.middleware_onion.inject(geth_poa_middleware, layer=0)
bsc_web3.middleware_onion.inject(geth_poa_middleware, layer=0)
# Cache for calc results
cache = {
"data": None,
"timestamp": time.time()
}
def fetch_asset_price(coin_id):
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart?vs_currency=usd&days=1&interval=daily"
try:
response = requests.get(url)
response.raise_for_status()
price = response.json()['prices'][0][1]
return Decimal(price)
except Exception as e:
return Decimal(0)
def get_checksum_address(address):
try:
return Web3.to_checksum_address(address)
except ValueError:
return None
def get_balance(web3, contract_address, wallet, abi):
contract = web3.eth.contract(address=contract_address, abi=abi)
try:
checksum_wallet = get_checksum_address(wallet)
if not checksum_wallet:
return Decimal(0)
balance = contract.functions.balanceOf(checksum_wallet).call()
return Decimal(balance) / Decimal(1e9)
except Exception:
return Decimal(0)
def read_price_indexing_file():
file_exists = os.path.isfile('price_indexing.json')
if not file_exists:
default_data = {"last_timestamp": "0", "price_avax": "0", "price_bsc": "0", "price_diff": "0", "market_cap": "0"}
with open('price_indexing.json', 'w+') as file:
json.dump(default_data, file)
return default_data
else:
with open('price_indexing.json', 'r') as file:
return json.load(file)
def write_price_indexing_file(data):
with open('price_indexing.json', 'w+') as file:
json.dump(data, file)
def fetch_market_cap(coin_id):
url = f"https://api.coingecko.com/api/v3/coins/{coin_id}/market_chart?vs_currency=usd&days=1&interval=daily"
try:
response = requests.get(url)
response.raise_for_status()
market_caps = response.json().get('market_caps', [])
if market_caps:
market_cap = market_caps[-1][-1]
return "{:,.0f}".format(market_cap) # Format with commas
return "0"
except Exception as e:
print(f"Error fetching market cap: {e}")
return "0"
def format_large_number(number):
"""
Formats a large number into a more readable format with suffixes (K, M, B).
"""
if number >= 1_000_000_000:
return f"{number // 1_000_000_000}B"
elif number >= 1_000_000:
return f"{number // 1_000_000}M"
elif number >= 1_000:
return f"{number // 1_000}K"
else:
return str(number)
def calc():
global cache
current_time = time.time()
# Serve cached data if still fresh
if cache["data"] != None and (current_time - cache["timestamp"]) < 120:
print("Serving cached data")
print(cache["data"])
# cache["data"].pop("last_timestamp")
return cache["data"]
spore_address_avax = get_checksum_address(avax_tokens["spore"]["address"][43114])
wavax_address = get_checksum_address(avax_tokens["wavax"]["address"][43114])
spore_balance = get_balance(avax_web3, spore_address_avax, avax_LP, spore_abi)
wavax_balance = get_balance(avax_web3, wavax_address, avax_LP, spore_abi)
avax_usdt_price = fetch_asset_price("avalanche-2")
spore_price_avax = (wavax_balance * avax_usdt_price) / (spore_balance * Decimal(1e3))
liquidity_avax = wavax_balance * avax_usdt_price * 2
spore_address_bnb = get_checksum_address(bsc_tokens["spore"]["address"][56])
wbnb_address = get_checksum_address(bsc_tokens["wbnb"]["address"][56])
spore_balance_bsc = get_balance(bsc_web3, spore_address_bnb, bsc_LP, spore_abi)
bnb_balance = get_balance(bsc_web3, wbnb_address, bsc_LP, spore_abi)
bsc_usdt_price = fetch_asset_price("binancecoin")
bsc_spore_price = (bnb_balance * bsc_usdt_price) / (spore_balance_bsc * Decimal(1e3))
liquidity_bnb = bnb_balance * bsc_usdt_price * 2
total_liquidity = liquidity_avax + liquidity_bnb
# Check to avoid division by zero in calculating liquidity percentages
if total_liquidity == 0:
percent_liquidity_avax = 0
percent_liquidity_bnb = 0
else:
percent_liquidity_avax = (liquidity_avax / (liquidity_avax + liquidity_bnb)) * 100
percent_liquidity_bnb = (liquidity_bnb / (liquidity_avax + liquidity_bnb)) * 100
# Wrap in try/except to catch decimal.InvalidOperation (zero division, etc.)
try:
percent_difference_raw = (
(spore_price_avax - ((bnb_balance * bsc_usdt_price) / (spore_balance_bsc * Decimal(1e3))))
/ spore_price_avax
) * 100
except InvalidOperation:
percent_difference_raw = Decimal(0) # Fallback if spore_balance_bsc = 0 or other invalid operation
percent_difference = "{:.2f}".format(abs(percent_difference_raw))
market_cap = fetch_market_cap("spore")
if market_cap == "0" or spore_price_avax == '0.00000000' or bsc_spore_price == '0.00000000':
data = read_price_indexing_file()
last_timestamp = data["last_timestamp"]
if current_time - float(last_timestamp) < 120:
data.pop("last_timestamp")
return data
formatted_liquidity_avax = format_large_number(liquidity_avax / 10**9)
formatted_liquidity_bnb = format_large_number(liquidity_bnb / 10**9)
print(spore_price_avax)
print(format(spore_price_avax* 10**6, '.8f'))
print("here")
updated_data = {
"AvaxSporePrice": format(spore_price_avax* 10**6, '.8f'),
"BscSporePrice": format(bsc_spore_price * 10**6, '.8f'),
"PriceDiff": percent_difference,
"MarketCap": market_cap,
"LiquidityAvax": formatted_liquidity_avax,
"LiquidityBnb": formatted_liquidity_bnb,
"PercentLiquidityAvax": format(percent_liquidity_avax, '.2f'),
"PercentLiquidityBnb": format(percent_liquidity_bnb, '.2f')
}
cache["data"] = updated_data
cache["timestamp"] = current_time
print("cache")
print(cache)
print(updated_data)
updated_data["last_timestamp"] = current_time
write_price_indexing_file(updated_data)
print(updated_data)
return {
"AvaxSporePrice": format(spore_price_avax * Decimal(1e6), '.8f'),
"BscSporePrice": format(bsc_spore_price * Decimal(1e6), '.8f'),
"PriceDiff": percent_difference,
"MarketCap": market_cap,
"LiquidityAvax": formatted_liquidity_avax,
"LiquidityBnb": formatted_liquidity_bnb,
"PercentLiquidityAvax": format(percent_liquidity_avax, '.2f'),
"PercentLiquidityBnb": format(percent_liquidity_bnb, '.2f')
}