Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(berajs): add gas limit estimation to prevent hitting gas limits … #258

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/berajs/src/hooks/modules/dex/useCreatePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { balancerPoolCreationHelperAbi } from "~/abi";
import { ADDRESS_ZERO } from "~/config";
import { IContractWrite } from "~/hooks/useContractWrite";
import { Token, TokenInput } from "~/types";
import { DEFAULT_METAMASK_GAS_LIMIT } from "~/utils";

const DEFAULT_WEIGHTS_DUPLICATION_THRESHOLD = 0.005;
const DEFAULT_POOL_CREATE_GAS_LIMIT = 7920027n; // NOTE: this is the metamask gas limit, in experiments we find we can easily use 75% of this.

interface UseCreatePoolProps {
poolCreateTokens: Token[];
Expand Down Expand Up @@ -56,7 +56,7 @@ const createStablePoolArgs = (
poolSymbol: string,
owner: string,
amplification: number,
gasLimit: bigint = DEFAULT_POOL_CREATE_GAS_LIMIT,
gasLimit: bigint = DEFAULT_METAMASK_GAS_LIMIT,
) => {
// Map and sort pool creation token addresses NOTE: we should never see BERA in this array.
const sortedPoolCreateAddresses = poolCreateTokens
Expand Down Expand Up @@ -135,7 +135,7 @@ const createWeightedPoolArgs = (
poolName: string,
poolSymbol: string,
owner: string,
gasLimit: bigint = DEFAULT_POOL_CREATE_GAS_LIMIT,
gasLimit: bigint = DEFAULT_METAMASK_GAS_LIMIT,
) => {
// When joining weighted pools with BERA, we allow it as a joinPoolToken but never as a createPoolToken.
const createPoolTokens: string[] = [];
Expand Down
76 changes: 63 additions & 13 deletions packages/berajs/src/hooks/useContractWrite/useBeraContractWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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,
Expand Down Expand Up @@ -45,33 +46,82 @@ const useBeraContractWrite = ({
params,
value = 0n,
data,
gasLimit = 2000000n,
gasLimit,
...rest
}: IContractWrite): Promise<void> => {
dispatch({ type: ActionEnum.LOADING });
onLoading?.();
let receipt: Awaited<ReturnType<typeof sendTransactionAsync>>;
if (!publicClient) return;
if (!publicClient || !account) return;
try {
// Get the next nonce for the account
const nonce = await publicClient.getTransactionCount({
address: account,
blockTag: "pending",
});

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,
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 } = await publicClient.simulateContract({
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: gasLimit ?? request.gas,
...simulationResult.value.request,
gas: estimatedGas,
nonce: nonce,
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/berajs/src/utils/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* 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;
1 change: 1 addition & 0 deletions packages/berajs/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./formatNumber";
export * from "./formatTimestamps";
export * from "./tokenWrapping";
export * from "./errorMessages";
export * from "./const";
Loading