Skip to content

Commit

Permalink
fix: map virtual swap InsufficientToken as SlippageTooLow (#784)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsy1218 authored Dec 13, 2024
1 parent 31ec714 commit 69eed4a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/providers/tenderly-simulation-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ export class TenderlySimulator extends Simulator {
): Promise<SwapRoute> {
const currencyIn = swapRoute.trade.inputAmount.currency;
const tokenIn = currencyIn.wrapped;
const currencyOut = swapRoute.trade.outputAmount.currency;
const tokenOut = currencyOut.wrapped;
const chainId = this.chainId;

if (TENDERLY_NOT_SUPPORTED_CHAINS.includes(chainId)) {
Expand Down Expand Up @@ -492,6 +494,8 @@ export class TenderlySimulator extends Simulator {
return {
...swapRoute,
simulationStatus: breakDownTenderlySimulationError(
tokenIn,
tokenOut,
(resp.result[2] as JsonRpcError).error.data
),
};
Expand Down
7 changes: 7 additions & 0 deletions src/providers/token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,13 @@ export const USDC_NATIVE_BASE = new Token(
'USDbC',
'USD Base Coin'
);
export const VIRTUAL_BASE = new Token(
ChainId.BASE,
'0x0b3e328455c4059EEb9e3f84b5543F74E24e7E1b',
18,
'VIRTUAL',
'Virtual Protocol'
);

// Base Goerli Tokens
export const USDC_BASE_GOERLI = new Token(
Expand Down
17 changes: 16 additions & 1 deletion src/util/tenderlySimulationErrorBreakDown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { SimulationStatus } from '../providers';
import { Token } from '@uniswap/sdk-core';
import { SimulationStatus, VIRTUAL_BASE } from '../providers';

export function breakDownTenderlySimulationError(
tokenIn: Token,
tokenOut: Token,
data?: string
): SimulationStatus {
if (data) {
Expand All @@ -12,6 +15,18 @@ export function breakDownTenderlySimulationError(
case '0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000025556e697377617056323a20494e53554646494349454e545f4f55545055545f414d4f554e54000000000000000000000000000000000000000000000000000000': // INSUFFICIENT_OUTPUT_AMOUNT
case '0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034949410000000000000000000000000000000000000000000000000000000000': // IIA
return SimulationStatus.SlippageTooLow;
case '0x675cae38': // InsufficientToken
if (
tokenIn.address.toLowerCase() ===
VIRTUAL_BASE.address.toLowerCase() ||
tokenOut.address.toLowerCase() === VIRTUAL_BASE.address.toLowerCase()
) {
// if this is from virtual, we'd guess it's due to slippage too low, although it might be due to something else
return SimulationStatus.SlippageTooLow;
}

// Otherwise we don't wanna guess, just return generic failed.
return SimulationStatus.Failed;
default: // we don't know why onchain execution reverted, just return generic failed.
return SimulationStatus.Failed;
}
Expand Down
28 changes: 19 additions & 9 deletions test/unit/util/tenderlySimulationErrorBreakDown.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
import {
breakDownTenderlySimulationError
} from '../../../src/util/tenderlySimulationErrorBreakDown';
import { SimulationStatus } from '../../../build/main';
import {
SimulationStatus,
USDC_MAINNET,
USDT_MAINNET,
VIRTUAL_BASE
} from '../../../build/main';

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

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

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

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

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

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

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

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

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

0 comments on commit 69eed4a

Please sign in to comment.