generated from nathanwoodburn/python-webserver-template
feat: Initial version
All checks were successful
Build Docker / BuildImage (push) Successful in 1m5s
All checks were successful
Build Docker / BuildImage (push) Successful in 1m5s
This commit is contained in:
118
server.py
118
server.py
@@ -1,4 +1,4 @@
|
||||
from functools import cache
|
||||
from functools import lru_cache
|
||||
import json
|
||||
from flask import (
|
||||
Flask,
|
||||
@@ -15,11 +15,30 @@ import json
|
||||
import requests
|
||||
from datetime import datetime
|
||||
import dotenv
|
||||
import solana
|
||||
import solana.rpc
|
||||
from solana.rpc.api import Client
|
||||
import solana.rpc.api
|
||||
from solders.pubkey import Pubkey
|
||||
from solana.rpc.types import TokenAccountOpts
|
||||
from pycoingecko import CoinGeckoAPI
|
||||
from cachetools import TTLCache
|
||||
from cachetools import cached
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
solana_client = Client(os.environ["SOLANA_URL"])
|
||||
token = Pubkey.from_string("mNT61ixgiLnggJ4qf5hDCNj7vTiCqnqysosjncrBydf")
|
||||
vault = Pubkey.from_string("NWywvhcqdkJsm1s9VVviPm9UfyDtyCW9t8kDb24PDPN")
|
||||
|
||||
fiat = "USD"
|
||||
usd_to_fiat = 1
|
||||
stablecoins = ["usdc", "usdt", "dai"]
|
||||
|
||||
coingecko_client = CoinGeckoAPI()
|
||||
|
||||
|
||||
def find(name, path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
@@ -74,7 +93,9 @@ def wellknown(path):
|
||||
# region Main routes
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
tokenSupply = getTokenSupplyString()
|
||||
tokenValue = getTokenPrice()
|
||||
return render_template("index.html", value=tokenValue, supply=tokenSupply)
|
||||
|
||||
|
||||
@app.route("/<path:path>")
|
||||
@@ -102,6 +123,99 @@ def catch_all(path: str):
|
||||
# endregion
|
||||
|
||||
|
||||
# region Solana
|
||||
|
||||
coin_price_cache = TTLCache(maxsize=100, ttl=3600)
|
||||
@cached(coin_price_cache)
|
||||
def get_coin_price(coin_id):
|
||||
|
||||
try:
|
||||
if coin_id in stablecoins:
|
||||
return 1 * usd_to_fiat
|
||||
|
||||
price = coingecko_client.get_price(ids=coin_id, vs_currencies=fiat)
|
||||
|
||||
return price[coin_id][fiat.lower()]
|
||||
except:
|
||||
return 0
|
||||
|
||||
token_price_cache = TTLCache(maxsize=100, ttl=3600)
|
||||
@cached(token_price_cache)
|
||||
def get_token_price(token_address:str):
|
||||
try:
|
||||
price = coingecko_client.get_token_price(id='solana',contract_addresses=token_address,vs_currencies=fiat)
|
||||
return price[token_address][fiat.lower()]
|
||||
except:
|
||||
return 0
|
||||
|
||||
|
||||
token_supply_str_cache = TTLCache(maxsize=100, ttl=3600)
|
||||
@cached(token_supply_str_cache)
|
||||
def getTokenSupplyString() -> str:
|
||||
supply = solana_client.get_token_supply(token)
|
||||
return supply.value.ui_amount_string
|
||||
|
||||
token_supply_cache = TTLCache(maxsize=100, ttl=3600)
|
||||
@cached(token_supply_cache)
|
||||
def getTokenSupply() -> int:
|
||||
supply = solana_client.get_token_supply(token)
|
||||
return supply.value.ui_amount
|
||||
|
||||
vault_balance_cache = TTLCache(maxsize=100, ttl=3600)
|
||||
@cached(vault_balance_cache)
|
||||
def getVaultBalance() -> int:
|
||||
# Get balance of vault
|
||||
# SOLbalance = solana_client.get_balance(vault).value / 1000000000
|
||||
# SOLPrice = get_coin_price("solana")
|
||||
# Ignore SOL for now as the vault will be handling tokens
|
||||
SOLbalance = 0
|
||||
SOLPrice = 0
|
||||
|
||||
programID = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
|
||||
|
||||
tokenAccounts = solana_client.get_token_accounts_by_owner(
|
||||
vault,
|
||||
TokenAccountOpts(program_id=programID)
|
||||
)
|
||||
|
||||
tokenAccounts = tokenAccounts.value
|
||||
tokens = []
|
||||
|
||||
tokenValue = 0
|
||||
|
||||
for tokenAccount in tokenAccounts:
|
||||
pubkey = tokenAccount.pubkey
|
||||
account = solana_client.get_token_account_balance(pubkey)
|
||||
mint = tokenAccount.account.data[:32]
|
||||
mint = Pubkey(mint)
|
||||
# Decode the mint
|
||||
token = {
|
||||
"mint": str(mint),
|
||||
"balance": account.value.ui_amount
|
||||
}
|
||||
|
||||
token["price"] = get_token_price(token["mint"])
|
||||
token["value"] = token["price"] * token["balance"]
|
||||
tokenValue += token["value"]
|
||||
tokens.append(token)
|
||||
|
||||
print(tokens)
|
||||
return (SOLbalance * SOLPrice) + tokenValue
|
||||
|
||||
|
||||
def getTokenPrice():
|
||||
# Get number of tokens minted
|
||||
supply = getTokenSupply()
|
||||
vaultBalance = getVaultBalance()
|
||||
value = vaultBalance/supply
|
||||
# Round value to 2 decimal places and ensure it shows 2 decimal places
|
||||
value = "{:.2f}".format(value)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region Error Catching
|
||||
# 404 catch all
|
||||
@app.errorhandler(404)
|
||||
|
||||
Reference in New Issue
Block a user