feat: Add some safety limits and some cache checks
All checks were successful
Build Docker / BuildImage (push) Successful in 1m5s

This commit is contained in:
Nathan Woodburn 2025-01-27 18:07:32 +11:00
parent a82fedabaf
commit dbc496ddab
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
21 changed files with 38 additions and 20 deletions

View File

@ -50,6 +50,10 @@ def file_cache(ttl=3600):
# Start a background thread for auto-refresh if it doesn't exist # Start a background thread for auto-refresh if it doesn't exist
if not os.path.exists(cache_file): if not os.path.exists(cache_file):
result = func(*args, **kwargs) 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: with open(cache_file, "w") as f:
json.dump({"timestamp": time(), "result": result}, 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 # If cache is expired or invalid, recompute and update
result = func(*args, **kwargs) 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: with open(cache_file, "w") as f:
json.dump({"timestamp": time(), "result": result}, f) json.dump({"timestamp": time(), "result": result}, f)

View File

@ -1 +1 @@
{"timestamp": 1736148137.3610523, "result": 1.605033} {"timestamp": 1737961149.9915102, "result": 1.62}

View File

@ -1 +1 @@
{"timestamp": 1736148072.9105287, "result": 3657.18} {"timestamp": 1737961020.5400536, "result": 3088.64}

View File

@ -1 +1 @@
{"timestamp": 1736148135.5155199, "result": 1.078} {"timestamp": 1737961147.8386934, "result": 0.894771}

View File

@ -1 +1 @@
{"timestamp": 1736148011.002414, "result": 4.39} {"timestamp": 1737961022.586637, "result": 4.59}

View File

@ -1 +1 @@
{"timestamp": 1736148008.9088044, "result": 709.43} {"timestamp": 1737961023.5081046, "result": 647.36}

View File

@ -1 +1 @@
{"timestamp": 1736148072.0298233, "result": 229.74} {"timestamp": 1737961021.5623932, "result": 247.18}

View File

@ -1 +1 @@
{"timestamp": 1736148133.9794865, "result": 5.16} {"timestamp": 1737961146.5078144, "result": 3.62}

View File

@ -1 +1 @@
{"timestamp": 1736148006.2010005, "result": 214.35} {"timestamp": 1737961018.6946898, "result": 227.55}

View File

@ -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}]}

View File

@ -1 +1 @@
{"timestamp": 1736148134.3905075, "result": 5.35} {"timestamp": 1737961146.9175854, "result": 3.78}

View File

@ -1 +1 @@
{"timestamp": 1736148365.6652994, "result": 618.742658875} {"timestamp": 1737961499.5175362, "result": 655.149311525}

View File

@ -1 +1 @@
{"timestamp": 1736148005.1766274, "result": 0.004523624000000004} {"timestamp": 1737961018.187586, "result": 0.009853956000000004}

View File

@ -1 +1 @@
{"timestamp": 1736148009.8783817, "result": 0.998474} {"timestamp": 1737961024.4296334, "result": 0.999902}

View File

@ -1 +1 @@
{"timestamp": 1736148007.9315152, "result": 99073} {"timestamp": 1737961085.4691918, "result": 99018}

View File

@ -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"}]}

View File

@ -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"}]}

View File

@ -1 +1 @@
{"balance": 82.92772} {"balance": 83.036766}

View File

@ -1 +1 @@
{"timestamp": 1736148435.4791589, "result": 82.92772} {"timestamp": 1737961447.8469446, "result": 83.036766}

View File

@ -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"}]}

View File

@ -55,10 +55,15 @@ vault_cardano_address = "stake1uy4qd785pcds7ph2jue2lrhhxa698c5959375lqdv3yphcgwc
vault_sui_address = "0x7e4fa1592e4fad084789f9fe1a4d7631a2e6477b658e777ae95351681bcbe8da" vault_sui_address = "0x7e4fa1592e4fad084789f9fe1a4d7631a2e6477b658e777ae95351681bcbe8da"
sol_reserve = 0.051 # This is used for TX fees and rent 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" fiat = "USD"
stablecoins = ["usdc", "usdt", "dai"] stablecoins = ["usdc", "usdt", "dai"]
usd_to_aud_backup = 1.56 usd_to_aud_backup = 1.62
coingecko_client = CoinGeckoAPI() coingecko_client = CoinGeckoAPI()
@ -810,6 +815,12 @@ async def mint_stWDBRN(USD_amount, to_user_account):
if amount > 10: if amount > 10:
amount = amount - 0.05 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) print(f"Minting {amount} stWDBRN to {to_user_account}", flush=True)
TOKEN_PROGRAM_ID = Pubkey.from_string("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb") TOKEN_PROGRAM_ID = Pubkey.from_string("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb")
WALLET = os.getenv("WALLET") WALLET = os.getenv("WALLET")