feat: Add Cardano Address
All checks were successful
Build Docker / BuildImage (push) Successful in 1m3s

This commit is contained in:
Nathan Woodburn 2024-12-05 12:26:58 +11:00
parent 8d28cb8858
commit 2f9c167cec
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
3 changed files with 65 additions and 16 deletions

View File

@ -29,9 +29,11 @@ dotenv.load_dotenv()
app = Flask(__name__) app = Flask(__name__)
solana_client = Client(os.environ["SOLANA_URL"]) solana_client = Client(os.getenv("SOLANA_URL"))
token = Pubkey.from_string("mNT61ixgiLnggJ4qf5hDCNj7vTiCqnqysosjncrBydf") blockFrost_API = os.getenv("BLOCKFROST")
vault = Pubkey.from_string("NWywvhcqdkJsm1s9VVviPm9UfyDtyCW9t8kDb24PDPN") stWDBRN_token_mint = Pubkey.from_string("mNT61ixgiLnggJ4qf5hDCNj7vTiCqnqysosjncrBydf")
vault_sol_address = Pubkey.from_string("NWywvhcqdkJsm1s9VVviPm9UfyDtyCW9t8kDb24PDPN")
vault_cardano_address = "stake1uy4qd785pcds7ph2jue2lrhhxa698c5959375lqdv3yphcgwc8qna"
fiat = "USD" fiat = "USD"
usd_to_fiat = 1 usd_to_fiat = 1
@ -97,11 +99,30 @@ def index():
tokenValue = getTokenPrice() tokenValue = getTokenPrice()
tokens = getTokens() tokens = getTokens()
solValue = getSolValue() 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 = [(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)}")) 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("/<path:path>") @app.route("/<path:path>")
@ -125,7 +146,6 @@ def catch_all(path: str):
return render_template("404.html"), 404 return render_template("404.html"), 404
# endregion # endregion
@ -158,19 +178,19 @@ def get_token_price(token_address:str):
token_supply_str_cache = TTLCache(maxsize=1, ttl=3600) token_supply_str_cache = TTLCache(maxsize=1, ttl=3600)
@cached(token_supply_str_cache) @cached(token_supply_str_cache)
def getTokenSupplyString() -> str: 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 return supply.value.ui_amount_string
token_supply_cache = TTLCache(maxsize=1, ttl=3600) token_supply_cache = TTLCache(maxsize=1, ttl=3600)
@cached(token_supply_cache) @cached(token_supply_cache)
def getTokenSupply() -> int: def getTokenSupply() -> int:
supply = solana_client.get_token_supply(token) supply = solana_client.get_token_supply(stWDBRN_token_mint)
return supply.value.ui_amount return supply.value.ui_amount
sol_value_cache = TTLCache(maxsize=1, ttl=3600) sol_value_cache = TTLCache(maxsize=1, ttl=3600)
@cached(sol_value_cache) @cached(sol_value_cache)
def getSolValue() -> int: 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") SOLPrice = get_coin_price("solana")
return SOLbalance * SOLPrice return SOLbalance * SOLPrice
@ -178,14 +198,17 @@ vault_balance_cache = TTLCache(maxsize=1, ttl=3600)
@cached(vault_balance_cache) @cached(vault_balance_cache)
def getVaultBalance() -> int: def getVaultBalance() -> int:
# Get balance of vault # Get balance of vault
SOLbalance = getSolValue() vaultBalance = 0
vaultBalance += getSolValue()
tokens = getTokens() tokens = getTokens()
tokenValue = 0 tokenValue = 0
for token in tokens: for token in tokens:
tokenValue += token["value"] tokenValue += token["value"]
print(tokens) vaultBalance += tokenValue
return SOLbalance + tokenValue vaultBalance += getCardanoValue(vault_cardano_address)
return vaultBalance
get_tokens_cache = TTLCache(maxsize=1, ttl=3600) get_tokens_cache = TTLCache(maxsize=1, ttl=3600)
@ -194,7 +217,7 @@ def getTokens():
programID = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA") programID = Pubkey.from_string("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
tokenAccounts = solana_client.get_token_accounts_by_owner( tokenAccounts = solana_client.get_token_accounts_by_owner(
vault, vault_sol_address,
TokenAccountOpts(program_id=programID) TokenAccountOpts(program_id=programID)
) )
@ -253,6 +276,32 @@ def getTokenPrice():
# endregion # 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 # region Error Catching
# 404 catch all # 404 catch all
@app.errorhandler(404) @app.errorhandler(404)

Binary file not shown.

View File

@ -35,9 +35,9 @@
<header class="text-center text-white masthead"> <header class="text-center text-white masthead">
<div class="masthead-content"> <div class="masthead-content">
<div class="container"> <div class="container">
<h1 class="masthead-heading mb-0">Woodburn Portfolio Vault</h1> <h1 class="masthead-heading mb-0">Woodburn Vault</h1>
<h2 class="masthead-subheading mb-0">An easy way to start crypto investing</h2> <h2 class="masthead-subheading mb-0">An easy way to buy into a diverse crypto portfolio.</h2>
<p>stWDBRN Token Supply: {{supply}}<br>Current Token Value: {{value}} USD</p> <p>Woodburn Vault Balance: {{vault}} USD<br>stWDBRN Token Supply: {{supply}}<br>Current Token Value: {{value}} USD</p>
</div> </div>
</div> </div>
<div class="bg-circle-1 bg-circle"></div> <div class="bg-circle-1 bg-circle"></div>
@ -47,7 +47,7 @@
</header> </header>
<section style="margin-top: 50px;margin-bottom: 50px;max-width: 100vw;overflow: hidden;"> <section style="margin-top: 50px;margin-bottom: 50px;max-width: 100vw;overflow: hidden;">
<div class="text-center"> <div class="text-center">
<h1>Vault Balances</h1> <h1>Current Vault Contents</h1>
<div id="pie-chart" style="margin: auto;"><script type="text/javascript"> <div id="pie-chart" style="margin: auto;"><script type="text/javascript">
window.onload = function () { window.onload = function () {