l2-base-bridging/dist/index.js

48 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-08-20 18:52:05 +00:00
// SPDX-License-Identifier: MIT
import optimismSDK from "@eth-optimism/sdk";
import ethers from "ethers";
import fs from "fs/promises";
import dotenv from "dotenv";
const env = process.env.NODE_ENV || "local";
dotenv.config({ path: `../10grans-ng/.env.${env}` });
const ABI = ["function balanceOf(address owner) view returns (uint)"];
const wei = process.argv[2];
const l1Addr = (await fs.readFile(`../10grans-ng/.l1-token-address.${env}`, "utf-8")).trim();
const l2Addr = (await fs.readFile(`../10grans-ng/.l2-token-address.${env}`, "utf-8")).trim();
const l1ApiUrl = process.env.L1_RPC_URL;
const l1RpcProvider = new ethers.providers.JsonRpcProvider(l1ApiUrl);
const l1ChainId = (await l1RpcProvider.getNetwork()).chainId;
const l1PrivateKey = ethers.utils.HDNode.fromMnemonic(process.env.MNEMONIC).derivePath(ethers.utils.defaultPath).privateKey;
const l1Wallet = new ethers.Wallet(l1PrivateKey, l1RpcProvider);
const l1Artifacts = JSON.parse(await fs.readFile("../10grans-ng/artifacts/contracts/L1Token.sol/L1Token.json", "utf-8"));
const l1Token = new ethers.Contract(l1Addr, ABI, l1RpcProvider);
const l2ApiUrl = process.env.L2_RPC_URL;
const l2RpcProvider = new ethers.providers.JsonRpcProvider(l2ApiUrl);
const l2ChainId = (await l2RpcProvider.getNetwork()).chainId;
const l2Wallet = new ethers.Wallet(l1PrivateKey, l2RpcProvider);
const l2Artifacts = JSON.parse(await fs.readFile("../10grans-ng/artifacts/contracts/L2Token.sol/L2Token.json", "utf-8"));
const l2Token = new ethers.Contract(l2Addr, ABI, l2RpcProvider);
let l1Balance = await l1Token.balanceOf(l1Wallet.address);
console.log(`BEFORE L1 wallet balance: ${l1Balance}`);
let l2Balance = await l2Token.balanceOf(l1Wallet.address);
console.log(`BEFORE L2 wallet balance: ${l2Balance}`);
const crossChainMessenger = new optimismSDK.CrossChainMessenger({
l1ChainId: l1ChainId,
l2ChainId: l2ChainId,
l1SignerOrProvider: l1Wallet,
l2SignerOrProvider: l2Wallet
});
console.log("Approving transfer...");
const depositTx1 = await crossChainMessenger.approveERC20(l1Token.address, l2Addr, wei);
await depositTx1.wait();
const blockNumber = await l2RpcProvider.getBlockNumber();
console.log("Depositing...");
const depositTx2 = await crossChainMessenger.depositERC20(l1Token.address, l2Addr, wei);
await depositTx2.wait();
console.log("Waiting for response, could take a while...");
await crossChainMessenger.waitForMessageStatus(depositTx2.hash, optimismSDK.MessageStatus.RELAYED, { fromBlockOrBlockHash: blockNumber });
l1Balance = await l1Token.balanceOf(l1Wallet.address);
console.log(`AFTER L1 wallet balance: ${l1Balance}`);
l2Balance = await l2Token.balanceOf(l1Wallet.address);
console.log(`AFTER L2 wallet balance: ${l2Balance}`);