Skip to content

Commit

Permalink
Fix issue with router trying to find v2 pools (#1)
Browse files Browse the repository at this point in the history
* Fix issue with router trying to find v2 pools

* lint
  • Loading branch information
mycodecrafting authored Jan 28, 2025
1 parent f46aafe commit d94cd93
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Protocol } from "@uniswap/router-sdk";
import { ethers } from "ethers";

import { AlphaRouter, ChainId, SwapRoute } from "./contrib/smart-order-router";
import { V2NullProvider } from "./providers/v2/v2-null-provider";

import { logger, transformSwapRouteToGetQuoteResult } from "./utils";
import { GetQuoteParams } from "./types";
Expand Down Expand Up @@ -63,7 +64,11 @@ export const getQuote: HttpFunction = async (req, res) => {
tokenOutDecimals,
tokenOutSymbol,
);
const amountIn = CurrencyAmount.fromRawAmount(tokenIn, amount);

const baseCurrency = type === "exactIn" ? tokenIn : tokenOut;
const quoteCurrency = type === "exactIn" ? tokenOut : tokenIn;

const amountInOut = CurrencyAmount.fromRawAmount(baseCurrency, amount);

// create router and get quote
const provider = new ethers.providers.JsonRpcProvider(
Expand All @@ -73,13 +78,15 @@ export const getQuote: HttpFunction = async (req, res) => {
const router = new AlphaRouter({
chainId,
provider,
v2PoolProvider: new V2NullProvider(), // no v2 pools
});

console.log("amountIn", amountIn);
console.log("amount", amountInOut);
console.log("tokenOut", tokenOut);

const swapRoute: SwapRoute | null = await router.route(
amountIn,
tokenOut,
amountInOut,
quoteCurrency,
type === "exactIn" ? TradeType.EXACT_INPUT : TradeType.EXACT_OUTPUT,
/*swapConfig*/ undefined,
{ protocols: [Protocol.V3] },
Expand All @@ -93,7 +100,11 @@ export const getQuote: HttpFunction = async (req, res) => {
.send({ error: "Failed to generate client side quote" });
}

const data = transformSwapRouteToGetQuoteResult(type, amountIn, swapRoute);
const data = transformSwapRouteToGetQuoteResult(
type,
amountInOut,
swapRoute,
);

return res.json({ data });
} catch (error) {
Expand Down
19 changes: 19 additions & 0 deletions src/providers/v2/v2-null-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Token } from "@uniswap/sdk-core";
import { IV2PoolProvider } from "../../contrib/smart-order-router/providers/v2/pool-provider";

export class V2NullProvider implements IV2PoolProvider {
async getPools() {
return {
getPool: () => undefined,
getPoolByAddress: () => undefined,
getAllPools: () => [],
};
}
getPoolAddress(tokenA: Token, tokenB: Token) {
return {
poolAddress: "",
token0: tokenA,
token1: tokenB,
};
}
}

0 comments on commit d94cd93

Please sign in to comment.