Cloud function "Too many requests to Web3 API"

I am running a cloud function on the afterSave Trigger. This trigger is based on the watchedEthAddress table, so whenever I add a new wallet to sync, this should get fired once and t hen whenever its updated.

I am getting a too many requests to web3 api error from this user (the user is the default coreuser service). Why is this limited, especially when only making 1 call after I added a wallet to the sync plugin.

how do you know that coreservices user is used for that call? I think that it tracks the IP and not necessarily the user.

did you try to set different rate limit to see if it works after that?

can you use logging to see how many times that afterSave trigger is called?

Its the only user I have in my database, I dont have any other users. And since I call this form within a trigger function (no request from some front-end) I am assuming its the coreuser.

The function gets only triggered once when I put in a new address, but as it is used on the WatchedEthAddress I hope it is also getting called everytime an address is syncing and saving.

what is the code that you use for that trigger? I know that you posted it in another thread but it is not easy to find it.

The use case is to always have the updated eth balance for wallets that get synced by Moralis. Since by default they dont store native balance I came up with this solution.


Moralis.Cloud.afterSave("WatchedEthAddress", async (request) => {
    const logger = Moralis.Cloud.getLogger();
 	const address = request.object.get("address");
  
  	logger.info("afterSave triggered for address " + address);
    const options = { chain: "eth", address: address};
	const {balance} = await Moralis.Web3API.account.getNativeBalance(options);
  	const ethBalance = Moralis.Cloud.units({
		method: "fromWei",
		value: balance
	});
  
  	logger.info("afterSave balance response " + ethBalance);
	request.object.set("eth_balance", ethBalance);
	await request.object.save(null,{useMasterKey:true});
});

I looked in the server logs and it looks like there are a significant number of requests made in a short period of time. As in that function is called more than 10 times in a short period of time.

At some point it will hit api limits

where would significant amount of numbers come from if it gets called only once after an address has been added to the sync wallet? What would be a suitable workarround for this, as I need the balances in realtime on every update for all watched addresses.

Since its an afterSave trigger and I am saving the object at the end of the function, could this be a recursive call? Otherwise one wouldnt be able to add fields or columns afterSave cause it needs to be saved.

I’m not sure if it is a recursive call, that is not impossible either

so anyone an idea how to solve this now? What arethe options to archieve whatI want to do?

you could try to save that info in a separate table and not in same table

I dont need too as the values get stored in the table I want, just that once I add one or two more I get β€œtoo many request to web3 API” errors. When I clear my database and just add a single address the value is stored correctly.

do you know how to look in logs in your dashboard in order to see how many times that function is called and when?

seems to get triggered multiple times after adding an address to the sync tab. Could be that the WatchedAddresses have several tasks that are beeing used to populate it and every tasks saves seperately to retrigger afterSave. Im not sure here is the log.

But this time its also a very busy address (Exchange address). Before I only did with private ones but they threw Rate Limit Errors too

. 2022-02-14T12:40:54.367Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842454364.0x9bac8d59701027021744d155c9247efcf2a1eb8db77eb3c437bad38836465997"}}
2. 2022-02-14T12:40:54.341Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
3. 2022-02-14T12:40:54.331Z - afterSave balance response 1996008.341958665569721055
4. 2022-02-14T12:40:54.032Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842454030.0xf6a6d108e01f93fdfe0ec8597bb837a44c10f3e24f0a85e9fa1ca5f37b5bb349"}}
5. 2022-02-14T12:40:54.015Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
6. 2022-02-14T12:40:54.006Z - afterSave balance response 1996008.341958665569721055
7. 2022-02-14T12:40:53.963Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453960.0x4a0ca8a875dd3fd1d41506c1507eab38156170667ac9b64a6480fabafffa7c64"}}
8. 2022-02-14T12:40:53.937Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
9. 2022-02-14T12:40:53.928Z - afterSave balance response 1996008.341958665569721055
10. 2022-02-14T12:40:53.887Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453885.0x0bc0f9c7a9a4d36e4a578617410fdf741f93762470b7a4ff7654894081d8c646"}}
11. 2022-02-14T12:40:53.869Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
12. 2022-02-14T12:40:53.861Z - afterSave balance response 1996008.341958665569721055
13. 2022-02-14T12:40:53.770Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453768.0x3ca2c9aedbc84555267b27a09c172a6b2d8023476f6eec142ae0af21daf8854f"}}
14. 2022-02-14T12:40:53.746Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
15. 2022-02-14T12:40:53.737Z - afterSave balance response 1996008.341958665569721055
16. 2022-02-14T12:40:53.695Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453694.0xddeeec52920223c57edab9cf4b5789c851f3fc4ba6ca80977a6616a339604d29"}}
17. 2022-02-14T12:40:53.679Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
18. 2022-02-14T12:40:53.671Z - afterSave balance response 1996008.341958665569721055
19. 2022-02-14T12:40:53.636Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453634.0xa627c46bb6c8c0d21476e4163f0e000ef626f31cc3181b59a4c68658b79b5b79"}}
20. 2022-02-14T12:40:53.618Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
21. 2022-02-14T12:40:53.605Z - afterSave balance response 1996008.341958665569721055
22. 2022-02-14T12:40:53.562Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453561.0xa648912b6ffbe9e058c518f322a52ecf6024b1b6a14b9710b969563258eb143c"}}
23. 2022-02-14T12:40:53.540Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
24. 2022-02-14T12:40:53.525Z - afterSave balance response 1996008.341958665569721055
25. 2022-02-14T12:40:53.491Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453489.0xe5fd2c59c51e5250ef21796a2e33a9b3b671e7770f7cabaeae04213a37b7aa2b"}}
26. 2022-02-14T12:40:53.471Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
27. 2022-02-14T12:40:53.458Z - afterSave balance response 1996008.341958665569721055
28. 2022-02-14T12:40:53.412Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453410.0x3706debdb3572398b56e4cfb33548b0b49a6c144b8a9de842b34bc028411b3ae"}}
29. 2022-02-14T12:40:53.393Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
30. 2022-02-14T12:40:53.384Z - afterSave balance response 1996008.341958665569721055
31. 2022-02-14T12:40:53.162Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453158.0xf15b4b9204f9db9795d445e0e008fb7af2df46cccb54b1729878994936cd13e9"}}
32. 2022-02-14T12:40:53.111Z - afterSave triggered for address 0xbe0eb53f46cd790cd13851d5eff43d12404d33e8
33. 2022-02-14T12:40:53.102Z - afterSave balance response 1996008.341958665569721055
34. 2022-02-14T12:40:53.064Z - Ran cloud function coreservices_getWeb3ApiToken for user undefined with: Input: {} Result: {"status":200,"data":{"success":true,"result":"O65569.1644842453062.0x43b70f8607b9c6e33025a66f45ea3235c2e19a2b529da6614419c06fb42a9ee0"}}

You could try to use a flag and not try to update that value multiple times

but it needs to be updated everytime that address makes any kind of activity. Cause I want the balance to be realtime effective

Can you try with beforeSave then?