2

I'm trying to call a function every time that function's parameters change and this has generated a loop that I don't know how to solve. I've tried implementing this in several ways, such as:

const [fromAmount, setFromAmount] = useState(1);
const [fromToken, setFromToken] = useState<keyof typeof tokens>("WBNB");
const [toToken, setToToken] = useState<keyof typeof tokens>("CAKE");

const path = useMemo(
    () => [tokens[fromToken].address, tokens[toToken].address],
    [fromToken, toToken]
  );

const setAmountsOutParams = useCallback(() => {
    if (fromAmount > 0) {
      getAmountsOut.setParams({
        amountIn: ethers.utils.parseUnits(String(fromAmount), 18),
        path,
      });
    }
  }, [fromAmount, path]);

  useEffect(() => {
    setAmountsOutParams()
  }, [setAmountsOutParams]);

As context, I have a hook that provides me with the getAmountsOut, and this is the code regarding it:

const [paramsGetAmountsOut, setParamsGetAmountsOut] = useState({
        amountIn: ethers.utils.parseUnits('0', 18),
        path: ['', ''],
    });

    const { data: dataGetAmountsOut } = useContractRead({
        ...swapper,
        functionName: 'getAmountsOut',
        args: [paramsGetAmountsOut.amountIn, paramsGetAmountsOut.path],
    });

    const getAmountsOut = {
        data: dataGetAmountsOut,
        setParams: setParamsGetAmountsOut,
    };

How to solve it?

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
n3n3
  • 288
  • 1
  • 7

0 Answers0