Skip to content

Commit

Permalink
Merge pull request #540 from hemilabs/cap-gas-limit-tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
gndelia authored Sep 23, 2024
2 parents 29210d2 + 8ed7f70 commit 84e92d3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
40 changes: 38 additions & 2 deletions patches/@eth-optimism+sdk+3.2.1.patch
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,27 @@ index 743d4b4..6eafe8d 100644
waitForMessageReceipt(message: MessageLike, opts?: {
fromBlockOrBlockHash?: BlockTag;
diff --git a/node_modules/@eth-optimism/sdk/dist/cross-chain-messenger.js b/node_modules/@eth-optimism/sdk/dist/cross-chain-messenger.js
index 01ac898..bc760d9 100644
index 01ac898..fcaeb3c 100644
--- a/node_modules/@eth-optimism/sdk/dist/cross-chain-messenger.js
+++ b/node_modules/@eth-optimism/sdk/dist/cross-chain-messenger.js
@@ -113,7 +113,7 @@ class CrossChainMessenger {
return legacyL1XDM.populateTransaction.relayMessage(resolved.target, resolved.sender, resolved.message, resolved.messageNonce, proof, (opts === null || opts === void 0 ? void 0 : opts.overrides) || {});
}
},
- depositETH: async (amount, opts, isEstimatingGas = false) => {
+ depositETH: async (amount, opts, isEstimatingGas = true) => {
const getOpts = async () => {
if (isEstimatingGas) {
return opts;
@@ -130,7 +130,7 @@ class CrossChainMessenger {
const bridge = await this.getBridgeForTokenPair(l1Token, l2Token);
return bridge.populateTransaction.approve(l1Token, l2Token, amount, opts);
},
- depositERC20: async (l1Token, l2Token, amount, opts, isEstimatingGas = false) => {
+ depositERC20: async (l1Token, l2Token, amount, opts, isEstimatingGas = true) => {
const bridge = await this.getBridgeForTokenPair(l1Token, l2Token);
const getOpts = async () => {
var _a, _b, _c, _d;
@@ -439,7 +439,7 @@ class CrossChainMessenger {
return b.blockNumber - a.blockNumber;
});
Expand Down Expand Up @@ -56,7 +74,7 @@ index 01ac898..bc760d9 100644
const messageHashV1 = (0, core_utils_1.hashCrossDomainMessagev1)(resolved.messageNonce, resolved.sender, resolved.target, resolved.value, resolved.minGasLimit, resolved.message);
const messenger = resolved.direction === interfaces_1.MessageDirection.L1_TO_L2
diff --git a/node_modules/@eth-optimism/sdk/src/cross-chain-messenger.ts b/node_modules/@eth-optimism/sdk/src/cross-chain-messenger.ts
index 7d17f33..60a9f9e 100644
index 7d17f33..61f6ee6 100644
--- a/node_modules/@eth-optimism/sdk/src/cross-chain-messenger.ts
+++ b/node_modules/@eth-optimism/sdk/src/cross-chain-messenger.ts
@@ -620,7 +620,8 @@ export class CrossChainMessenger {
Expand Down Expand Up @@ -102,3 +120,21 @@ index 7d17f33..60a9f9e 100644
// legacy withdrawals relayed prebedrock are v1
const messageHashV0 = hashCrossDomainMessagev0(
resolved.target,
@@ -2150,7 +2154,7 @@ export class CrossChainMessenger {
l2GasLimit?: NumberLike
overrides?: PayableOverrides
},
- isEstimatingGas: boolean = false
+ isEstimatingGas: boolean = true
): Promise<TransactionRequest> => {
const getOpts = async () => {
if (isEstimatingGas) {
@@ -2240,7 +2244,7 @@ export class CrossChainMessenger {
l2GasLimit?: NumberLike
overrides?: CallOverrides
},
- isEstimatingGas: boolean = false
+ isEstimatingGas: boolean = true
): Promise<TransactionRequest> => {
const bridge = await this.getBridgeForTokenPair(l1Token, l2Token)
// we need extra buffer for gas limit
41 changes: 34 additions & 7 deletions webapp/hooks/useL2Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ import { useAccount } from 'wagmi'
import { useEstimateFees } from './useEstimateFees'
import { useHemi } from './useHemi'

// Adding a cap to gasLimit for L1 operations in the tunnel, as the SDK overestimates
// the gas estimation by many orders of magnitude, causing the app to show a extremely high gas estimation that is later not paid
// This number was calculated by Max after analyzing several Tunnel operations in Sepolia, and may be removed in the future
// See https://github.com/hemilabs/ui-monorepo/issues/539
const l1GasLimitOverride = 2_000_000

const tunnelOverrides = {
// enable usage of EIP-1559
overrides: { type: 2 },
}

const l1Overrides = merge(
{
overrides: {
gasLimit: l1GasLimitOverride,
},
},
tunnelOverrides,
)

type GasEstimationOperations = Extract<
keyof CrossChainMessengerProxy['estimateGas'],
| 'depositERC20'
Expand Down Expand Up @@ -99,13 +114,24 @@ const useEstimateGasFees = function <T extends GasEstimationOperations>({
walletConnectedToChain,
)

const hardcodedOps = [
'depositERC20',
'depositETH',
'finalizeMessage',
'proveMessage',
]

const { data = BigInt(0), status } = useQuery({
enabled:
enabled &&
isConnectedToExpectedChain &&
crossChainMessengerStatus === 'success' &&
Object.keys(crossChainMessenger.estimateGas).length > 0,
async queryFn() {
if (hardcodedOps.includes(operation)) {
// See https://github.com/hemilabs/ui-monorepo/issues/539
return BigInt(l1GasLimitOverride)
}
// @ts-expect-error this works, unsure why TS is not picking it up
const estimate = await crossChainMessenger.estimateGas[operation](...args)
return estimate.toBigInt()
Expand All @@ -123,6 +149,10 @@ const useEstimateGasFees = function <T extends GasEstimationOperations>({
chainId: walletConnectedToChain,
enabled: status === 'success',
gasUnits: data,
// As the gas limit is hardcoded for some operations, we don't need an overestimation
// use 1 to get the exact same value
// See https://github.com/hemilabs/ui-monorepo/issues/539
overEstimation: hardcodedOps.includes(operation) ? 1 : undefined,
})
}

Expand Down Expand Up @@ -333,7 +363,7 @@ export const useDepositErc20Token = function ({
l1Address,
l2Address,
amount,
tunnelOverrides,
l1Overrides,
)
return response.hash as Hash
},
Expand Down Expand Up @@ -388,10 +418,7 @@ export const useDepositNativeToken = function ({
reset: resetDepositNativeToken,
} = useMutation({
async mutationFn(amount: string) {
const response = await crossChainMessenger.depositETH(
amount,
tunnelOverrides,
)
const response = await crossChainMessenger.depositETH(amount, l1Overrides)
return response.hash as Hash
},
})
Expand Down Expand Up @@ -495,7 +522,7 @@ export const useFinalizeMessage = function ({
async mutationFn(toFinalize: Hash) {
const response = await crossChainMessenger.finalizeMessage(
toFinalize,
tunnelOverrides,
l1Overrides,
)
return response.hash as Hash
},
Expand Down Expand Up @@ -544,7 +571,7 @@ export const useProveMessage = function ({
async mutationFn(toProve: Hash) {
const response = await crossChainMessenger.proveMessage(
toProve,
tunnelOverrides,
l1Overrides,
)
return response.hash as Hash
},
Expand Down

0 comments on commit 84e92d3

Please sign in to comment.