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 1 commit
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
35 changes: 25 additions & 10 deletions packages/berajs/src/hooks/useContractWrite/useBeraContractWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
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 @@ -45,7 +46,8 @@ const useBeraContractWrite = ({
params,
value = 0n,
data,
gasLimit = 2000000n,
gasLimit = DEFAULT_METAMASK_GAS_LIMIT,
...rest
}: IContractWrite): Promise<void> => {
dispatch({ type: ActionEnum.LOADING });
onLoading?.();
Expand All @@ -60,18 +62,31 @@ const useBeraContractWrite = ({
gas: gasLimit,
});
} 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 [{ 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,
}),
]);

receipt = await writeContractAsync({
...request,
gas: gasLimit ?? request.gas,
gas:
increaseByPercentage(wagmiPubEstimateContractGas, 10) ?? gasLimit,
BrownBrownBear marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/berajs/src/utils/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
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
BrownBrownBear marked this conversation as resolved.
Show resolved Hide resolved
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