-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_bridge_v2.cjs
More file actions
81 lines (63 loc) · 3.14 KB
/
base_bridge_v2.cjs
File metadata and controls
81 lines (63 loc) · 3.14 KB
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
const { ethers } = require('ethers');
require('dotenv').config();
// Base L1 Bridge addresses - there are two interfaces:
// 1. L1StandardBridge (old): 0x3154Cf16ccdb4C6d922629664174b904d80F2C35
// 2. OptimismPortal (new): 0x49048044D57e1C92A77f79988d21Fa8fAF74E97e
const OPTIMISM_PORTAL = '0x49048044D57e1C92A77f79988d21Fa8fAF74E97e';
async function bridgeToBase() {
console.log('🔷 ETH → Base (OptimismPortal Direct Deposit)');
console.log('═══════════════════════════════════════════════════════════════');
const alchemyKey = process.env.BASE_MAINNET_RPC.split('/v2/')[1];
const provider = new ethers.JsonRpcProvider(`https://eth-mainnet.g.alchemy.com/v2/${alchemyKey}`);
const pk = process.env.ETH_PRIVATE_KEY;
if (!pk) {
console.log('❌ ETH_PRIVATE_KEY not found');
return;
}
const wallet = new ethers.Wallet(pk, provider);
console.log('Wallet:', wallet.address);
const balance = await provider.getBalance(wallet.address);
console.log('ETH Balance:', ethers.formatEther(balance), 'ETH');
// Keep 0.0005 ETH for gas
const gasReserve = ethers.parseEther('0.0005');
const bridgeAmount = balance - gasReserve;
if (bridgeAmount <= 0n) {
console.log('❌ Insufficient balance');
return;
}
console.log('Bridge Amount:', ethers.formatEther(bridgeAmount), 'ETH');
// OptimismPortal depositTransaction function
const portalABI = [
'function depositTransaction(address _to, uint256 _value, uint64 _gasLimit, bool _isCreation, bytes _data) payable'
];
const portal = new ethers.Contract(OPTIMISM_PORTAL, portalABI, wallet);
console.log('\n🚀 Bridging via OptimismPortal...');
try {
const feeData = await provider.getFeeData();
console.log(' Gas Price:', ethers.formatUnits(feeData.gasPrice, 'gwei'), 'gwei');
// depositTransaction - send ETH to ourselves on L2
const tx = await portal.depositTransaction(
wallet.address, // recipient on L2
bridgeAmount, // amount to send
100000n, // L2 gas limit
false, // not a contract creation
'0x', // no calldata
{
value: bridgeAmount,
gasLimit: 200000,
maxFeePerGas: feeData.maxFeePerGas,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas
}
);
console.log('\n✅ Bridge TX Submitted!');
console.log(' TX Hash:', tx.hash);
console.log(' Etherscan: https://etherscan.io/tx/' + tx.hash);
const receipt = await tx.wait();
console.log(' ✅ Confirmed in block:', receipt.blockNumber);
console.log(' Gas Used:', receipt.gasUsed.toString());
console.log('\n💰 ETH will arrive on Base in ~10-15 minutes');
} catch (err) {
console.log('❌ Bridge error:', err.reason || err.message);
}
}
bridgeToBase().catch(console.error);