Moralis Scan - Invalid Function watchEthAddress

Hi I tried to clone the project from


and pasted the cloud functions into my server but it doesn’t seem to work

getting the following err in dashboard logs

Error: Invalid function: “watchEthAddress”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (internal/process/task_queues.js:95:5)

My cloud func is setup identical to the cloud-query.js file in the repo


Moralis.Cloud.define("searchEthAddress", async function (request) {
  const { address } = request.params;
  if (!address) {
    return null;
  }

  // find out if address is already watched
  const query = new Moralis.Query("WatchedEthAddress");
  query.equalTo("address", address);
  const watchCount = await query.count();

  if (watchCount > 0) {
    // already watched don't sync again
    return null;
  }

  return Moralis.Cloud.run("watchEthAddress", { address });
});

Moralis.Cloud.define("getTransactions", function (request) {
  const { userAddress, pageSize, pageNum } = request.params;
  const offset = (pageNum - 1) * pageSize;

  const query = new Moralis.Query("EthTransactions");
  query.equalTo("from_address", userAddress);
  query.descending("block_number");
  query.withCount();
  query.skip(offset);
  query.limit(pageSize);

  return query.find();
});

Moralis.Cloud.define("getTokenTranfers", async (request) => {
  const { userAddress, pageSize, pageNum } = request.params;
  const offset = (pageNum - 1) * pageSize;
  const output = {
    results: [],
    count: 0,
  };

  // count results
  const matchPipeline = {
    match: {
      $expr: {
        $or: [
          { $eq: ["$from_address", userAddress] },
          { $eq: ["$to_address", userAddress] },
        ],
      },
    },
    sort: { block_number: -1 },
    count: "count",
  };
  const query = new Moralis.Query("EthTokenTransfers");
  const countResult = await query.aggregate(matchPipeline);
  output.count = countResult[0].count;

  // get page results
  const lookupPipeline = {
    ...matchPipeline,
    skip: offset,
    limit: pageSize,
    lookup: {
      from: "EthTokenBalance",
      let: { tokenAddress: "$token_address", userAddress },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                { $eq: ["$token_address", "$$tokenAddress"] },
                { $eq: ["$address", "$$userAddress"] },
              ],
            },
          },
        },
      ],
      as: "EthTokenBalance",
    },
    unwind: "$EthTokenBalance",
  };
  delete lookupPipeline.count;

  output.results = await query.aggregate(lookupPipeline);
  return output;
});

Moralis.Cloud.define("getTokenBalances", async (request) => {
  const { userAddress, pageSize, pageNum } = request.params;
  const offset = (pageNum - 1) * pageSize;

  // count results
  const query = new Moralis.Query("EthTokenBalance");
  const matchPipeline = {
    match: {
      address: userAddress,
      contract_type: "ERC20",
      balance: { $ne: "0" },
    },
  };
  const countPipeline = { ...matchPipeline, count: "count" };
  const countResult = await query.aggregate(countPipeline);

  // get page
  const pagePipeline = {
    ...matchPipeline,
    addFields: {
      adjBal: {
        $divide: [
          { $toDouble: "$balance" },
          { $pow: [10, { $toDouble: "$decimals" }] },
        ],
      },
    },
    sort: { adjBal: -1 },
    skip: offset,
    limit: pageSize,
  };
  const pageResults = await query.aggregate(pagePipeline);

  const output = {
    results: pageResults,
    count: countResult[0].count,
  };

  return output;
});

Hi,

Please check out this post and let me know if it got solved. –

Thanks.
A.Malik

I’ve tried changing it to WatchedEthAddress and the error is the same but with the new func name

Error: Invalid function: “WatchedEthAddress”
at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
at /moralis-server/lib/PromiseRouter.js:85:20
at processTicksAndRejections (internal/process/task_queues.js:95:5)

what boggles me is that it’s working fine in the moralis-scan tutorial series.it seems to me that there were some breaking changes in one of your server updates or something

this seems to be an issue as your code has no such function

regarding breaking changes - you are probably correct we build a lot, we need to re-record a lot when we get a bit more time

Yea, if I’m honest, I am not entirely sure why it’s there, the instructor in the video was doing a bit too much copy-paste without explaining. can you please try running the Moralis-scan repo?

We will come back to it once we have more time - for now try to understand the code yourself and figure it out :raised_hands:

Potentially @malik has time now

Hi @mrnewton,

That’s strange. I cloned the project and ran the exact code and it worked as expected.

Could you provide more details of your setup. Which network is it connected to?
Was the class WatchedEthAddress created?

Can you run this on your JS console on the dashboard and see if the class WatchedEthAddress was created (check dashboard to make sure) –

const user= "0x6ebad75d5db7a5b4e11dc985382a538739bf5ec0";
await Parse.Cloud.run("watchEthAddress",{address: user})

Hi @malik

I am running on the BSC mainnet. I’ve used the code in my first post in my cloud functions and I tried running the snippet you posted in the JS console and it came back with the following err

"Error":{3 items
"message":string"Invalid function: "watchEthAddress""
"name":string"Error"
"stack":string"Error: Invalid function: "watchEthAddress" at handleError (https://bgyvk8ia77ku.moralis.io:2083/bundles/dashboard.bundle.js:97:199328) at async eval (eval at <anonymous> (https://bgyvk8ia77ku.moralis.io:2083/bundles/dashboard.bundle.js:110:1142065), <anonymous>:10:1)"
}

I do not see any created classes in my dashboard aswell

Hi,

Yes, so it seems that on binance network server, this function is not inbuilt. I will contact the core team and figure out if that’s a feature or bug.

Thank you for pointing out. Really appreciate your patience. :slight_smile:

1 Like

Hi @mrnewton ,

There we go. The BSC server has a different function name for the same functionality.

The function name - watchBscAddress
The class created by using the function will be - WatchedBscAddress

We will update the docs to contain this information as well.

Thank you for your amazing contribution. :star_struck:

1 Like

@malik Thank you so much for helping me out. :star_struck: I’ve just tried it and the error is gone now but I’m getting results: undefined

Ran cloud function watchBscAddress for user undefined with:
  Input: {"address":"0x6ebad75d5db7a5b4e11dc985382a538739bf5ec0"}
  Result: undefined

Can you please ask the team if I should also rename Eth -> Bsc in other functions too?

For example, would a class on BSC mainet be called EthTokenTransfers or BscTokenTransfers?

Hi,

Yes, the class names are different for BSC. Check the screenshot below –

Hope this helps. :slight_smile: