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

chore: add gas estimation quality metric #767

Open
wants to merge 5 commits into
base: main
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
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 { EthEstimateGasSimulator } from './eth-estimate-gas-provider';
Expand Down Expand Up @@ -690,6 +691,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
Loading