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

@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