Skip to content

Commit

Permalink
feat(config): refactor env vars and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Mani Brar authored and Mani Brar committed Aug 31, 2024
1 parent 83b892c commit f01e4f9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 44 deletions.
4 changes: 4 additions & 0 deletions relayer-cli/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ VEAINBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C
VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS=0x906dE43dBef27639b1688Ac46532a16dc07Ce410
VEAOUTBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C

# Subgraph endpoints, Example: "85918/vea-inbox-arb-sepolia-devnet/version/latest"
VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_SUBGRAPH=11111/your-subgraph/version/your-version
VEAINBOX_ARBSEPOLIA_TO_CHIADO_SUBGRAPH=11111/your-subgraph/version/your-version

TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA=0xe7953da7751063d0a41ba727c32c762d3523ade8
TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO=0xcC0a08D4BCC5f91ee9a1587608f7a2975EA75d73

Expand Down
45 changes: 45 additions & 0 deletions relayer-cli/src/consts/bridgeRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// File for handling contants and configurations
require("dotenv").config();

interface IBridge {
chainId: number;
veaInbox: string;
veaOutbox: string;
batcher: string;
rpcOutbox: string;
}

// Using destination chainId to get the route configuration.
const bridges: { [chainId: number]: IBridge } = {
11155111: {
chainId: 11155111,
veaInbox: process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS,
veaOutbox: process.env.VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS,
batcher: process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA,
rpcOutbox: process.env.RPC_SEPOLIA,
},
10200: {
chainId: 10200,
veaInbox: process.env.VEAINBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS,
veaOutbox: process.env.VEAOUTBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS,
batcher: process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO,
rpcOutbox: process.env.RPC_CHIADO,
},
};

const getBridgeConfig = (chainId: number): IBridge | undefined => {
return bridges[chainId];
};

const getInboxSubgraph = (chainId: number): string => {
switch (chainId) {
case 11155111:
return process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_SUBGRAPH;
case 10200:
return process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_SUBGRAPH;
default:
throw new Error("Invalid chainid");
}
};

export { getBridgeConfig, getInboxSubgraph };
1 change: 0 additions & 1 deletion relayer-cli/src/devnetRelayExample.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { relayAllFrom } from "./utils/relay";
import * as fs from "fs";
require("dotenv").config();

// let chain_ids = [5, 10200];
let chain_ids = [11155111];
Expand Down
4 changes: 4 additions & 0 deletions relayer-cli/src/state/11155111.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ts": 1725086460,
"nonce": "6"
}
20 changes: 6 additions & 14 deletions relayer-cli/src/utils/proof.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import request from "graphql-request";
import { getInboxSubgraph } from "../consts/bridgeRoutes";

