diff --git a/server.py b/server.py index b7bdb68..03dad7f 100644 --- a/server.py +++ b/server.py @@ -29,9 +29,11 @@ dotenv.load_dotenv() app = Flask(__name__) -solana_client = Client(os.environ["SOLANA_URL"]) -token = Pubkey.from_string("mNT61ixgiLnggJ4qf5hDCNj7vTiCqnqysosjncrBydf") -vault = Pubkey.from_string("NWywvhcqdkJsm1s9VVviPm9UfyDtyCW9t8kDb24PDPN") +solana_client = Client(os.getenv("SOLANA_URL")) +blockFrost_API = os.getenv("BLOCKFROST") +stWDBRN_token_mint = Pubkey.from_string("mNT61ixgiLnggJ4qf5hDCNj7vTiCqnqysosjncrBydf") +vault_sol_address = Pubkey.from_string("NWywvhcqdkJsm1s9VVviPm9UfyDtyCW9t8kDb24PDPN") +vault_cardano_address = "stake1uy4qd785pcds7ph2jue2lrhhxa698c5959375lqdv3yphcgwc8qna" fiat = "USD" usd_to_fiat = 1 @@ -97,11 +99,30 @@ def index(): tokenValue = getTokenPrice() tokens = getTokens() solValue = getSolValue() + vaultBalance = getVaultBalance() + vaultBalance = "{:.2f}".format(vaultBalance) + + + # For testing + # tokenSupply = 20 + # tokenValue = 1.01 + # tokens = [{"symbol":"stWDBRN","name":"Stake With Us","value":1.01}] + # solValue = 10 + # vaultBalance = "20.00" + pie_chart_data = [(token['symbol'].upper(), token['value'], f"{token['name']}: ${'{:.2f}'.format(token['value'])}") for token in tokens] pie_chart_data.append(("SOL", solValue, f"Solana: ${'{:.2f}'.format(solValue)}")) - return render_template("index.html", value=tokenValue, supply=tokenSupply, pie_chart=pie_chart_data) + + + + + cardanoBalance = getCardanoValue(vault_cardano_address) + cardanoBalance = "{:.2f}".format(cardanoBalance) + pie_chart_data.append(("ADA", cardanoBalance, f"Cardano: ${cardanoBalance}")) + + return render_template("index.html", value=tokenValue, supply=tokenSupply, pie_chart=pie_chart_data,vault=vaultBalance) @app.route("/") @@ -125,7 +146,6 @@ def catch_all(path: str): return render_template("404.html"), 404 - # endregion @@ -158,19 +178,19 @@ def get_token_price(token_address:str): token_supply_str_cache = TTLCache(maxsize=1, ttl=3600) @cached(token_supply_str_cache) def getTokenSupplyString() -> str: - supply = solana_client.get_token_supply(token) + supply = solana_client.get_token_supply(stWDBRN_token_mint) return supply.value.ui_amount_string token_supply_cache = TTLCache(maxsize=1, ttl=3600) @cached(token_supply_cache) def getTokenSupply() -> int: - supply = solana_client.get_token_supply(token) + supply = solana_client.get_token_supply(stWDBRN_token_mint) return supply.value.ui_amount sol_value_cache = TTLCache(maxsize=1, ttl=3600) @cached(sol_value_cache) def getSolValue() -> int: - SOLbalance = solana_client.get_balance(vault).value / 1000000000 + SOLbalance = solana_client.get_balance(vault_sol_address).value / 1000000000 SOLPrice = get_coin_price("solana") return SOLbalance * SOLPrice @@ -178,14 +198,17 @@ vault_balance_cache = TTLCache(maxsize=1, ttl=3600) @cached(vault_balance_cache) def getVaultBalance() -> int: # Get balance of vault - SOLbalance = getSolValue() + vaultBalance = 0 + vaultBalance += getSolValue() tokens = getTokens() tokenValue = 0 for token in tokens: tokenValue += token["value"] - print(tokens) - return SOLbalance + tokenValue + vaultBalance += tokenValue + vaultBalance += getCardanoValue(vault_cardano_address) + + return vaultBalance get_tokens_cache = TTLCache(maxsize=1, ttl=3600) @@ -194,7 +217,7 @@ def getTokens(): programID = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA") tokenAccounts = solana_client.get_token_accounts_by_owner( - vault, + vault_sol_address, TokenAccountOpts(program_id=programID) ) @@ -253,6 +276,32 @@ def getTokenPrice(): # endregion +# region Cardano +get_cardano_balance_cache = TTLCache(maxsize=1, ttl=3600) +@cached(get_cardano_balance_cache) +def getCardanoBalance(address: str): + # Get balance of cardano address + try: + response = requests.get(f"https://cardano-mainnet.blockfrost.io/api/v0/accounts/{address}",headers={"project_id": blockFrost_API}) + if response.status_code != 200: + print("Error getting cardano balance") + return 0 + data = response.json() + if "controlled_amount" in data: + return int(data["controlled_amount"]) / 1000000 + return 0 + + except: + print("Error getting cardano balance") + return 0 + +def getCardanoValue(address: str): + balance = getCardanoBalance(address) + price = get_coin_price("cardano") + return balance * price + +# endregion + # region Error Catching # 404 catch all @app.errorhandler(404) diff --git a/stWDBRN.bsdesign b/stWDBRN.bsdesign index 689c2e0..a1b5f28 100644 Binary files a/stWDBRN.bsdesign and b/stWDBRN.bsdesign differ diff --git a/templates/index.html b/templates/index.html index d5924a4..63c6ac0 100644 --- a/templates/index.html +++ b/templates/index.html @@ -35,9 +35,9 @@
-

Woodburn Portfolio Vault

-

An easy way to start crypto investing

-

stWDBRN Token Supply: {{supply}}
Current Token Value: {{value}} USD

+

Woodburn Vault

+

An easy way to buy into a diverse crypto portfolio.

+

Woodburn Vault Balance: {{vault}} USD
stWDBRN Token Supply: {{supply}}
Current Token Value: {{value}} USD

@@ -47,7 +47,7 @@
-

Vault Balances

+

Current Vault Contents