-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.ts
136 lines (128 loc) · 5.15 KB
/
chain.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { ChainInfo, TokenInfo } from "./configure";
import { ProxyAdmin } from "./contract";
import { Log } from "./log";
import {
Messager,
Eth2ArbSendService,
Eth2ArbReceiveService,
LayerZeroMessager,
DarwiniaMsglineMessager,
} from "./messager";
import {
Wallet,
providers,
} from "ethers";
export class Chain {
public wallet: Wallet | providers.Provider;
public name: string;
public id: number;
public lzChainId: number;
public dao: string;
public operator: string;
public proxyAdmin: ProxyAdmin;
public protocolFeeReceiver: string;
public messagers: Map<string, Messager>;
public tokens: Map<string, TokenInfo>;
public log: Log;
public proxyAdminCache: Map<string, string>;
constructor(chainInfo: ChainInfo) {
// if need send tx, use wallet with private key
this.wallet = new providers.JsonRpcProvider(chainInfo.url);
this.proxyAdmin = new ProxyAdmin(chainInfo.proxyAdmin, this.wallet);
this.name = chainInfo.name;
this.id = chainInfo.id;
this.lzChainId = chainInfo.lzChainId;
this.dao = chainInfo.dao;
this.operator = chainInfo.operator;
this.protocolFeeReceiver = chainInfo.protocolFeeReceiver;
this.messagers = new Map<string, Messager>();
this.tokens = new Map<string, TokenInfo>();
this.proxyAdminCache = new Map<string, string>();
this.log = new Log();
if (chainInfo.Eth2ArbSendService) {
const messager = new Eth2ArbSendService(chainInfo.Eth2ArbSendService!, this.wallet);
this.messagers.set(messager.name, messager);
}
if (chainInfo.Eth2ArbReceiveService) {
const messager = new Eth2ArbReceiveService(chainInfo.Eth2ArbReceiveService!, this.wallet);
this.messagers.set(messager.name, messager);
}
if (chainInfo.layerZeroMessager) {
const messager = new LayerZeroMessager(chainInfo.layerZeroMessager!, this.wallet);
this.messagers.set(messager.name, messager);
}
if (chainInfo.DarwiniaMsglineMessager) {
const messager = new DarwiniaMsglineMessager(chainInfo.DarwiniaMsglineMessager!, this.wallet);
this.messagers.set(messager.name, messager);
}
chainInfo.tokens?.forEach((token: TokenInfo) => {
this.tokens.set(token.symbol, token);
});
}
async checkProxyAdminDao(): Promise<boolean> {
return (await this.proxyAdmin.owner()).toLowerCase() === this.dao.toLowerCase();
}
async checkProxyAdmin(proxy: string): Promise<boolean> {
let proxyAdmin = this.proxyAdminCache.get(proxy);
if (!proxyAdmin) {
proxyAdmin = await this.proxyAdmin.getProxyAdmin(proxy);
this.proxyAdminCache.set(proxy, proxyAdmin);
}
return proxyAdmin.toLowerCase() === this.proxyAdmin.address.toLowerCase();
}
}
export class ChainManager {
public log: Log;
public chains: Map<string, Chain> = new Map<string, Chain>();
constructor(chainInfos: ChainInfo[]) {
this.log = new Log();
for (const chainInfo of chainInfos) {
this.chains.set(chainInfo.name, new Chain(chainInfo));
}
}
async checkProxyAdminDao(): Promise<void> {
const chains = Array.from(this.chains.values());
const count = this.chains.size;
var index = 0;
await this.log.progress2("waiting for check proxyAdmin dao", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const chain = chains[index];
index++;
draw(index, count, `[${chain.name}]check proxyAdmin: ${chain.proxyAdmin.address} dao: ${chain.dao}`, '');
const result = await chain.checkProxyAdminDao();
if (!result) {
draw(index, count, '', `[${chain.name}]proxyAdmin's dao invalid`);
return false;
}
return index < count;
});
}
async checkMessagerDao(): Promise<void> {
const chains = Array.from(this.chains.values());
const count = chains.reduce((sum, current) => sum + current.messagers.size, 0);
var chainIndex = 0;
var messagerIndex = 0;
var index = 0;
await this.log.progress2("waiting for check messager dao", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const chain = chains[chainIndex];
const messagers = Array.from(chain.messagers.values());
const messager = messagers[messagerIndex];
messagerIndex++;
index++;
if (messagerIndex >= chain.messagers.size) {
messagerIndex = 0;
chainIndex++;
}
draw(index, count, `[${chain.name}-${messager.name}]check messager: ${messager.address()} dao: ${chain.dao}`, '');
const result = await messager.checkDao(chain.dao);
if (!result) {
draw(index, count, '', `[${chain.name}-${messager.name}]messager dao invalid`);
return false;
}
return index < count;
});
}
}