[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.

2 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