Add Native Balance to WatchedEthAddress afterSave

I am trying to add the native address balance everytime I add a new address to the sync function, but somehow it doesnt do anything. Where do I go wrong on this?

Moralis.Cloud.afterSave("WatchedEthAddress", (request) => {
 	const address = request.object.get("address);
    const options = { chain: "eth", address: address};
	const balance = await Moralis.Web3API.account.getNativeBalance(options);
	request.object.set("eth_balance", balance);
	await request.object.save();
});

based on the syntax highlighting here, seems like you’re missing a quote here on the second line

const address = request.object.get("address");

woops you are right. Unfortunately results in the same error with quotes fixed.

2022-02-14T07:16:42.548Z - SyntaxError: Unexpected identifier
    at customUserPlugin (/moralis-server/cloud/main.js:153:26)
    at /moralis-server/lib/cloud-code/plugins/index.js:154:15
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:143:3)
2022-02-14T07:16:42.546Z - CLOUD FUNCTION ERROR PLEASE CHECK YOUR CLOUD FUNCTION CODE

hmmm how about try to restart the server? :thinking:

I dont think you have to restart the server everytime you add a cloud function. Also even when trying its popping up like this

There is a Cloud Function Error (Syntax/Undefined Variables) on your code, in order to Update/Restart your server fix the error first, You can check the full Log on the Dashboard -> Logs -> Error

you shouldn’t yes coz it usually will do so

hmmm but strange that seems to be the only syntax error there is :thinking:

ohhh!!! you forgot to make your function async but you used await there

Woops you are right again. Seems to be too early for me to code. Added the async like this, no errors but its not doing anything either.

Moralis.Cloud.afterSave("WatchedEthAddress", async (request) => {

ohhh did any new entry was added to WatchedEthAddress? afterSave is triggered only when new entry is added

yes the addresses I am adding are getting stored and synced, but the “eth_balance” column is not existent.

I am a step closer, now I am getting these errors without any further changed made.

2022-02-14T07:42:05.855Z - Error: Permission denied for action addField on class WatchedEthAddress.

and also

2022-02-14T07:42:22.328Z - Error: Too many requests to Web3 API from this particular client, the clients needs to wait before sending more requests. This can be adjusted using Moralis.settings.setAPIRateLimit. Read More: https://docs.moralis.io/moralis-server/web3-sdk/rate-limit.
at fetch (/moralis-server/lib/cloud-code/plugins/helpers/apiUtils.js:179:26)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.Moralis.Web3API.. [as getNativeBalance] (/moralis-server/lib/cloud-code/plugins/convenience/web3Api.js:29:70)
at async eval (eval at customUserPlugin (/moralis-server/cloud/main.js:153:21), :1:540)

I am confused to why I get too many requests when I actually make them from inside the server itself. Shouldnt that be not rate limited?

ohhh I see, not that’ll be rate limited too for sure on per minute basis

but you can increase it of course (as long as not passing your tier limits) with Moralis.setting.setAPIRateLimit in your cloud function too

why would it not allow me to add a column to the object either when I run this from the cloud functions with the core service user? I have also heard that running cloud function code or sync wallets does not affect rate limits.

Hmmm I believe it’s coz the process failed on line where you called the Web3 API so it doesn’t continue on to the next line where it writes to the DB.

and where did you heard it? can you show me some reference? :thinking: