Uniswap / v2-sdk

🛠 An SDK for building applications on top of Uniswap V2
https://uniswap.org/docs/v2/SDK/getting-started
MIT License
428 stars 1.09k forks source link

Retrive the Route for two tokens without a pair #79

Open Linch1 opened 3 years ago

Linch1 commented 3 years ago

How can i retrive the Route for two tokens that have not a pair ?

The following code uses two tokens that doesn't have a pair on uniswap, and during the execution encouter an error that is reported below. The same code if applied on tokens with a pair works well ( forx example WETH and DAI ) , so i am wondering what i have to change to make this works also with tokens without a pair?

CODE

const { ChainId, Token, TokenAmount, Pair, Route, Fetcher } = require("@uniswap/sdk");

( async () => {
    const AKITA = new Token(
        ChainId.MAINNET,
        "0x3301ee63fb29f863f2333bd4466acb46cd8323e6",
        18
    );
    const SHIBA = new Token(
        ChainId.MAINNET,
        "0xf32aa187d5bc16a2c02a6afb7df1459d0d107574",
        18,
    );
    const pair = await Fetcher.fetchPairData(AKITA, SHIBA);
    const route = new Route([pair], SHIBA)  
    const trade = new Trade(route, new TokenAmount( SHIBA, '1000000000000000000' ), TradeType.EXACT_INPUT)

    console.log(trade.executionPrice.toSignificant(6))

})();

OUTPUT

Warning: 0x3301ee63fb29f863f2333bd4466acb46cd8323e6 is not checksummed.
Warning: 0xf32aa187d5bc16a2c02a6afb7df1459d0d107574 is not checksummed.
(node:138647) UnhandledPromiseRejectionWarning: Error: call revert exception (method="getReserves()", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.4.1)
    at Logger.makeError (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/node_modules/@ethersproject/logger/lib/index.js:199:21)
    at Logger.throwError (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/node_modules/@ethersproject/logger/lib/index.js:208:20)
    at Interface.decodeFunctionResult (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/node_modules/@ethersproject/abi/lib/interface.js:384:23)
    at Contract.<anonymous> (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/lib/index.js:338:56)
    at step (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/lib/index.js:48:23)
    at Object.next (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/lib/index.js:29:53)
    at fulfilled (/home/pero/projects/web3-testing/node_modules/@ethersproject/contracts/lib/index.js:20:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:138647) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:138647) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Florian-S-A-W commented 1 year ago

Hi @Linch1 and anyone else looking for an answer,

The best option for routing at the moment is the Universal Router or the corresponding SDK. To take a deep dive into the routing logic Uniswap uses take a look at the Smart Order Router.

I didn't find an option provided by Uniswap to only consider V2 pairs. You could use the V2 Subgraph to query all pairs and use a pathfinding algorithm to find routes between them, for example Bellman-Ford.