Skip to content

Commit

Permalink
chore: add gas estimation quality metric (#767)
Browse files Browse the repository at this point in the history
* chore: add gas estimation quality metric

* fix

* metric in gas units

* extra metric

* fix test
  • Loading branch information
xrsv authored Dec 9, 2024
1 parent 94375a7 commit cd071ee
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/providers/eth-estimate-gas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BEACON_CHAIN_DEPOSIT_ADDRESS, log } from '../util';
import {
calculateGasUsed,
initSwapRouteFromExisting,
logGasEstimationVsSimulationMetrics,
} from '../util/gas-factory-helpers';

import { IPortionProvider } from './portion-provider';
Expand Down Expand Up @@ -125,6 +126,9 @@ export class EthEstimateGasSimulator extends Simulator {
this.provider,
providerConfig
);

logGasEstimationVsSimulationMetrics(route, estimatedGasUsed, this.chainId);

return {
...initSwapRouteFromExisting(
route,
Expand Down
4 changes: 4 additions & 0 deletions src/providers/tenderly-simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { APPROVE_TOKEN_FOR_TRANSFER } from '../util/callData';
import {
calculateGasUsed,
initSwapRouteFromExisting,
logGasEstimationVsSimulationMetrics,
} from '../util/gas-factory-helpers';

import { breakDownTenderlySimulationError } from '../util/tenderlySimulationErrorBreakDown';
Expand Down Expand Up @@ -707,6 +708,9 @@ export class TenderlySimulator extends Simulator {
this.provider,
providerConfig
);

logGasEstimationVsSimulationMetrics(swapRoute, estimatedGasUsed, chainId);

return {
...initSwapRouteFromExisting(
swapRoute,
Expand Down
59 changes: 59 additions & 0 deletions src/util/gas-factory-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
GasModelProviderConfig,
getQuoteThroughNativePool,
MethodParameters,
metric,
MetricLoggerUnit,
MixedRouteWithValidQuote,
RouteWithValidQuote,
SwapOptions,
Expand Down Expand Up @@ -690,3 +692,60 @@ export const calculateL1GasFeesHelper = async (
return calculateArbitrumToL1FeeFromCalldata(data, gasData, chainId);
}
};

// Logs metrics about the diff between original estimatedGasUsed based on heuristics and the simulated gas used.
// This will help us track the quality of our gas estimation quality per chain.
export const logGasEstimationVsSimulationMetrics = (
route: SwapRoute,
simulationGasUsed: BigNumber,
chainId: ChainId
) => {
try {
// Log the diff between original estimatedGasUsed and the simulated gas used
const estimatedGasUsed = route.estimatedGasUsed.toNumber();
const simulatedGasUsed = simulationGasUsed.toNumber();
const diff = estimatedGasUsed - simulatedGasUsed;
const absDiff = Math.abs(estimatedGasUsed - simulatedGasUsed);
const misEstimatePercent = (diff / estimatedGasUsed) * 100;
const misEstimateAbsPercent = (absDiff / estimatedGasUsed) * 100;

log.info(
{
estimatedGasUsed: estimatedGasUsed,
simulatedGasUsed: simulatedGasUsed,
absDiff: absDiff,
diff: diff,
},
'Gas used diff between estimatedGasUsed and simulatedGasUsed'
);
log.info(
{
misEstimateAbsPercent: misEstimateAbsPercent,
},
'Gas used mis-estimate percent'
);

metric.putMetric(
`TenderlySimulationGasUsed_AbsDiff_Chain_${chainId}`,
absDiff,
MetricLoggerUnit.Count
);
metric.putMetric(
`TenderlySimulationGasUsed_MisEstimateAbsPercent_Chain_${chainId}`,
misEstimateAbsPercent,
MetricLoggerUnit.Count
);

const label = diff >= 0 ? 'OverEstimate' : 'UnderEstimate';
metric.putMetric(
`TenderlySimulationGasUsed_${label}Percent_Chain_${chainId}`,
misEstimatePercent,
MetricLoggerUnit.Count
);
} catch (err) {
log.error(
{ err: err },
'Failed to log diff between original estimatedGasUsed and the simulated gas used'
);
}
};
1 change: 1 addition & 0 deletions test/unit/providers/simulation-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jest.mock('../../../src/util/gas-factory-helpers', () => ({
quoteGasAdjusted,
};
},
logGasEstimationVsSimulationMetrics: jest.fn(),
}));

const provider = new JsonRpcProvider();
Expand Down

0 comments on commit cd071ee

Please sign in to comment.