[SOLVED] Cloud function useMasterKey

I am trying to replicate the whale watching and have the code the same, but I am getting the following error when trying to track a wallet.

Input: {“address”:“0xdac17f958d2ee523a2206206994597c13d831ec7”,“syncHistorical”:false}
Error: {“message”:“Validation failed. Master key is required to complete this request.”,“code”:142}

I have tried adding ‘useMasterKey:true’ to the Moralis.Cloud.run() function but I can’t get it to work

what is the syntax that you tried?

  Moralis.Cloud.run('watchEthAddress', {
    address,
    syncHistorical: false,
    useMasterKey: true,
  });

I’ve tried adding it to the other queries as well below is the whole cloud code:

Moralis.Cloud.define('watchAddress', async (request) => {
  const logger = Moralis.Cloud.getLogger();

  //Check address has been inputted

  if (!request.params.address) {
    logger.info('error:missing address param');
  }

  const address = request.params.address;

  if (!address) {
    return null;
  }

  //Check address is not already being watched
  const countQuery = new Moralis.Query('WatchedEthAddress', {
    useMasterKey: true,
  });
  countQuery.equalTo('address', address, { useMasterKey: true });
  const watchCount = await countQuery.count({ useMasterKey: true });

  if (watchCount > 0) {
    return null;
  }

  //Add address to watch list
  Moralis.Cloud.run('watchEthAddress', {
    address,
    syncHistorical: false,
    useMasterKey: true,
  });

  //Fire alert every time there is a new tx

  Moralis.Cloud.afterSave('EthTransactions', async (request) => {
    //Check address is in watch list
    const to_address = request.object.get('to_address');
    // Query lsit iof watched addresses
    const query = new Moralis.Query('WatchedEthAddress', {
      useMasterKey: true,
    });
    // Check address of tx === to_address
    query.equalTo('address', to_address);
    const results = await query.find({ useMasterKey: true });

    //If results exist fire alert
    if (results) {
      logger.info('----------------');
      logger.info('https://etherscan.io/tx/' + request.object.get('hash'));
      logger.info('--🚨ALERT 🚨--');
    }
  });
  return true;
});

From your question I have realise the syntax is wrong… this works

 Moralis.Cloud.run(
    'watchEthAddress',
    {
      address,
      syncHistorical: false,
    },
    { useMasterKey: true }
  );

Thanks!

2 Likes