Ethereum for the React developer

Takeaways from building an overnight GO game with react

Ethereum for the React developer

Sleek v2.0 public release is here

Lorem ipsum dolor sit amet, consectetur adipiscing elit lobortis arcu enim urna adipiscing praesent velit viverra sit semper lorem eu cursus vel hendrerit elementum morbi curabitur etiam nibh justo, lorem aliquet donec sed sit mi at ante massa mattis.

  1. Neque sodales ut etiam sit amet nisl purus non tellus orci ac auctor
  2. Adipiscing elit ut aliquam purus sit amet viverra suspendisse potent i
  3. Mauris commodo quis imperdiet massa tincidunt nunc pulvinar
  4. Adipiscing elit ut aliquam purus sit amet viverra suspendisse potenti

What has changed in our latest release?

Lorem ipsum dolor sit amet, consectetur adipiscing elit ut aliquam, purus sit amet luctus venenatis, lectus magna fringilla urna, porttitor rhoncus dolor purus non enim praesent elementum facilisis leo, vel fringilla est ullamcorper eget nulla facilisi etiam dignissim diam quis enim lobortis scelerisque fermentum dui faucibus in ornare quam viverra orci sagittis eu volutpat odio facilisis mauris sit amet massa vitae tortor condimentum lacinia quis vel eros donec ac odio tempor orci dapibus ultrices in iaculis nunc sed augue lacus

All new features available for all public channel users

At risus viverra adipiscing at in tellus integer feugiat nisl pretium fusce id velit ut tortor sagittis orci a scelerisque purus semper eget at lectus urna duis convallis. porta nibh venenatis cras sed felis eget neque laoreet libero id faucibus nisl donec pretium vulputate sapien nec sagittis aliquam nunc lobortis mattis aliquam faucibus purus in.

  • Neque sodales ut etiam sit amet nisl purus non tellus orci ac auctor
  • Adipiscing elit ut aliquam purus sit amet viverra suspendisse potenti
  • Mauris commodo quis imperdiet massa tincidunt nunc pulvinar
  • Adipiscing elit ut aliquam purus sit amet viverra suspendisse potenti
Coding collaboration with over 200 users at once

Nisi quis eleifend quam adipiscing vitae aliquet bibendum enim facilisis gravida neque. Velit euismod in pellentesque massa placerat volutpat lacus laoreet non curabitur gravida odio aenean sed adipiscing diam donec adipiscing tristique risus. amet est placerat in egestas erat imperdiet sed euismod nisi.

“Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum”
Real-time code save every 0.1 seconds

Eget lorem dolor sed viverra ipsum nunc aliquet bibendum felis donec et odio pellentesque diam volutpat commodo sed egestas aliquam sem fringilla ut morbi tincidunt augue interdum velit euismod eu tincidunt tortor aliquam nulla facilisi aenean sed adipiscing diam donec adipiscing ut lectus arcu bibendum at varius vel pharetra nibh venenatis cras sed felis eget dolor cosnectur drolo.

Recently I’ve joined forces with a great team of some gaming veterans in the development of a super-cool, first-ever, breakthrough game of GO, 100% based on the Ethereum blockchain. The game (which is actually classified as a dapp in the professional jargon) comprised of Ethereum smart contracts where the logic resided, and on the Front-end a React client where the game’s UI was brought into life.

Ah yes, there was also a minor super-easy non-brainer out-of-the-box integration to develop between React and the blockchain. Really, it was nothing and we already had everything prepared and figured out.

Or did we?…

As the deadline we’ve set for ourselves was quite demanding (we were looking to get feedback out of the MVP with minimum effort), and the entire app / game comprised of only 2 pages and some calls to the smart contract, no thorough architecture designs were in place and as soon as we got the UI designs we just started coding the React Front-end.

Let’s just get it done in no time

Well, time it took alright. We were using create-react-app so it wasn’t that bad on the bootstrapping and project config tasks. We quickly got to our basic set of components:

However, the real issue started when we had to integrate with the blockchain.

To give some context, we used Metamask to connect us to the blockchain. From the Front-end app viewpoint, you get a web3 component on the window object which facilitates your communication with the blockchain:

Metamask takes care of the transactions approval process so that all you have to do is make API calls to the contract and each payable contract method gets a Metamask payment popover before actually committing the transaction to the blockchain:

To get a nice API to the native web3 object exposed by Metamask we used the npm web3 package (not to be confused with the Metamask web3 object) which wraps the native web3 component and lets you call contract methods easily (more on that in a separate post).

Happy happy joy joy! We’re sending out events on the blockchain, things are moving, transactions are committed and everybody’s happy!

The fun lasted alright while we were using the local ganache development server, however when we switched to using the Ropsten test network, is when we realized we’re in dire need of state management.

You see, the blockchain is slow, much slower than any server you, as a React developer, have ever worked against. That makes notifying the user on the progress of any server calls ultra-important.

Moreover, a user playing the game should know the current score, current balance, time left for turn etc. All this data is coming from the blockchain and reading from the network is free.

So, how do we know when transactions are completed?

The simple answer is events. To be more precise, the excellent web3 API docs detail out the subscribe method to listen to events coming back from the contract.

So up to here we have:

  • React components
  • Asynchnously calling methods on the blockchain, getting back a TX (Transaction ID) for each call.
  • Getting data back from these methods once the transaction is complete.
  • Watching events for completed transactions and notifying the user the transaction is complete.
  • Storing all that into our state and our notification system.
  • Managing all the above for multiple boards, each with its own state.

Taking all that into account on day 1 would simply spell out Redux, mobx or the like.

But, what do you do when you originally planned points 1&2 done in no-time and the rest maybe done later, in order to just get to your MVP ASAP with minimum effort?

What you do is you improvise. And you improvise on and on until… you build your own state management! Which is the worst and most time-wasteful thing you could do.
But again, it’s going to be ready tomorrow and there’s no sense to spend a full day adding state management & re-testing all you’ve built, right?

NOT RIGHT!

After working with Redux for almost 2 years, you can’t really build an on-the-go state management system that looks much different. So this is what you come up with:

Looks familiar? Yes, it’s mobx without observables, or redux without immutability, actions, or reducers.

Is it any good? Well, it solves your basic question of “where do I keep my state”.
Does it have any scalability coming with it? Central logging? Time Travel? No, and that’s the problem.

Initial values, null objects, refreshing components on props changing – all that has to be re-invented. And tested, and fixed.

The advantage of quickly scaffolding a game is long gone by the time you’re sitting late at night fixing bugs with your made-up state management system, instead of adding features to the game at hand.

Moreover, “ready tomorrow” becomes a cliché even you don’t believe in anymore.

So, is scaffolding-your-state-management-on-the-go a good thing or a bad thing?

Well, that depends. On the context.

Tell me that about any of my other React apps and I’ll tell you that you’re nuts and did all the classic mistakes of building a React app with no state management.

However, if you told me you’re looking to release an app within a few days and really you’re interested to first see how it catches fire and get validation first, I’d say splurge on it, you can always refractor / rewrite later.

About the author

Nadav Lebovitch is the Founder & CTO of nSoft, helping VC-backed startups build scalable web and mobile solutions. With 18+ years of experience, he has led hundreds of projects using modern technologies like React, Node.js, and PostgreSQL.