-
Notifications
You must be signed in to change notification settings - Fork 7
Feat/multi network relayer #415
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
a9c874b
feat: network support
7227bcc
chore: update tests
f85ca1a
chore: rename relayer
23775ee
fix: jest export module
bb145a7
chore: migrate web3 & batcher
8106d13
feat: batcher logs
058f60c
chore: batcher test update
786a113
fix: relay deps
0cb7037
Merge pull request #414 from kleros/dev
mani99brar 0ca8b88
chore(relayer): consts refactor
b5910b1
chore(relayer): dep bump
0c62fc7
chore(relayer): checks to support pm2
d34f076
feat: klerosV2 batcher
59a13cc
chore(relayer): deps bump
b92194a
chore(relayer): var cleanup
30f9a38
fix(relayer): typos
3e05b9c
fix(relayer): default case
d8d60e3
chore(relayer): refactor start function
b2b71b5
fix: sonar review
b219f6c
fix: env file
jaybuidl 4436e57
chore: minor
jaybuidl eed1d2e
chore(relayer): update typechain to ethers v6
42b6def
fix(relayer): clean up lock file on exits
37da5f8
fix(relayer): batcher env var name
776b01b
chore(relayer): add env var comments
b9c6550
Merge branch 'dev' into feat/multi-network-relayer
jaybuidl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| // Adapted from https://github.com/daostack/web3-transaction-batcher/blob/1b88d2ea062f8f2d9fdfdf9bbe85d2bbef780151/contracts/Batcher.sol | ||
|
|
||
| contract TransactionBatcher { | ||
| function batchSend(address[] memory targets, uint256[] memory values, bytes[] memory datas) public payable { | ||
| for (uint256 i = 0; i < targets.length; i++) { | ||
| (bool success, ) = targets[i].call{value: values[i]}(datas[i]); | ||
| if (!success) revert("transaction failed"); // All the calls must succeed. | ||
| } | ||
| } | ||
|
|
||
| function batchSendUnchecked( | ||
| address[] memory targets, | ||
| uint256[] memory values, | ||
| bytes[] memory datas | ||
| ) public payable { | ||
| for (uint256 i = 0; i < targets.length; i++) { | ||
| targets[i].call{value: values[i]}(datas[i]); // Intentionally ignoring return value. | ||
| } | ||
| } | ||
jaybuidl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,24 @@ | ||
| PRIVATE_KEY= | ||
|
|
||
| # Devnet Sender Address | ||
| DEVNET_SENDER=0x906dE43dBef27639b1688Ac46532a16dc07Ce410 | ||
| ### Sender Address | ||
| # Zero address is used for allowing all senders. | ||
| # No senders are used to disable network. | ||
| SENDER_ADDRESSES_DEVNET=0x906dE43dBef27639b1688Ac46532a16dc07Ce410,0x123456789abcdef123456789abcdef1234567890 | ||
| SENDER_ADDRESSES_TESTNET=0x0000000000000000000000000000000000000000 | ||
| SENDER_ADDRESS_MAINNET= | ||
|
|
||
| RPC_CHIADO=https://rpc.chiadochain.net | ||
| RPC_SEPOLIA= | ||
|
|
||
| VEAOUTBOX_CHAIN_ID=1115111 | ||
| VEAOUTBOX_CHAINS=11155111,10200 | ||
|
|
||
|
|
||
| VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS=0x906dE43dBef27639b1688Ac46532a16dc07Ce410 | ||
| VEAINBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C | ||
| VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS=0x906dE43dBef27639b1688Ac46532a16dc07Ce410 | ||
| VEAOUTBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS=0xAb53e341121448Ae259Da8fa17f216Cb0e21199C | ||
| # Subgraph endpoint, Example: "85918/vea-inbox-arb-sepolia-devnet/version/latest" | ||
| RELAYER_SUBGRAPH="61738/vea-inbox-arb-sepolia/version/latest" | ||
|
|
||
| # 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 Addresses | ||
| TRANSACTION_BATCHER_CONTRACT_SEPOLIA=0xe7953da7751063d0a41ba727c32c762d3523ade8 | ||
| TRANSACTION_BATCHER_CONTRACT_CHIADO=0xcC0a08D4BCC5f91ee9a1587608f7a2975EA75d73 | ||
|
|
||
| TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA=0xe7953da7751063d0a41ba727c32c762d3523ade8 | ||
| TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO=0xcC0a08D4BCC5f91ee9a1587608f7a2975EA75d73 | ||
|
|
||
| STATE_DIR="/home/user/vea/relayer-cli/state" | ||
| # Ensure the path ends with a trailing slash ("/") | ||
| STATE_DIR="/home/user/vea/relayer-cli/state/" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,88 @@ | ||
| // File for handling contants and configurations | ||
| require("dotenv").config(); | ||
| import veaOutboxArbToEthContract from "@kleros/vea-contracts/deployments/sepolia/VeaOutboxArbToEthTestnet.json"; | ||
| import veaOutboxArbToGnosisContract from "@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisTestnet.json"; | ||
| import veaInboxArbToEthDevnet from "@kleros/vea-contracts/deployments/arbitrumSepolia/VeaInboxArbToEthDevnet.json"; | ||
| import veaOutboxArbToEthDevnet from "@kleros/vea-contracts/deployments/sepolia/VeaOutboxArbToEthDevnet.json"; | ||
| import veaInboxArbToEthTestnet from "@kleros/vea-contracts/deployments/arbitrumSepolia/VeaInboxArbToEthTestnet.json"; | ||
| import veaOutboxArbToEthTestnet from "@kleros/vea-contracts/deployments/sepolia/VeaOutboxArbToEthTestnet.json"; | ||
|
|
||
| import veaInboxArbToGnosisDevnet from "@kleros/vea-contracts/deployments/arbitrumSepolia/VeaInboxArbToGnosisDevnet.json"; | ||
| import veaOutboxArbToGnosisDevnet from "@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisDevnet.json"; | ||
|
|
||
| import veaInboxArbToGnosisTestnet from "@kleros/vea-contracts/deployments/arbitrumSepolia/VeaInboxArbToGnosisTestnet.json"; | ||
| import veaOutboxArbToGnosisTestnet from "@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisTestnet.json"; | ||
|
|
||
| interface IBridge { | ||
| chainId: number; | ||
| chain: string; | ||
| epochPeriod: number; | ||
| veaInboxAddress: string; | ||
| veaOutboxAddress: string; | ||
| batcher: string; | ||
| veaContracts: { [key in Network]: VeaContracts }; | ||
| batcherAddress: string; | ||
| rpcOutbox: string; | ||
| veaOutboxContract: any; | ||
| } | ||
|
|
||
| type VeaContracts = { | ||
| veaInbox: any; | ||
| veaOutbox: any; | ||
| }; | ||
|
|
||
| enum Network { | ||
| DEVNET = "devnet", | ||
| TESTNET = "testnet", | ||
| } | ||
|
|
||
| const arbToEthContracts: { [key in Network]: VeaContracts } = { | ||
| [Network.DEVNET]: { | ||
| veaInbox: veaInboxArbToEthDevnet, | ||
| veaOutbox: veaOutboxArbToEthDevnet, | ||
| }, | ||
| [Network.TESTNET]: { | ||
| veaInbox: veaInboxArbToEthTestnet, | ||
| veaOutbox: veaOutboxArbToEthTestnet, | ||
| }, | ||
| }; | ||
|
|
||
| const arbToGnosisContracts: { [key in Network]: VeaContracts } = { | ||
| [Network.DEVNET]: { | ||
| veaInbox: veaInboxArbToGnosisDevnet, | ||
| veaOutbox: veaOutboxArbToGnosisDevnet, | ||
| }, | ||
| [Network.TESTNET]: { | ||
| veaInbox: veaInboxArbToGnosisTestnet, | ||
| veaOutbox: veaOutboxArbToGnosisTestnet, | ||
| }, | ||
| }; | ||
|
|
||
| // Using destination chainId to get the route configuration. | ||
| const bridges: { [chainId: number]: IBridge } = { | ||
| 11155111: { | ||
| chainId: 11155111, | ||
| chain: "sepolia", | ||
| epochPeriod: 7200, | ||
| veaInboxAddress: process.env.VEAINBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS, | ||
| veaOutboxAddress: process.env.VEAOUTBOX_ARBSEPOLIA_TO_SEPOLIA_ADDRESS, | ||
| batcher: process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_SEPOLIA, | ||
| rpcOutbox: process.env.RPC_SEPOLIA, | ||
| veaOutboxContract: veaOutboxArbToEthContract, | ||
| veaContracts: arbToEthContracts, | ||
| batcherAddress: process.env.TRANSACTION_BATCHER_CONTRACT_SEPOLIA!, | ||
| rpcOutbox: process.env.RPC_SEPOLIA!, | ||
| }, | ||
| 10200: { | ||
| chainId: 10200, | ||
| chain: "chiado", | ||
| epochPeriod: 3600, | ||
| veaInboxAddress: process.env.VEAINBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS, | ||
| veaOutboxAddress: process.env.VEAOUTBOX_ARBSEPOLIA_TO_CHIADO_ADDRESS, | ||
| batcher: process.env.TRANSACTION_BATCHER_CONTRACT_ADDRESS_CHIADO, | ||
| rpcOutbox: process.env.RPC_CHIADO, | ||
| veaOutboxContract: veaOutboxArbToGnosisContract, | ||
| veaContracts: arbToGnosisContracts, | ||
| batcherAddress: process.env.TRANSACTION_BATCHER_CONTRACT_CHIADO!, | ||
| rpcOutbox: process.env.RPC_CHIADO!, | ||
| }, | ||
| }; | ||
jaybuidl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Getters | ||
| const getBridgeConfig = (chainId: number): IBridge | undefined => { | ||
| return bridges[chainId]; | ||
| const getBridgeConfig = (chainId: number): IBridge => { | ||
| const bridge = bridges[chainId]; | ||
| if (!bridge) throw new Error(`Unsupported chainId: ${chainId}`); | ||
| return bridge; | ||
| }; | ||
|
|
||
| const getEpochPeriod = (chainId: number): number => { | ||
| return bridges[chainId].epochPeriod; | ||
| }; | ||
|
|
||
| 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_CHIADO_SUBGRAPH; | ||
| default: | ||
| throw new Error("Invalid chainId"); | ||
| } | ||
| const bridge = bridges[chainId]; | ||
| if (!bridge.epochPeriod) throw new Error(`Unsupported chainId: ${chainId}`); | ||
| return bridge.epochPeriod; | ||
| }; | ||
|
|
||
| export { getBridgeConfig, getInboxSubgraph, getEpochPeriod }; | ||
| export { getBridgeConfig, getEpochPeriod, Network }; | ||
This file was deleted.
Oops, something went wrong.
jaybuidl marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| require("dotenv").config(); | ||
| import { EventEmitter } from "node:events"; | ||
| import { ethers } from "ethers"; | ||
| import { relayBatch, relayAllFrom } from "./utils/relay"; | ||
| import { | ||
| initialize as initializeNonce, | ||
| updateStateFile, | ||
| delay, | ||
| setupExitHandlers, | ||
| ShutdownManager, | ||
| getNetworkConfig, | ||
| RelayerNetworkConfig, | ||
| } from "./utils/relayerHelpers"; | ||
| import { initialize as initializeEmitter } from "./utils/logger"; | ||
| import { BotEvents } from "./utils/botEvents"; | ||
| import { getEpochPeriod, Network } from "./consts/bridgeRoutes"; | ||
|
|
||
| interface RelayerConfig { | ||
| networkConfigs: RelayerNetworkConfig[]; | ||
| shutdownManager: ShutdownManager; | ||
| emitter: EventEmitter; | ||
| } | ||
|
|
||
| /** | ||
| * Start the relayer | ||
| * @param config.networkConfigs The network configurations retrieved from the env. | ||
| * @param config.shutdownManager The shutdown manager | ||
| * @param config.emitter The event emitter | ||
| */ | ||
| export async function start({ networkConfigs, shutdownManager, emitter }: RelayerConfig) { | ||
| initializeEmitter(emitter); | ||
| let delayAmount = 7200 * 1000; // 2 hours in ms | ||
| while (!shutdownManager.getIsShuttingDown()) { | ||
| for (const networkConfig of networkConfigs) { | ||
| delayAmount = await processNetworkConfig(networkConfig, shutdownManager, emitter, delayAmount); | ||
jaybuidl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| emitter.emit(BotEvents.WAITING, delayAmount); | ||
| await delay(delayAmount); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Process the network configuration | ||
| * @param networkConfig The network configuration | ||
| * @param shutdownManager The shutdown manager | ||
| * @param emitter The event emitter | ||
| * @param currentDelay The current delay | ||
| * @returns The new delay | ||
| */ | ||
| async function processNetworkConfig( | ||
| networkConfig: RelayerNetworkConfig, | ||
| shutdownManager: ShutdownManager, | ||
| emitter: EventEmitter, | ||
| currentDelay: number | ||
| ): Promise<number> { | ||
| const { chainId, network, senders } = networkConfig; | ||
| emitter.emit(BotEvents.STARTED, chainId, network); | ||
| const maxBatchSize = 10; // 10 messages per batch | ||
|
|
||
| await setupExitHandlers(chainId, shutdownManager, network, emitter); | ||
|
|
||
| let nonce = await initializeNonce(chainId, network, emitter); | ||
| if (nonce == null) return currentDelay; | ||
|
|
||
| const toRelayAll = senders[0] === ethers.ZeroAddress; | ||
| nonce = toRelayAll | ||
| ? await relayBatch({ chainId, network, nonce, maxBatchSize, emitter }) | ||
| : await relayAllFrom(chainId, network, nonce, senders, emitter); | ||
|
|
||
| if (nonce == null) return currentDelay; | ||
|
|
||
| await updateStateFile(chainId, Math.floor(Date.now() / 1000), nonce, network, emitter); | ||
|
|
||
| if (network === Network.DEVNET) { | ||
| return 1000 * 10; // 10 seconds for devnet | ||
| } else { | ||
jaybuidl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const currentTS = Math.floor(Date.now() / 1000); | ||
| const epochPeriod = getEpochPeriod(chainId); | ||
| const timeLeft = (epochPeriod - (Math.floor(currentTS / 1000) % epochPeriod)) * 1000 + 100 * 1000; | ||
| return Math.min(currentDelay, timeLeft); | ||
| } | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| const emitter = new EventEmitter(); | ||
| const shutdownManager = new ShutdownManager(false); | ||
| const networkConfigs = getNetworkConfig(); | ||
| const testnetRelayerConfig: RelayerConfig = { | ||
| networkConfigs, | ||
| shutdownManager, | ||
| emitter, | ||
| }; | ||
|
|
||
| start(testnetRelayerConfig); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.