Saving extra data in object when syncing contract event

Hi all,

I’m currently syncing the standard openzeppelin Transfer(address,address,uint256) event when minting an NFT, and saving the data in an object called NFT (I’m also using a filter so the object only gets saved when the “from” address is 0x0). That way, I have a table where I can see all the NFTs that have been minted in my app.

However, I would like to save an extra column in that object (the NFT series to which that NFT belongs, I’m trying to implement series purely on the frontend, there is only one smart contract), but the data in it is never seen by the smart contract, only by the frontend.

How can I do this? Thanks in advance

To provide some context, what I’m trying to do is to implement series/collections using only one smart contract where all NFTs are stored. So the collections would be “known” only by Moralis, but not by the smart contract. Specifically, I created a “Collection” object, and each time an NFT is minted the user has to decide which collection to associate it with. As such, the NFT object I alluded to in the previous post would need to store the objectId of that collection.
Does this make sense or am I approaching this problem the wrong way?

A temporary solution I came up with is to have the user link the nft to a collection manually, after the minting has finished, but I’m not sure its the best way to do this UX-wise. Any input from you would be greatly appreciated

somehow you have to associate a token id with that collection

probably the easier solution would be that one of setting the collection for that token id after it was minted, at that time you already know the token id, the user address and you only have to set that collection field somewhere in the database

right, but when to do that association? it should be triggered only after the smart contract event is called, because otherwise the tx might fail and the database would still get populated with wrong data

you could also associate that collection name with a transaction hash, and in this case you can check later if the transaction failed or not and ignore that association when that transaction is not a successful mint

I will try that, thank you

could you explain what you meant in more detail, please? I’m a bit lost. A step by step walkthrough would be very much appreciated.

get the transaction hash of the mint, write in db in an object transaction hash and collection name,
later check that association based on the events as the events will have the transaction hash

So I should use a trigger afterSave of the object that gets created with the event sync? How can I access the tx hash inside the trigger?

I found how to access the tx hash, but is using a trigger what you meant?

you can use a trigger if you want, you can also add a column to that event specific table if you want where you can set that collection name in that trigger

you can also run a query in two tables when you need that info without using a trigger

I’m thinking I could execute a trigger after the PolygonNFTTransfers row gets added and its “confirmed” column changed to true. This way I would creating 2 tables, instead having only one. How can I do that?
Thank you

you can use afterSave trigger for that table

I’m having trouble with setting up the trigger. This the cloud code function I created for testing purposes:

const logger = Moralis.Cloud.getLogger();
Moralis.Cloud.afterSave("PolygonNFTTransfers", async function (request) {
  const confirmed = request.object.get("confirmed");"testing"); 

However when I look at the logs I dont see anything

what is the server url?

it looks like there is some problem with updating cloud code, I would expect it to get fixed tomorrow

Oh ok, I’ll try again tomorrow then. Thank you!

can you try again now to update the cloud code? maybe change that testing message to “testing 2”?