diff --git a/scripts/register-and-bridge.js b/scripts/register-and-bridge.js index 19efc5f..7411bff 100644 --- a/scripts/register-and-bridge.js +++ b/scripts/register-and-bridge.js @@ -5,6 +5,7 @@ const hre = require("hardhat"); const chalk = require("chalk"); const ethers = hre.ethers; + async function main() { const SESH_DECIMALS = 9; const SESH_TO_TRANSFER = "200000000"; @@ -22,58 +23,80 @@ async function main() { const ownerAddress = await owner.getAddress(); console.log(chalk.green(`Using deployer: ${ownerAddress}`)); - // --- Register token on L1 to token on L2 via the L1CustomGateway contract --- - console.log(chalk.blue("\nRegistering L1 token to L2 token via L1CustomGateway...")); - - const l1CustomGatewayABI = [ - "function registerTokenToL2(address _l1Token, address _l2Token) external" + // Constants + const feeData = await owner.provider.getFeeData(); + const l1GasPriceBid = feeData.gasPrice ? feeData.gasPrice * BigInt(2) : ethers.parseUnits('10', 'gwei'); + const l2GasPriceBid = BigInt("1000000000"); + const maxGasForCustomGateway = BigInt(1000000); + const maxGasForRouter = BigInt(500000); + const maxSubmissionCostForCustomGateway = BigInt("500000000000000"); + const maxSubmissionCostForRouter = BigInt("300000000000000"); + const gatewayGasCost = maxGasForCustomGateway * l2GasPriceBid; + const routerGasCost = maxGasForRouter * l2GasPriceBid; + const valueForGateway = maxSubmissionCostForCustomGateway + gatewayGasCost; + const valueForRouter = maxSubmissionCostForRouter + routerGasCost; + const totalValue = valueForGateway + valueForRouter; + + console.log(chalk.blue("\nRegistering L1 token to L2 token...")); + + const registerTokenABI = [ + "function registerTokenOnL2(address l2CustomTokenAddress, uint256 maxSubmissionCostForCustomGateway, uint256 maxSubmissionCostForRouter, uint256 maxGasForCustomGateway, uint256 maxGasForRouter, uint256 gasPriceBid, uint256 valueForGateway, uint256 valueForRouter, address creditBackAddress) payable" ]; - - const l1CustomGateway = new ethers.Contract( - L1_CUSTOM_GATEWAY_ADDRESS, - l1CustomGatewayABI, + + const l1Token = new ethers.Contract( + L1_TOKEN_ADDRESS, + registerTokenABI, owner ); - + try { - const registerTx = await l1CustomGateway.registerTokenToL2(L1_TOKEN_ADDRESS, L2_TOKEN_ADDRESS); + console.log("Transaction parameters:"); + console.log(`L2 Token Address: ${L2_TOKEN_ADDRESS}`); + console.log(`Max Submission Cost (Gateway): ${maxSubmissionCostForCustomGateway}`); + console.log(`Max Submission Cost (Router): ${maxSubmissionCostForRouter}`); + console.log(`Max Gas (Gateway): ${maxGasForCustomGateway}`); + console.log(`Max Gas (Router): ${maxGasForRouter}`); + console.log(`Gas Price Bid: ${l2GasPriceBid}`); + console.log(`Value For Gateway: ${valueForGateway}`); + console.log(`Value For Router: ${valueForRouter}`); + console.log(`Total Value: ${totalValue}`); + + const registerTx = await l1Token.registerTokenOnL2( + L2_TOKEN_ADDRESS, + maxSubmissionCostForCustomGateway, + maxSubmissionCostForRouter, + maxGasForCustomGateway, + maxGasForRouter, + l2GasPriceBid, + valueForGateway, + valueForRouter, + ownerAddress, + { value: totalValue } + ); + console.log("Registration transaction submitted, waiting for confirmation..."); const registerReceipt = await registerTx.wait(); - console.log(chalk.green(`L1CustomGateway registration successful: ${registerReceipt.transactionHash}`)); - } catch (error) { - console.error(chalk.red("Error during L1CustomGateway registration:"), error); - process.exit(1); - } - - // --- Register token on L1 to the L1GatewayRouter --- - console.log(chalk.blue("\nRegistering token to L1GatewayRouter...")); - - const l1GatewayRouterABI = [ - "function setGateway(address _token, address _gateway) external", - "function outboundTransferCustomRefund(address _token, address _refundTo, address _to, uint256 _amount, uint256 _maxGas, uint256 _gasPriceBid, bytes calldata _data) external payable returns (bytes memory)", - ]; - - const l1GatewayRouter = new ethers.Contract( - L1_GATEWAY_ROUTER_ADDRESS, - l1GatewayRouterABI, - owner - ); - - try { - const setGatewayTx = await l1GatewayRouter.setGateway(L1_TOKEN_ADDRESS, L1_CUSTOM_GATEWAY_ADDRESS); - console.log("SetGateway transaction submitted, waiting for confirmation..."); - const setGatewayReceipt = await setGatewayTx.wait(); - console.log(chalk.green(`L1GatewayRouter registration successful: ${setGatewayReceipt.transactionHash}`)); + console.log(chalk.green(`Token registration successful: ${registerReceipt.transactionHash}`)); } catch (error) { - console.error(chalk.red("Error during L1GatewayRouter registration:"), error); + console.error(chalk.red("Error during token registration:"), error); process.exit(1); } - console.log(chalk.green("\nToken registration on L1 complete.")); - // --- Bridging Tokens over to L2 --- console.log(chalk.blue("\nInitiating token bridge via the custom bridge...")); + // Parameters for the bridge function + const l1MaxGas = BigInt(300000); + const l2MaxGas = BigInt(1000000); + const maxSubmissionCost = BigInt("500000000000000"); + const callHookData = "0x"; + const l2amount = ethers.parseEther("0.002"); + const totalL2GasCost = l2MaxGas * l2GasPriceBid; + const totalL2Value = maxSubmissionCost + totalL2GasCost + l2amount; + const extraData = ethers.AbiCoder.defaultAbiCoder().encode( + ["uint256", "bytes"], + [maxSubmissionCost, callHookData] + ); // Approve the token for the gateway transfer. const depositAmount = ethers.parseUnits(SESH_TO_TRANSFER, SESH_DECIMALS); @@ -91,23 +114,18 @@ async function main() { process.exit(1); } - // Calculate parameters for the outbound transfer - const l1MaxGas = BigInt(300000); - const l2MaxGas = BigInt(1000000); - const feeData = await owner.provider.getFeeData(); - let l1GasPriceBid = feeData.gasPrice ? feeData.gasPrice * BigInt(2) : ethers.parseUnits('10', 'gwei'); - const l2GasPriceBid = BigInt("1000000000"); - const maxSubmissionCost = BigInt("500000000000000"); - const callHookData = "0x"; - const l2amount = ethers.parseEther("0.002"); - const totalL2GasCost = l2MaxGas * l2GasPriceBid; - const totalL2Value = maxSubmissionCost + totalL2GasCost + l2amount; - const extraData = ethers.AbiCoder.defaultAbiCoder().encode( - ["uint256", "bytes"], - [maxSubmissionCost, callHookData] - ); console.log("Initiating outbound transfer on the custom bridge..."); + const l1GatewayRouterABI = [ + "function outboundTransferCustomRefund(address _token, address _refundTo, address _to, uint256 _amount, uint256 _maxGas, uint256 _gasPriceBid, bytes calldata _data) external payable returns (bytes memory)", + ]; + + const l1GatewayRouter = new ethers.Contract( + L1_GATEWAY_ROUTER_ADDRESS, + l1GatewayRouterABI, + owner + ); + try { const outboundTx = await l1GatewayRouter.outboundTransferCustomRefund( L1_TOKEN_ADDRESS,