-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bridge.js
84 lines (71 loc) · 3.27 KB
/
Bridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Load environment variables for secure configuration management.
require('dotenv').config();
const Web3 = require('web3');
// Updated ABIs to match the new contract structures
const { EthereumBridgeEntryABI, ZkSyncBridgeABI } = require('./abis');
const zkSyncRpcUrl = process.env.ZKSYNC_RPC_URL;
const ethereumRpcUrl = process.env.ETHEREUM_RPC_URL;
const zkSyncBridgeAddress = process.env.ZKSYNC_BRIDGE_CONTRACT_ADDRESS;
const ethereumBridgeAddress = process.env.ETHEREUM_BRIDGE_CONTRACT_ADDRESS;
const zkSyncWeb3 = new Web3(zkSyncRpcUrl);
const ethereumWeb3 = new Web3(ethereumRpcUrl);
zkSyncWeb3.eth.accounts.wallet.add(process.env.OWNER_PRIVATE_KEY);
ethereumWeb3.eth.accounts.wallet.add(process.env.OWNER_PRIVATE_KEY);
const zkSyncBridgeContract = new zkSyncWeb3.eth.Contract(ZkSyncBridgeABI, zkSyncBridgeAddress);
const ethereumBridgeContract = new ethereumWeb3.eth.Contract(EthereumBridgeEntryABI, ethereumBridgeAddress);
function handleError(error) {
console.error(error);
}
// Define a common function for sending transactions.
// This function encapsulates the steps required to estimate gas, sign, and send transactions, thereby reducing code redundancy.
async function sendTransaction(web3Instance, txObject, privateKey) {
try {
const gas = await web3Instance.eth.estimateGas(txObject);
const gasPrice = await web3Instance.eth.getGasPrice();
const tx = {
...txObject,
gas,
gasPrice
};
const signedTx = await web3Instance.eth.accounts.signTransaction(tx, privateKey);
await web3Instance.eth.sendSignedTransaction(signedTx.rawTransaction);
} catch (error) {
handleError(error);
}
}
// Update function names and logic to match the new contract methods
async function handleUnlockOnEthereum(recipient, amount) {
const unlockData = ethereumBridgeContract.methods.unlockTokens(recipient, amount).encodeABI();
const txObject = { to: ethereumBridgeAddress, data: unlockData };
await sendTransaction(ethereumWeb3, txObject, process.env.OWNER_PRIVATE_KEY);
}
async function handleUnlockOnZkSync(recipient, amount) {
const unlockData = zkSyncBridgeContract.methods.unlockTokens(recipient, amount).encodeABI();
const txObject = { to: zkSyncBridgeAddress, data: unlockData };
await sendTransaction(zkSyncWeb3, txObject, process.env.OWNER_PRIVATE_KEY);
}
// Define a function for polling new events.
// This function is essential for the Relayer to detect when tokens are locked on one chain and need to be unlocked on the other.
async function pollForNewEvents() {
try {
// Implement the logic to check for new events, such as locked tokens, since the last block.
} catch (error) {
handleError(error);
}
}
// Define the main loop of the application.
// This loop is the heart of the Relayer, constantly monitoring for events and handling them appropriately.
async function main() {
try {
while (true) {
await pollForNewEvents();
await new Promise(resolve => setTimeout(resolve, 5000)); // 5-second delay
}
} catch (error) {
handleError(error);
main(); // Restart main loop after an error
}
}
// Start the application.
// This is the entry point of the script, kicking off the Relayer's operations.
main();