Skip to content

Commit

Permalink
fix: gas limit issue in testnet, close #2710
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 committed Jan 13, 2025
1 parent 50984c1 commit 1099f73
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4820,6 +4820,8 @@ export class WalletController extends BaseController {

getCustomTestnetGasPrice = customTestnetService.getGasPrice;

getCustomBlockGasLimit = customTestnetService.getBlockGasLimit;

getCustomTestnetGasMarket = customTestnetService.getGasMarket;

getCustomTestnetToken = customTestnetService.getToken;
Expand Down
10 changes: 10 additions & 0 deletions src/background/service/customTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createClient, defineChain, erc20Abi, http, isAddress } from 'viem';
import {
estimateGas,
getBalance,
getBlock,
getGasPrice,
getTransactionCount,
getTransactionReceipt,
Expand Down Expand Up @@ -347,6 +348,15 @@ class CustomTestnetService {
return res.toString();
};

getBlockGasLimit = async (chainId: number) => {
const client = this.getClient(+chainId);
if (!client) {
throw new Error(`Invalid chainId: ${chainId}`);
}
const res = await getBlock(client);
return res.gasLimit.toString();
};

getGasMarket = async ({
chainId,
custom,
Expand Down
24 changes: 18 additions & 6 deletions src/ui/views/Approval/components/SignTestnetTx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,32 @@ export const SignTestnetTx = ({ params, origin }: SignTxProps) => {
async () => {
try {
const currentAccount = (await wallet.getCurrentAccount())!;
const res = await wallet.estimateCustomTestnetGas({
const estimateGas = await wallet.estimateCustomTestnetGas({
address: currentAccount.address,
chainId: chainId,
tx: tx,
});
const blockGasLimit = await wallet.getCustomBlockGasLimit(chainId);

if (!gasLimit) {
let recommendGasLimit = new BigNumber(estimateGas)
.times(DEFAULT_GAS_LIMIT_RATIO)
.toFixed(0);

if (
blockGasLimit &&
new BigNumber(recommendGasLimit).gt(blockGasLimit)
) {
recommendGasLimit = new BigNumber(blockGasLimit)
.times(0.95)
.toFixed(0);
}

setGasLimit(
`0x${new BigNumber(res)
.multipliedBy(1.5)
.integerValue()
.toString(16)}`
`0x${new BigNumber(recommendGasLimit).integerValue().toString(16)}`
);
}
return `0x${new BigNumber(res).integerValue().toString(16)}`;
return `0x${new BigNumber(estimateGas).integerValue().toString(16)}`;
} catch (e) {
console.error(e);
const fallback = intToHex(2000000);
Expand Down

0 comments on commit 1099f73

Please sign in to comment.