Skip to content

Commit

Permalink
fix(berajs): fix nonce and estimation values
Browse files Browse the repository at this point in the history
  • Loading branch information
BrownBrownBear committed Dec 23, 2024
1 parent 10b5153 commit a0ea4bb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 27 deletions.
79 changes: 53 additions & 26 deletions packages/berajs/src/hooks/useContractWrite/useBeraContractWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { usePublicClient, useSendTransaction, useWriteContract } from "wagmi";
import { getErrorMessage, getRevertReason } from "~/utils/errorMessages";
import { ActionEnum, initialState, reducer } from "~/utils/stateReducer";
import { useBeraJs } from "~/contexts";
import { DEFAULT_METAMASK_GAS_LIMIT } from "~/utils";
import { usePollTransactionCount } from "../usePollTransactionCount";
import {
type IContractWrite,
type IUseContractWriteArgs,
type useContractWriteApi,
} from "./types";
import { DEFAULT_METAMASK_GAS_LIMIT } from "~/utils";

const increaseByPercentage = (value: bigint, percentage: number) => {
return value + (value * BigInt(percentage)) / BigInt(100);
Expand Down Expand Up @@ -46,7 +46,7 @@ const useBeraContractWrite = ({
params,
value = 0n,
data,
gasLimit = DEFAULT_METAMASK_GAS_LIMIT,
gasLimit,
...rest
}: IContractWrite): Promise<void> => {
dispatch({ type: ActionEnum.LOADING });
Expand All @@ -61,40 +61,67 @@ const useBeraContractWrite = ({
});

if (data) {
// Add gas estimation for direct transactions
const estimatedGas =
gasLimit ??
(await publicClient
.estimateGas({
account,
to: address,
data,
value,
})
.catch(() => DEFAULT_METAMASK_GAS_LIMIT));

receipt = await sendTransactionAsync({
data,
to: address,
value,
gas: gasLimit,
nonce: nonce + 1,
gas: estimatedGas,
nonce: nonce,
});
} else {
// Run simulation and gas estimation in parallel
// TODO: figure out clean way to early detect errors and effectively show them on the UI
const [{ request }, wagmiPubEstimateContractGas] = await Promise.all([
publicClient.simulateContract({
address: address,
abi: abi,
functionName: functionName,
args: params,
value: value,
account: account,
}),
publicClient.estimateContractGas({
address: address,
abi: abi,
functionName: functionName,
args: params,
value: value,
account: account,
}),
]);
const [simulationResult, gasEstimateResult] =
await Promise.allSettled([
publicClient.simulateContract({
address: address,
abi: abi,
functionName: functionName,
args: params,
value: value,
account: account,
}),
// Only estimate gas if no gasLimit is provided
...(!gasLimit
? [
publicClient.estimateContractGas({
address: address,
abi: abi,
functionName: functionName,
args: params,
value: value,
account: account,
}),
]
: []),
]);

if (simulationResult.status === "rejected") {
throw simulationResult.reason;
}

const estimatedGas =
gasLimit ??
(gasEstimateResult.status === "fulfilled"
? increaseByPercentage(gasEstimateResult.value, 10)
: DEFAULT_METAMASK_GAS_LIMIT);

receipt = await writeContractAsync({
...request,
gas:
increaseByPercentage(wagmiPubEstimateContractGas, 10) ?? gasLimit,
nonce: nonce + 1,
...simulationResult.value.request,
gas: estimatedGas,
nonce: nonce,
});
}

Expand Down
6 changes: 5 additions & 1 deletion packages/berajs/src/utils/const.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const DEFAULT_METAMASK_GAS_LIMIT = 7920027n; // NOTE: this is the metamask gas limit, https://github.com/MetaMask/metamask-extension/blob/06cf7459b963e26d0ae60312d4f9a342d07ab89d/ui/pages/confirmations/send/send.constants.js#L8
/**
* Default gas limit used by MetaMask
* @see {@link https://github.com/MetaMask/metamask-extension/blob/06cf7459b963e26d0ae60312d4f9a342d07ab89d/ui/pages/confirmations/send/send.constants.js#L8}
*/
export const DEFAULT_METAMASK_GAS_LIMIT = 7920027n;

0 comments on commit a0ea4bb

Please sign in to comment.