-
Notifications
You must be signed in to change notification settings - Fork 0
/
messager.ts
112 lines (103 loc) · 4.94 KB
/
messager.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
import {
Wallet,
providers,
BigNumber,
} from "ethers";
import {
LnAccessController,
Eth2ArbSendServiceContract,
Eth2ArbReceiveServiceContract,
LayerZeroMessagerContract,
DarwiniaMsglineMessagerContract,
} from "./contract";
import {
Chain
} from "./chain";
export abstract class Messager {
public name: string;
public lnAccessController: LnAccessController;
constructor(name: string, messagerContract: LnAccessController) {
this.name = name;
this.lnAccessController = messagerContract;
}
address(): string {
return this.lnAccessController.address.toLowerCase();
}
async checkDao(dao: string): Promise<boolean> {
return (await this.lnAccessController.dao()) === dao;
}
abstract isConnected(remoteChain: Chain, remoteMessager: string): Promise<boolean>;
abstract remoteAppIsSender(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean>;
abstract remoteAppIsReceiver(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean>;
};
export class Eth2ArbSendService extends Messager {
public contract: Eth2ArbSendServiceContract;
constructor(address: string, signer: Wallet | providers.Provider) {
const contract = new Eth2ArbSendServiceContract(address, signer);
super("arbitrumL1ToL2", contract);
this.contract = contract;
}
async isConnected(remoteChain: Chain, remoteMessager: string): Promise<boolean> {
return remoteMessager.toLowerCase() === await this.contract.remoteMessager();
}
async remoteAppIsSender(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return false;
}
async remoteAppIsReceiver(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return remoteApp.toLowerCase() === await this.contract.appPair(localApp);
}
}
export class Eth2ArbReceiveService extends Messager {
public contract: Eth2ArbReceiveServiceContract;
constructor(address: string, signer: Wallet | providers.Provider) {
const contract = new Eth2ArbReceiveServiceContract(address, signer);
super("arbitrumL1ToL2", contract);
this.contract = contract;
}
async isConnected(remoteChain: Chain, remoteMessager: string): Promise<boolean> {
const remoteAddress = await this.contract.remoteMessager();
return remoteAddress === remoteMessager;
}
async remoteAppIsSender(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return remoteApp.toLowerCase() === await this.contract.appPair(localApp);
}
async remoteAppIsReceiver(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return false;
}
}
export class LayerZeroMessager extends Messager {
public contract: LayerZeroMessagerContract;
constructor(address: string, signer: Wallet | providers.Provider) {
const contract = new LayerZeroMessagerContract(address, signer);
super("layerzero", contract);
this.contract = contract;
}
async isConnected(remoteChain: Chain, remoteMessager: string): Promise<boolean> {
const remote = await this.contract.remoteMessager(BigNumber.from(remoteChain.id.toString()));
return remote.messager.toLowerCase() === remoteMessager.toLowerCase() && remote.lzRemoteChainId === remoteChain.lzChainId;
}
async remoteAppIsSender(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return remoteApp.toLowerCase() === await this.contract.remoteAppSender(BigNumber.from(remoteChainId.toString()), localApp);
}
async remoteAppIsReceiver(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return remoteApp.toLowerCase() === await this.contract.remoteAppReceiver(BigNumber.from(remoteChainId.toString()), localApp);
}
}
export class DarwiniaMsglineMessager extends Messager {
public contract: DarwiniaMsglineMessagerContract;
constructor(address: string, signer: Wallet | providers.Provider) {
const contract = new DarwiniaMsglineMessagerContract(address, signer);
super("msgline", contract);
this.contract = contract;
}
async isConnected(remoteChain: Chain, remoteMessager: string): Promise<boolean> {
const remote = await this.contract.remoteMessager(BigNumber.from(remoteChain.id.toString()));
return remote.messager.toLowerCase() === remoteMessager.toLowerCase();
}
async remoteAppIsSender(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return remoteApp.toLowerCase() === await this.contract.remoteAppSender(BigNumber.from(remoteChainId.toString()), localApp);
}
async remoteAppIsReceiver(remoteChainId: number, localApp: string, remoteApp: string): Promise<boolean> {
return remoteApp.toLowerCase() === await this.contract.remoteAppReceiver(BigNumber.from(remoteChainId.toString()), localApp);
}
}