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

Hey Im working on building a uniswap like dex and im getting an issue when using the getquote function/ it seams that it is expecting a number string but i notice some garbage values in the console


i dont know if there is something wrong in my code or if i need to use some type of conversion function

you can try to use Moralis.Units to do that conversion: https://docs.moralis.io/moralis-server/tools/moralis-units#converting-erc20-token-to-wei

//Example: We want to convert 0.5 ETH to Wei
const ethInWei = Moralis.Units.ETH("0.5")
// expected output: 500000000000000000 Wei

image

how would i write that syntax into this function?

async function getQuote() {

  if (!currentTrade.from || !currentTrade.to || !document.getElementById("from_amount").value) return;

  let amount = Number(document.getElementById("from_amount").value * 10 ** currentTrade.from.decimals);

  const quote = await Moralis.Plugins.oneInch.quote({

    chain: "eth", // The blockchain you want to use (eth/bsc/polygon)

    fromTokenAddress: currentTrade.from.address, // The token you want to swap

    toTokenAddress: currentTrade.to.address, // The token you want to receive

    amount: amount,

  })

  console.log(quote);

  // document.getElementById("gas_estimate").innerHTML = quote.estimatedGas;

  document.getElementById("to_amount").value = quote.toTokenAmount / 10 ** quote.toToken.decimals;

}

you could replace this line with something that starts like amount = Moralis.Units.

using this syntax:

//Example: We want to convert 0.5 BUSD. It has 18 decimals
const busdInWei = Moralis.Units.Token("0.5", "18")
// expected output: 500000000000000000 Wei


for some reason this isnt working

i declared it at the beginig
image
ive never declared a var with empty brackets before so im not sure how its supposed to look. can you take a look at ,y code and see if somethings wrong. I copied it almost perfectly from the repo. I was thinking maybe its an isuue with the form input react preset. maybe it interprets the text into it differently depending on what version you have

does it give an error, or only that message?
did you set that from key in that currentTrade dict?

this is the error
image

const busdInWei = Moralis.Units.Token(β€œ0.5”, β€œ18”).toString()

idk what you mean by set that from key in that current trade dict.
currentTrade is an empty obj and its supposed to be filled when i call this func

async function selectToken(address) {
  closeModal();
  console.log(tokens);
  currentTrade[currentSelectSide] = tokens[address];
  console.log(currentTrade);
  renderInterface();
  getQuote();
}

but i dont think its working because im geetting this prompt when i hover over from or to, which are the 2 parameters that are supposed to fill it
image

solved the issue, I had to change let to var

image

I have the same Bad Rquest but only when the amount more that 999.99
I notice that was fine only with token with decimals=0

  1. estimatedGas: 454462

  2. fromToken:

  3. address: β€œ0x0000000000004946c0e9f43f4dee607b0ef1fa1c”

  4. decimals: 0

  5. logoURI: β€œhttps://tokens.1inch.io/0x0000000000004946c0e9f43f4dee607b0ef1fa1c.png”

  6. name: β€œChi Gastoken by 1inch”

  7. symbol: β€œCHI”

  8. [[Prototype]]: Object

  9. fromTokenAmount: β€œ1000”

  10. protocols: [Array(1)]

  11. toToken:

  12. address: β€œ0x55d398326f99059ff775485246999027b3197955”

  13. decimals: 18

  14. logoURI: β€œhttps://tokens.1inch.io/0xdac17f958d2ee523a2206206994597c13d831ec7.png”

  15. name: β€œTether USD”

  16. symbol: β€œUSDT”

  17. [[Prototype]]: Object

  18. toTokenAmount: β€œ32446335465475735628”

  19. [[Prototype]]: Object

if you put 1000 in amount it works?

I’m not sure what you mean with this part

when I put 1000 does not work with all tokens with decimals=18

what are the steps to replicate that error?

the bad request with amout >= 1000 with only tokens with decimals =18

const getQuote = async function () {
  if (
    !currentTrade.from ||
    !currentTrade.to ||
 !document.querySelector('#from-amount').value || document.querySelector('#from-amount').value <= 0
  )
    return;

 const amount =Moralis.Units.Token(document.querySelector('#from-amount').value, currentTrade.from.decimals).toString();
 
  console.log("fromamount:" + amount);
  const quote = await Moralis.Plugins.oneInch.quote({
    chain: 'bsc', // The blockchain you want to use (eth/bsc/polygon)
    fromTokenAddress: currentTrade.from.address, // The token you want to swap
    toTokenAddress: currentTrade.to.address, // The token you want to receive
    amount: amount,
  });
    
  console.log(quote);
   
  const t_amount= Moralis.Units.FromWei(quote.toTokenAmount, currentTrade.to.decimals);
     
  document.querySelector('#to-amount').value =t_amount;
      
  document.querySelector('#gas').innerHTML = quote.estimatedGas;
 
  checkallow();
};
1 Like

so this is the part that will generate an error?

can you also provide the parameters that generate that error? (fromTokenAddress, toTokenAddress, amount)

are you using latest version for Moralis SDK?

Yes the Error in quote function

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>


//CurrentTrade
let currentTrade = {};


//CurrentSelectSide
let CurrentSelectSide;

//Select Token
const selectToken = function (address) {
    
    
  modalClose();
  currentTrade[CurrentSelectSide] = tokens[address];
  console.log(currentTrade);
  renderSelects();
  getQuote();
};




//Rendering interface
const renderSelects = function () {
    

  
  if (currentTrade.from) {
     
  
    document.querySelector('#from-token__img').src = currentTrade.from.logoURI;
    document.querySelector('#from-token__symbol').innerHTML =
      currentTrade.from.symbol;
 
  }
  
  if (currentTrade.to) {
      
    document.querySelector('#to-token__img').src = currentTrade.to.logoURI;
    document.querySelector('#to-token__symbol').innerHTML =
      currentTrade.to.symbol;
    
  }
    
};

I mean I want only the values for those variables, not the entire code, so what I could test only that function in particular