1nch getQuote function type def error : "amount should be a number string"

amount now 10 DOT, if i put 1000 DOT or more, will get that Error

fromToken:
address: "0x7083609fce4d1d8dc0c979aab8c869ea2c873402"
decimals: 18
logoURI: “https://tokens.1inch.io/0x7083609fce4d1d8dc0c979aab8c869ea2c873402.png
name: “DOT”
symbol: “DOT”
[[Prototype]]: Object
fromTokenAmount: "10000000000000000000"
protocols: [Array(1)]
toToken:
address: "0x55d398326f99059ff775485246999027b3197955"
decimals: 18
logoURI: “https://tokens.1inch.io/0xdac17f958d2ee523a2206206994597c13d831ec7.png
name: “Tether USD”
symbol: “USDT”
[[Prototype]]: Object
toTokenAmount: “263180094569369782000”
[[Prototype]]: Object


Ok, so you say that this doesn’t work for you:

 const quote = await Moralis.Plugins.oneInch.quote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: "0x7083609fce4d1d8dc0c979aab8c869ea2c873402", // The token you want to swap
    toTokenAddress: "0x55d398326f99059ff775485246999027b3197955", // The token you want to receive
    amount: "100000000000000000000",
  });

it looks like I get no error when I run that code

Check that one please.


 const quote = await Moralis.Plugins.oneInch.quote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: "0x7083609fce4d1d8dc0c979aab8c869ea2c873402", // The token you want to swap
    toTokenAddress: "0x55d398326f99059ff775485246999027b3197955", // The token you want to receive
    amount: "1000000000000000000000",
  });

it seems to work for me:

{fromToken: {…}, toToken: {…}, toTokenAmount: '26801213676952616565557', fromTokenAmount: '1000000000000000000000', protocols: Array(1), …}

Sure, I have a mistake…

you could also try to update 1Inch plugin in case that it isn’t latest version

1inch Pluginby Moralis (v0.0.29)Preformatted text

that is also the version that I have

if you run this code directly in your browser console, it works?

const quote = await Moralis.Plugins.oneInch.quote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: "0x7083609fce4d1d8dc0c979aab8c869ea2c873402", // The token you want to swap
    toTokenAddress: "0x55d398326f99059ff775485246999027b3197955", // The token you want to receive
    amount: "10000000000000000000000",
  });

const quote = await Moralis.Plugins.oneInch.quote({
chain: ‘bsc’, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: “0x7083609fce4d1d8dc0c979aab8c869ea2c873402”, // The token you want to swap
toTokenAddress: “0x55d398326f99059ff775485246999027b3197955”, // The token you want to receive
amount: “10000000000000000000000”,
});

undefined

ok, sounds good, now try this code:

quote2 = await Moralis.Plugins.oneInch.quote({
chain: ‘bsc’, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: “0x7083609fce4d1d8dc0c979aab8c869ea2c873402”, // The token you want to swap
toTokenAddress: “0x55d398326f99059ff775485246999027b3197955”, // The token you want to receive
amount: “10000000000000000000000”,
});

quote2 = await Moralis.Plugins.oneInch.quote({
chain: ‘bsc’, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: “0x7083609fce4d1d8dc0c979aab8c869ea2c873402”, // The token you want to swap
toTokenAddress: “0x55d398326f99059ff775485246999027b3197955”, // The token you want to receive
amount: “10000000000000000000000”,
});
VM409:2 Uncaught SyntaxError: Invalid or unexpected token

that must be because of the quotes, try this one:

quote3 = await Moralis.Plugins.oneInch.quote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: "0x7083609fce4d1d8dc0c979aab8c869ea2c873402", // The token you want to swap
    toTokenAddress: "0x55d398326f99059ff775485246999027b3197955", // The token you want to receive
    amount: "1000000000000000000000",
  });
[quote="cryptokid, post:33, topic:4354"]

