|
| 1 | +import requests |
| 2 | +from dotenv import load_dotenv |
| 3 | +from web3 import Web3 |
| 4 | + |
| 5 | +from utils.cache import cache_filename, get_last_value_for_key_from_file, write_last_value_to_file |
| 6 | +from utils.chains import Chain |
| 7 | +from utils.telegram import send_telegram_message |
| 8 | +from utils.web3_wrapper import ChainManager |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +# Constants |
| 13 | +PROTOCOL = "usdai" |
| 14 | +VAULT_ADDR = Web3.to_checksum_address("0x0A1a1A107E45b7Ced86833863f482BC5f4ed82EF") |
| 15 | +WM_TOKEN = Web3.to_checksum_address("0x437cc33344a0b27a429f795ff6b469c72698b291") |
| 16 | +SUSDAI_ADDR = Web3.to_checksum_address("0x0B2b2B2076d95dda7817e785989fE353fe955ef9") |
| 17 | +GRAPHQL_URL = "https://protocol-api.m0.org/graphql" |
| 18 | + |
| 19 | + |
| 20 | +def main(): |
| 21 | + client = ChainManager.get_client(Chain.ARBITRUM) |
| 22 | + |
| 23 | + # Common ABI |
| 24 | + from utils.abi import load_abi |
| 25 | + |
| 26 | + erc20_abi = load_abi("common-abi/ERC20.json") |
| 27 | + |
| 28 | + wm = client.get_contract(WM_TOKEN, erc20_abi) |
| 29 | + |
| 30 | + try: |
| 31 | + # --- On-Chain Supply --- |
| 32 | + # USDai Supply (wM held by Vault) |
| 33 | + vault_shares = wm.functions.balanceOf(VAULT_ADDR).call() |
| 34 | + # Decimals will always be the same = 6 |
| 35 | + wm_decimals = 6 |
| 36 | + usdai_supply_fmt = vault_shares / (10**wm_decimals) |
| 37 | + |
| 38 | + # 2. Get Mint Ratio via API |
| 39 | + query = """ |
| 40 | + query { |
| 41 | + mintRatio: protocolConfigs( |
| 42 | + where: {key: "mint_ratio"} |
| 43 | + orderBy: blockTimestamp |
| 44 | + orderDirection: desc |
| 45 | + first: 1 |
| 46 | + ) { |
| 47 | + value |
| 48 | + blockTimestamp |
| 49 | + } |
| 50 | + } |
| 51 | + """ |
| 52 | + |
| 53 | + mint_ratio = 10000 # Default to 1:1 (scaled 1e4 for bps) if not found |
| 54 | + |
| 55 | + try: |
| 56 | + res = requests.post(GRAPHQL_URL, json={"query": query}, timeout=10) |
| 57 | + if res.status_code == 200: |
| 58 | + data = res.json().get("data", {}) |
| 59 | + |
| 60 | + # --- Mint Ratio --- |
| 61 | + configs = data.get("mintRatio", []) |
| 62 | + if configs: |
| 63 | + # Mint ratio is scaled by 1e4 (e.g. 9950 = 99.5%) |
| 64 | + mint_ratio_raw = int(configs[0].get("value", 10000)) |
| 65 | + mint_ratio = mint_ratio_raw |
| 66 | + |
| 67 | + except Exception as e: |
| 68 | + print(f"API Error: {e}") |
| 69 | + |
| 70 | + # Derived Collateral from Mint Ratio |
| 71 | + # Scaling: mint_ratio is in bps (1e4). |
| 72 | + # So mint_ratio_fmt = mint_ratio / 10000. |
| 73 | + mint_ratio_fmt = mint_ratio / 10000 |
| 74 | + |
| 75 | + # Avoid division by zero |
| 76 | + required_collateral = 0 |
| 77 | + if mint_ratio_fmt > 0: |
| 78 | + required_collateral = usdai_supply_fmt / mint_ratio_fmt |
| 79 | + |
| 80 | + print("\n--- USDai Stats ---") |
| 81 | + print(f"USDai Supply: ${usdai_supply_fmt:,.2f}") |
| 82 | + print(f"Mint Ratio: {mint_ratio_fmt:.4f}") |
| 83 | + |
| 84 | + collateral_metric = required_collateral |
| 85 | + # Buffer = Collateral - Supply |
| 86 | + buffer = collateral_metric - usdai_supply_fmt |
| 87 | + |
| 88 | + print(f"Collateral: ${collateral_metric:,.2f}") |
| 89 | + print(f"Buffer: ${buffer:,.2f}") |
| 90 | + |
| 91 | + if collateral_metric > 0: |
| 92 | + # 1. Check for Mint Ratio Change (Critical) |
| 93 | + cache_key_ratio = f"{PROTOCOL}_mint_ratio" |
| 94 | + last_ratio = int(get_last_value_for_key_from_file(cache_filename, cache_key_ratio)) |
| 95 | + |
| 96 | + if last_ratio != 0 and last_ratio != mint_ratio: |
| 97 | + msg = f"⚠️ *USDai Mint Ratio Changed*\n\nOld: {last_ratio / 10000:.4f}\nNew: {mint_ratio / 10000:.4f}" |
| 98 | + send_telegram_message(msg, PROTOCOL) |
| 99 | + |
| 100 | + # Always update ratio cache |
| 101 | + write_last_value_to_file(cache_filename, cache_key_ratio, mint_ratio) |
| 102 | + |
| 103 | + # 2. Check for Buffer Drop (Activity) |
| 104 | + cache_key_buffer = f"{PROTOCOL}_buffer" |
| 105 | + last_buffer = float(get_last_value_for_key_from_file(cache_filename, cache_key_buffer)) |
| 106 | + |
| 107 | + if last_buffer != 0: |
| 108 | + change = buffer - last_buffer |
| 109 | + # Alert if buffer drops by more than $10,000 |
| 110 | + if change < -10000: |
| 111 | + msg = f"📉 *USDai Buffer Drop Alert*\n\nBuffer dropped by ${abs(change):,.2f}!\nOld Buffer: ${last_buffer:,.2f}\nNew Buffer: ${buffer:,.2f}\n(Collateral: ${collateral_metric:,.2f})" |
| 112 | + send_telegram_message(msg, PROTOCOL) |
| 113 | + |
| 114 | + write_last_value_to_file(cache_filename, cache_key_buffer, buffer) |
| 115 | + |
| 116 | + except Exception as e: |
| 117 | + print(f"Error: {e}") |
| 118 | + send_telegram_message(f"⚠️ USDai monitoring failed: {e}", PROTOCOL, False, True) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments