node.js using nested functions from different files

46 Views Asked by At

I wanted to write a understandable code in node.js, so I want to put some functions, which are used very often, into other node.js files, and access them from there. So I get a function, which calls a function from another node.js file and in this other node.js file, also another one is called. Important to know, if I put all in one file, the code works, so it should be an issue with module export and using functions in another file.

I have one file, getting quotes from a decentralised exchange. Looking like this (quoter_uni_v2.js):

module.exports = function quotes_uni_v2(tokenIn, tokenOut, amountIn, router) {
  const quotedAmountOut = router.getAmountsOut(amountIn.toString(), [
    tokenIn,
    tokenOut,
  ]);
  return quotedAmountOut;
};

And I am importing this function in my second helper file (quotes_5.js) (It is splitted in two files, because in the second one I have to call the function multiple times):

var quotes_uni_v2 = require("./quotes_uni_v2");

module.exports = async function (router1, router2, route, amount_wei) {
  console.log(route);
  var amount_Out = await quotes_uni_v2.quotes_uni_v2(
    route[1],
    route[2],
    amount_wei,
    router1
  );
...
  return (
    Math.round(ethers.utils.formatEther(amount_Out[1].toString()) * 100) / 100
  );
};

After that I try to call the function in my main.js:

const quotes_uni_v2 = require("./quotes_uni_v2");
const quotes_5 = require("./quotes_5");
async function calc(route) {
amountOut = await new quotes_5(
          quickswap_router,
          sushiswap_router,
          route,
          amount_wei
        );
return amountOut;
};

But calling the quotes function does not work... The error is:

TypeError: quotes_5 is not a constructor...

Can someone help me?

Thanks!

0

There are 0 best solutions below