-
Notifications
You must be signed in to change notification settings - Fork 1
/
hardhat.config.js
558 lines (488 loc) · 17.1 KB
/
hardhat.config.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* Profile: https://linktr.ee/nid_z
* Team ThaiChain Foundation (https://www.thaichain.io/)
*
* @title cli-op-stack-Bridge-erc20-to-native
* @notice cli-withdrawal
* @author nidz-the-fact
*/
const { keccak256 } = require('@ethersproject/keccak256');
const { defaultAbiCoder } = require('@ethersproject/abi');
const { HashZero } = require('@ethersproject/constants');
const { mnemonic } = require('./secrets.json');
const l1BridgeAbi = require('./l1standardbridge.json');
const tokenAbi = require('./erc20.json');
const l2BridgeAbi = require('./l2standardbridge.json');
const l2Tol1MessagePasserAbi = require('./l2tol1messagepasser.json');
const l2OuputOracleAbi = require('./l2OutputOracle.json');
const optimismPortalAbi = require('./optimismPortal.json');
require('@nomicfoundation/hardhat-toolbox');
require('@nomicfoundation/hardhat-ethers');
// edit
const l1StandardBridge = '0x3C91efB30c55FbD5782be4BbA3D9628C1074a18D';
const l2StandardBridge = '0x4200000000000000000000000000000000000010';
const l2ToL1MessagePasser = '0x4200000000000000000000000000000000000016';
const l2OutputOracle = '0x74Ad6E0FB793eB5e6c1ff1225B03F5C5fFB7EF0c';
const optimismPortal = '0x0d605bb7d4FB586eAB750205F5247825F4D8AF4B';
const defaultGasAmount = '1000000';
const emptyData = '0x';
const l2ExplorerApi = 'https://exp.hera.jbcha.in/api'; // L2
const getL1Wallet = () => {
const provider = new ethers.JsonRpcProvider
(hreConfig.networks['JibcahinTestnet'].url); // edit - L1
const wallet = ethers.Wallet.fromPhrase(mnemonic);
const signer = wallet.connect(provider);
return signer;
};
const getL2Wallet = () => {
const provider = new ethers.JsonRpcProvider(
hreConfig.networks['HeraTestnet-JibcahinTestnet'].url,); // edit - L2-L1
const wallet = ethers.Wallet.fromPhrase(mnemonic);
const signer = wallet.connect(provider);
return signer;
};
/** @type import('hardhat/config').HardhatUserConfig */
const hreConfig = {
solidity: '0.8.17',
defaultNetwork: 'JibcahinTestnet', // edit - main
networks: {
hardhat: {},
// edit
JibcahinTestnet: {
url: 'https://rpc.testnet.jibchain.net', // L1
accounts: {
mnemonic,
},
gasPrice: 1000000,
},
// L2-L1
'HeraTestnet-JibcahinTestnet': {
url: 'https://rpc.hera.jbcha.in', // L2
accounts: {
mnemonic,
},
gasPrice: 1000000,
},
},
};
module.exports = hreConfig;
const getPortalContract = (signer) => {
const portalContract = new ethers.Contract(
optimismPortal,
optimismPortalAbi,
signer,
);
return portalContract;
};
const getOracleContract = (signer) => {
const oracleContract = new ethers.Contract(
l2OutputOracle,
l2OuputOracleAbi,
signer,
);
return oracleContract;
};
const getMessageContract = (signer) => {
const messageContract = new ethers.Contract(
l2ToL1MessagePasser,
l2Tol1MessagePasserAbi,
signer,
);
return messageContract;
};
const getL1StandardBridgeContract = (signer) => {
const bridgeContract = new ethers.Contract(
l1StandardBridge,
l1BridgeAbi,
signer,
);
return bridgeContract;
};
const getL2StandardBridgeContract = (signer) => {
const bridgeContract = new ethers.Contract(
l2StandardBridge,
l2BridgeAbi,
signer,
);
return bridgeContract;
};
const getTokenContract = (signer, address) => {
const tokenContract = new ethers.Contract(address, tokenAbi, signer);
return tokenContract;
};
const makeStateTrieProof = async (provider, blockNumber, address, slot) => {
const proof = await provider.send('eth_getProof', [
address,
[slot],
blockNumber,
]);
return {
accountProof: proof.accountProof,
storageProof: proof.storageProof[0].proof,
storageValue: BigInt(proof.storageProof[0].value),
storageRoot: proof.storageHash,
};
};
const hashWithdrawal = (withdrawalMessage) => {
const types = [
'uint256',
'address',
'address',
'uint256',
'uint256',
'bytes',
];
const encoded = defaultAbiCoder.encode(types, [
withdrawalMessage.nonce,
withdrawalMessage.sender,
withdrawalMessage.target,
withdrawalMessage.value,
withdrawalMessage.gasLimit,
withdrawalMessage.data,
]);
return keccak256(encoded);
};
const getWithdrawalMessage = async (messageContract, withdrawal, isToken) => {
let messageLog = withdrawal.logs.find((log) => {
if (log.address === l2ToL1MessagePasser) {
const parsed = messageContract.interface.parseLog(log);
console.log('parsed', parsed);
return parsed.name === 'MessagePassed';
}
return false;
});
console.log('messageLog', messageLog);
if (!messageLog) {
messageLog = withdrawal.logs[0];
}
const parsedLog = messageContract.interface.parseLog(messageLog);
const withdrawalMessage = {
nonce: parsedLog.args.nonce,
sender: parsedLog.args.sender,
target: parsedLog.args.target,
value: parsedLog.args.value,
gasLimit: parsedLog.args.gasLimit,
data: parsedLog.args.data,
};
console.log('withdrawalMessage', withdrawalMessage);
return withdrawalMessage;
};
const fetchTransactions = async (address) => {
const params = {
address,
action: 'txlist',
module: 'account',
filterby: 'from',
startblock: '0',
};
const searchParams = new URLSearchParams(params).toString();
const url = new URL(l2ExplorerApi);
url.search = searchParams;
const transactions = await fetch(url, {
headers: {
Accept: 'application/json',
},
});
const data = await transactions.json();
return data;
};
task('balance', "Prints an account's balance").setAction(async (taskArgs) => {
const signer = await ethers.provider.getSigner();
const balance = await ethers.provider.getBalance(await signer.getAddress());
console.log(ethers.formatEther(balance), 'ETH');
});
// task('bridge', 'Bridges ETH to HeraTestnet-JibcahinTestnet')
// .addParam('amount', 'The amount to bridge')
// .setAction(async (taskArgs) => {
// const signer = await ethers.provider.getSigner();
// const bridgeContract = getL1StandardBridgeContract(signer);
// const sender = await bridgeContract.l2TokenBridge();
// console.log('sender', sender);
// const fmtAmount = ethers.parseUnits(taskArgs.amount);
// console.log('fmtAmount', fmtAmount);
// try {
// const bridgeResult = await bridgeContract.bridgeETH(
// defaultGasAmount,
// emptyData,
// {
// value: fmtAmount,
// },
// );
// console.log('bridgeResult', bridgeResult);
// const transactionReceipt = await bridgeResult.wait();
// console.log('transactionReceipt', transactionReceipt);
// } catch (e) {
// console.log('bridgeResult error', e);
// }
// });
// task('bridgeToken', 'Bridges erc20 token to HeraTestnet-JibcahinTestnet')
// .addParam('amount', 'The amount to bridge')
// .addParam('l1token', 'The token address on JibcahinTestnet')
// .addParam('l2token', 'The token address on HeraTestnet-JibcahinTestnet')
// .setAction(async (taskArgs) => {
// const signer = await ethers.provider.getSigner();
// const bridgeContract = getL1StandardBridgeContract(signer);
// const sender = await bridgeContract.l2TokenBridge();
// console.log('sender', sender);
// const fmtAmount = ethers.parseUnits(taskArgs.amount);
// console.log('fmtAmount', fmtAmount);
// const tokenContract = getTokenContract(signer, taskArgs.l1token);
// try {
// const allowance = await tokenContract.allowance(
// await signer.getAddress(),
// l1StandardBridge,
// );
// if (allowance < fmtAmount) {
// console.log('approve bridge to access token');
// const approveResult = await tokenContract.approve(
// l1StandardBridge,
// fmtAmount,
// );
// console.log('approve result', approveResult);
// } else {
// console.log('token is approved to deposit');
// }
// const bridgeResult = await bridgeContract.depositERC20(
// taskArgs.l1token,
// taskArgs.l2token,
// fmtAmount,
// defaultGasAmount,
// emptyData,
// );
// console.log('bridge token result', bridgeResult);
// const transactionReceipt = await bridgeResult.wait();
// console.log('token transaction receipt', transactionReceipt);
// } catch (e) {
// console.log('bridge token result error', e);
// }
// });
task(
'withdraw',
'Initiates a native token withdrawal from HeraTestnet-JibcahinTestnet to JibcahinTestnet',
)
.addParam('amount', 'The amount to bridge')
.setAction(async (taskArgs) => {
const signer = await getL2Wallet();
const messageContract = getMessageContract(signer);
try {
const nonce = await messageContract.messageNonce();
console.log('Message nonce:', nonce);
const fmtAmount = ethers.parseUnits(taskArgs.amount);
console.log('Amount to bridge:', fmtAmount.toString());
const bridgeResult = await messageContract.initiateWithdrawal(
signer.address,
defaultGasAmount,
emptyData,
{ value: fmtAmount },
);
console.log('Withdrawal successful. Transaction hash:', bridgeResult.hash);
console.log('Withdrawn amount:', taskArgs.amount);
} catch (error) {
console.error('Withdrawal failed. Error:', error.message);
}
});
task(
'withdrawToken',
'Initiates a native token withdrawal from HeraTestnet-JibcahinTestnet to JibcahinTestnet',
)
.addParam('amount', 'The amount to bridge')
.addParam('token', 'The token address on HeraTestnet-JibcahinTestnet')
.setAction(async (taskArgs) => {
const signer = await getL2Wallet();
const l2StandardBridgeContract = getL2StandardBridgeContract(signer);
const fmtAmount = ethers.parseUnits(taskArgs.amount);
console.log('fmtAmount', fmtAmount);
try {
const bridgeResult = await l2StandardBridgeContract.withdraw(
taskArgs.token,
fmtAmount,
defaultGasAmount,
'0x01',
);
console.log('withdrawal result', bridgeResult);
const transactionReceipt = await bridgeResult.wait();
console.log('withdrawal transaction receipt', transactionReceipt);
} catch (e) {
console.log('withdrawal error', e);
}
});
task(
'proveWithdrawal',
'Proves a native token withdrawal from HeraTestnet-JibcahinTestnet to JibcahinTestnet',
)
.addParam('tx', 'The transaction hash of the withdrawal')
.setAction(async (taskArgs) => {
const signer = await getL2Wallet();
const l1Signer = await getL1Wallet();
const oracleContract = getOracleContract(l1Signer);
const messageContract = getMessageContract(signer);
const portalContract = getPortalContract(l1Signer);
const withdrawal = await signer.provider.getTransactionReceipt(taskArgs.tx);
console.log('withdrawal receipt', withdrawal.blockNumber, withdrawal);
const l2OutputIdx = await oracleContract.getL2OutputIndexAfter(
withdrawal.blockNumber,
);
console.log('l2OutputIdx', l2OutputIdx);
const l2Output = await oracleContract.getL2Output(l2OutputIdx);
console.log('l2Output', l2Output);
const withdrawalMessage = await getWithdrawalMessage(
messageContract,
withdrawal,
);
const hashedWithdrawal = hashWithdrawal(withdrawalMessage);
const messageSlot = keccak256(
defaultAbiCoder.encode(
['bytes32', 'uint256'],
[hashedWithdrawal, HashZero],
),
);
const l2BlockNumber = '0x' + BigInt(l2Output[2]).toString(16);
const proof = await makeStateTrieProof(
signer.provider,
l2BlockNumber,
l2ToL1MessagePasser,
messageSlot,
);
console.log('proof', proof);
const block = await signer.provider.send('eth_getBlockByNumber', [
l2BlockNumber,
false,
]);
console.log('block', block);
const outputProof = {
version: HashZero,
stateRoot: block.stateRoot,
messagePasserStorageRoot: proof.storageRoot,
latestBlockhash: block.hash,
};
console.log('outputProof', outputProof);
try {
const proving = await portalContract.proveWithdrawalTransaction(
withdrawalMessage,
l2OutputIdx,
outputProof,
proof.storageProof,
);
console.log('proving', proving);
console.log('Waiting for proveWithdrawal...');
const result = await proving.wait();
console.log('proving result', result);
console.log('proveWithdrawal successful.');
} catch (e) {
console.log('withdrawal error', e);
console.log('proveWithdrawal successful.');
}
});
task(
'finalizeWithdrawal',
'Finalizes a native token withdrawal from HeraTestnet-JibcahinTestnet to JibcahinTestnet',
)
.addParam('tx', 'The transaction hash of the withdrawal')
.setAction(async (taskArgs) => {
const signer = await getL2Wallet();
const l1Signer = await getL1Wallet();
const portalContract = getPortalContract(l1Signer);
const messageContract = getMessageContract(signer);
const withdrawal = await signer.provider.getTransactionReceipt(taskArgs.tx);
console.log('withdrawal receipt', withdrawal.blockNumber, withdrawal);
const msg = await getWithdrawalMessage(messageContract, withdrawal);
console.log('msg', msg);
try {
const finalizing =
await portalContract.finalizeWithdrawalTransaction(msg);
console.log('finalizing', finalizing);
const result = await finalizing.wait();
console.log('finalizing result', result);
console.log(`Withdrawal successful.`);
console.log(`View transaction: https://exp.testnet.jibchain.net/`);
} catch (e) {
console.log('finalize error', e);
}
});
task('fetchWithdrawals', 'Fetchs all withdrawals')
.addFlag('full', 'Show the full address')
.setAction(async (taskArgs) => {
const signer = await getL2Wallet();
const l1Signer = await getL1Wallet();
const portalContract = getPortalContract(l1Signer);
const messageContract = getMessageContract(signer);
const oracleContract = getOracleContract(l1Signer);
const l2StandardBridgeContract = getL2StandardBridgeContract(signer);
try {
const data = await fetchTransactions(await signer.getAddress());
const withdrawals = [];
for (let i = 0; i < data.result.length; i++) {
const tx = data.result[i];
console.log(i, tx);
if (tx.isError === '1') continue;
if (tx.to === l2ToL1MessagePasser && tx.value !== '0')
withdrawals.push(tx);
if (tx.to === optimismPortal && tx.value !== '0') withdrawals.push(tx);
if (tx.to === l2StandardBridge) {
const functionName = l2StandardBridgeContract.interface.getFunction(
tx.input.slice(0, 10),
).name;
console.log('functionName', functionName);
if (functionName === 'withdraw') {
const decodedWithdrawData =
l2StandardBridgeContract.interface.decodeFunctionData(
tx.input.slice(0, 10),
tx.input,
);
tx.value = decodedWithdrawData[1].toString();
const tokenDetails = getTokenContract(
signer,
decodedWithdrawData[0],
);
tx.symbol = await tokenDetails.symbol();
withdrawals.push(tx);
}
}
}
console.log('raw transactions', withdrawals);
const latestBlockNumber = await oracleContract.latestBlockNumber();
const finalizationPeriod =
await oracleContract.FINALIZATION_PERIOD_SECONDS();
for (let i = 0; i < withdrawals.length; i++) {
const withdrawal = withdrawals[i];
const receipt = await signer.provider.getTransactionReceipt(
withdrawal.hash,
);
console.log('receipt', receipt);
const wm = await getWithdrawalMessage(messageContract, receipt);
const hash = hashWithdrawal(wm);
const isFinalized = await portalContract.finalizedWithdrawals(hash);
withdrawal.isFinalized = isFinalized;
const rawProof = await portalContract.provenWithdrawals(hash);
withdrawal.rawProof = rawProof;
const isProven = rawProof[0] !== HashZero;
withdrawal.isReadyToFinalize =
Math.floor(Date.now() / 1000) > rawProof[1] + finalizationPeriod &&
!isFinalized &&
isProven;
withdrawal.isProven = isProven;
withdrawal.isReadyToProve =
latestBlockNumber >= receipt.blockNumber && !isFinalized && !isProven;
}
console.log('withdrawals', withdrawals);
const sorted = withdrawals.sort((a, b) => {
return a.timeStamp > b.timeStamp;
});
const withdrawalTable = sorted.map((withdrawal) => ({
hash: taskArgs.full
? withdrawal.hash
: withdrawal.hash.substring(0, 6) +
'...' +
withdrawal.hash.substring(withdrawal.hash.length - 6),
symbol: withdrawal.symbol || 'ETH',
value: withdrawal.value,
isReadyToProve: withdrawal.isReadyToProve,
isProven: withdrawal.isProven,
isReadyToFinalize: withdrawal.isReadyToFinalize,
isFinalized: withdrawal.isFinalized,
}));
console.table(withdrawalTable);
} catch (e) {
console.log('fetch withdrawals error', e);
}
});