Skip to content

Commit

Permalink
Merge pull request #389 from SocketDotTech/fix/limit-estimate-helpers
Browse files Browse the repository at this point in the history
gas limit estimate helpers
  • Loading branch information
ameeshaagrawal authored Jul 3, 2024
2 parents a26bbce + 6026bfe commit 6636566
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 98 deletions.
44 changes: 16 additions & 28 deletions scripts/socket-helpers/arb-estimate.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,43 @@
require("dotenv").config();
import { utils, BigNumber, providers } from "ethers";
import { utils, BigNumber } from "ethers";
import { ArbGasInfo__factory } from "@arbitrum/sdk/dist/lib/abi/factories/ArbGasInfo__factory";
import { NodeInterface__factory } from "@arbitrum/sdk/dist/lib/abi/factories/NodeInterface__factory";
import {
ARB_GAS_INFO,
NODE_INTERFACE_ADDRESS,
} from "@arbitrum/sdk/dist/lib/dataEntities/constants";
import { defaultAbiCoder } from "ethers/lib/utils";
import { DeploymentMode, getAddresses } from "@socket.tech/dl-core";
import PlugABI from "@socket.tech/dl-core/artifacts/abi/IPlug.json";
import { addresses, dstChainSlug, srcChainSlug } from "./config";
import { TxData } from "./utils";
import { StaticJsonRpcProvider } from "@ethersproject/providers";

const dstChainRPC = process.env.ARBITRUM_RPC;

export const getArbitrumGasEstimate = async (
amount: string,
receiver: string
export const getArbitrumGasLimitEstimate = async (
provider: StaticJsonRpcProvider,
txData: TxData
): Promise<BigNumber> => {
const provider = new providers.StaticJsonRpcProvider(dstChainRPC);

const arbGasInfo = ArbGasInfo__factory.connect(ARB_GAS_INFO, provider);
const nodeInterface = NodeInterface__factory.connect(
NODE_INTERFACE_ADDRESS,
provider
);
// Getting the gas prices from ArbGasInfo.getPricesInWei()
const gasComponents = await arbGasInfo.callStatic.getPricesInWei();

const payload = defaultAbiCoder.encode(
["address", "uint256"],
[receiver, amount]
);
const abiInterface = new utils.Interface(PlugABI);
const txData = abiInterface.encodeFunctionData("inbound", [
srcChainSlug,
payload,
]);

const gasEstimateComponents =
await nodeInterface.callStatic.gasEstimateComponents(
addresses[dstChainSlug].USDC.connectors[srcChainSlug].FAST,
txData.to,
false,
txData,
{ from: getAddresses(dstChainSlug, DeploymentMode.PROD).Socket }
txData.data,
{ from: txData.from }
);

const l2GasUsed = gasEstimateComponents.gasEstimate.sub(
gasEstimateComponents.gasEstimateForL1
);
const L1S = 140 + utils.hexDataLength(txData);

// 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
35 changes: 0 additions & 35 deletions scripts/socket-helpers/config.ts

This file was deleted.

60 changes: 60 additions & 0 deletions scripts/socket-helpers/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require("dotenv").config();
import { BigNumber, providers, utils } from "ethers";
import { DeploymentMode } from "@socket.tech/dl-core";
import PlugABI from "@socket.tech/dl-core/artifacts/abi/IPlug.json";

import { ChainDetails, Inputs, getPayload } from "./utils";
import { getJsonRpcUrl } from "../constants";
import { arbChains, arbL3Chains, getAddresses } from "../../src";
import { getArbitrumGasLimitEstimate } from "./arb-estimate";
import { getOpAndEthGasLimitEstimate } from "./op-n-eth-estimate";

export const main = async (
chainDetails: ChainDetails,
inputs: Inputs,
withoutHook?: boolean
): Promise<BigNumber> => {
const srcChainSlug = chainDetails.srcChainSlug;
const dstChainSlug = chainDetails.dstChainSlug;

const provider = new providers.StaticJsonRpcProvider(
getJsonRpcUrl(dstChainSlug)
);
const payload = await getPayload(inputs, provider, withoutHook);

const abiInterface = new utils.Interface(PlugABI);
const data = abiInterface.encodeFunctionData("inbound", [
srcChainSlug,
payload,
]);

const txData = {
from: getAddresses(dstChainSlug, DeploymentMode.PROD).Socket,
to: inputs.connectorPlug,
data,
};

if (
arbChains.includes(chainDetails.dstChainSlug) ||
arbL3Chains.includes(chainDetails.dstChainSlug)
) {
return await getArbitrumGasLimitEstimate(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
);
14 changes: 14 additions & 0 deletions scripts/socket-helpers/op-n-eth-estimate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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 getOpAndEthGasLimitEstimate = async (
provider: StaticJsonRpcProvider,
txData: TxData
): Promise<BigNumber> => {
const l2Provider = asL2Provider(provider);
const gasLimit = await l2Provider.estimateGas(txData);
return gasLimit;
};
35 changes: 0 additions & 35 deletions scripts/socket-helpers/opt-estimate.ts

This file was deleted.

68 changes: 68 additions & 0 deletions scripts/socket-helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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;
to: string;
data: string;
};

export type Inputs = {
amount: string;
receiver: string;
executionData: string;
connectorPlug: string;
};

export type ChainDetails = {
srcChainSlug: ChainSlug;
dstChainSlug: ChainSlug;
};

export const abiInterface = new utils.Interface(PlugABI);

const ConnectorABI = [
{
inputs: [],
name: "getMessageId",
outputs: [
{
internalType: "bytes32",
name: "",
type: "bytes32",
},
],
stateMutability: "view",
type: "function",
},
];

export const getPayload = async (
inputs: Inputs,
provider: StaticJsonRpcProvider,
withoutHook?: boolean
) => {
let payload;
if (withoutHook) {
payload = defaultAbiCoder.encode(
["address", "uint256"],
[inputs.receiver, inputs.amount]
);
} else {
const connectorContract = new Contract(
inputs.connectorPlug,
ConnectorABI,
provider
);
const msgId = await connectorContract.getMessageId();
payload = defaultAbiCoder.encode(
["address", "uint256", "bytes32", "bytes"],
[inputs.receiver, inputs.amount, msgId, inputs.executionData]
);
}

return payload;
};

0 comments on commit 6636566

Please sign in to comment.