Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 committed Dec 7, 2024
1 parent 8158337 commit 70968c5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/providers/tenderly-simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
initSwapRouteFromExisting,
} 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 @@ -466,15 +467,12 @@ export class TenderlySimulator extends Simulator {
);

if ((resp.result[2] as JsonRpcError).error.data) {
switch ((resp.result[2] as JsonRpcError).error.data) {
case '0x739dbe52': // V3TooMuchRequested
case '0x39d35496': // V3TooLittleReceived
case '0x849eaf98': // V2TooLittleReceived
case '0x8ab0bc16': // V2TooMuchRequested
case '0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000025556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54000000000000000000000000000000000000000000000000000000': // INSUFFICIENT_OUTPUT_AMOUNT
case '0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034949410000000000000000000000000000000000000000000000000000000000': // IIA
return { ...swapRoute, simulationStatus: SimulationStatus.SlippageTooLow };
}
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 70968c5

Please sign in to comment.