[SOLVED] React useOneInchSwap not working

import { useOneInchSwap, useMoralis } from "react-moralis";

export default function Quote() {
    const { isAuthenticated, logout, user } = useMoralis();
  const { swap, data, isFetching, isLoading, error } = useOneInchSwap();
  
  const options = {
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: '0x0da6ed8b13214ff28e9ca979dd37439e8a88f6c4', // The token you want to swap
    toTokenAddress: '0x6fd7c98458a943f469e1cf4ea85b173f5cd342f4', // The token you want to receive
    amount: 1000,
    fromAddress: '0x6217e65d864d77DEcbFF0CFeFA13A93f7C1dD064', // Your wallet address
    slippage: 1,
  }


  
  return (
     <div>
        {error && <>{JSON.stringify(error)}</>}
        <button onClick={()=> swap(options)}>Swap</button>
    </div>
  );
}

this is the code (a copy and paste was originally me testing one inch Quote but i get the same issue).

this is the error i’m getting.

Im fairly new to code in general but ive been getting interested in web3 and most of moralis is pretty much straight forward.

could someone guide me on how to get this swap to work correctly and not throw an error? the same thing happens when i try to get oneInchQuote but there is barely anything covering this in next.js/react format.

thank you

were you authenticated at that time?

yes i was when i add the code it is after i have logged in and authenticated, but i dont get a chance to even attempt to swap it throws an error on refresh not button click.

should i be saving authentication to state or something?
edit: isnt authentication saved automatically after login throughout the project?

this sounds like there is a different problem and not with the swap

one second let me show you something different the quote throws a different error as soon as i add the code

import { useOneInchQuote } from "react-moralis";

export default function Quote() {
  const { getQuote, data, isFetching, isLoading, error } = useOneInchQuote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: '0x0da6ed8b13214ff28e9ca979dd37439e8a88f6c4', // The token you want to swap
    toTokenAddress: '0x6fd7c98458a943f469e1cf4ea85b173f5cd342f4', // The token you want to receive
    amount: 1000,
  });
  return (
     <div>
        {error && <>{JSON.stringify(error)}</>}
        <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  );
}

code

error

any ideas?

I’m not really expert in react, I found that in source code/documentation

you are using latest version of moralis and react-moralis?

I am i actually just uninstalled and reinstalled it, ill look around hopefully i get this figured out its kind of annoying

i figured out the issue i didnt have the plugin installed lol

2 Likes

Hi Wayne. I also have this same issue. How did you fix yours please? Did you install the plugin on react?

Nope, the pulgin is installed on the server rather.

Thanks Ayo for the update. I installed the plugin on the server but I’m still experiencing same issue, do you know the fix for this?

What’s the issue you got?

TypeError: Cannot read properties of undefined {reading “chain”}

If i add the chain, it show same error for address…

It is the same error the user who opened this issue had.

I am getting this same error, were you able to a
Solve this and how, thanks in advance

Hope you have the plugin installed on the server you’re using ?

@qudusayo, yeah of course, I used useOneInchTokens hook with no error,and there are very limited articles online about moralis, it’s like you have to solve your issues yourself.

This is the only thing that could help that I found online https://github.com/MoralisWeb3/react-moralis/issues/242

Which errors are you getting? Can you post your 1inch code?

@glad just mere calling the Useoneinchswap hook at line 39 gives the error