Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram support #19

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ A CLI tool to deploy, monitor and manage validator keys for the Lido CSM.
- Exit validators:
- Manually, with its public key
- Automatically, by monitoring the Lido Validator Exit Bus Oracle and actioning exit requests
- Telegram notifications for detected exit requests
- Support for remote signer setups (e.g. web3signer)

## Pre-requisites
Expand Down Expand Up @@ -71,6 +72,10 @@ url="http://my-remote-signer.xyz:port"

[monitoring]
node_operator_ids = [420, 666]

[telegram]
bot_token = 'xxxxxxxx-telegram-bot-token'
channel_id = '-123456'
```

Some of these parameters are optional and can be omitted depending on the functionality you want to use. See the next section for more details.
Expand Down Expand Up @@ -136,6 +141,13 @@ Only required for state checks and automated exits, not for deploying validators

- `node_operator_ids`: List of one or more node operator IDs to monitor for exit requests

### Telegram Configuration

Only required for receiving validator exit notifications via Telegram.

- `bot_token`: Telegram bot token
- `channel_id`: Telegram channel ID

## Execute

### Deploying validators
Expand Down Expand Up @@ -168,6 +180,8 @@ This subcommand will submit an exit request for a validator with a given public

This subcommand will monitor the Lido Validator Exit Bus Oracle and log exit requests for the configured node operator ids.

`python -m swarm auto_exit [--delete]`
`python -m swarm auto_exit [--delete] [--telegram]`

The `--delete` flag indicates that the validator will be automatically exited when an exit request has been detected.

The `--telegram` flag indicates that the exit request will be relayed to a Telegram channel.
4 changes: 4 additions & 0 deletions config.toml.holesky.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ url="http://my-remote-signer.xyz:port"

[monitoring]
node_operator_ids = [420, 666]

[telegram]
bot_token = 'xxxxxxxx-telegram-bot-token'
channel_id = '-123456'
4 changes: 4 additions & 0 deletions config.toml.mainnet.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ url="http://my-remote-signer.xyz:port"

[monitoring]
node_operator_ids = [999999]

[telegram]
bot_token = 'xxxxxxxx-telegram-bot-token'
channel_id = '-123456'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pytest-asyncio
requests==2.32.3
toml==0.10.2
web3
python-telegram-bot
1 change: 1 addition & 0 deletions swarm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

parser_exit_monitor = subparsers.add_parser('auto_exit', help='Monitor for validator exit requests')
parser_exit_monitor.add_argument('--delete', action='store_true', help='automatically exit validators')
parser_exit_monitor.add_argument('--telegram', action='store_true', help='send telegram notifications')
parser_exit_monitor.set_defaults(func=exit.automated_exit)

args = parser.parse_args()
Expand Down
8 changes: 6 additions & 2 deletions swarm/exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ async def automated_exit(config: dict, args: Namespace) -> None:
csm = CSM(config)
handler = ExitHandler(config)


if args.telegram:
await handler.send_telegram_message('Automated exit monitor started')

async for exit_event in csm.exit_monitor():
pubkey = web3.Web3.to_hex(exit_event.args.validatorPubkey)
print(f'requested exit for validator with pubkey {pubkey}')
print(f'Requested exit for validator with pubkey {pubkey}')
if args.delete:
handler.exit(pubkey)
if args.telegram:
await handler.send_telegram_message(f'Requested exit for validator with pubkey {pubkey}')
12 changes: 12 additions & 0 deletions swarm/validator/exit_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from eth_typing import Address
import requests
import telegram

from swarm.exception import ExitSignException, ExitBroadcastException, ConsensusLayerRPCException
from ..validator.ssh_tunnel import SSHTunnel
Expand All @@ -25,6 +26,10 @@ def __init__(self, config: dict) -> None:
'ContentType': 'application/json'
}

if 'telegram' in config and 'bot_token' in config['telegram'] and 'channel_id' in config['telegram']:
self.telegram_bot_token = config['telegram']['bot_token']
self.telegram_channel_id = config['telegram']['channel_id']

def exit(self, pubkey: Address) -> None:
# try local validator
with SSHTunnel(self.keymanager_ssh, self.keymanager_port):
Expand All @@ -47,3 +52,10 @@ def exit(self, pubkey: Address) -> None:
raise(ExitBroadcastException('Could not publish signed exit message'))

print(f'published exit message for validator: {pubkey}')

async def send_telegram_message(self, message: str) -> None:
try:
bot = telegram.Bot(token=self.telegram_bot_token)
await bot.send_message(chat_id=self.telegram_channel_id, text=message)
except Exception as e:
print(f"Error sending Telegram message: {str(e)}")
Loading