joshstevens19 / simple-uniswap-sdk

Uniswap SDK which handles the routes automatically for you, changes in trade quotes reactive subscriptions, exposure to formatted easy to understand information, bringing back the best trade quotes automatically, generating transactions for you and much more.
MIT License
188 stars 95 forks source link

findBestRoute method returns wrong value for expected amount #48

Closed AjayiMike closed 2 years ago

AjayiMike commented 2 years ago
try {
        const uniswapPairFactory = await uniswapPair.createFactory();
        const result = await uniswapPairFactory.findBestRoute(
            amount,
            TradeDirection.input
        );

        return {
            path: result.bestRouteQuote.routePathArray,
            expectedOutput:
                result.bestRouteQuote
                    .expectedConvertQuoteOrTokenAmountInMaxWithSlippage,
        };
    } catch (error) {
        console.log("error: ", error);
        throw error;
    }

I used cloneUniswapContractDetails to make this works on cronos chain and every things works fine. but for a particular token, the result.bestRouteQuote.expectedConvertQuoteOrTokenAmountInMaxWithSlippage gives a very wrong value

AjayiMike commented 2 years ago

For Context purpose, this is the full code:

import {
    UniswapPair,
    UniswapVersion,
    UniswapPairSettings,
    TradeDirection,
} from "simple-uniswap-sdk";
import { constants } from "ethers";

interface Params {
    fromTokenContractAddress: string;
    toTokenContractAddress: string;
    amount: string;
    provider: any;
}

export const findBestRoute = async (
    params: Params
): Promise<{ path: string[]; expectedOutput: string }> => {
    const {
        fromTokenContractAddress,
        toTokenContractAddress,
        amount,
        provider,
    } = params;

    const cloneUniswapContractDetails = {
        v2Override: {
            routerAddress: "0x145677FC4d9b8F19B5D56d1820c48e0443049a30",
            factoryAddress: "0xd590cC180601AEcD6eeADD9B7f2B7611519544f4",
            pairAddress: "0xd590cC180601AEcD6eeADD9B7f2B7611519544f4",
        },
    };

    const uniswapPair = new UniswapPair({
        fromTokenContractAddress,
        toTokenContractAddress,
        ethereumAddress: constants.AddressZero,
        ethereumProvider: provider,
        settings: new UniswapPairSettings({
            slippage: 0.001,
            deadlineMinutes: 20,
            disableMultihops: false,
            uniswapVersions: [UniswapVersion.v2],
            cloneUniswapContractDetails: cloneUniswapContractDetails,
            customNetwork: {
                nameNetwork: "Cronos Mainnet Beta",
                multicallContractAddress:
                    "0x5e954f5972EC6BFc7dECd75779F10d848230345F",
                nativeCurrency: {
                    name: "cro",
                    symbol: "CRO",
                },
                nativeWrappedTokenInfo: {
                    chainId: 25,
                    contractAddress:
                        "0x5C7F8A570d578ED84E63fdFA7b1eE72dEae1AE23",
                    decimals: 18,
                    symbol: "WCRO",
                    name: "Wrapped CRO",
                },
            },
        }),
    });

    try {
        const uniswapPairFactory = await uniswapPair.createFactory();
        const result = await uniswapPairFactory.findBestRoute(
            amount,
            TradeDirection.input
        );

        return {
            path: result.bestRouteQuote.routePathArray,
            expectedOutput:
                result.bestRouteQuote
                    .expectedConvertQuoteOrTokenAmountInMaxWithSlippage,
        };
    } catch (error) {
        console.log("error: ", error);
        throw error;
    }
};

The toTokenContractAddress: "0x7b8aD6d7560FAcd1959cfb4b4163D7d297c4bFc0"

I've used MUSD(0x97749c9B61F878a880DfE312d2594AE07AEd7656) and USDC(0xc21223249CA28397B4B6541dfFaEcC539BfF0c59) as the fromTokenContractAddress No matter the input amount provided, I keep getting value between 4.3 to 4.9

the decentralized exchange whose contracts I'm using (mm.finance) gives me correct value so I don't know if there is something I'm doing wrongly

AjayiMike commented 2 years ago

A loom video showing the error in action: loom In the video, for every other coin, the minimum received is almost exactly what you get on the exchange's interface (mm.finance) except for the last coin CRP(0x7b8aD6d7560FAcd1959cfb4b4163D7d297c4bFc0), The minimum received is always 4.xxx no matter the input amount

AjayiMike commented 2 years ago

The provider url of Cronos Mainnet Beta chain: https://rpc.nebkas.ro

joshstevens19 commented 2 years ago

Hey so we spoke over twitter but will explain again.. but this was due to no liquidity on main routes and it was always going to MMF token which the sdk did not know about, you need to add that base token and then the lib will route to it..

if you add (use one of the base tokens to map the MMF token)

baseTokens: {
    dai: {
        chainId: 25,
        contractAddress: '0x97749c9B61F878a880DfE312d2594AE07AEd7656',
         decimals: 18,
         symbol: 'MMF',
         name: 'MMF',
  }
}

aka like

image in the custom network object it works

image

joshstevens19 commented 2 years ago

this is now fixed have confirmed my end!

AjayiMike commented 2 years ago

Hey so we spoke over twitter but will explain again.. but this was due to no liquidity on main routes and it was always going to MMF token which the sdk did not know about, you need to add that base token and then the lib will route to it..

if you add (use one of the base tokens to map the MMF token)

baseTokens: {
    dai: {
        chainId: 25,
        contractAddress: '0x97749c9B61F878a880DfE312d2594AE07AEd7656',
         decimals: 18,
         symbol: 'MMF',
         name: 'MMF',
  }
}

aka like

image in the custom network object it works

image

Works like charm. Thank you very much