|
| 1 | +import { SlashCommandBuilder } from "discord.js"; |
| 2 | +import { ValidatorService } from "../services/validator-service.js"; |
| 3 | +import { ChainInfoService } from "../services/chaininfo-service.js"; |
| 4 | +export default { |
| 5 | + data: new SlashCommandBuilder() |
| 6 | + .setName("validator") |
| 7 | + .setDescription("Manage validator addresses") |
| 8 | + .addSubcommand((subcommand) => subcommand |
| 9 | + .setName("add") |
| 10 | + .setDescription("Add yourself to the validator set") |
| 11 | + .addStringOption((option) => option |
| 12 | + .setName("address") |
| 13 | + .setDescription("Your validator address"))) |
| 14 | + .addSubcommand((subcommand) => subcommand |
| 15 | + .setName("check") |
| 16 | + .setDescription("Check if you are a validator") |
| 17 | + .addStringOption((option) => option |
| 18 | + .setName("address") |
| 19 | + .setDescription("The validator address to check"))), |
| 20 | + execute: async (interaction) => { |
| 21 | + const address = interaction.options.getString("address"); |
| 22 | + if (!address) { |
| 23 | + return interaction.reply({ |
| 24 | + content: "Address is required.", |
| 25 | + ephemeral: true, |
| 26 | + }); |
| 27 | + } |
| 28 | + // Basic address validation |
| 29 | + if (!address.match(/^0x[a-fA-F0-9]{40}$/)) { |
| 30 | + return interaction.reply({ |
| 31 | + content: "Please provide a valid Ethereum address.", |
| 32 | + ephemeral: true, |
| 33 | + }); |
| 34 | + } |
| 35 | + await interaction.deferReply(); |
| 36 | + if (interaction.options.getSubcommand() === "add") { |
| 37 | + try { |
| 38 | + await ValidatorService.addValidator(address); |
| 39 | + await interaction.editReply({ |
| 40 | + content: `Successfully added validator address: ${address}`, |
| 41 | + }); |
| 42 | + } |
| 43 | + catch (error) { |
| 44 | + await interaction.editReply({ |
| 45 | + content: `Failed to add validator address: ${error instanceof Error ? error.message : String(error)}`, |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + else if (interaction.options.getSubcommand() === "check") { |
| 50 | + try { |
| 51 | + const info = await ChainInfoService.getInfo(); |
| 52 | + const { validators, committee } = info; |
| 53 | + let reply = ""; |
| 54 | + if (validators.includes(address)) { |
| 55 | + reply += "You are a validator\n"; |
| 56 | + } |
| 57 | + if (committee.includes(address)) { |
| 58 | + reply += "You are a committee member\n"; |
| 59 | + } |
| 60 | + await interaction.editReply({ |
| 61 | + content: reply, |
| 62 | + }); |
| 63 | + } |
| 64 | + catch (error) { |
| 65 | + await interaction.editReply({ |
| 66 | + content: `Failed to check validator address: ${error instanceof Error ? error.message : String(error)}`, |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | +}; |
0 commit comments