Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: OPinit bot #10

Merged
merged 38 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7006080
remove docker setup
JSHan94 Nov 10, 2023
b23952c
add consts for testing
JSHan94 Dec 14, 2023
4f34728
remove blockSDK empty tx
JSHan94 Dec 14, 2023
45296c1
fix as review
JSHan94 Dec 15, 2023
6f64a06
change check order
JSHan94 Dec 15, 2023
22b87a1
convert .envrc to .env
JSHan94 Dec 15, 2023
4974623
add link
JSHan94 Dec 15, 2023
81b56d2
use .env.WORKER_NAME as default
JSHan94 Dec 15, 2023
42d3440
fix gas prices
JSHan94 Dec 15, 2023
e7d7d5c
add DELETE_OUTPUT_PROPOSAL on readme
JSHan94 Dec 18, 2023
23d6562
add notify on slack in challenger
JSHan94 Dec 18, 2023
c26be80
trace error by info
JSHan94 Dec 19, 2023
0dac345
colorize
JSHan94 Dec 19, 2023
6519049
add L2 gas prices and clean up
JSHan94 Dec 21, 2023
0273865
remove unsafe optional chaining
JSHan94 Dec 22, 2023
0341818
remove chaining and use try-catch
JSHan94 Dec 26, 2023
c77f82b
use winston-embeded format for logger
JSHan94 Jan 2, 2024
3093c16
sanitize DailyRotateFile
JSHan94 Jan 3, 2024
7ff492f
fix convention in lib and lintjob
Vritra4 Jan 3, 2024
8ce89e2
fix convention
Vritra4 Jan 3, 2024
f29ef62
Merge pull request #14 from initia-labs/fix/convention
JSHan94 Jan 3, 2024
71cbd49
lint
JSHan94 Jan 3, 2024
7c383c6
disable lint for js file
JSHan94 Jan 3, 2024
c8afb11
bump ecmascript to es2021
Vritra4 Jan 3, 2024
2559557
fix import
Vritra4 Jan 3, 2024
3eab5e4
remove warning by replacing deps
Vritra4 Jan 3, 2024
b77a8a0
remove unnecessary castings to any
Vritra4 Jan 3, 2024
8ac34a8
Merge pull request #15 from initia-labs/bump-ecma
JSHan94 Jan 3, 2024
82e8fcf
use swagger
JSHan94 Jan 5, 2024
5694be8
Merge branch 'main' of https://github.com/initia-labs/OPinit into fea…
JSHan94 Jan 5, 2024
7542a26
support v0.50
JSHan94 Jan 5, 2024
da38a7c
notify chain id
JSHan94 Jan 5, 2024
b47a91f
fix fetch evts
JSHan94 Jan 8, 2024
ac8f7ea
add axios
JSHan94 Jan 8, 2024
8bc30fd
add initia.js
JSHan94 Jan 8, 2024
c93dbf9
bump to initia.js 1.0.27
JSHan94 Jan 17, 2024
7d457c9
remove unused import
JSHan94 Jan 17, 2024
417b5b1
fix descending condition
JSHan94 Jan 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading