Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Building a Ethereum wallet watcher using concurrent programming in GoLang

Posted on Jul 3 • Originally published at ronilsonalves.com Cover photo by Mika Baumeister / UnsplashIn my last article I talked about how simple it is to build applications that make use of Concurrent Programming using the Go language, we saw how simple it is to implement and how efficient it can be, thanks to its construction that provides developers with powerful tools to take full advantage of the potential of concurrent programming. In this article we will take a practical step-by-step look at how to build an Ethereum wallet watcher taking advantage of this strength of GoLang.Web3 since I returned to my programming studies has always been a topic I am very interested in, I am an enthusiast of the blockchain technology behind most of the products developed and which has been adopted in more traditional solutions so to speak recently. Some wallets have their private keys publicly exposed, most of them being made available by development kits where you can run a test node in a local environment. Some users, by carelessness and lack of attention, end up sending values to the address of this wallet on the main network (in production so to speak).We will build a service in GoLang to look at Ethereum wallets and if there are any incoming balance transactions, we will try to perform an outgoing transaction to get those values.To further exemplify the use of concurrent programming we will also provide an API for querying information from an Ethereum address.In summary, we will run our wallet watcher service side by side with a rest API. Let's go?The source code of this project is on my GitHub, you can clone it using git clone and running it on your machine, don't forget to create an .env file in the root of the project following the .env.exampleIf you also want to build the Rest API for queries, you will need to have an API key from Etherscan.io, for this you will need to have an account and request a free key at https://etherscan.io/myapikeyWhere we will save our project and start it, for this, in the terminal we need to type:Before proceeding, let's install the go-ethereum and godotenv modules that will help us build our service:Now, we must create a directory called internal where we will organize the internal files of our project, inside it we will must create another directory folder which will be our domain package and finally, we will create our file wallet.go which will contain a struct that will represent an Ethereum wallet:Also, we must create an struct to represent a transaction, we will use it in further:Still inside internal directory we will create another package named 'watcher', inside it we will create our service.go file where we will implement our watcher, fist we will create a function responsible for starting our service:In the above code snippet we load our environment variables where we will store data that should not be exposed, in this example we will load 20 wallets and their respective private keys using the for range.Still in our service.go, we will create an synchronization group for our goroutines that will be created:Following, we will create our goroutines:Finally, we will create our function responsible for generating and signing a transaction that will send the wallets' balance to another wallet:Our wallet watcher service is ready, our watcher/service.go should be like:At this point, if we don't want to create an API Rest, we need just call our StartWatcherService() into our main.goWe will use the Gin Web Framework to build a Rest API where we will expose an endpoint to querying a wallet's balance and recent transactions from an address. To do this we need to add o gin-gonic module to our project:Now, inside internal we will create a 'wallet' package, in this package we will create a service.go file, this is where we will make calls to the Etherscan.io API to balance and transactions queries:First we will create a method to get informations from address that we will receive as param (we will see soon the gin handler function):Inside this we will get the Etherscan.io APIKey from our environment:Following, we will make a HTTP GET cal to Etherscan API and read the response content:Finally we will parse the Etherscan.io API response and structure it according with our struct, also we need to make some validations and return the data:We already have our method to get balance information from a wallet address, now let's create another method in our wallet/service.go file to show the transactions, the logic will be the same as the previous method, the difference will be in how we map the Etherscan.io API response and how we will build the URL endpoint to make GET request, because we will have as parameters the page and the number of items per page beyond the address:We finished our wallet/service.go and the entire file should be like the gist bellow:Following we will create our gin handlerFunc to interact with our wallet/service.go and expose the necessary endpoints to querying wallet's balance and transactions from an Ethereum wallet.We will create a directory in the our project root and mame it as cmd (here we will put our main.go and the handler package from our API, the directory structure must be like that:Finally we will create our handler package, inside it we will create a wallet.go file:Inside this file we will create two handler functions: GetWalletByAddress() and GetTransactionsByAddress() to show wallet's balance and transactions:Back to concurrent programming, inside our main.go file we will instantiate our walletService and walletHandler, create a gin server and instantiate it inside a goroutine:We need start our gin server inside a goroutine to our API and our wallet watcher service run simultaneous, so finally, we will start inside another goroutine our watcher service:We finalize the implementation of our API and watcher service, as we want the watcher related goroutines to keep running while our application ins running the select{} will wait for their completion, when we created our goroutine for each one of the wallets we include a 'for' with no exit clause, it will be responsible for preventing our goroutines from being completed after their first execution:The concurrent programming is a powerful tool in the software development, in this article/tutorial step by step we saw how implement it and extract their best in a Ethereum wallet watcher, exchange the 20 wallets for thousands, or the thousands of wallets for thousands of messaging queues or data streams, the go scheduler will take care and use efficiently the available resources. In our example, the 20 goroutines running simultaneous every 300ms consumed 63MB from available memory and the CPU utilization was 6%, this service was running inside a shared instance from 256Mb from Fly.io free tier:I hope that this article has been useful and has helped you to better understand concurrent programming and goroutines in Go.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Eduard Krivanek - Jun 14 anes - Jun 14 Danities Ichaba - May 16 Utpal Nadiger - May 25 Once suspended, ronilsonalves will not be able to comment or publish posts until their suspension is removed. Once unsuspended, ronilsonalves will be able to comment and publish posts again. Once unpublished, all posts by ronilsonalves will become hidden and only accessible to themselves. If ronilsonalves is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Ronilson Alves. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag ronilsonalves: ronilsonalves consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging ronilsonalves will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Building a Ethereum wallet watcher using concurrent programming in GoLang

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×