Skip to content

Commit

Permalink
fix as review
Browse files Browse the repository at this point in the history
  • Loading branch information
JSHan94 committed Dec 15, 2023
1 parent 4f34728 commit 45296c1
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 87 deletions.
8 changes: 6 additions & 2 deletions bots/.envrc_sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export USE_LOG_FILE=false
export EXECUTOR_PORT=5000
export BATCH_PORT=5001

# slack notification
export SLACK_WEB_HOOK=

# l2 setup (need challenger, output, executor mnemonic)
export SUBMISSION_INTERVAL=10000
export FINALIZED_TIME=10000
export FINALIZATION_PERIOD=10000
export IBC_METADATA='channel-1'

export L1_LCD_URI=http://localhost:1317
Expand All @@ -33,4 +36,5 @@ export BATCH_SUBMITTER_MNEMONIC=''
export CHALLENGER_MNEMONIC=''

# output submitter config
export OUTPUT_SUBMITTER_MNEMONIC=''
export OUTPUT_SUBMITTER_MNEMONIC=''
export DELETE_OUTPUT_PROPOSAL='false'
2 changes: 2 additions & 0 deletions bots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const msg = new MsgCreateBridge(executor.key.accAddress, bridgeConfig);
| BATCH_SUBMITTER_MNEMONIC | Mnemonic seed for submitter | '' |
| OUTPUT_SUBMITTER_MNEMONIC | Mnemonic seed for output submitter | '' |
| CHALLENGER_MNEMONIC | Mnemonic seed for challenger | '' |
| SLACK_WEB_HOOK | Slack web hook for notification | '' |


