Skip to content
Merged
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
55 changes: 55 additions & 0 deletions src/core/modes/router/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,61 @@ describe("Test findBestRouterTrade", () => {
});
});

it("should try partial trade if full trade fails with NoRoute reason", async () => {
const mockFullTradeError = Result.err({
reason: SimulationHaltReason.NoRoute,
spanAttributes: { error: "no route" },
noneNodeError: "no route for pair trade size",
});
const mockPartialTradeSuccess = Result.ok({
type: "routeProcessor",
spanAttributes: { foundOpp: true },
estimatedProfit: 50n,
oppBlockNumber: 123,
});

(trySimulateTradeSpy as Mock)
.mockResolvedValueOnce(mockFullTradeError)
.mockResolvedValueOnce(mockPartialTradeSuccess);
(mockRainSolver.state.router.findLargestTradeSize as Mock).mockReturnValue(500n);

const result: SimulationResult = await findBestRouterTrade.call(
mockRainSolver,
orderDetails,
signer,
ethPrice,
toToken,
fromToken,
blockNumber,
);

assert(result.isOk());
expect(result.value.spanAttributes.foundOpp).toBe(true);
expect(result.value.estimatedProfit).toBe(50n);
expect(result.value.type).toBe("routeProcessor");
expect(mockRainSolver.state.router.findLargestTradeSize).toHaveBeenCalledWith(
orderDetails,
toToken,
fromToken,
1000n,
100n,
undefined,
);
expect(trySimulateTradeSpy).toHaveBeenCalledTimes(2);
expect(simulatorWithArgsSpy).toHaveBeenLastCalledWith({
type: TradeType.Router,
solver: mockRainSolver,
orderDetails,
fromToken,
toToken,
signer,
maximumInputFixed: 500n,
ethPrice,
isPartial: true,
blockNumber: 123n,
});
});

it("should return error if partial trade size cannot be found", async () => {
const mockFullTradeError = Result.err({
type: TradeType.Router,
Expand Down
10 changes: 7 additions & 3 deletions src/core/modes/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ export async function findBestRouterTrade(
}
extendObjectWithHeader(spanAttributes, fullTradeSizeSimResult.error.spanAttributes, "full");

// return early if no route was found for this order's pair or dryrun failed
// return early if dryrun failed
// in other words only try partial trade size if the full trade size failed due
// to order ratio being greater than market price
// to order ratio being greater than market price or there was no route for full
// trade size, that's because if for example for a pair there is only 1 pool and that
// pool has certain amount of reserves that cant cover the full trade size but can
// cover partial, we still need to try it
if (
fullTradeSizeSimResult.error.reason !== SimulationHaltReason.NoRoute &&
fullTradeSizeSimResult.error.reason !==
SimulationHaltReason.OrderRatioGreaterThanMarketPrice
SimulationHaltReason.OrderRatioGreaterThanMarketPrice
) {
return Result.err({
type: fullTradeSizeSimResult.error.type,
Expand Down
Loading