-
Notifications
You must be signed in to change notification settings - Fork 8
/
sdk.ts
188 lines (174 loc) · 5.87 KB
/
sdk.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Signer, providers, BigNumber, Contract } from "ethers";
import {
ExternalProvider,
Web3Provider,
JsonRpcProvider,
} from "@ethersproject/providers";
import {
CrossChainMessenger,
DeepPartial,
DEFAULT_L2_CONTRACT_ADDRESSES,
OEContractsLike,
StandardBridgeAdapter,
} from "@eth-optimism/sdk";
import { Network } from "@ethersproject/networks";
import { ERC721BridgeAdapter } from "./erc721-bridge-adapter";
import * as Util from "./util";
import { L1BlockTimeSeconds, DepositConfirmationBlocks } from "./constants";
import ERC20 from "./contracts/ERC20.json";
import ERC721 from "./contracts/ERC721.json";
export class BridgeSDK {
eProvider: Web3Provider;
jProvider: JsonRpcProvider;
isL1: boolean;
util = Util;
messenger: CrossChainMessenger;
private contracts: DeepPartial<OEContractsLike>;
private l1StandardBridge: string;
private l1ERC721Bridge: string;
private MAX_RETRIES: number = 100;
constructor(
ethereum: ExternalProvider,
rpc: string,
isL1: boolean,
addressManager: string,
l1CrossDomainMessenger: string,
l1StandardBridge: string,
l1ERC721Bridge: string,
portalAddress: string,
outputOracle: string,
) {
this.util.assertAddresse(addressManager);
this.util.assertAddresse(l1CrossDomainMessenger);
this.util.assertAddresse(l1StandardBridge);
this.util.assertAddresse(l1ERC721Bridge);
this.util.assertAddresse(portalAddress);
this.util.assertAddresse(outputOracle);
this.eProvider = new providers.Web3Provider(ethereum);
this.jProvider = new JsonRpcProvider(rpc);
this.isL1 = isL1;
this.contracts = {
l1: {
AddressManager: addressManager,
L1CrossDomainMessenger: l1CrossDomainMessenger,
L1StandardBridge: l1StandardBridge, // dummy address
StateCommitmentChain: this.util.ZERO_ADDRESS, // dummy address
CanonicalTransactionChain: this.util.ZERO_ADDRESS, // dummy address
BondManager: this.util.ZERO_ADDRESS, // dummy address
OptimismPortal: portalAddress,
L2OutputOracle: outputOracle,
},
l2: DEFAULT_L2_CONTRACT_ADDRESSES,
};
this.l1StandardBridge = l1StandardBridge;
this.l1ERC721Bridge = l1ERC721Bridge;
}
async connectMetamask(): Promise<string[]> {
const signer = this.eProvider.getSigner();
const eChainId = (await this.eProvider.getNetwork()).chainId;
const jChainId = (await this.jProvider.getNetwork()).chainId;
const standardBridgeAdapter = {
Adapter: StandardBridgeAdapter,
l1Bridge: this.l1StandardBridge,
l2Bridge: "0x4200000000000000000000000000000000000010",
};
const erc721BridgeAdapter = {
Adapter: ERC721BridgeAdapter,
l1Bridge: this.l1ERC721Bridge,
l2Bridge: "0x6200000000000000000000000000000000000001",
};
this.messenger = new CrossChainMessenger({
l1SignerOrProvider: this.isL1 ? signer : this.jProvider,
l2SignerOrProvider: this.isL1 ? this.jProvider : signer,
l1ChainId: this.isL1 ? eChainId : jChainId,
l2ChainId: this.isL1 ? jChainId : eChainId,
depositConfirmationBlocks: DepositConfirmationBlocks,
l1BlockTimeSeconds: L1BlockTimeSeconds,
contracts: this.contracts,
bridges: { Standard: standardBridgeAdapter, ERC721: erc721BridgeAdapter },
bedrock: true,
});
return this.eProvider.send("eth_requestAccounts", []);
}
async getNetwork(): Promise<Network> {
return await this.eProvider.getNetwork();
}
async getBalances(address: string): Promise<{
l1: BigNumber;
l2: BigNumber;
}> {
const eBalance = await this.eProvider.getBalance(address);
const jBalance = await this.jProvider.getBalance(address);
return {
l1: this.isL1 ? eBalance : jBalance,
l2: this.isL1 ? jBalance : eBalance,
};
}
async getERC20Balance(
address: string,
tokenL1: string,
tokenL2: string,
): Promise<{
l1: BigNumber;
l2: BigNumber;
}> {
const L1Provider = this.isL1 ? this.eProvider : this.jProvider;
const L2Provider = this.isL1 ? this.jProvider : this.eProvider;
const l1Contract = new Contract(tokenL1, ERC20.abi, L1Provider);
const l2Contract = new Contract(tokenL2, ERC20.abi, L2Provider);
return {
l1: await l1Contract.balanceOf(address),
l2: await l2Contract.balanceOf(address),
};
}
async getERC721Balance(
address: string,
tokenL1: string,
tokenL2: string,
): Promise<{
l1: BigNumber;
l2: BigNumber;
}> {
const L1Provider = this.isL1 ? this.eProvider : this.jProvider;
const L2Provider = this.isL1 ? this.jProvider : this.eProvider;
const l1Contract = new Contract(tokenL1, ERC721.abi, L1Provider);
const l2Contract = new Contract(tokenL2, ERC721.abi, L2Provider);
return {
l1: await l1Contract.balanceOf(address),
l2: await l2Contract.balanceOf(address),
};
}
async waitForMessageRelayed(
txHash: string,
retry: number = this.MAX_RETRIES,
): Promise<void> {
const RELAYED = 6;
const RETRY_DELAY_MS = 5000; // Delay between retries in milliseconds
let attempts = 0;
while (attempts < retry) {
try {
await this.messenger.waitForMessageStatus(txHash, RELAYED);
return; // Success, exit the loop and function
} catch (error: any) {
if (
typeof error.message === "string" &&
error.message.includes("withdrawal index 0 out of bounds")
) {
attempts++;
console.log(
`Attempt ${attempts}: Encountered a retryable error, retrying...`,
);
if (attempts >= retry) {
throw new Error(
`Retry limit reached with persistent errors. err: ${error.message}`,
);
}
await this.util.sleep(RETRY_DELAY_MS); // Wait before retrying
} else {
// If error is not the one we're looking for, rethrow it
throw error;
}
}
}
}
}