feat: Add token api
All checks were successful
Build Docker / BuildImage (push) Successful in 35s

This commit is contained in:
Nathan Woodburn 2024-12-05 13:38:29 +11:00
parent feba858628
commit 5f3c88574d
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1 @@
{"timestamp": 1733365705.1888373, "result": 0.006517062}

View File

@ -184,6 +184,12 @@ def getTokenSupply() -> int:
supply = solana_client.get_token_supply(stWDBRN_token_mint) supply = solana_client.get_token_supply(stWDBRN_token_mint)
return supply.value.ui_amount 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() @cache.file_cache()
def getSolValue() -> int: def getSolValue() -> int:
SOLbalance = solana_client.get_balance( SOLbalance = solana_client.get_balance(
@ -301,6 +307,65 @@ def getCardanoValue(address: str):
# endregion # 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 # region Error Catching
# 404 catch all # 404 catch all