1

I'm currently developing a betting website built on top of Ethereum and am facing the following doubt.

In my contract there is an event at the end of the placeBet function which tracks the summary of the bet:

event NewBet(address, uint, string, address[], uint[]);

   function placeBet(uint gameID, string teamID, address[] tokens, uint[] amounts) public returns(bool) {

       // ... Add bet logic ...

       emit NewBet(msg.sender, gameID, teamID, tokens, amounts);

       return true;
   }

and on my backend I'm scouting for this events in order to add a bet to my off-chain database:

MyContract.events
  .NewBet({
    fromBlock: 0
  })
  .on('data', event => {
    // Get data from event object and add it to mongoDB
  })

1. How safe is this? Can I 100% assume that if the event is fired then the bet was placed and I can safely add it to the database? The event is fired very fast on rinkeby and I'm worried a little bit.

2. What if many many users bet at the same time, will my subscription catch all those event and add them all to mongoDB database one by one?

3. Is this generally how people do this kind of things?


Just a side note: Yes, I really need this Bet objects in my mongoDB, it's not enough for me to store them on the blockchain.

2
  1. How safe is this? Can I 100% assume that if the event is fired then the bet was placed and I can safely add it to the database? The event is fired very fast on rinkeby and I'm worried a little bit.

The event is fired when your node receives a block containing the transaction and the event.

A possible caution item is that since your implementation listens from block 0, it will recite events previously received every time you restart the node. This may not be ideal, however it can be useful for bootstrapping an empty uninitialized server.

  1. What if many many users bet at the same time, will my subscription catch all those event and add them all to mongoDB database one by one?

The timing of the bets isn't important. It's the mining. Transactions will arrive in a specific order and you receive the corresponding events with the transactions themselves.

  1. Is this generally how people do this kind of things?

Generally, yes. Events exist primarily for the purpose of informing external software clients.

Hope it helps.

  • "A possible caution item is that since your implementation listens from block 0." So what would I need to do to listen only to the latest transactions, what do you think? Thanks for help btw, I really appreciate! – lemme Oct 26 '18 at 19:54
  • Right. To add a further complication, I forgot to mention that the event logs are subject to the same reorganization rules as everything else. So, depending on the value at stake, you might want to wait for confirmations before acting. You can also safely uniquely identify transactions by their txHash even in the case that a reorganization re-orders the transactions after you first see them. – Rob Hitchens B9lab Oct 26 '18 at 20:03
  • Excuse me my further questions but how would I really handle waiting for confirmations for hundreds of users concurrently? I think placing some kind of "wait for confirmation" function in the callback of event would clog my server surely? And what are the risks if I don't wait for confirmations, I thought that if the event occurs (which means function is executed) there is no going back and transaction is set in stone? – lemme Oct 26 '18 at 20:13
  • There are several questions there and chatty dialogue is discouraged by the mods on this site. The main topic to consider is finalization. A lot is written about waiting for confirmations appropriately for the magnitude of the transaction. Events are wrapped inside confirmed transactions, so the same logic applies. – Rob Hitchens B9lab Oct 26 '18 at 20:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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