From 6026bfe46611ebe402e9a19a33f77322a58c7192 Mon Sep 17 00:00:00 2001 From: Ameesha Agrawal Date: Wed, 3 Jul 2024 14:04:49 +0530 Subject: [PATCH] fix: types and comments --- scripts/socket-helpers/arb-estimate.ts | 9 ++++- scripts/socket-helpers/main.ts | 37 +++++++++++++------ .../{opt-estimate.ts => op-n-eth-estimate.ts} | 5 ++- scripts/socket-helpers/utils.ts | 11 +++--- 4 files changed, 42 insertions(+), 20 deletions(-) rename scripts/socket-helpers/{opt-estimate.ts => op-n-eth-estimate.ts} (68%) diff --git a/scripts/socket-helpers/arb-estimate.ts b/scripts/socket-helpers/arb-estimate.ts index 831a9379..ca67a480 100644 --- a/scripts/socket-helpers/arb-estimate.ts +++ b/scripts/socket-helpers/arb-estimate.ts @@ -6,9 +6,10 @@ import { NODE_INTERFACE_ADDRESS, } from "@arbitrum/sdk/dist/lib/dataEntities/constants"; import { TxData } from "./utils"; +import { StaticJsonRpcProvider } from "@ethersproject/providers"; export const getArbitrumGasLimitEstimate = async ( - provider, + provider: StaticJsonRpcProvider, txData: TxData ): Promise => { const arbGasInfo = ArbGasInfo__factory.connect(ARB_GAS_INFO, provider); @@ -29,8 +30,14 @@ export const getArbitrumGasLimitEstimate = async ( const l2GasUsed = gasEstimateComponents.gasEstimate.sub( gasEstimateComponents.gasEstimateForL1 ); + + // Size in bytes of the calldata to post on L1 const L1S = 140 + utils.hexDataLength(txData.data); + + // Estimated L1 gas cost const L1C = gasComponents[1].mul(L1S); + + // Extra buffer const B = L1C.div(gasComponents[5]); // G (Gas Limit) = l2GasUsed + B diff --git a/scripts/socket-helpers/main.ts b/scripts/socket-helpers/main.ts index d93ee5d8..9fbd54d3 100644 --- a/scripts/socket-helpers/main.ts +++ b/scripts/socket-helpers/main.ts @@ -5,27 +5,22 @@ import PlugABI from "@socket.tech/dl-core/artifacts/abi/IPlug.json"; import { ChainDetails, Inputs, getPayload } from "./utils"; import { getJsonRpcUrl } from "../constants"; -import { ChainSlug, arbChains, arbL3Chains, getAddresses } from "../../src"; +import { arbChains, arbL3Chains, getAddresses } from "../../src"; import { getArbitrumGasLimitEstimate } from "./arb-estimate"; -import { getOptimismGasLimitEstimate } from "./opt-estimate"; +import { getOpAndEthGasLimitEstimate } from "./op-n-eth-estimate"; -export const getEstimatedGasLimit = async ( +export const main = async ( chainDetails: ChainDetails, inputs: Inputs, withoutHook?: boolean ): Promise => { - const srcChainSlug = chainDetails.srcChainSlug as ChainSlug; - const dstChainSlug = chainDetails.dstChainSlug as ChainSlug; + const srcChainSlug = chainDetails.srcChainSlug; + const dstChainSlug = chainDetails.dstChainSlug; const provider = new providers.StaticJsonRpcProvider( getJsonRpcUrl(dstChainSlug) ); - const payload = getPayload( - inputs, - inputs.connectorPlug, - provider, - withoutHook - ); + const payload = await getPayload(inputs, provider, withoutHook); const abiInterface = new utils.Interface(PlugABI); const data = abiInterface.encodeFunctionData("inbound", [ @@ -38,10 +33,28 @@ export const getEstimatedGasLimit = async ( to: inputs.connectorPlug, data, }; + if ( arbChains.includes(chainDetails.dstChainSlug) || arbL3Chains.includes(chainDetails.dstChainSlug) ) { return await getArbitrumGasLimitEstimate(provider, txData); - } else return await getOptimismGasLimitEstimate(provider, txData); + } else { + // works for opt and eth like chains + return await getOpAndEthGasLimitEstimate(provider, txData); + } }; + +main( + { + srcChainSlug: 42161, + dstChainSlug: 1324967486, + }, + { + amount: "2000000000", + connectorPlug: "0x663dc7e91157c58079f55c1bf5ee1bdb6401ca7a", + executionData: "0x", + receiver: "0x663dc7e91157c58079f55c1bf5ee1bdb6401ca7a", + }, + false +); diff --git a/scripts/socket-helpers/opt-estimate.ts b/scripts/socket-helpers/op-n-eth-estimate.ts similarity index 68% rename from scripts/socket-helpers/opt-estimate.ts rename to scripts/socket-helpers/op-n-eth-estimate.ts index 2fbddbfd..b7806187 100644 --- a/scripts/socket-helpers/opt-estimate.ts +++ b/scripts/socket-helpers/op-n-eth-estimate.ts @@ -1,10 +1,11 @@ import { BigNumber } from "ethers"; +import { StaticJsonRpcProvider } from "@ethersproject/providers"; import { asL2Provider } from "@eth-optimism/sdk"; import { TxData } from "./utils"; // Get optimism gas limit from the SDK -export const getOptimismGasLimitEstimate = async ( - provider, +export const getOpAndEthGasLimitEstimate = async ( + provider: StaticJsonRpcProvider, txData: TxData ): Promise => { const l2Provider = asL2Provider(provider); diff --git a/scripts/socket-helpers/utils.ts b/scripts/socket-helpers/utils.ts index 8ad23842..cd3cbb1b 100644 --- a/scripts/socket-helpers/utils.ts +++ b/scripts/socket-helpers/utils.ts @@ -1,6 +1,8 @@ import { Contract, utils } from "ethers"; import { defaultAbiCoder } from "ethers/lib/utils"; +import { StaticJsonRpcProvider } from "@ethersproject/providers"; import PlugABI from "@socket.tech/dl-core/artifacts/abi/IPlug.json"; +import { ChainSlug } from "../../src"; export type TxData = { from: string; @@ -16,8 +18,8 @@ export type Inputs = { }; export type ChainDetails = { - srcChainSlug: number; - dstChainSlug: number; + srcChainSlug: ChainSlug; + dstChainSlug: ChainSlug; }; export const abiInterface = new utils.Interface(PlugABI); @@ -40,8 +42,7 @@ const ConnectorABI = [ export const getPayload = async ( inputs: Inputs, - connectorAddress: string, - provider, + provider: StaticJsonRpcProvider, withoutHook?: boolean ) => { let payload; @@ -52,7 +53,7 @@ export const getPayload = async ( ); } else { const connectorContract = new Contract( - connectorAddress, + inputs.connectorPlug, ConnectorABI, provider );