To_block not working for avalanche and Moralis.Web3API.defi.getPairReserves

Hi,

I am trying to fetch historical reserve using the Moralis Web3 API.

The following code works.

export const Web3Api = () => {
  useEffect(() => {
    const fetchData = async () => {
      await (Moralis as any).enableWeb3();

      const reserves = await Moralis.Web3API.defi.getPairReserves({
        pair_address:
          "0xd7538cABBf8605BdE1f4901B47B8D42c61DE0367".toLowerCase(),
        chain: "avalanche",
      });
      console.log(reserves);
    };

    fetchData();
  }, []);

  return <></>;
};

But if I were to add the to_block parameter in order to fetch historical figure, the API returns {code: 141, error: ‘No reserves data found for pair’} I am just wondering what am I doing wrong here. Thanks

export const Web3Api = () => {
  useEffect(() => {
    const fetchData = async () => {
      await (Moralis as any).enableWeb3();

      const reserves = await Moralis.Web3API.defi.getPairReserves({
        pair_address:
          "0xd7538cABBf8605BdE1f4901B47B8D42c61DE0367".toLowerCase(),
        chain: "avalanche",
        to_block: "7538642",
      });
      console.log(reserves);
    };

    fetchData();
  }, []);

  return <></>;
};

I think that it requires an archive node to make that work and it looks like we don’t have an archive node for avalanche

Is adding an avalanche archival node on the agenda? Thank you.

it looks like it is on the agenda

On a related question, I see in your tutorial (https://www.youtube.com/watch?v=90dMpTlPNZ0), when grabbing price data time series, you guys do something like the follows (one api call per data point):

useEffect(() => {
    const fetchData = async () => {
      let days = 7;
      let dates = Array(Number(days))
        .fill(undefined)
        .map((e, i) => moment().subtract(i, "d").format("YYYY-MM-DD"))
        .reverse();

      console.log(dates);

      let reserves = await Promise.all(
        dates.map(
          async (e, i) =>
            await Moralis.Web3API.defi.getPairReserves({
              pair_address:
                "0xd7538cABBf8605BdE1f4901B47B8D42c61DE0367".toLowerCase(),
              chain: "avalanche",
              to_date: e,
            })
        )
      );
      console.log(reserves);
    };

    fetchData();
  }, []);
 

Doesn’t this send a lot of API requests to your server? Is this your recommended approach for a web application?

This is how the api works now, it doesn’t accept a list of parameters and only one parameter. Ideally you can use a cache and not make those requests every time for every user and only the requests that were not done before and get the data from local Moralis server database most of the time.