# 监控以太坊gas突增并发送钉钉通知 **Published by:** [0xcodinnnng](https://paragraph.com/@0xcodinnnng/) **Published on:** 2023-09-12 **URL:** https://paragraph.com/@0xcodinnnng/gas ## Content import logging from loguru import logger import time import hmac import hashlib import base64 import urllib.parse import requests, json def send_ding(access_token, text): timestamp = str(round(time.time() * 1000)) # 注册钉钉开发者平台获取私钥 secret = '' secret_enc = secret.encode('utf-8') string_to_sign = '{}\n{}'.format(timestamp, secret) string_to_sign_enc = string_to_sign.encode('utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) headers = {'Content-Type': 'application/json'} webhook = f'https://oapi.dingtalk.com/robot/send?access_token={access_token}' + '&timestamp=' + timestamp + "&sign=" + sign data = { "text": { "content": "[course]" + text }, "msgtype": "text" } res = requests.post(webhook, data=json.dumps(data), headers=headers) def get_gas(): while True: try: url = "https://ultrasound.money/api/v2/fees/base-fee-over-time" response = requests.get(url) if response.status_code == 200: data = response.json() return data except Exception as e: logging.error('ultrasoud get gas error', e) def get_gas_burn(): while True: try: url = "https://ultrasound.money/api/fees/grouped-analysis-1" response = requests.get(url) if response.status_code == 200: data = response.json() return data # print(data) except Exception as e: logging.error('ultrasoud get gas burn error', e) if __name__ == '__main__': # 钉钉新建群聊,拉自定义机器人进去,设置自定义关键词course,将Webhook里access_token=后面一串复制到bot_token里 bot_token = '' # 这是uniswap、L2和xen相关的地址,不需要监控,如有特殊需要可以自己加回去 unalert_address = ['0x3db52ce065f728011ac6732222270b3f2360d919', '0xfd14567eaf9ba941cb8c8a94eec14831ca7fd1b4', '0x2f848984984d6c3c036174ce627703edaf780479', '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', '0x0de8bf93da2f7eecb3d9169422413a9bef4ef628', '0x7a250d5630b4cf539739df2c5dacb4c659f2488d', '0x0a252663dbcc0b073063d6420a40319e438cfa59', '0x55032650b14df07b85bf18a3a3ec8e0af2e028d5', '0x0000000000771a79d0fc7f3b7fe270eb4498f20b', '0x881d4032abe4188e2237efcd27ab435e81fc6bb1'] while True: try: gas = get_gas() data = gas['m5'] gas_0 = data[0]['wei'] gas_last = data[len(data) - 1]['wei'] gas_10 = data[10]['wei'] difference_1 = gas_last / gas_0 - 1 difference_2 = gas_last / gas_10 - 1 logger.info(f"差异百分比(第一个元素和最后一个元素):{difference_1} ; gas_last : {round(gas_last / 1000000000, 2)}") precent = 0.3 if difference_1 >= precent: gas_burn = get_gas_burn() m5_burn = gas_burn['leaderboards']['leaderboard5m'] m5_burn_first = m5_burn[0] m5_burn_first_address = 'null' m5_burn_first_name = 'null' m5_burn_first_category = 'null' m5_burn_first_detail = 'null' if m5_burn_first['address'] is not None: m5_burn_first_address = m5_burn_first['address'] if m5_burn_first_address is not None: m5_burn_first_name = m5_burn_first['name'] if m5_burn_first['category'] is not None: m5_burn_first_category = m5_burn_first['category'] if m5_burn_first['detail'] is not None: m5_burn_first_detail = m5_burn_first['detail'] m5_burn_second = m5_burn[1] m5_burn_third = m5_burn[2] text = f'主网gas短时间增长{precent * 100}%+,当前gas{round(gas_last / 1000000000, 2)} \n' \ f'gas消耗合约top1:\n' \ f'\taddress : {m5_burn_first_address}\n' \ f'\tname : {m5_burn_first_name}\n' \ f'\tcategory : {m5_burn_first_category}\n' \ f'\tdetail : {m5_burn_first_detail}' if m5_burn_first_address not in unalert_address: logger.info(text) send_ding(bot_token, text) else: logger.info(text) time.sleep(30) except Exception as e: logging.error(f'monitor gas burn error:{e}') ## Publication Information - [0xcodinnnng](https://paragraph.com/@0xcodinnnng/): Publication homepage - [All Posts](https://paragraph.com/@0xcodinnnng/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@0xcodinnnng): Subscribe to updates - [Twitter](https://twitter.com/0xcodinnnng): Follow on Twitter