const getMessageDataToRelay = async (chainid: number, nonce: number) => {
try {
const subgraph = getSubgraph(chainid);
const subgraph = getInboxSubgraph(chainid);

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

try {
const subgraph = getSubgraph(chainid);
const subgraph = getInboxSubgraph(chainid);

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

const proof = [];

Expand Down Expand Up @@ -75,13 +76,4 @@ const getProofIndices = (nonce: number, count: number) => {
return proof;
};

const getSubgraph = (chainid: number): string => {
switch (chainid) {
case 11155111:
return "vea-inbox-arb-sepolia-devnet";
default:
throw new Error("Invalid chainid");
}
};

export { getProofAtCount, getSubgraph, getMessageDataToRelay };
export { getProofAtCount, getMessageDataToRelay };
48 changes: 19 additions & 29 deletions relayer-cli/src/utils/relay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getProofAtCount, getMessageDataToRelay, getSubgraph } from "./proof";
import { getProofAtCount, getMessageDataToRelay } from "./proof";
import { getVeaOutboxArbToEth } from "./ethers";
import request from "graphql-request";
import { VeaInboxArbToEth, VeaOutboxArbToEth } from "@kleros/vea-contracts/typechain-types";
import { VeaOutboxArbToEth } from "@kleros/vea-contracts/typechain-types";
import { getBridgeConfig, getInboxSubgraph } from "../consts/bridgeRoutes";
const fs = require("fs");

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

const getParams = (chainid: number): [string, string, string] => {
if (chainid !== 11155111) throw new Error("Invalid chainid");

return [
process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA,
process.env.VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS,
process.env.RPC_SEPOLIA,
];
};

const getCount = async (veaOutbox: VeaOutboxArbToEth, chainid: number): Promise<number> => {
const subgraph = getSubgraph(chainid);
const subgraph = getInboxSubgraph(chainid);
const stateRoot = await veaOutbox.stateRoot();

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

const relay = async (chainid: number, nonce: number) => {
const [TRANSACTION_BATCHER_CONTRACT_ADDRESS, VEAOUTBOX_ADDRESS, RPC_VEAOUTBOX] = getParams(chainid);
const routeParams = getBridgeConfig(chainid);

const veaOutbox = getVeaOutboxArbToEth(VEAOUTBOX_ADDRESS, process.env.PRIVATE_KEY, RPC_VEAOUTBOX);
const veaOutbox = getVeaOutboxArbToEth(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox);
const count = await getCount(veaOutbox, chainid);

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

const relayBatch = async (chainid: number, nonce: number, iterations: number) => {
const [TRANSACTION_BATCHER_CONTRACT_ADDRESS, VEAOUTBOX_ADDRESS, RPC_VEAOUTBOX] = getParams(chainid);
const routeParams = getBridgeConfig(chainid);

const web3 = new Web3(RPC_VEAOUTBOX);
const batchedSend = _batchedSend(web3, TRANSACTION_BATCHER_CONTRACT_ADDRESS, process.env.PRIVATE_KEY, 0);
const web3 = new Web3(routeParams.rpcOutbox);
const batchedSend = _batchedSend(web3, routeParams.rpcOutbox, process.env.PRIVATE_KEY, 0);

const contract = new web3.eth.Contract(_contract.abi, VEAOUTBOX_ADDRESS);
const veaOutbox = getVeaOutboxArbToEth(VEAOUTBOX_ADDRESS, process.env.PRIVATE_KEY, RPC_VEAOUTBOX);
const contract = new web3.eth.Contract(_contract.abi, routeParams.veaOutbox);
const veaOutbox = getVeaOutboxArbToEth(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox);
const count = await getCount(veaOutbox, chainid);

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

console.log(txns);
await batchedSend(txns);
};

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

const web3 = new Web3(RPC_VEAOUTBOX);
const web3 = new Web3(routeParams.rpcOutbox);
const batchedSend = _batchedSend(
web3, // Your web3 object.
// 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.
TRANSACTION_BATCHER_CONTRACT_ADDRESS,
routeParams.batcher,
process.env.PRIVATE_KEY, // The private key of the account you want to send transactions from.
0 // The debounce timeout period in milliseconds in which transactions are batched.
);

const contract = new web3.eth.Contract(_contract.abi, VEAOUTBOX_ADDRESS);
const veaOutbox = getVeaOutboxArbToEth(VEAOUTBOX_ADDRESS, process.env.PRIVATE_KEY, RPC_VEAOUTBOX);
const contract = new web3.eth.Contract(_contract.abi, routeParams.veaOutbox);
const veaOutbox = getVeaOutboxArbToEth(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox);
const count = await getCount(veaOutbox, chainid);

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

const getNonceFrom = async (chainid: number, nonce: number, msgSender: string) => {
try {
const subgraph = getSubgraph(chainid);
const subgraph = getInboxSubgraph(chainid);

const result = await request(
`https://api.studio.thegraph.com/query/85918/${subgraph}/version/latest`,
`https://api.studio.thegraph.com/query/${subgraph}`,
`{
messageSents(first: 1000, where: {nonce_gte: ${nonce}, msgSender_: {id: "${msgSender}"}}, orderBy: nonce, orderDirection: asc) {
nonce
Expand Down

0 comments on commit f01e4f9

Please sign in to comment.