Skip to content

Tag: typescript

Part 4: Beyond The Code and What I’ve Learned – Ethereum Payment

In this series of articles, I’m doing a walkthrough of a hobby project I’ve worked on, a payment system for the Ethereum blockchain. You can read the intro article for more context about the project.

I have covered the majority of the technical details in parts one through three. In this fourth and final part, I will to cover the following:

  • How to extend the code to support more features or data providers
  • Important things to consider when dealing with blockchain data
  • Insights on what blockchain and web3 can enable in terms of product experience design

The content of this article will feel like a collection of somewhat disconnected ideas, and the goal is to do a brain dump of the biggest takeaways and learnings I had while I was developing a system for the Ethereum blockchain.

Part 3: Processing Payments – Ethereum Payment

In this series of articles, I’m doing a walkthrough of a hobby project I’ve worked on, a payment system for the Ethereum blockchain. You can read the intro article for more context about the project.

For this article, I will assume that you have read part 1 about authentication and wallet management, and part 2 about the data models used in the system. Therefore, I will start from the assumption that you’re aware of the basic foundation on how to let users connect their crypto wallets to as website, how to display a list of available digital products that they can buy from, and that you are also somewhat familiar with the data models presented in part 2.

In this part, I will cover how payments are processed and validated. This includes the following steps, in order:

  • Server-side prevalidation: validating, before even processing anything, that the wallet is authorized to buy a particular digital product, and that this product is available.
  • Purchase: sending a payment transaction to the Ethereum blockchain for the required price, and getting a transaction identifier in return (also called a “transaction hash”)
  • Polling the blockchain: using the transaction hash to check if the transaction has succeeded or failed.
  • Triggering post-payments actions: dealing with the aftermath of the transaction status, which means showing an error message in case the transaction failed, or starting further steps by allowing access to resources in case the transaction succeeded.

The Ethereum blockchain makes it very easy to process payments, because every transaction is given a unique transaction hash, which means that once you have submitted a transaction to the Ethereum network for validation, all you need is this transaction hash, and you’ll be able to track the status of the transaction by polling the Ethereum blockchain.

In reality, for a small application such as the hobby project I’m presenting here, there is no need to poll the Ethereum blockchain directly. Instead, my program uses a third-party provider that makes polling the blockchain easier via an API. Many medium to large applications nowadays are running their own Ethereum nodes so that they can poll the blockchain directly. It costs them extra teams and complexity to maintain that infrastructure, but it comes with the benefits of higher trust and control of their data, and also processing speed. For them, the tradeoff is worth it.

Processing Payments

Below is a systems diagram showing how I have implemented the payment processing part. It assumes that the user has connected his wallet and already obtained a JWT that identifies him uniquely.

The diagram contains a description of each step, and the rest of this section gives further details as to what happens in each step.

Systems diagram showing how a purchase gets processed, from the user clicking the payment button and the transaction that it triggers on the user’s wallet, to validating that transaction and creating a new JWT to give the user access. (click on the image to enlarge)

Building a Payment System for the Ethereum Blockchain

Over the past few years, I’ve oriented the content of this blog towards engineering management and tech leadership, and consequently, I haven’t written anything purely technical for a while. Some of my most technical article series ended up finding a large audience, namely Implementing a Key-Value Store, and Coding for SSDs, and it now feels like it’s been an eternity since I wrote them.

Crypto protocols have made a serious dent in the tech space, and even more so over the past couple of years, often referred to as a whole as crypto or web3 technologies. With my personal commitment to keep upskilling myself, I have decided to make it a top priority for me to understand how blockchain technology was built and operated, and what kinds of businesses and product experiences this technology could enable in the long term.

I decided to build something basic yet complex enough that it would force me to interact with blockchain data in interesting ways. Because token transactions and crypto payments are simplest interactions on the blockchain, I settled on building a payment system.

Therefore in this series of articles, I will be sharing how I implemented a fully-functional payment system on top of the Ethereum blockchain, with the following features:

  1. Managing a list of products and time-bound subscriptions. Allowing for coupons and whitelistings.
  2. Displaying the current and past purchased products for a user.
  3. Guiding users through payment transactions on the Ethereum blockchain, by connecting their Ethereum wallets.
  4. Handling complex situations of network and service disruptions, to prevent double payments, avoid hanged transactions, and cover all other edge cases. This is mostly done by making all operations idempotent and idempotency keys.
  5. Monitoring the Ethereum blockchain to verify that payments for a particular product has been received.
  6. Once payment has been received, allowing paid users to access the private parts of a website by signing on with their Ethereum wallet to authenticate.

Launching Sidenote 🚀

If there’s one lesson that 2020 has taught me, it’s the value of focusing on opportunities rather than lamenting myself on what is not possible or what was taken away. I took advantage of the quarantine to go back to coding on a regular basis during my free time, and in the process I learned about TypeScript, Node.js, and MongoDB.

When I took a more managerial path early 2016, my focus naturally switched towards organizational topics. I must admit it did feel that my tech chops were slowly getting rusty, and I knew I had to do something about it. So when the first quarantine started, I set a simple goal for myself: to code for at least three hours per week. I actually tracked the time I spent on it to hold myself accountable, and I did hold onto it.

Today, after more than one hundred hours of coding and learning spent between April and November 2020, I am super proud to announce that I am launching an MVP for Sidenote.me, a web app to take timestamped notes on videos. If you’re reading this post until here, it would mean the world to me if you gave it a try just for a minute and then posted your feedback in the comments section below. And if you like it, talk about it on your favorite social media.

By the way, I’m going to be posting on this tech blog again very soon, about engineering and leadership topics. Subscribe to my newsletter below for fresh articles.

Join my email list