Sync and Watch Contract Events plugin : Event data not being registered

@SachaH I want to test prepaired code

Ah, I can do it by myself

UPDATE: No, I can’t :sweat_smile:

Hey I didn’t get what you wanted me to do, yes it is my contract, if you want to generate an event just call this method :wink: https://testnet.bscscan.com/address/0x5e192b22b5f30efd8a1897699813b7cd022ad0c0#writeContract

Hey @SachaH

There was a problem with name _auditor. Column names can’t began with symbol _. So above you can see the solution:

Moralis.Cloud.beforeConsume("TableEventName", (event) => {
  const logger = Moralis.Cloud.getLogger();
  event.auditor = JSON.stringify(event._auditor);
  return event;
});

Let nme know hot it works for you

1 Like

@SachaH

ABI:

{
  "anonymous": false,
  "inputs": [
    {
      "indexed": true,
      "internalType": "address",
      "name": "_auditor",
      "type": "address"
    }
  ],
  "name": "auditorRegistered",
  "type": "event"
}

Topic name 0x01d541ecc7e468bb4cc2794ffb6b02738b04182d627c19a490155feeaf025e03

Hey @Yomoo ,

The topic have to be hashed ? If so how did you got the topic ? from bscscan ?
Strange enough It doesn’t seem that it’s passing in beforeConsume

Moralis.Cloud.beforeConsume('EventSyncAuditorRegistered', async (event) => {
  const query = new Moralis.Query('Contract');
  query.equalTo('chainId', '0x61');
  query.equalTo('name', 'Factory');
  const [factoryContractInfos] = await query.find();

  const newEvent = {
    ...event,
    auditor: JSON.stringify(event._auditor),
  };

  await factoryContractInfos.save(
    {
      test: event,
    },
    { useMasterKey: true },
  ); // This is because my logs page is not working currently (reported in https://forum.moralis.io/t/logs-are-not-working/1315)

  return newEvent;
});

You can use not hashed name auditorRegistered(address) as well. Yes, you can find hashed topic name on bscscan.

1 Like

Does this code work?

No unfortunately it doesn’t seem the beforeConsume is triggered within my cloud function at all

Can you please try only my code and check the name of the function. You should paste the name from the database object

@Yomoo
Ok Now it’s passing in beforeConsume ! But the new field is not created here is what’s returned at the end of beforeconsume hook

{
  "transaction_hash": "0x6f27a61df6bb1760fd6f2e9d05fc8b99f8fd21b99f7cd6078b31230de271ef9d",
  "log_index": 1,
  "_auditor": "0xf07c1080e25cd96cd37951d7893e372128551cfe",
  "address": "0x88683dbdb8e1d25d5db85d9187437e319b51ca02",
  "block_hash": "0x17892c1e61a24074363c60e723c754f330a24fd3acf23edb4b34b64367451de7",
  "block_number": 10958454,
  "transaction_index": 4,
  "block_timestamp": {
    "__type": "Date",
    "iso": "2021-07-27T14:35:56.548Z"
  },
  "auditor": "\"0xf07c1080e25cd96cd37951d7893e372128551cfe\""
}

CODE

Moralis.Cloud.beforeConsume('EventSyncAuditorRegistered', async (event) => {
  event.auditor = JSON.stringify(event._auditor);
  return event;
});

Why using JSON.stringify since _auditor is already a string ?

@SachaH

Thanks for your attentiveness. I just forgot to remove JSON.stringify. It isn’t needed there

I have the same code and it works correctly:

You can try to create a new column manually, but it have to work automatically :face_with_monocle:

OK It’s working fine now thank you very much !!! Didn’t have to create a new column :wink:

@Yomoo

Am i authorized to save new objects within before consume here is my code

Moralis.Cloud.beforeConsume('EventSyncAuditorRegistered', async (event) => {
  // No choice here
  // eslint-disable-next-line no-param-reassign
  event.auditor = event._auditor;
  const Auditor = Moralis.Object.extend('Auditor');
  const queryAuditor = new Moralis.Query('Auditor');
  queryAuditor.equalTo('address', event.auditor.toLowerCase());
  let [dbAuditor] = await queryAuditor.find();

  if (!dbAuditor) {
    dbAuditor = new Auditor();
  }

  query.equalTo('name', 'Auditor');
  const [auditorContractInfos] = await query.find();
  const auditorContract = new web3.eth.Contract(
    JSON.parse(auditorContractInfos.attributes.abi),
    event.auditor.toLowerCase(),
  );

  const companyName = await auditorContract.methods.companyName().call();
  const owner = await auditorContract.methods.owner().call();
  const isCertified = await auditorContract.methods.isCertified().call();

  const auditor = convertAddressesToLowerCase({
    ...cleanData(dbAuditor.attributes),
    address: event.auditor,
    companyName,
    owner,
    isCertified,
    createdAt: event.block_timestamp,
  });

  await dbAuditor.save(auditor, { useMasterKey: true });

  return event;
});

I haven’t tested your code. But at first look there are missing { useMasterKey: true } in .find()

Hey those tables are publicly searchable so i shouldn’t need useMasterKey normally

In that case yes. You are right :muscle:

@Yomoo

Everything finally works fine thank you very much once again for your help <3

Great job! :muscle:

Happy BUIDLing :man_factory_worker:

1 Like