Skip to content

Commit

Permalink
bot: add key param (#44)
Browse files Browse the repository at this point in the history
* add key param

* add resolve logic

* add L2_GAS_PRICE for executor

* fix import

---------

Co-authored-by: Vritra4 <[email protected]>
  • Loading branch information
JSHan94 and Vritra4 authored Apr 16, 2024
1 parent af1d0f3 commit 3d4dc7e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions bots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You should set `.env` file for each bot in `bots/worker`. To transfer assets bet
| L1_RPC_URI | L1 node RPC URI | <http://127.0.0.1:26657> |
| L2_LCD_URI | L2 node LCD URI | <http://127.0.0.1:1317> |
| L2_RPC_URI | L2 node RPC URI | <http://127.0.0.1:26657> |
| L2_GAS_PRICES | Gas prices for L2 chain | '0.15umin' |
| BRIDGE_ID | Bridge ID | '' |
| EXECUTOR_PORT | Executor port | 5000 |
| EXECUTOR_MNEMONIC | Mnemonic seed for executor | '' |
Expand Down
2 changes: 1 addition & 1 deletion bots/src/controller/batch/BatchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Validator
} from 'koa-joi-controllers'
import { responses, routeConfig, z } from 'koa-swagger-decorator'
import { success } from 'lib/response'
import { success } from '../../lib/response'
import { GetBatchResponse } from 'sawgger/batch_model'
import { getBatch } from 'service/batch/BatchService'

Expand Down
35 changes: 30 additions & 5 deletions bots/src/lib/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,44 @@ import * as https from 'https'
import UnconfirmedTxEntity from '../orm/executor/UnconfirmedTxEntity'
import { ChallengedOutputEntity } from '../orm/index'

const postedKeys = new Set<string>()

const ax = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
timeout: 15000
})

export async function notifySlack(text: { text: string }) {
if (config.SLACK_WEB_HOOK == '') return
await ax.post(config.SLACK_WEB_HOOK, text).catch(() => {
console.error('Slack Notification Error')
})
export async function notifySlack(key: string, text: { text: string }, isError: boolean = true) {
if (config.SLACK_WEB_HOOK === undefined || config.SLACK_WEB_HOOK === '') return

const keyExists = postedKeys.has(key)

if (isError) {
if (!keyExists) {
await ax.post(config.SLACK_WEB_HOOK, text)
postedKeys.add(key)
}
} else {
if (keyExists) {
await ax.post(config.SLACK_WEB_HOOK, text)
postedKeys.delete(key)
}
}
}

export function buildResolveErrorNotification(description: string): { text: string } {
let notification = '```'
notification += `[INFO] Error Resolved Notification\n`
notification += `\n`
notification += `${description}\n`
notification += '```'
return {
text: notification
}
}


export function buildNotEnoughBalanceNotification(
wallet: Wallet,
balance: number,
Expand Down
14 changes: 8 additions & 6 deletions bots/src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@initia/initia.js'
import { sendTx } from './tx'
import { config } from '../config'
import { buildNotEnoughBalanceNotification, notifySlack } from './slack'
import { buildNotEnoughBalanceNotification, buildResolveErrorNotification, notifySlack } from './slack'

export enum WalletType {
Challenger = 'challenger',
Expand Down Expand Up @@ -87,13 +87,15 @@ export class TxWallet extends Wallet {
denom
)

if (
balance.amount &&
parseInt(balance.amount) < config.SLACK_NOT_ENOUGH_BALANCE_THRESHOLD
) {
const key = `${this.key.accAddress}-${balance.amount}`;
if (balance.amount && parseInt(balance.amount) < config.SLACK_NOT_ENOUGH_BALANCE_THRESHOLD) {
await notifySlack(
buildNotEnoughBalanceNotification(this, parseInt(balance.amount), denom)
key, buildNotEnoughBalanceNotification(this, parseInt(balance.amount), denom)
)
} else {
await notifySlack(
key, buildResolveErrorNotification(`Balance for ${this.key.accAddress} is restored.`), false
);
}
}

Expand Down
10 changes: 5 additions & 5 deletions bots/src/worker/bridgeExecutor/Resurrector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DataSource } from 'typeorm'
import Bluebird from 'bluebird'
import winston from 'winston'
import { TxWallet, WalletType, getWallet, initWallet } from '../../lib/wallet'
import { buildFailedTxNotification, notifySlack } from '../../lib/slack'
import { buildFailedTxNotification, buildResolveErrorNotification, notifySlack } from '../../lib/slack'

export class Resurrector {
private db: DataSource
Expand Down Expand Up @@ -35,9 +35,8 @@ export class Resurrector {
)
}

async resubmitFailedDepositTx(
unconfirmedTx: UnconfirmedTxEntity
): Promise<void> {
async resubmitFailedDepositTx(unconfirmedTx: UnconfirmedTxEntity): Promise<void> {
const txKey = `${unconfirmedTx.sender}-${unconfirmedTx.receiver}-${unconfirmedTx.amount}`
const msg = new MsgFinalizeTokenDeposit(
this.executor.key.accAddress,
unconfirmedTx.sender,
Expand All @@ -51,13 +50,14 @@ export class Resurrector {
try {
await this.executor.transaction([msg])
await this.updateProcessed(unconfirmedTx)
await notifySlack(txKey, buildResolveErrorNotification(`[INFO] Transaction successfully resubmitted and processed for ${unconfirmedTx.sender} to ${unconfirmedTx.receiver} of amount ${unconfirmedTx.amount}.`), false)
} catch (err) {
if (this.errorCounter++ < 20) {
await Bluebird.delay(5 * 1000)
return
}
this.errorCounter = 0
await notifySlack(buildFailedTxNotification(unconfirmedTx))
await notifySlack(txKey, buildFailedTxNotification(unconfirmedTx))
}
}

Expand Down
6 changes: 3 additions & 3 deletions bots/src/worker/challenger/challenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getOutputInfoByIndex,
getBridgeInfo
} from '../../lib/query'
import MonitorHelper from 'lib/monitor/helper'
import MonitorHelper from '../../lib/monitor/helper'
import winston from 'winston'
import { TxWallet, WalletType, getWallet, initWallet } from '../../lib/wallet'
import { buildChallengerNotification, notifySlack } from '../../lib/slack'
Expand Down Expand Up @@ -313,7 +313,7 @@ export class Challenger {
await this.deleteOutputProposal(outputIndex)
}

await notifySlack(buildChallengerNotification(challengedOutput))
process.exit()
await notifySlack(`${outputIndex}-${this.bridgeId}`, buildChallengerNotification(challengedOutput));
process.exit();
}
}

0 comments on commit 3d4dc7e

Please sign in to comment.