How to call contract method from cloud functions

Let’s say I want to check some contract permission/role. How do I make a contract call from Moralis beforeSave trigger/function?
Is Web3 available in triggers/cloud functions?

Hi, we do not have access to web3 in the cloudfunctions/triggers at the moment.

One way that you could achieve this is to sync the data that you need for permission checks to Moralis.

Let’s say you need to check for a certain role, then you could could emit an event when the role changes. Moralis can aggregate these events and make sure the correct role is served in Moralis (see

You could do the same for a tokenbalance or other data that you would need for a permission check.

I hope this helps.

Web3/contract interaction support inside cloud functions would be awesome.

What about cloud function webhooks?

Yes you could use a beforeSave webhook and make the check externally.
But the best solution as you say would be to use Web3 inside the cloud functions…

1 Like

I’m successfully using web3 in cloud functions without an issue.
I’m storing the ABI in a separate table, initialize a web3 contract and call the functions I need.

Moralis.Cloud.beforeSave('Markets', async req => {
  const marketAddress = req.object.get('market');

  const query = new Moralis.Query('Contracts');
  query.equalTo('name', 'Market');
  const res = await query.first({ useMasterKey: true });
  const contract = new Moralis.web3.eth.Contract(JSON.parse(res.get('abi')), marketAddress);

  const nftType = await contract.methods.nftType().call();

  req.object.set('nftType', nftType);

That’s awesome!

It’s an additional query storing abi in db. (For production) I don’t see benefits.

I put it there to avoid a long string in my cloud code file. But I don’t see the additional query being a big issue honestly