0

Ethereum noob over here. I'm working with ethereum locally using HardHat. I've created a contract for NFTs and I have a database that stays up to date with the NFTs that have been created by listening for "created" events.

I have a web client that provides an interface for viewing and creating NFTs and listens for transactions to complete (for the NFT to be mined), at which point it calls the db for the latest state. However, the issue I'm having is that often times the client captures the "transaction complete" event from the contract and queries the db before the db has had a chance to capture the similar event - e.g. the db has stale data.

These are options I've been thinking about in order to solve this but none of them seem great.

1 - I could check if the etag on the request has changed and long poll to wait for the "create" event to be triggered if it has. However, this still feels error prone in that if multiple transactions are being written to the contract I could accidentally capture the wrong one.

2 - I could do a "hard refresh" from the blockchain in which the db fetches the most recent data from the contract. However, is this even possible?

3 - Upon capturing that the transaction completed on the client, I could add a slight delay before querying the db, thus increasing the likelihood that the recent transaction has been captured in the db. However, of these 3 solutions this is my least favorite in that it feels quite brittle.

4 - Another option that is proposed in the comments is using Web Sockets on the server to notify the client of the change (instead of the client listening for changes directly from the blockchain). I'm hoping to avoid this, however, since it would put additional demand on my server (to keep the sockets open) and my server is not very powerful.

How is this problem usually solved?

2
  • 2
    How about your client listen to your DB (backend) through a (socket) connection and it will notify the client once its state is updated
    – shonjs
    Jul 15, 2021 at 2:51
  • Thanks @shonjs. That could work but I forgot to mention in the question that I'm hoping to avoid leaving sockets open on my server to handle this. I'll update the question now...
    – gxc
    Jul 15, 2021 at 23:48

1 Answer 1

1

I do it with infura node so I have a socket with this node to keep tracks of events.

If you don't want a socket maybe you can run your node locally and your database will make call() to your own node ?

2
  • Thanks @GrindCode, that's interesting. Could you help me understand this approach a bit better? Why would I need my own node for this?
    – gxc
    Jul 17, 2021 at 6:37
  • If you don't want to use Socket.io with your node provider for event you will need to request every block/event and your request's number will be too large so you will need a paid account for that. Socket.io can listen to it pretty easily. And if you don't want this, you need to be your own node.
    – GrindCode
    Jul 17, 2021 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.