-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #389 from SocketDotTech/fix/limit-estimate-helpers
gas limit estimate helpers
- Loading branch information
Showing
6 changed files
with
158 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |