-
Notifications
You must be signed in to change notification settings - Fork 2k
/
crosschain.js
59 lines (44 loc) · 2.15 KB
/
crosschain.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
import { ethers } from "ethers";
// 初始化两条链的provider
const providerGoerli = new ethers.JsonRpcProvider("Goerli_Provider_URL");
const providerSepolia = new ethers.JsonRpcProvider("Sepolia_Provider_URL://eth-sepolia.g.alchemy.com/v2/RgxsjQdKTawszh80TpJ-14Y8tY7cx5W2");
// 初始化两条链的signer
// privateKey填管理者钱包的私钥
const privateKey = "Your_Key";
const walletGoerli = new ethers.Wallet(privateKey, providerGoerli);
const walletSepolia = new ethers.Wallet(privateKey, providerSepolia);
// 合约地址和ABI
const contractAddressGoerli = "0xa2950F56e2Ca63bCdbA422c8d8EF9fC19bcF20DD";
const contractAddressSepolia = "0xad20993E1709ed13790b321bbeb0752E50b8Ce69";
const abi = [
"event Bridge(address indexed user, uint256 amount)",
"function bridge(uint256 amount) public",
"function mint(address to, uint amount) external",
];
// 初始化合约实例
const contractGoerli = new ethers.Contract(contractAddressGoerli, abi, walletGoerli);
const contractSepolia = new ethers.Contract(contractAddressSepolia, abi, walletSepolia);
const main = async () => {
try{
console.log(`开始监听跨链事件`)
// 监听chain Sepolia的Bridge事件,然后在Goerli上执行mint操作,完成跨链
contractSepolia.on("Bridge", async (user, amount) => {
console.log(`Bridge event on Chain Sepolia: User ${user} burned ${amount} tokens`);
// 在Goerli上执行mint操作
let tx = await contractGoerli.mint(user, amount);
await tx.wait();
console.log(`Minted ${amount} tokens to ${user} on Chain Goerli`);
});
// 监听chain Goerli的Bridge事件,然后在Sepolia上执行mint操作,完成跨链
contractGoerli.on("Bridge", async (user, amount) => {
console.log(`Bridge event on Chain Goerli: User ${user} burned ${amount} tokens`);
// 在Sepolia上执行mint操作
let tx = await contractSepolia.mint(user, amount);
await tx.wait();
console.log(`Minted ${amount} tokens to ${user} on Chain Sepolia`);
});
}catch(e){
console.log(e);
}
}
main();