Skip to content

Commit

Permalink
Merge branch 'main' into xrsv/gas-estimate-quality-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
xrsv authored Dec 9, 2024
2 parents 0545bad + 94375a7 commit 1b76ceb
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uniswap/smart-order-router",
"version": "4.8.6",
"version": "4.8.7",
"description": "Uniswap Smart Order Router",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/providers/simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export enum SimulationStatus {
InsufficientBalance = 3,
NotApproved = 4,
SystemDown = 5,
SlippageTooLow = 6,
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/providers/tenderly-simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
logGasEstimationVsSimulationMetrics,
} from '../util/gas-factory-helpers';

import { breakDownTenderlySimulationError } from '../util/tenderlySimulationErrorBreakDown';
import { EthEstimateGasSimulator } from './eth-estimate-gas-provider';
import { IPortionProvider } from './portion-provider';
import {
Expand Down Expand Up @@ -465,6 +466,22 @@ export class TenderlySimulator extends Simulator {
2
)}.`
);

if (
resp &&
resp.result &&
resp.result.length >= 3 &&
(resp.result[2] as JsonRpcError).error &&
(resp.result[2] as JsonRpcError).error.data
) {
return {
...swapRoute,
simulationStatus: breakDownTenderlySimulationError(
(resp.result[2] as JsonRpcError).error.data
),
};
}

return { ...swapRoute, simulationStatus: SimulationStatus.Failed };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,16 @@ export abstract class TickBasedHeuristicGasModelFactory<
// Only use syntheticGasCostInTermsOfQuoteToken if it's within 30% of the original gasCostInTermsOfQuoteToken as a safeguard.
if (
syntheticGasCostInTermsOfQuoteToken !== null &&
(gasCostInTermsOfQuoteToken === null || (
syntheticGasCostInTermsOfQuoteToken.lessThan(
(gasCostInTermsOfQuoteToken === null ||
(syntheticGasCostInTermsOfQuoteToken.lessThan(
gasCostInTermsOfQuoteToken.asFraction
) &&
gasCostInTermsOfQuoteToken.subtract(syntheticGasCostInTermsOfQuoteToken)
.lessThan(gasCostInTermsOfQuoteToken.multiply(new Percent(30, 100)).asFraction)
))
gasCostInTermsOfQuoteToken
.subtract(syntheticGasCostInTermsOfQuoteToken)
.lessThan(
gasCostInTermsOfQuoteToken.multiply(new Percent(30, 100))
.asFraction
)))
) {
log.info(
{
Expand Down
21 changes: 21 additions & 0 deletions src/util/tenderlySimulationErrorBreakDown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SimulationStatus } from '../providers';

export function breakDownTenderlySimulationError(
data?: string
): SimulationStatus {
if (data) {
switch (data) {
case '0x739dbe52': // V3TooMuchRequested
case '0x39d35496': // V3TooLittleReceived
case '0x849eaf98': // V2TooLittleReceived
case '0x8ab0bc16': // V2TooMuchRequested
case '0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000025556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54000000000000000000000000000000000000000000000000000000': // INSUFFICIENT_OUTPUT_AMOUNT
case '0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034949410000000000000000000000000000000000000000000000000000000000': // IIA
return SimulationStatus.SlippageTooLow;
default: // we don't know why onchain execution reverted, just return generic failed.
return SimulationStatus.Failed;
}
}

return SimulationStatus.Failed;
}
46 changes: 46 additions & 0 deletions test/unit/util/tenderlySimulationErrorBreakDown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
breakDownTenderlySimulationError
} from '../../../src/util/tenderlySimulationErrorBreakDown';
import { SimulationStatus } from '../../../build/main';

describe('tenderly simulation error break down', () => {
it('V3TooMuchRequested', async () => {
const simulationStatus = breakDownTenderlySimulationError('0x739dbe52')
expect(simulationStatus).toEqual(SimulationStatus.SlippageTooLow)
})

it('V3TooLittleReceived', async () => {
const simulationStatus = breakDownTenderlySimulationError('0x39d35496')
expect(simulationStatus).toEqual(SimulationStatus.SlippageTooLow)
})

it('V2TooLittleReceived', async () => {
const simulationStatus = breakDownTenderlySimulationError('0x849eaf98')
expect(simulationStatus).toEqual(SimulationStatus.SlippageTooLow)
})

it('V2TooMuchRequested', async () => {
const simulationStatus = breakDownTenderlySimulationError('0x8ab0bc16')
expect(simulationStatus).toEqual(SimulationStatus.SlippageTooLow)
})

it('INSUFFICIENT_OUTPUT_AMOUNT', async () => {
const simulationStatus = breakDownTenderlySimulationError('0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000025556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54000000000000000000000000000000000000000000000000000000');
expect(simulationStatus).toEqual(SimulationStatus.SlippageTooLow)
})

it('IIA', async () => {
const simulationStatus = breakDownTenderlySimulationError('0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034949410000000000000000000000000000000000000000000000000000000000');
expect(simulationStatus).toEqual(SimulationStatus.SlippageTooLow)
})

it('InsufficientToken', () => {
const simulationStatus = breakDownTenderlySimulationError('0x675cae38');
expect(simulationStatus).toEqual(SimulationStatus.Failed);
});

it('unknown data', () => {
const simulationStatus = breakDownTenderlySimulationError(undefined);
expect(simulationStatus).toEqual(SimulationStatus.Failed);
});
});

0 comments on commit 1b76ceb

Please sign in to comment.