From 5f3c88574d69a04eb9fcf93fac398dea7d2a97da Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 5 Dec 2024 13:38:29 +1100 Subject: [PATCH] feat: Add token api --- cache/790b28a9a21cf694ad9577ef1072ac9e.json | 1 + server.py | 65 +++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 cache/790b28a9a21cf694ad9577ef1072ac9e.json diff --git a/cache/790b28a9a21cf694ad9577ef1072ac9e.json b/cache/790b28a9a21cf694ad9577ef1072ac9e.json new file mode 100644 index 0000000..c62859e --- /dev/null +++ b/cache/790b28a9a21cf694ad9577ef1072ac9e.json @@ -0,0 +1 @@ +{"timestamp": 1733365705.1888373, "result": 0.006517062} \ No newline at end of file diff --git a/server.py b/server.py index eca7260..efcd659 100644 --- a/server.py +++ b/server.py @@ -184,6 +184,12 @@ def getTokenSupply() -> int: supply = solana_client.get_token_supply(stWDBRN_token_mint) return supply.value.ui_amount +@cache.file_cache() +def getSolBalance() -> int: + SOLbalance = solana_client.get_balance( + vault_sol_address).value / 1000000000 + return SOLbalance + @cache.file_cache() def getSolValue() -> int: SOLbalance = solana_client.get_balance( @@ -301,6 +307,65 @@ def getCardanoValue(address: str): # endregion +# region API Routes + +@app.route("/api/v1/token") +def api_token(): + # Get number of tokens minted + supply = getTokenSupply() + token = {} + # Get balance of vault + token["SOL"] = { + "name": "Solana", + "amount": getSolBalance() / supply, + "value": getSolValue() / supply + } + if token["SOL"]["value"] < 0.01: + token["SOL"]["amount"] = 0 + token["SOL"]["value"] = 0 + + tokens = getTokens() + for t in tokens: + token[t["symbol"].upper()] = t + if token[t["symbol"].upper()]["value"]/supply < 0.01: + token[t["symbol"].upper()]["amount"] = 0 + token[t["symbol"].upper()]["value"] = 0 + else: + token[t["symbol"].upper()]["amount"] = t["balance"] / supply + token[t["symbol"].upper()]["value"] = t["price"] * t["balance"] / supply + # Round value to 4 decimal places + token[t["symbol"].upper()]["value"] = round(token[t["symbol"].upper()]["value"], 4) + token[t["symbol"].upper()]["amount"] = round(token[t["symbol"].upper()]["amount"], 4) + + # Remove balance key + del token[t["symbol"].upper()]["balance"] + + + + token["ADA"] = { + "name": "Cardano", + "amount": getCardanoBalance(vault_cardano_address) / supply, + "value": getCardanoValue(vault_cardano_address) / supply + } + if token["ADA"]["value"] < 0.01: + token["ADA"]["amount"] = 0 + token["ADA"]["value"] = 0 + + token["total"] = { + "name": "stWDBRN", + "description": "stWDBRN total value (USD)", + "amount": 1, + "value":float(getTokenPrice()) + } + + + return jsonify(token) + + + +# endregion + + # region Error Catching # 404 catch all