quote3 = await Moralis.Plugins.oneInch.quote({
chain: ‘bsc’, // The blockchain you want to use (eth/bsc/polygon)
fromTokenAddress: “0x7083609fce4d1d8dc0c979aab8c869ea2c873402”, // The token you want to swap
toTokenAddress: “0x55d398326f99059ff775485246999027b3197955”, // The token you want to receive
amount: “1000000000000000000000”,
});

[/quote]

quote3 = await Moralis.Plugins.oneInch.quote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: "0x7083609fce4d1d8dc0c979aab8c869ea2c873402", // The token you want to swap
    toTokenAddress: "0x55d398326f99059ff775485246999027b3197955", // The token you want to receive
    amount: "1000000000000000000000",
  });
{fromToken: {…}, toToken: {…}, toTokenAmount: '27002990345225343336448', fromTokenAmount: '1000000000000000000000', protocols: Array(1), …}estimatedGas: 635672fromToken: {symbol: 'DOT', name: 'DOT', decimals: 18, address: '0x7083609fce4d1d8dc0c979aab8c869ea2c873402', logoURI: 'https://tokens.1inch.io/0x7083609fce4d1d8dc0c979aab8c869ea2c873402.png'}fromTokenAmount: "1000000000000000000000"protocols: [Array(2)]toToken: {symbol: 'USDT', name: 'Tether USD', decimals: 18, address: '0x55d398326f99059ff775485246999027b3197955', logoURI: 'https://tokens.1inch.io/0xdac17f958d2ee523a2206206994597c13d831ec7.png'}toTokenAmount: "27002990345225343336448"[[Prototype]]: Object

maybe there is conversion error in Moralis SDK when it computes that amount and it doesn’t really compute the right amount or not in the correct format before it gets sent to 1Inch plugin as parameter

Hi, I am actually having the same issue… Tracing back the error it looks like when converting the amount in my case at this Moralis.Units.Token("1234", "18") it breaks. These are my SDK current versions
"moralis": "0.0.184", "react-moralis": "^0.3.11",.
Get quote function:

 const getQuote = async (params) => {
    console.log("Params amount:", Moralis.Units.Token(params.fromAmount, params.fromToken.decimals).toString())
    return await Moralis.Plugins.oneInch.quote({
      chain: params.chain, // The blockchain  you want to use (eth/bsc/polygon)
      fromTokenAddress: params.fromToken.address, // The token you want to swap
      toTokenAddress: params.toToken.address, // The token you want to receive
      amount: Moralis.Units.Token(params.fromAmount, params.fromToken.decimals).toString(),
    })
  };

Below are the how to produce the error.
Working:


Breaking:

Console logging Moralis.Units.Token("1234", "18") till it breaks

Any help would be much appreciated!

How did you implement the price there in the UI? based on the console there it is not breaking but instead using the scientific notation to represent the number

The UI uses an input field of type number
<Input type="number" bordered={false} placeholder="0.00" style={{ ...styles.input, marginLeft: "-10px" }} onChange={(e) => setFromAmount(e.target.value)} value={fromAmount} />,
Console logging the input value, it dynamically updates its value till calling the getQuote function at the fourth number, and there the amount field returns the scientific notation…

amount: Moralis.Units.Token(params.fromAmount, params.fromToken.decimals).toString()

By the way the code is 100% from the React ethereum boilerplate.

Hey @Assan

I do think that the boilerplate is converting the amount from string to number.
This should never happen since the plugin asks for a string as amount and use numbers to describe big number in JS carries many issues.

This method returns strings as far as I see Moralis.Units.Token("1234", "40") so the conversion seems to happen after that.

Where did you add this console log?

Thanks @Dani,
I used some console logs in the getQuote function, the from token amount which is the user input never changes until I pass it as a first argument in Moralis.Units.Token(params.fromAmount, params.fromToken.decimals).ToString()), so I think the code might be breaking in there.