[react-moralis] Error when using 1inch plugin dex [SOLVED]

Hi. I would like to use 1inch plugin in my react application. I installed plugin for moralis server and I have I think latest version of moralis: 0.0.49 … I wrote this code

const getDex = async () => {
    await Moralis.initPlugins();
    const dex = Moralis.Plugins.oneInch;
    const NATIVE_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
    const ONEINCH_ADDRESS = "0x111111111117dc0aa78b770fa6a738034120c302";

    const options = {
      chain: "eth",
      fromTokenAddress: NATIVE_ADDRESS,
      toTokenAddress: ONEINCH_ADDRESS,
      amount: Number(Moralis.Units.ETH("0.01")),
      fromAddress: Moralis.User.current().get("ethAddress"),
      slippage: 1,
    };

    var receipt = await dex.swap(options);
    console.log(receipt);
  };

but when I try to execute this getDex function I will receive this error

Uncaught (in promise) Error: Something went wrong
    at MoralisWeb3.js:515
    at tryCatch (runtime.js:63)
    at Generator.invoke [as _invoke] (runtime.js:294)
    at Generator.next (runtime.js:119)
    at asyncGeneratorStep (asyncToGenerator.js:5)
    at _next (asyncToGenerator.js:27)

am I doing something wrong or plugins are not supported yet for react ? or this cannot be used with testnet ? Thanks

1 Like

this type of code is working so either am I doing wrong something or this does not work for testnet.

const getSupportedTokens = async () => {
    await Moralis.initPlugins();
    const tokens = await Moralis.Plugins.oneInch.getSupportedTokens({
      chain: "bsc", // The blockchain you want to use (eth/bsc/polygon)
    });
    console.log(tokens);
  };

Do you have enough Ether in your wallet?

I have 0.29 ETH on Ropsten. Is it not enough ?

Testnets are not supported by oneInch.
Please use mainnet (suggest bsc or polygon so that you have near-0 tx fees).
Soon will push an improvements for the error returned by the plugin.

3 Likes

Ok thanks. I will use mainnet.

1 Like

@dani I wanted to do swap in BSC chain so I wrote this code. I added fund to my BSC mainnet in order to do swap

const getDex = async () => {
    await Moralis.initPlugins();
    const dex = Moralis.Plugins.oneInch;
    const NATIVE_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
    const CAKE_ADDRESS = "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82";

    const options = {
      chain: "bsc",
      fromTokenAddress: NATIVE_ADDRESS,
      toTokenAddress: CAKE_ADDRESS,
      amount: Number(Moralis.Units.ETH("0.01")),
      fromAddress: Moralis.User.current().get("ethAddress"),
      slippage: 1,
    };

    console.log(dex);

    const receipt = await dex.swap(options);
    console.log(receipt);
  };

console.log(dex) is ok but dex.swap gives an error

MoralisWeb3.js:620 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'eth')
    at Function.<anonymous> (MoralisWeb3.js:620)
    at tryCatch (runtime.js:63)
    at Generator.invoke [as _invoke] (runtime.js:294)
    at Generator.next (runtime.js:119)
    at asyncGeneratorStep (asyncToGenerator.js:5)
    at _next (asyncToGenerator.js:27)
    at asyncToGenerator.js:34
    at new Promise (<anonymous>)
    at new Wrapper (export.js:15)
    at Function.<anonymous> (asyncToGenerator.js:23)
    at Function.<anonymous> (MoralisWeb3.js:652)
    at MoralisWeb3.js:524
    at tryCatch (runtime.js:63)
    at Generator.invoke [as _invoke] (runtime.js:294)
    at Generator.next (runtime.js:119)
    at asyncGeneratorStep (asyncToGenerator.js:5)
    at _next (asyncToGenerator.js:27)

Which version of the SDK are you using?
I think it’s because you don’t have an instance of web3 active in your Moralis class.
Run await Moralis.enable() before await Moralis.initPlugins();

Let me know

1 Like

@dani yes I have to add await Moralis.enable() in front. Thanks for your help. It is working

2 Likes

Hi Michal, Please i am setting up a dex and i have done all neccesasry but the problem is how do i add the list of all token and the images or icon to swap box?

You need to add the custom token object to the list of the tokes object following a similar structure

<TOKENS_OBJECT_NAME>[<TOKEN_CONTRACT_ADDRESS>] = {
  address: <TOKEN_CONTRACT_ADDRESS>,
  decimals: <TOKEN_DECIMAL>,
  logoURI: <TOKEN_LOGO_URI>,
  name: <TOKEN_NAME>,
  symbol: <TOKEN_SYMBOL>
}
``

Hi guys, i’m Papilo
i’ve coded along on the uniswap dex project but i’m still having problems with;

  1. activating my “connect with metamask” button,
    2 I’m getting an error message on my console log: “Failed to load resource: the server responded with a status of 400” and “Failed to load resource: the server responded with a status of 500”

You can check out my code on the repository below :

Thanks.

Everything looks good. You can replace await Moralis.web3.authenticate(); with this currentUser = await Moralis.authenticate();

Oh, it’s fine. You can remove your number for safety purpose.
I don’t use Whatsapp a lot though. You can surely find me on the discord server.

ok man i’ll see you there