From dbc496ddab5a4574186dc6e62887b2ab07253539 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Mon, 27 Jan 2025 18:07:32 +1100 Subject: [PATCH] feat: Add some safety limits and some cache checks --- cache.py | 7 +++++++ cache/154903f1c716802fd57eec282afe3cb8.json | 2 +- cache/1981e30de11cf0845e8b5affb4dc75e0.json | 2 +- cache/1ccff5c6f117409fea0c861aa44b8e62.json | 2 +- cache/29409a8a40dd2d547a7a44b8f6758f54.json | 2 +- cache/32fbffa989a7d6c0472092fc8982713b.json | 2 +- cache/4104ed0427efe63d4ca0dead970a4391.json | 2 +- cache/46c1ec7f5edb30c070003dc54e072f41.json | 2 +- cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json | 2 +- cache/60265613786b14e87a2dfba823dac3e7.json | 2 +- cache/6cc6c6a0b2d0654f551d33209c1fd824.json | 2 +- cache/6eec370e2713cfc84c84e1080b8a191a.json | 2 +- cache/790b28a9a21cf694ad9577ef1072ac9e.json | 2 +- cache/a071d7bdda25c22e42ad7840f17c4b0e.json | 2 +- cache/a099e7e540e16a185b0279b9494e5099.json | 2 +- cache/a0ee60913ba556f39d128e7d7249e788.json | 2 +- cache/c652e896f57f598d23e3dbebdb815ef6.json | 2 +- cache/cardano_balance.json | 2 +- cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json | 2 +- cache/d4e81748ee91683b2c48adaa5cf95187.json | 2 +- server.py | 13 ++++++++++++- 21 files changed, 38 insertions(+), 20 deletions(-) diff --git a/cache.py b/cache.py index 4392937..c07220b 100644 --- a/cache.py +++ b/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) diff --git a/cache/154903f1c716802fd57eec282afe3cb8.json b/cache/154903f1c716802fd57eec282afe3cb8.json index b1dd264..aa7cc07 100644 --- a/cache/154903f1c716802fd57eec282afe3cb8.json +++ b/cache/154903f1c716802fd57eec282afe3cb8.json @@ -1 +1 @@ -{"timestamp": 1736148137.3610523, "result": 1.605033} \ No newline at end of file +{"timestamp": 1737961149.9915102, "result": 1.62} \ No newline at end of file diff --git a/cache/1981e30de11cf0845e8b5affb4dc75e0.json b/cache/1981e30de11cf0845e8b5affb4dc75e0.json index e077b84..25fb2ed 100644 --- a/cache/1981e30de11cf0845e8b5affb4dc75e0.json +++ b/cache/1981e30de11cf0845e8b5affb4dc75e0.json @@ -1 +1 @@ -{"timestamp": 1736148072.9105287, "result": 3657.18} \ No newline at end of file +{"timestamp": 1737961020.5400536, "result": 3088.64} \ No newline at end of file diff --git a/cache/1ccff5c6f117409fea0c861aa44b8e62.json b/cache/1ccff5c6f117409fea0c861aa44b8e62.json index 6211f58..a2d008d 100644 --- a/cache/1ccff5c6f117409fea0c861aa44b8e62.json +++ b/cache/1ccff5c6f117409fea0c861aa44b8e62.json @@ -1 +1 @@ -{"timestamp": 1736148135.5155199, "result": 1.078} \ No newline at end of file +{"timestamp": 1737961147.8386934, "result": 0.894771} \ No newline at end of file diff --git a/cache/29409a8a40dd2d547a7a44b8f6758f54.json b/cache/29409a8a40dd2d547a7a44b8f6758f54.json index 9ec9a0b..e5fee25 100644 --- a/cache/29409a8a40dd2d547a7a44b8f6758f54.json +++ b/cache/29409a8a40dd2d547a7a44b8f6758f54.json @@ -1 +1 @@ -{"timestamp": 1736148011.002414, "result": 4.39} \ No newline at end of file +{"timestamp": 1737961022.586637, "result": 4.59} \ No newline at end of file diff --git a/cache/32fbffa989a7d6c0472092fc8982713b.json b/cache/32fbffa989a7d6c0472092fc8982713b.json index 92f4b14..64b56a2 100644 --- a/cache/32fbffa989a7d6c0472092fc8982713b.json +++ b/cache/32fbffa989a7d6c0472092fc8982713b.json @@ -1 +1 @@ -{"timestamp": 1736148008.9088044, "result": 709.43} \ No newline at end of file +{"timestamp": 1737961023.5081046, "result": 647.36} \ No newline at end of file diff --git a/cache/4104ed0427efe63d4ca0dead970a4391.json b/cache/4104ed0427efe63d4ca0dead970a4391.json index 74e896f..59798a7 100644 --- a/cache/4104ed0427efe63d4ca0dead970a4391.json +++ b/cache/4104ed0427efe63d4ca0dead970a4391.json @@ -1 +1 @@ -{"timestamp": 1736148072.0298233, "result": 229.74} \ No newline at end of file +{"timestamp": 1737961021.5623932, "result": 247.18} \ No newline at end of file diff --git a/cache/46c1ec7f5edb30c070003dc54e072f41.json b/cache/46c1ec7f5edb30c070003dc54e072f41.json index 8f31ca6..28a7d5f 100644 --- a/cache/46c1ec7f5edb30c070003dc54e072f41.json +++ b/cache/46c1ec7f5edb30c070003dc54e072f41.json @@ -1 +1 @@ -{"timestamp": 1736148133.9794865, "result": 5.16} \ No newline at end of file +{"timestamp": 1737961146.5078144, "result": 3.62} \ No newline at end of file diff --git a/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json b/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json index 72fbb22..30f63d3 100644 --- a/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json +++ b/cache/598f5dbf97fb0d45cbc6e1a5b0a3b575.json @@ -1 +1 @@ -{"timestamp": 1736148006.2010005, "result": 214.35} \ No newline at end of file +{"timestamp": 1737961018.6946898, "result": 227.55} \ No newline at end of file diff --git a/cache/60265613786b14e87a2dfba823dac3e7.json b/cache/60265613786b14e87a2dfba823dac3e7.json index d92d0f0..21ae1ce 100644 --- a/cache/60265613786b14e87a2dfba823dac3e7.json +++ b/cache/60265613786b14e87a2dfba823dac3e7.json @@ -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}]} \ No newline at end of file +{"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}]} \ No newline at end of file diff --git a/cache/6cc6c6a0b2d0654f551d33209c1fd824.json b/cache/6cc6c6a0b2d0654f551d33209c1fd824.json index cc9a910..efe442c 100644 --- a/cache/6cc6c6a0b2d0654f551d33209c1fd824.json +++ b/cache/6cc6c6a0b2d0654f551d33209c1fd824.json @@ -1 +1 @@ -{"timestamp": 1736148134.3905075, "result": 5.35} \ No newline at end of file +{"timestamp": 1737961146.9175854, "result": 3.78} \ No newline at end of file diff --git a/cache/6eec370e2713cfc84c84e1080b8a191a.json b/cache/6eec370e2713cfc84c84e1080b8a191a.json index 0d05ed1..d013770 100644 --- a/cache/6eec370e2713cfc84c84e1080b8a191a.json +++ b/cache/6eec370e2713cfc84c84e1080b8a191a.json @@ -1 +1 @@ -{"timestamp": 1736148365.6652994, "result": 618.742658875} \ No newline at end of file +{"timestamp": 1737961499.5175362, "result": 655.149311525} \ No newline at end of file diff --git a/cache/790b28a9a21cf694ad9577ef1072ac9e.json b/cache/790b28a9a21cf694ad9577ef1072ac9e.json index f646203..73d74dc 100644 --- a/cache/790b28a9a21cf694ad9577ef1072ac9e.json +++ b/cache/790b28a9a21cf694ad9577ef1072ac9e.json @@ -1 +1 @@ -{"timestamp": 1736148005.1766274, "result": 0.004523624000000004} \ No newline at end of file +{"timestamp": 1737961018.187586, "result": 0.009853956000000004} \ No newline at end of file diff --git a/cache/a071d7bdda25c22e42ad7840f17c4b0e.json b/cache/a071d7bdda25c22e42ad7840f17c4b0e.json index 59eb5d3..665304f 100644 --- a/cache/a071d7bdda25c22e42ad7840f17c4b0e.json +++ b/cache/a071d7bdda25c22e42ad7840f17c4b0e.json @@ -1 +1 @@ -{"timestamp": 1736148009.8783817, "result": 0.998474} \ No newline at end of file +{"timestamp": 1737961024.4296334, "result": 0.999902} \ No newline at end of file diff --git a/cache/a099e7e540e16a185b0279b9494e5099.json b/cache/a099e7e540e16a185b0279b9494e5099.json index 8ba8373..2bcf22a 100644 --- a/cache/a099e7e540e16a185b0279b9494e5099.json +++ b/cache/a099e7e540e16a185b0279b9494e5099.json @@ -1 +1 @@ -{"timestamp": 1736148007.9315152, "result": 99073} \ No newline at end of file +{"timestamp": 1737961085.4691918, "result": 99018} \ No newline at end of file diff --git a/cache/a0ee60913ba556f39d128e7d7249e788.json b/cache/a0ee60913ba556f39d128e7d7249e788.json index b4e76b7..f576fd6 100644 --- a/cache/a0ee60913ba556f39d128e7d7249e788.json +++ b/cache/a0ee60913ba556f39d128e7d7249e788.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/cache/c652e896f57f598d23e3dbebdb815ef6.json b/cache/c652e896f57f598d23e3dbebdb815ef6.json index 68ba3d4..d29b8c7 100644 --- a/cache/c652e896f57f598d23e3dbebdb815ef6.json +++ b/cache/c652e896f57f598d23e3dbebdb815ef6.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/cache/cardano_balance.json b/cache/cardano_balance.json index d9fef21..98ab9f8 100644 --- a/cache/cardano_balance.json +++ b/cache/cardano_balance.json @@ -1 +1 @@ -{"balance": 82.92772} \ No newline at end of file +{"balance": 83.036766} \ No newline at end of file diff --git a/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json b/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json index 9885021..8e92e3a 100644 --- a/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json +++ b/cache/ccf2a009e56f1b05d471a55d9c9ea8ea.json @@ -1 +1 @@ -{"timestamp": 1736148435.4791589, "result": 82.92772} \ No newline at end of file +{"timestamp": 1737961447.8469446, "result": 83.036766} \ No newline at end of file diff --git a/cache/d4e81748ee91683b2c48adaa5cf95187.json b/cache/d4e81748ee91683b2c48adaa5cf95187.json index b519d5f..b7f3875 100644 --- a/cache/d4e81748ee91683b2c48adaa5cf95187.json +++ b/cache/d4e81748ee91683b2c48adaa5cf95187.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/server.py b/server.py index b985c85..dcca5c2 100644 --- a/server.py +++ b/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")