Skip to content

Commit

Permalink
fix path and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
JSHan94 committed Apr 17, 2024
1 parent ba9b983 commit 47f5183
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/controller/batch/BatchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from 'koa-joi-controllers'
import { responses, routeConfig, z } from 'koa-swagger-decorator'
import { success } from '../../lib/response'
import { GetBatchResponse } from 'sawgger/batch_model'
import { getBatch } from 'service/batch/BatchService'
import { GetBatchResponse } from '../../swagger/batch_model'
import { getBatch } from '../../service/batch/BatchService'

const Joi = Validator.Joi

Expand Down
30 changes: 18 additions & 12 deletions src/lib/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,32 @@ const ax = axios.create({
timeout: 15000
})

export async function notifySlack(key: string, text: { text: string }, isError: boolean = true) {
if (config.SLACK_WEB_HOOK === undefined || config.SLACK_WEB_HOOK === '') return
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)
}
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)
}
if (keyExists) {
await ax.post(config.SLACK_WEB_HOOK, text)
postedKeys.delete(key)
}
}
}

export function buildResolveErrorNotification(description: string): { text: string } {
export function buildResolveErrorNotification(description: string): {
text: string;
} {
let notification = '```'
notification += `[INFO] Error Resolved Notification\n`
notification += `\n`
Expand All @@ -44,7 +51,6 @@ export function buildResolveErrorNotification(description: string): { text: stri
}
}


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

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

const key = `${this.key.accAddress}-${balance.amount}`;
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(
key, 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
);
key,
buildResolveErrorNotification(
`Balance for ${this.key.accAddress} is restored.`
),
false
)
}
}

Expand Down
36 changes: 0 additions & 36 deletions src/sawgger/batch_model.ts

This file was deleted.

36 changes: 36 additions & 0 deletions src/swagger/batch_model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { z } from 'koa-swagger-decorator'

const L1DataPathsSchema = z.array(
z.object({
index: z.number(),
txHash: z.string()
})
)

const L1BatchInfoSchema = z.object({
type: z.literal('l1'),
dataPaths: L1DataPathsSchema
})

const CelestiaDataPathsSchema = z.array(
z.object({
index: z.number(),
height: z.number(),
commitment: z.string()
})
)

const CelestiaBatchInfoSchema = z.object({
type: z.literal('celestia'),
dataPaths: CelestiaDataPathsSchema
})

const BatchInfoStruct = z.union([L1BatchInfoSchema, CelestiaBatchInfoSchema])

const GetBatchResponse = z.object({
bridge_id: z.number(),
batch_index: z.number(),
batch_info: BatchInfoStruct
})

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

export class Resurrector {
private db: DataSource
Expand Down Expand Up @@ -35,7 +39,9 @@ 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,
Expand All @@ -50,7 +56,13 @@ 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)
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)
Expand Down
7 changes: 5 additions & 2 deletions src/worker/challenger/challenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,10 @@ export class Challenger {
await this.deleteOutputProposal(outputIndex)
}

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

0 comments on commit 47f5183

Please sign in to comment.