> In OPinit bots, we use [direnv](https://direnv.net) for managing environment variable for development. See [sample of .envrc](.envrc_sample).
Expand Down
100 changes: 85 additions & 15 deletions bots/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
},
"dependencies": {
"@initia/builder.js": "^0.0.9",
"@initia/initia.js": "^0.1.19",
"@initia/initia.js": "^0.1.22",
"@koa/cors": "^4.0.0",
"@sentry/node": "^7.60.0",
"@types/bluebird": "^3.5.38",
Expand Down
19 changes: 7 additions & 12 deletions bots/src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ const config = getConfig();
export async function getLastOutputInfo(
bridgeId: number
): Promise<OutputInfo | null> {
const [outputInfos, pagination] = await config.l1lcd.ophost.outputInfos(
bridgeId
const [outputInfos, _pagination] = await config.l1lcd.ophost.outputInfos(
bridgeId,
{
'pagination.limit': '1',
'pagination.reverse': 'true'
}
);
if (outputInfos.length === 0) return null;
return await config.l1lcd.ophost.outputInfo(bridgeId, pagination.total);
return outputInfos[0];
}

// get the output by index from L1 chain
Expand All @@ -40,15 +44,6 @@ export async function getBridgeInfo(bridgeId: number): Promise<BridgeInfo> {
return await config.l1lcd.ophost.bridgeInfo(bridgeId);
}

export async function getBalanceByDenom(
lcd: LCDClient,
account: string,
denom: string
): Promise<Coin | undefined> {
const [coins, _pagination] = await lcd.bank.balance(account);
return coins.get(denom);
}

export async function getTokenPairByL1Denom(denom: string): Promise<TokenPair> {
return await config.l1lcd.ophost.tokenPairByL1Denom(config.BRIDGE_ID, denom);
}
Expand Down
38 changes: 28 additions & 10 deletions bots/src/lib/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ import { getConfig } from 'config';
import * as http from 'http';
import * as https from 'https';
import FailedTxEntity from 'orm/executor/FailedTxEntity';
import { ChallengedOutputEntity } from 'orm/index';

const config = getConfig();

const { SLACK_WEB_HOOK } = process.env;

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 (SLACK_WEB_HOOK == undefined || SLACK_WEB_HOOK == '') return;
await ax.post(SLACK_WEB_HOOK, text).catch(() => {
console.error('Slack Notification Error');
});
}

export function buildNotEnoughBalanceNotification(
wallet: Wallet,
balance: number,
Expand Down Expand Up @@ -57,15 +71,19 @@ export function buildFailedTxNotification(data: FailedTxEntity): {
};
}

const ax = axios.create({
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
timeout: 15000
});
export function buildChallengerNotification(
challengedOutput: ChallengedOutputEntity
): { text: string } {
let notification = '```';
notification += `[WARN] Challenger Notification\n`;
notification += `\n`;
notification += `Bridge ID : ${challengedOutput.bridgeId}\n`;
notification += `OutputIndex : ${challengedOutput.outputIndex}\n`;
notification += `Reason : ${challengedOutput.reason}\n`;
notification += '```';
const text = `${notification}`;

export async function notifySlack(text: { text: string }) {
if (SLACK_WEB_HOOK == undefined || SLACK_WEB_HOOK == '') return;
await ax.post(SLACK_WEB_HOOK, text).catch(() => {
console.error('Slack Notification Error');
});
return {
text
};
}
12 changes: 6 additions & 6 deletions bots/src/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class WithdrawStorage {
Buffer.concat([
bridge_id_buf,
sequence_buf,
Buffer.from(AccAddress.toHex(tx.sender).replace('0x', ''), 'hex'),
Buffer.from(AccAddress.toHex(tx.receiver).replace('0x', ''), 'hex'),
AccAddress.toBuffer(tx.sender),
AccAddress.toBuffer(tx.receiver),
Buffer.from(tx.l1_denom, 'utf8'),
amount_buf
])
Expand Down Expand Up @@ -56,8 +56,8 @@ export class WithdrawStorage {
Buffer.concat([
bridge_id_buf,
sequence_buf,
Buffer.from(AccAddress.toHex(tx.sender).replace('0x', ''), 'hex'),
Buffer.from(AccAddress.toHex(tx.receiver).replace('0x', ''), 'hex'),
AccAddress.toBuffer(tx.sender),
AccAddress.toBuffer(tx.receiver),
Buffer.from(tx.l1_denom, 'utf8'),
amount_buf
])
Expand Down Expand Up @@ -90,8 +90,8 @@ export class WithdrawStorage {
Buffer.concat([
bridge_id_buf,
sequence_buf,
Buffer.from(AccAddress.toHex(tx.sender).replace('0x', ''), 'hex'),
Buffer.from(AccAddress.toHex(tx.receiver).replace('0x', ''), 'hex'),
AccAddress.toBuffer(tx.sender),
AccAddress.toBuffer(tx.receiver),
Buffer.from(tx.l1_denom, 'utf8'),
amount_buf
])
Expand Down
7 changes: 3 additions & 4 deletions bots/src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@initia/initia.js';
import { sendTx } from './tx';
import { getConfig } from 'config';
import { getBalanceByDenom } from './query';
import { buildNotEnoughBalanceNotification, notifySlack } from './slack';

const config = getConfig();
Expand Down Expand Up @@ -84,13 +83,13 @@ export class TxWallet extends Wallet {
async checkEnoughBalance() {
const gasPrices = new Coins(this.lcd.config.gasPrices);
const denom = gasPrices.denoms()[0];
const balance = await getBalanceByDenom(
this.lcd,

const balance = await this.lcd.bank.balanceByDenom(
this.key.accAddress,
denom
);

if (balance?.amount && parseInt(balance.amount) < 100_000_000) {
if (balance.amount && parseInt(balance.amount) < 100_000_000) {
await notifySlack(
buildNotEnoughBalanceNotification(this, parseInt(balance.amount), denom)
);
Expand Down
13 changes: 13 additions & 0 deletions bots/src/orm/challenger/ChallengeEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';

@Entity('challenge')
export default class ChallengeEntity {
@PrimaryColumn('text')
name: string;

@Column('int')
l1DepositSequenceToChallenge: number;

@Column('int')
l2OutputIndexToChallenge: number;
}
2 changes: 1 addition & 1 deletion bots/src/orm/challenger/DeletedOutputEntity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Column, Entity, Index, PrimaryColumn } from 'typeorm';

@Entity('challenger_deleted_output')
export default class ChallengerDeletedOutputEntity {
export default class ChallengedOutputEntity {
@PrimaryColumn('bigint')
outputIndex: number;

Expand Down
Loading

0 comments on commit 45296c1

Please sign in to comment.