generated from nathanwoodburn/python-webserver-template
feat: Add vault balances to website
All checks were successful
Build Docker / BuildImage (push) Successful in 34s
All checks were successful
Build Docker / BuildImage (push) Successful in 34s
This commit is contained in:
parent
db0b194635
commit
1a5cac57eb
62
server.py
62
server.py
@ -95,7 +95,13 @@ def wellknown(path):
|
|||||||
def index():
|
def index():
|
||||||
tokenSupply = getTokenSupplyString()
|
tokenSupply = getTokenSupplyString()
|
||||||
tokenValue = getTokenPrice()
|
tokenValue = getTokenPrice()
|
||||||
return render_template("index.html", value=tokenValue, supply=tokenSupply)
|
tokens = getTokens()
|
||||||
|
solValue = getSolValue()
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<path:path>")
|
@app.route("/<path:path>")
|
||||||
@ -149,28 +155,42 @@ def get_token_price(token_address:str):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
token_supply_str_cache = TTLCache(maxsize=100, 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(token)
|
||||||
return supply.value.ui_amount_string
|
return supply.value.ui_amount_string
|
||||||
|
|
||||||
token_supply_cache = TTLCache(maxsize=100, 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(token)
|
||||||
return supply.value.ui_amount
|
return supply.value.ui_amount
|
||||||
|
|
||||||
vault_balance_cache = TTLCache(maxsize=100, ttl=3600)
|
sol_value_cache = TTLCache(maxsize=1, ttl=3600)
|
||||||
|
@cached(sol_value_cache)
|
||||||
|
def getSolValue() -> int:
|
||||||
|
SOLbalance = solana_client.get_balance(vault).value / 1000000000
|
||||||
|
SOLPrice = get_coin_price("solana")
|
||||||
|
return SOLbalance * SOLPrice
|
||||||
|
|
||||||
|
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 = solana_client.get_balance(vault).value / 1000000000
|
SOLbalance = getSolValue()
|
||||||
# SOLPrice = get_coin_price("solana")
|
tokens = getTokens()
|
||||||
# Ignore SOL for now as the vault will be handling tokens
|
tokenValue = 0
|
||||||
SOLbalance = 0
|
for token in tokens:
|
||||||
SOLPrice = 0
|
tokenValue += token["value"]
|
||||||
|
|
||||||
|
print(tokens)
|
||||||
|
return SOLbalance + tokenValue
|
||||||
|
|
||||||
|
|
||||||
|
get_tokens_cache = TTLCache(maxsize=1, ttl=3600)
|
||||||
|
@cached(get_tokens_cache)
|
||||||
|
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(
|
||||||
@ -181,8 +201,6 @@ def getVaultBalance() -> int:
|
|||||||
tokenAccounts = tokenAccounts.value
|
tokenAccounts = tokenAccounts.value
|
||||||
tokens = []
|
tokens = []
|
||||||
|
|
||||||
tokenValue = 0
|
|
||||||
|
|
||||||
for tokenAccount in tokenAccounts:
|
for tokenAccount in tokenAccounts:
|
||||||
pubkey = tokenAccount.pubkey
|
pubkey = tokenAccount.pubkey
|
||||||
account = solana_client.get_token_account_balance(pubkey)
|
account = solana_client.get_token_account_balance(pubkey)
|
||||||
@ -196,11 +214,27 @@ def getVaultBalance() -> int:
|
|||||||
|
|
||||||
token["price"] = get_token_price(token["mint"])
|
token["price"] = get_token_price(token["mint"])
|
||||||
token["value"] = token["price"] * token["balance"]
|
token["value"] = token["price"] * token["balance"]
|
||||||
tokenValue += token["value"]
|
|
||||||
|
data = getTokenData(str(mint))
|
||||||
|
token["name"] = data["name"]
|
||||||
|
token["symbol"] = data["symbol"]
|
||||||
tokens.append(token)
|
tokens.append(token)
|
||||||
|
|
||||||
print(tokens)
|
return tokens
|
||||||
return (SOLbalance * SOLPrice) + tokenValue
|
|
||||||
|
def getTokenData(tokenMint):
|
||||||
|
if not os.path.exists("tokens"):
|
||||||
|
os.makedirs("tokens")
|
||||||
|
|
||||||
|
if os.path.exists(f"tokens/{tokenMint}.json"):
|
||||||
|
with open(f"tokens/{tokenMint}.json") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
data = coingecko_client.get_coin_info_from_contract_address_by_id('solana', tokenMint)
|
||||||
|
with open(f"tokens/{tokenMint}.json", "w") as f:
|
||||||
|
json.dump(data, f)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
def getTokenPrice():
|
def getTokenPrice():
|
||||||
|
BIN
stWDBRN.bsdesign
BIN
stWDBRN.bsdesign
Binary file not shown.
@ -45,6 +45,52 @@
|
|||||||
<div class="bg-circle-3 bg-circle"></div>
|
<div class="bg-circle-3 bg-circle"></div>
|
||||||
<div class="bg-circle-4 bg-circle"></div>
|
<div class="bg-circle-4 bg-circle"></div>
|
||||||
</header>
|
</header>
|
||||||
|
<section style="margin-top: 50px;margin-bottom: 50px;">
|
||||||
|
<div class="text-center">
|
||||||
|
<h1>Vault Balances</h1>
|
||||||
|
<div id="pie-chart" style="margin: auto;max-width: 900px;max-height: 500px;background: rgba(255,255,255,0.76);"><script type="text/javascript">
|
||||||
|
|
||||||
|
window.onload = function () {
|
||||||
|
google.charts.load('current', {
|
||||||
|
packages: ['corechart']
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set a callback to run when the API is loaded.
|
||||||
|
google.charts.setOnLoadCallback(drawChart);
|
||||||
|
|
||||||
|
function drawChart() {
|
||||||
|
// Create data array from Flask data passed into the template
|
||||||
|
var data = new google.visualization.DataTable();
|
||||||
|
data.addColumn('string', 'Token');
|
||||||
|
data.addColumn('number', 'Value');
|
||||||
|
data.addColumn({type: 'string', role: 'tooltip', 'p': {'html': true}});
|
||||||
|
|
||||||
|
data.addRows([
|
||||||
|
{% for token, value, name in pie_chart %}
|
||||||
|
['{{ token }}', {{ value }}, '{{name}}'],
|
||||||
|
{% endfor %}
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Set chart options
|
||||||
|
var options = {
|
||||||
|
pieSliceText: 'label',
|
||||||
|
legend: {
|
||||||
|
position: 'right',
|
||||||
|
textStyle: {
|
||||||
|
color: 'white'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
backgroundColor: '#212529',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Instantiate and draw the chart.
|
||||||
|
var chart = new google.visualization.PieChart(document.getElementById('pie-chart'));
|
||||||
|
chart.draw(data, options);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script></div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
<section>
|
<section>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row align-items-center">
|
<div class="row align-items-center">
|
||||||
@ -54,7 +100,7 @@
|
|||||||
<div class="col-lg-6 order-lg-1">
|
<div class="col-lg-6 order-lg-1">
|
||||||
<div class="p-5">
|
<div class="p-5">
|
||||||
<h2 class="display-4">1. Buy tokens</h2>
|
<h2 class="display-4">1. Buy tokens</h2>
|
||||||
<p>To get started buy stWDBRN tokens at the current price to buy a percent of the vault value. The buy in value is then invested in various projects in order to increase the token's value.</p><a class="btn btn-primary" role="button" href="mailto:vault@woodburn.au">Buy</a>
|
<p>To get started buy stWDBRN tokens at the current price to buy a percent of the vault value. The buy in value is then invested in various projects in order to increase the token's value.<br><br>Send USDC or SOL to vault.woodburn.sol or NWywvhcqdkJsm1s9VVviPm9UfyDtyCW9t8kDb24PDPN and send me a message to mint stWDBRN</p><a class="btn btn-primary" role="button" href="mailto:vault@woodburn.au">Buy Tokens</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -84,7 +130,7 @@
|
|||||||
<div class="col-lg-6 order-lg-1">
|
<div class="col-lg-6 order-lg-1">
|
||||||
<div class="p-5">
|
<div class="p-5">
|
||||||
<h2 class="display-4">3. Sell your tokens</h2>
|
<h2 class="display-4">3. Sell your tokens</h2>
|
||||||
<p>When you want to cash out just sell your tokens back at the current token price.</p>
|
<p>When you want to cash out just sell your tokens back to us at the current token price.</p><a class="btn btn-primary" role="button" href="mailto:vault@woodburn.au">Sell Tokens</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -96,6 +142,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
|
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
|
||||||
|
<script src="https://www.gstatic.com/charts/loader.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
1
tokens/27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4.json
Normal file
1
tokens/27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4.json
Normal file
File diff suppressed because one or more lines are too long
1
tokens/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v.json
Normal file
1
tokens/EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user