Skip to content

Commit f01e4f9

Browse files
Mani BrarMani Brar
Mani Brar
authored and
Mani Brar
committed
feat(config): refactor env vars and constants
1 parent 83b892c commit f01e4f9

File tree

6 files changed

+78
-44
lines changed

6 files changed

+78
-44
lines changed

relayer-cli/.env.dist

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ VEAINBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C
88
VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS=0x906dE43dBef27639b1688Ac46532a16dc07Ce410
99
VEAOUTBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C
1010

11+
# Subgraph endpoints, Example: "85918/vea-inbox-arb-sepolia-devnet/version/latest"
12+
VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_SUBGRAPH=11111/your-subgraph/version/your-version
13+
VEAINBOX_ARBSEPOLIA_TO_CHIADO_SUBGRAPH=11111/your-subgraph/version/your-version
14+
1115
TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA=0xe7953da7751063d0a41ba727c32c762d3523ade8
1216
TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO=0xcC0a08D4BCC5f91ee9a1587608f7a2975EA75d73
1317

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// File for handling contants and configurations
2+
require("dotenv").config();
3+
4+
interface IBridge {
5+
chainId: number;
6+
veaInbox: string;
7+
veaOutbox: string;
8+
batcher: string;
9+
rpcOutbox: string;
10+
}
11+
12+
// Using destination chainId to get the route configuration.
13+
const bridges: { [chainId: number]: IBridge } = {
14+
11155111: {
15+
chainId: 11155111,
16+
veaInbox: process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS,
17+
veaOutbox: process.env.VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS,
18+
batcher: process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA,
19+
rpcOutbox: process.env.RPC_SEPOLIA,
20+
},
21+
10200: {
22+
chainId: 10200,
23+
veaInbox: process.env.VEAINBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS,
24+
veaOutbox: process.env.VEAOUTBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS,
25+
batcher: process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO,
26+
rpcOutbox: process.env.RPC_CHIADO,
27+
},
28+
};
29+
30+
const getBridgeConfig = (chainId: number): IBridge | undefined => {
31+
return bridges[chainId];
32+
};
33+
34+
const getInboxSubgraph = (chainId: number): string => {
35+
switch (chainId) {
36+
case 11155111:
37+
return process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_SUBGRAPH;
38+
case 10200:
39+
return process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_SUBGRAPH;
40+
default:
41+
throw new Error("Invalid chainid");
42+
}
43+
};
44+
45+
export { getBridgeConfig, getInboxSubgraph };

relayer-cli/src/devnetRelayExample.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { relayAllFrom } from "./utils/relay";
22
import * as fs from "fs";
3-
require("dotenv").config();
43

54
// let chain_ids = [5, 10200];
65
let chain_ids = [11155111];

relayer-cli/src/state/11155111.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ts": 1725086460,
3+
"nonce": "6"
4+
}

relayer-cli/src/utils/proof.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import request from "graphql-request";
2+
import { getInboxSubgraph } from "../consts/bridgeRoutes";
23

