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

better gas estimation by incorporating estimated overhead for action #448

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion libs/tx-builder/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LOCAL_ABI } from '@daohaus/abis';
export const EXPIRY = '.proposalExpiry';
export const FORM = '.formValues';
export const CURRENT_DAO = '.daoId';
export const gasBufferMultiplier = 5;
export const gasBufferMultiplier = 1.2; // buffers baalgas estimate
export const BaalContractBase = {
type: 'local',
contractName: 'Baal',
Expand Down
24 changes: 18 additions & 6 deletions libs/tx-builder/src/utils/multicall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MulticallArg,
StringSearch,
TXLego,
ACTION_GAS_LIMIT_ADDITION,
} from '@daohaus/utils';
import {
CONTRACT_KEYCHAINS,
Expand All @@ -39,14 +40,14 @@ import { createViemClient } from '@daohaus/utils';

export const estimateFunctionalGas = async ({
chainId,
constractAddress,
contractAddress,
from,
value,
data,
rpcs = HAUS_RPC,
}: {
chainId: ValidNetwork;
constractAddress: string;
contractAddress: string;
from: string;
value: bigint;
data: string;
Expand All @@ -59,11 +60,13 @@ export const estimateFunctionalGas = async ({

const functionGasFees = await client.estimateGas({
account: from as EthAddress,
to: constractAddress as EthAddress,
to: contractAddress as EthAddress,
value,
data: data as `0x${string}`,
});

console.log('functionGasFees', functionGasFees);

return Number(functionGasFees);
};

Expand Down Expand Up @@ -284,10 +287,12 @@ export const handleMulticallArg = async ({

export const gasEstimateFromActions = async ({
actions,
actionsCount,
chainId,
daoId,
}: {
actions: MetaTransaction[];
actionsCount: number;
chainId: ValidNetwork;
daoId: string;
safeId: string; // not used at the moment
Expand All @@ -297,10 +302,10 @@ export const gasEstimateFromActions = async ({
async (action) =>
await estimateFunctionalGas({
chainId: chainId,
constractAddress: action.to,
contractAddress: action.to,
from: daoId, // from value needs to be the safe module (baal) to estimate without revert
value: BigInt(Number(action.value)),
data: action.data,
data: action.data
})
)
);
Expand All @@ -310,9 +315,14 @@ export const gasEstimateFromActions = async ({
(a, b) => (a || 0) + (b || 0),
0
);


// extra gas overhead when calling the dao from the baal safe
const baalOnlyGas = actionsCount * ACTION_GAS_LIMIT_ADDITION;

console.log('totalGasEstimate', totalGasEstimate);
santteegt marked this conversation as resolved.
Show resolved Hide resolved

return totalGasEstimate;
return (totalGasEstimate || 0) + baalOnlyGas;
};

export const handleEncodeMulticallArg = async ({
Expand Down Expand Up @@ -386,12 +396,14 @@ export const handleGasEstimate = async ({
} as MetaTransaction;
const gasEstimate = await gasEstimateFromActions({
actions: encodeExecFromModule({ safeId, metaTx }),
actionsCount: actions.length,
chainId,
daoId,
safeId,
});

if (gasEstimate) {
// adds buffer to baalgas estimate
const buffer = arg.bufferPercentage || gasBufferMultiplier;
return Math.round(Number(gasEstimate) * Number(buffer));
} else {
Expand Down
7 changes: 5 additions & 2 deletions libs/utils/src/constants/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ export const PROPOSAL_FILTERS: Record<string, string> = {
failed: 'Defeated',
expired: 'Expired',
};

export const GAS_BUFFER_MULTIPLIER = 5;
// Processing gas estimate buffer
export const GAS_BUFFER_MULTIPLIER = 2;
// Adding to the gas limit to account for cost of processProposal
export const PROCESS_PROPOSAL_GAS_LIMIT_ADDITION = 150000;
// Adding to the gas limit to account for cost of each action
export const ACTION_GAS_LIMIT_ADDITION = 150000;

export const L2_ADDITIONAL_GAS = 5000000;
2 changes: 1 addition & 1 deletion libs/utils/src/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getGasCostEstimate = async (
): Promise<number | undefined> => {
const feeDataNew = await fetchFeeData({ chainId: chainId as ValidNetwork });

console.log('feeDataNew', feeDataNew);
// console.log('feeDataNew', feeDataNew);
santteegt marked this conversation as resolved.
Show resolved Hide resolved
return (
Number(getProcessingGasLimit(gasLimit, chainId)) *
Number(feeDataNew.maxFeePerGas || 0)
Expand Down
Loading