generated from nathanwoodburn/python-webserver-template
feat: Add some safety limits and some cache checks
All checks were successful
Build Docker / BuildImage (push) Successful in 1m5s
All checks were successful
Build Docker / BuildImage (push) Successful in 1m5s
This commit is contained in:
parent
a82fedabaf
commit
dbc496ddab
7
cache.py
7
cache.py
@ -50,6 +50,10 @@ def file_cache(ttl=3600):
|
||||
# Start a background thread for auto-refresh if it doesn't exist
|
||||
if not os.path.exists(cache_file):
|
||||
result = func(*args, **kwargs)
|
||||
if result is None:
|
||||
print(f"Function {func.__name__} returned None, skipping cache refresh")
|
||||
return None
|
||||
|
||||
with open(cache_file, "w") as f:
|
||||
json.dump({"timestamp": time(), "result": result}, f)
|
||||
|
||||
@ -70,6 +74,9 @@ def file_cache(ttl=3600):
|
||||
|
||||
# If cache is expired or invalid, recompute and update
|
||||
result = func(*args, **kwargs)
|
||||
if result is None:
|
||||
print(f"Function {func.__name__} returned None, skipping cache refresh")
|
||||
return None
|
||||
with open(cache_file, "w") as f:
|
||||
json.dump({"timestamp": time(), "result": result}, f)
|
||||
|
||||
|
2
cache/154903f1c716802fd57eec282afe3cb8.json
vendored
2
cache/154903f1c716802fd57eec282afe3cb8.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148137.3610523, "result": 1.605033}
|
||||
{"timestamp": 1737961149.9915102, "result": 1.62}
|
2
cache/1981e30de11cf0845e8b5affb4dc75e0.json
vendored
2
cache/1981e30de11cf0845e8b5affb4dc75e0.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148072.9105287, "result": 3657.18}
|
||||
{"timestamp": 1737961020.5400536, "result": 3088.64}
|
2
cache/1ccff5c6f117409fea0c861aa44b8e62.json
vendored
2
cache/1ccff5c6f117409fea0c861aa44b8e62.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148135.5155199, "result": 1.078}
|
||||
{"timestamp": 1737961147.8386934, "result": 0.894771}
|
2
cache/29409a8a40dd2d547a7a44b8f6758f54.json
vendored
2
cache/29409a8a40dd2d547a7a44b8f6758f54.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148011.002414, "result": 4.39}
|
||||
{"timestamp": 1737961022.586637, "result": 4.59}
|
2
cache/32fbffa989a7d6c0472092fc8982713b.json
vendored
2
cache/32fbffa989a7d6c0472092fc8982713b.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148008.9088044, "result": 709.43}
|
||||
{"timestamp": 1737961023.5081046, "result": 647.36}
|
2
cache/4104ed0427efe63d4ca0dead970a4391.json
vendored
2
cache/4104ed0427efe63d4ca0dead970a4391.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148072.0298233, "result": 229.74}
|
||||
{"timestamp": 1737961021.5623932, "result": 247.18}
|
2
cache/46c1ec7f5edb30c070003dc54e072f41.json
vendored
2
cache/46c1ec7f5edb30c070003dc54e072f41.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148133.9794865, "result": 5.16}
|
||||
{"timestamp": 1737961146.5078144, "result": 3.62}
|
2
cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json
vendored
2
cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148006.2010005, "result": 214.35}
|
||||
{"timestamp": 1737961018.6946898, "result": 227.55}
|
2
cache/60265613786b14e87a2dfba823dac3e7.json
vendored
2
cache/60265613786b14e87a2dfba823dac3e7.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148384.6488435, "result": [{"name": "Jupiter Perpetuals Liquidity Provider Token", "apy": 14.84, "url": "https://jup.ag/perps-earn", "updated": 1736066605}, {"name": "Volo Staked SUI", "apy": 2.71, "url": "https://stake.volosui.com/", "updated": 1736066605}, {"name": "Cardano", "apy": 2.88, "url": "https://adastat.net/pools/08f05bcfaada3bb5c038b8c88c6b502ceabfd9978973159458c6535b", "updated": 1736066605}, {"name": "Jupiter Staked Solana", "apy": 9.92, "url": "https://solanacompass.com/stake-pools/8VpRhuxa7sUUepdY3kQiTmX9rS5vx4WgaXiAnXq4KCtr", "updated": 1736066605}]}
|
||||
{"timestamp": 1737961617.8312876, "result": [{"name": "Jupiter Perpetuals Liquidity Provider Token", "apy": 22.22, "url": "https://jup.ag/perps-earn", "updated": 1736923766}, {"name": "Volo Staked SUI", "apy": 2.71, "url": "https://stake.volosui.com/", "updated": 1736923766}, {"name": "Cardano", "apy": 2.86, "url": "https://adastat.net/pools/08f05bcfaada3bb5c038b8c88c6b502ceabfd9978973159458c6535b", "updated": 1736923766}, {"name": "Jupiter Staked Solana", "apy": 11.42, "url": "https://solanacompass.com/stake-pools/8VpRhuxa7sUUepdY3kQiTmX9rS5vx4WgaXiAnXq4KCtr", "updated": 1736923766}]}
|
2
cache/6cc6c6a0b2d0654f551d33209c1fd824.json
vendored
2
cache/6cc6c6a0b2d0654f551d33209c1fd824.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148134.3905075, "result": 5.35}
|
||||
{"timestamp": 1737961146.9175854, "result": 3.78}
|
2
cache/6eec370e2713cfc84c84e1080b8a191a.json
vendored
2
cache/6eec370e2713cfc84c84e1080b8a191a.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148365.6652994, "result": 618.742658875}
|
||||
{"timestamp": 1737961499.5175362, "result": 655.149311525}
|
2
cache/790b28a9a21cf694ad9577ef1072ac9e.json
vendored
2
cache/790b28a9a21cf694ad9577ef1072ac9e.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148005.1766274, "result": 0.004523624000000004}
|
||||
{"timestamp": 1737961018.187586, "result": 0.009853956000000004}
|
2
cache/a071d7bdda25c22e42ad7840f17c4b0e.json
vendored
2
cache/a071d7bdda25c22e42ad7840f17c4b0e.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148009.8783817, "result": 0.998474}
|
||||
{"timestamp": 1737961024.4296334, "result": 0.999902}
|
2
cache/a099e7e540e16a185b0279b9494e5099.json
vendored
2
cache/a099e7e540e16a185b0279b9494e5099.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148007.9315152, "result": 99073}
|
||||
{"timestamp": 1737961085.4691918, "result": 99018}
|
2
cache/a0ee60913ba556f39d128e7d7249e788.json
vendored
2
cache/a0ee60913ba556f39d128e7d7249e788.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148382.1951156, "result": [{"mint": "9gP2kCy3wA1ctvYWQk75guqXuHfrEomqydHLtcTCqiLa", "balance": 0.03683279, "price": 709.43, "value": 26.130286209699996, "name": "Binance Coin (Wormhole)", "symbol": "bnb"}, {"mint": "7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs", "balance": 0.00255735, "price": 3657.18, "value": 9.352689273, "name": "Ethereum (Wormhole)", "symbol": "eth"}, {"mint": "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh", "balance": 0.00056828, "price": 99073, "value": 56.30120444, "name": "Wrapped BTC (Wormhole)", "symbol": "wbtc"}, {"mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", "balance": 1.0, "price": 0.998474, "value": 0.998474, "name": "USDC", "symbol": "usdc"}, {"mint": "jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v", "balance": 0.239815492, "price": 229.74, "value": 55.09521113208, "name": "Jupiter Staked SOL", "symbol": "jupsol"}, {"mint": "27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4", "balance": 38.996342, "price": 4.39, "value": 171.19394137999998, "name": "Jupiter Perpetuals Liquidity Provider Token", "symbol": "jlp"}, {"mint": "0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT", "balance": 22.852920128, "price": 5.35, "value": 122.2631226848, "name": "Volo Staked SUI", "symbol": "vsui"}, {"mint": "0x2::sui::SUI", "balance": 0.902850107, "price": 5.16, "value": 4.65870655212, "name": "Sui", "symbol": "sui"}]}
|
||||
{"timestamp": 1737961459.5788186, "result": [{"mint": "27G8MtK7VtTcCHkpASjSDdkWWYfoqT6ggEuKidVJidD4", "balance": 38.996342, "price": 4.59, "value": 178.99320978, "name": "Jupiter Perpetuals Liquidity Provider Token", "symbol": "jlp"}, {"mint": "7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs", "balance": 0.00255735, "price": 3088.64, "value": 7.898733503999999, "name": "Ethereum (Wormhole)", "symbol": "eth"}, {"mint": "3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh", "balance": 0.00094418, "price": 99018, "value": 93.49081524, "name": "Wrapped BTC (Wormhole)", "symbol": "wbtc"}, {"mint": "jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v", "balance": 0.239815492, "price": 247.18, "value": 59.27759331256, "name": "Jupiter Staked SOL", "symbol": "jupsol"}, {"mint": "9gP2kCy3wA1ctvYWQk75guqXuHfrEomqydHLtcTCqiLa", "balance": 0.03683279, "price": 647.36, "value": 23.8440749344, "name": "Binance Coin (Wormhole)", "symbol": "bnb"}, {"mint": "0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT", "balance": 22.852920128, "price": 3.78, "value": 86.38403808384, "name": "Volo Staked SUI", "symbol": "vsui"}, {"mint": "0x2::sui::SUI", "balance": 0.902850107, "price": 3.62, "value": 3.26831738734, "name": "Sui", "symbol": "sui"}]}
|
2
cache/c652e896f57f598d23e3dbebdb815ef6.json
vendored
2
cache/c652e896f57f598d23e3dbebdb815ef6.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148375.0232542, "result": [{"mint": "0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT", "balance": 22.852920128, "price": 5.35, "value": 122.2631226848, "name": "Volo Staked SUI", "symbol": "vsui"}, {"mint": "0x2::sui::SUI", "balance": 0.902850107, "price": 5.16, "value": 4.65870655212, "name": "Sui", "symbol": "sui"}]}
|
||||
{"timestamp": 1737961507.7148268, "result": [{"mint": "0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT", "balance": 22.852920128, "price": 3.78, "value": 86.38403808384, "name": "Volo Staked SUI", "symbol": "vsui"}, {"mint": "0x2::sui::SUI", "balance": 0.902850107, "price": 3.62, "value": 3.26831738734, "name": "Sui", "symbol": "sui"}]}
|
2
cache/cardano_balance.json
vendored
2
cache/cardano_balance.json
vendored
@ -1 +1 @@
|
||||
{"balance": 82.92772}
|
||||
{"balance": 83.036766}
|
2
cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json
vendored
2
cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148435.4791589, "result": 82.92772}
|
||||
{"timestamp": 1737961447.8469446, "result": 83.036766}
|
2
cache/d4e81748ee91683b2c48adaa5cf95187.json
vendored
2
cache/d4e81748ee91683b2c48adaa5cf95187.json
vendored
@ -1 +1 @@
|
||||
{"timestamp": 1736148442.606954, "result": [{"name": "Kamino USDC Lending", "description": "Kamino USDC Lending", "apy": 9.5, "url": "https://app.kamino.finance/lending/reserve/DxXdAyU3kCjnyggvHmY5nAwg5cRbbmdyX3npfDMjjMek/Ga4rZytCpq1unD4DbEJ5bkHeUz9g3oh9AAFEi6vSauXp", "value": 50.5, "updated": "1736061253", "initial": 50, "bought": "1733717057", "type": "Lending"}, {"name": "dYdX MegaVault", "description": "dYdX MegaVault", "apy": 28, "url": "https://dydx.trade/vault", "value": 61.67, "updated": "1736061253", "initial": 60, "bought": "1733300000", "type": "Lending"}]}
|
||||
{"timestamp": 1737961614.3270462, "result": [{"name": "Kamino USDC Lending", "description": "Kamino USDC Lending", "apy": 15.04, "url": "https://app.kamino.finance/lending/reserve/DxXdAyU3kCjnyggvHmY5nAwg5cRbbmdyX3npfDMjjMek/Ga4rZytCpq1unD4DbEJ5bkHeUz9g3oh9AAFEi6vSauXp", "value": 50.73, "updated": 1737960928, "initial": 50, "bought": 1733717057, "type": "Lending"}, {"name": "dYdX MegaVault", "description": "dYdX MegaVault", "apy": 23, "url": "https://dydx.trade/vault", "value": 62.8, "updated": 1737960928, "initial": 60, "bought": 1733300000, "type": "Lending"}]}
|
13
server.py
13
server.py
@ -55,10 +55,15 @@ vault_cardano_address = "stake1uy4qd785pcds7ph2jue2lrhhxa698c5959375lqdv3yphcgwc
|
||||
vault_sui_address = "0x7e4fa1592e4fad084789f9fe1a4d7631a2e6477b658e777ae95351681bcbe8da"
|
||||
sol_reserve = 0.051 # This is used for TX fees and rent
|
||||
|
||||
# Safety limits (change these to stay around token price)
|
||||
MINPRICE = 0.80
|
||||
MAXPRICE = 2
|
||||
|
||||
|
||||
|
||||
fiat = "USD"
|
||||
stablecoins = ["usdc", "usdt", "dai"]
|
||||
usd_to_aud_backup = 1.56
|
||||
usd_to_aud_backup = 1.62
|
||||
|
||||
coingecko_client = CoinGeckoAPI()
|
||||
|
||||
@ -810,6 +815,12 @@ async def mint_stWDBRN(USD_amount, to_user_account):
|
||||
if amount > 10:
|
||||
amount = amount - 0.05
|
||||
|
||||
|
||||
# Add some safety in case of token price errors
|
||||
if stWDBRN_price < MINPRICE or stWDBRN_price > MAXPRICE:
|
||||
print(f"Skipping minting of {USD_amount} USD to {to_user_account} as it is less than {MINPRICE} or greater than {MAXPRICE}", flush=True)
|
||||
return
|
||||
|
||||
print(f"Minting {amount} stWDBRN to {to_user_account}", flush=True)
|
||||
TOKEN_PROGRAM_ID = Pubkey.from_string("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb")
|
||||
WALLET = os.getenv("WALLET")
|
||||
|
Loading…
Reference in New Issue
Block a user