34
const getMessageDataToRelay = async (chainid: number, nonce: number) => {
45
try {
5-
const subgraph = getSubgraph(chainid);
6+
const subgraph = getInboxSubgraph(chainid);
67

78
const result = await request(
8-
`https://api.studio.thegraph.com/query/85918/${subgraph}/version/latest`,
9+
`https://api.studio.thegraph.com/query/${subgraph}`,
910
`{
1011
messageSents(first: 5, where: {nonce: ${nonce}}) {
1112
nonce
@@ -37,9 +38,9 @@ const getProofAtCount = async (chainid: number, nonce: number, count: number): P
3738
query += "}";
3839

3940
try {
40-
const subgraph = getSubgraph(chainid);
41+
const subgraph = getInboxSubgraph(chainid);
4142

42-
const result = await request(`https://api.studio.thegraph.com/query/85918/${subgraph}/version/latest`, query);
43+
const result = await request(`https://api.studio.thegraph.com/query/${subgraph}`, query);
4344

4445
const proof = [];
4546

@@ -75,13 +76,4 @@ const getProofIndices = (nonce: number, count: number) => {
7576
return proof;
7677
};
7778

78-
const getSubgraph = (chainid: number): string => {
79-
switch (chainid) {
80-
case 11155111:
81-
return "vea-inbox-arb-sepolia-devnet";
82-
default:
83-
throw new Error("Invalid chainid");
84-
}
85-
};
86-
87-
export { getProofAtCount, getSubgraph, getMessageDataToRelay };
79+
export { getProofAtCount, getMessageDataToRelay };

relayer-cli/src/utils/relay.ts

+19-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { getProofAtCount, getMessageDataToRelay, getSubgraph } from "./proof";
1+
import { getProofAtCount, getMessageDataToRelay } from "./proof";
22
import { getVeaOutboxArbToEth } from "./ethers";
33
import request from "graphql-request";
4-
import { VeaInboxArbToEth, VeaOutboxArbToEth } from "@kleros/vea-contracts/typechain-types";
4+
import { VeaOutboxArbToEth } from "@kleros/vea-contracts/typechain-types";
5+
import { getBridgeConfig, getInboxSubgraph } from "../consts/bridgeRoutes";
56
const fs = require("fs");
67

78
require("dotenv").config();
@@ -10,22 +11,12 @@ const Web3 = require("web3");
1011
const _batchedSend = require("web3-batched-send");
1112
const _contract = require("@kleros/vea-contracts/deployments/sepolia/VeaOutboxArbToEthDevnet.json");
1213

13-
const getParams = (chainid: number): [string, string, string] => {
14-
if (chainid !== 11155111) throw new Error("Invalid chainid");
15-
16-
return [
17-
process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA,
18-
process.env.VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS,
19-
process.env.RPC_SEPOLIA,
20-
];
21-
};
22-
2314
const getCount = async (veaOutbox: VeaOutboxArbToEth, chainid: number): Promise<number> => {
24-
const subgraph = getSubgraph(chainid);
15+
const subgraph = getInboxSubgraph(chainid);
2516
const stateRoot = await veaOutbox.stateRoot();
2617

2718
const result = await request(
28-
`https://api.studio.thegraph.com/query/85918/${subgraph}/version/latest`,
19+
`https://api.studio.thegraph.com/query/${subgraph}`,
2920
`{
3021
snapshotSaveds(first: 1, where: { stateRoot: "${stateRoot}" }) {
3122
count
@@ -39,9 +30,9 @@ const getCount = async (veaOutbox: VeaOutboxArbToEth, chainid: number): Promise<
3930
};
4031

4132
const relay = async (chainid: number, nonce: number) => {
42-
const [TRANSACTION_BATCHER_CONTRACT_ADDRESS, VEAOUTBOX_ADDRESS, RPC_VEAOUTBOX] = getParams(chainid);
33+
const routeParams = getBridgeConfig(chainid);
4334

44-
const veaOutbox = getVeaOutboxArbToEth(VEAOUTBOX_ADDRESS, process.env.PRIVATE_KEY, RPC_VEAOUTBOX);
35+
const veaOutbox = getVeaOutboxArbToEth(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox);
4536
const count = await getCount(veaOutbox, chainid);
4637

4738
const proof = await getProofAtCount(chainid, nonce, count);
@@ -52,13 +43,13 @@ const relay = async (chainid: number, nonce: number) => {
5243
};
5344

5445
const relayBatch = async (chainid: number, nonce: number, iterations: number) => {
55-
const [TRANSACTION_BATCHER_CONTRACT_ADDRESS, VEAOUTBOX_ADDRESS, RPC_VEAOUTBOX] = getParams(chainid);
46+
const routeParams = getBridgeConfig(chainid);
5647

57-
const web3 = new Web3(RPC_VEAOUTBOX);
58-
const batchedSend = _batchedSend(web3, TRANSACTION_BATCHER_CONTRACT_ADDRESS, process.env.PRIVATE_KEY, 0);
48+
const web3 = new Web3(routeParams.rpcOutbox);
49+
const batchedSend = _batchedSend(web3, routeParams.rpcOutbox, process.env.PRIVATE_KEY, 0);
5950

60-
const contract = new web3.eth.Contract(_contract.abi, VEAOUTBOX_ADDRESS);
61-
const veaOutbox = getVeaOutboxArbToEth(VEAOUTBOX_ADDRESS, process.env.PRIVATE_KEY, RPC_VEAOUTBOX);
51+
const contract = new web3.eth.Contract(_contract.abi, routeParams.veaOutbox);
52+
const veaOutbox = getVeaOutboxArbToEth(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox);
6253
const count = await getCount(veaOutbox, chainid);
6354

6455
let txns = [];
@@ -73,24 +64,23 @@ const relayBatch = async (chainid: number, nonce: number, iterations: number) =>
7364
});
7465
}
7566

76-
console.log(txns);
7767
await batchedSend(txns);
7868
};
7969

8070
const relayAllFrom = async (chainid: number, nonce: number, msgSender: string): Promise<number> => {
81-
const [TRANSACTION_BATCHER_CONTRACT_ADDRESS, VEAOUTBOX_ADDRESS, RPC_VEAOUTBOX] = getParams(chainid);
71+
const routeParams = getBridgeConfig(chainid);
8272

83-
const web3 = new Web3(RPC_VEAOUTBOX);
73+
const web3 = new Web3(routeParams.rpcOutbox);
8474
const batchedSend = _batchedSend(
8575
web3, // Your web3 object.
8676
// The address of the transaction batcher contract you wish to use. The addresses for the different networks are listed below. If the one you need is missing, feel free to deploy it yourself and make a PR to save the address here for others to use.
87-
TRANSACTION_BATCHER_CONTRACT_ADDRESS,
77+
routeParams.batcher,
8878
process.env.PRIVATE_KEY, // The private key of the account you want to send transactions from.
8979
0 // The debounce timeout period in milliseconds in which transactions are batched.
9080
);
9181

92-
const contract = new web3.eth.Contract(_contract.abi, VEAOUTBOX_ADDRESS);
93-
const veaOutbox = getVeaOutboxArbToEth(VEAOUTBOX_ADDRESS, process.env.PRIVATE_KEY, RPC_VEAOUTBOX);
82+
const contract = new web3.eth.Contract(_contract.abi, routeParams.veaOutbox);
83+
const veaOutbox = getVeaOutboxArbToEth(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox);
9484
const count = await getCount(veaOutbox, chainid);
9585

9686
if (!count) return null;
@@ -116,10 +106,10 @@ const relayAllFrom = async (chainid: number, nonce: number, msgSender: string):
116106

117107
const getNonceFrom = async (chainid: number, nonce: number, msgSender: string) => {
118108
try {
119-
const subgraph = getSubgraph(chainid);
109+
const subgraph = getInboxSubgraph(chainid);
120110

121111
const result = await request(
122-
`https://api.studio.thegraph.com/query/85918/${subgraph}/version/latest`,
112+
`https://api.studio.thegraph.com/query/${subgraph}`,
123113
`{
124114
messageSents(first: 1000, where: {nonce_gte: ${nonce}, msgSender_: {id: "${msgSender}"}}, orderBy: nonce, orderDirection: asc) {
125115
nonce

0 commit comments

Comments
 (0)