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

[UniswapError: invalid from or to contract tokens] #18

Closed lamman1820 closed 3 years ago

lamman1820 commented 3 years ago
const uniswapPair = new UniswapPair({
    // the contract address of the token you want to convert FROM
    fromTokenContractAddress: '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b',
    // the contract address of the token you want to convert TO
    toTokenContractAddress: '0x1985365e9f78359a9B6AD760e32412f4a445E862',
    // the ethereum address of the user using this part of the dApp
    ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
    chainId: ChainId.RINKEBY,
  });
    // now to create the factory you just do
    const uniswapPairFactory = await uniswapPair.createFactory();
    //the amount is the proper entered amount
    // so if they enter 10 pass in 10
    // it will work it all out for you
    const trade = await uniswapPairFactory.trade(10);

I try to get the trade information but it always shows the error: invalid from or to contract tokens.

Please help to check it.

Thanks

joshstevens19 commented 3 years ago

Hey you are using mainnet contract address which are not correct on rinkeby!

As you see > https://rinkeby.etherscan.io/address/0x1985365e9f78359a9B6AD760e32412f4a445E862 is not a contract but on mainnet > https://etherscan.io/address/0x1985365e9f78359a9B6AD760e32412f4a445E862

same with fun token see > https://rinkeby.etherscan.io/address/0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b is not a contract but on mainnet > https://etherscan.io/address/0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b

If you want to use testnets you have to use the correct token addresses for what you want to swap. In your code snippet if you change

chainId: ChainId.RINKEBY to chainId: ChainId.MAINNET

Then the quotes will work.