Skip to content

Commit

Permalink
FIX: avoid api rate limiting by exit loop (#6)
Browse files Browse the repository at this point in the history
This will avoid a constant start/exit loop if the operator name/id is unknown.
  • Loading branch information
daverolo authored May 5, 2023
1 parent 592090b commit 133a658
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/functions/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const startLoop = async (init = false) => {
try {
if (!operatorName) {
logger.error(`No operator name given`);
process.exit(3);
// return;
await new Promise((resolve) => setTimeout(resolve, 15000));
return;
}
if (init) {
logger.info(`System ready, listening for state changes of ${operatorName} validators...`);
Expand All @@ -28,8 +28,8 @@ export const startLoop = async (init = false) => {

if (!operatorIndex) {
logger.error(`Operator ${operatorName} unknown`);
process.exit(3);
// return;
await new Promise((resolve) => setTimeout(resolve, 15000));
return;
}

const validatorChain = validatorChains.find((e) => e.key == kapiStatus.chainId);
Expand Down
52 changes: 36 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,47 @@ import schedule from "node-schedule";
import bot from "./config/bot";
import logger from "./config/logger";
import { startLoop } from "./functions/start";
import { GrammyError } from "grammy";

logger.info(`Starting Slashing Guard (with log level: ${logger.level})`);

bot.start({
onStart: async (info) => {
logger.info(`Starting Telegram bot @${info.username}`);
await bot.api.setMyCommands([
{
command: "start",
description: `Start bot ${info.username}`,
},
{ command: "status", description: `Show service status` },
{ command: "operator", description: `Show operator status` },
{
command: "stop",
description: `Stop bot ${info.username}`,
},
]);
await startLoop(true);
schedule.scheduleJob("*/12 * * * * *", async () => {
await startLoop();
});
try {
await bot.api.setMyCommands([
{
command: "start",
description: `Start bot ${info.username}`,
},
{ command: "status", description: `Show service status` },
{ command: "operator", description: `Show operator status` },
{
command: "stop",
description: `Stop bot ${info.username}`,
},
]);
await startLoop(true);
schedule.scheduleJob("*/12 * * * * *", async () => {
await startLoop();
});
} catch (error: unknown) {
if (error instanceof GrammyError) {
const errnum = error.error_code;
const errmsg = error.description;
const errmth = error.method;
const errmprm = error.parameters;
const bansec = errmprm.retry_after;
if (errnum == 429) {
logger.debug(
`App blocked by Telegram API for spamming too many requests - ${bansec} seconds left ([${errnum}]: ${errmsg})`
);
} else {
logger.debug(`Grammy Error [${errnum}]: ${errmsg} (Method: ${errmth}] | Parameters: ${errmprm}])`);
}
} else {
logger.error(error);
}
}
},
});

0 comments on commit 133a658

Please sign in to comment.