Skip to content

Commit

Permalink
fix: types and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ameeshaagrawal committed Jul 3, 2024
1 parent a5362e8 commit 6026bfe
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
9 changes: 8 additions & 1 deletion scripts/socket-helpers/arb-estimate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigNumber> => {
const arbGasInfo = ArbGasInfo__factory.connect(ARB_GAS_INFO, provider);
Expand All @@ -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
Expand Down
37 changes: 25 additions & 12 deletions scripts/socket-helpers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigNumber> => {
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", [
Expand All @@ -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
);
Original file line number Diff line number Diff line change
@@ -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<BigNumber> => {
const l2Provider = asL2Provider(provider);
Expand Down
11 changes: 6 additions & 5 deletions scripts/socket-helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -40,8 +42,7 @@ const ConnectorABI = [

export const getPayload = async (
inputs: Inputs,
connectorAddress: string,
provider,
provider: StaticJsonRpcProvider,
withoutHook?: boolean
) => {
let payload;
Expand All @@ -52,7 +53,7 @@ export const getPayload = async (
);
} else {
const connectorContract = new Contract(
connectorAddress,
inputs.connectorPlug,
ConnectorABI,
provider
);
Expand Down

0 comments on commit 6026bfe

Please sign in to comment.