Skip to content

Commit

Permalink
Merge pull request #448 from HausDAO/feat/betterGasEstimation
Browse files Browse the repository at this point in the history
better gas estimation by incorporating estimated overhead for action
  • Loading branch information
santteegt authored Nov 9, 2023
2 parents 5f26d56 + c810678 commit e4ae1c7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
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
21 changes: 16 additions & 5 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,7 +302,7 @@ 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,
Expand All @@ -310,9 +315,13 @@ 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('baalOnlyGas addtition', baalOnlyGas);
console.log('totalGasEstimate', totalGasEstimate);

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

export const handleEncodeMulticallArg = async ({
Expand Down Expand Up @@ -386,12 +395,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;
1 change: 0 additions & 1 deletion libs/utils/src/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const getGasCostEstimate = async (
): Promise<number | undefined> => {
const feeDataNew = await fetchFeeData({ chainId: chainId as ValidNetwork });

console.log('feeDataNew', feeDataNew);
return (
Number(getProcessingGasLimit(gasLimit, chainId)) *
Number(feeDataNew.maxFeePerGas || 0)
Expand Down

0 comments on commit e4ae1c7

Please sign in to comment.