paint-brush
Scaling to Millions: Behind the World’s Most Solicited Smart Contract by@dacoco
447 reads
447 reads

Scaling to Millions: Behind the World’s Most Solicited Smart Contract

by DacocoJune 6th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Alien Worlds is the most popular [web3 game] with over 240,000 unique active wallets connecting daily. Its most solicited smart contract action – mining – gets called up to 13,000,000 times a day. Engineers are constantly optimizing their smart contracts to make them as efficient and lean as possible.
featured image - Scaling to Millions: Behind the World’s Most Solicited Smart Contract
Dacoco HackerNoon profile picture

Dallas Johnson is Chief Engineer for the Alien Worlds metaverse

Alien Worlds’ engineers are on a quest for smart contract efficiency

Alien Worlds is the most popular web3 game, with over 240,000 unique active wallets connecting daily. Within the game, its most solicited smart contract action – mining – gets called up to 13,000,000 times a day, making it the most frequently executed smart contract action in the world across all smart contracts and all blockchains.


Suffice it to say; scalability is one of the technical aspects that we pay particular attention to. Ensuring that the game runs smoothly and is ready to accept not thousands but millions of new users is a continuous challenge.


Of course, building on a highly scalable blockchain like WAX helps: with 0.5 second block time and 8,000 transactions per second, it is particularly well-suited for gaming. However, even WAX can get submerged by a massive transaction flow.


To save precious blockchain resources, we are constantly optimizing our smart contracts, making sure they are as efficient and lean as possible. This starts with quantitative analysis to identify areas for improvement and continues with designing and testing new solutions, such as simplifying smart contracts or breaking up actions.

Searching for the hot spots

To make the search for efficiency a continuous process, we have integrated a mechanism measuring the time each smart contract action takes into our development tools.


It allows us to collect the runtime statistics of all smart contract actions each time we run our unit tests (testing the smallest pieces of code that can be logically isolated in a system). The results, measured in CPU microseconds, show us the sections of code which could be improved.


This approach also allows us to quantitatively measure the performance impact of every change we make to the codebase at the end of the optimization process.

Keep it simple

Mining in Alien Worlds refers to a common action our players perform to strategically use a combination of NFTs to earn rewards in the form of the in-game token, Trilium.


Mining rewards split between the user and the owner of the land on which they mine used to be sent via token transfer as part of every mining transaction.


However, this obvious-at-first-glance method is, in reality, quite cumbersome; every mining action creates two token transfers, which in turn initiate 3-4 additional notification actions (a WAX chain peculiarity), and then those token transfers can trigger a smart contract on the recipient side.

This chain reaction, common to so many smart contracts involving token transfers, could have been dismissed because it did not impact the game itself. However, every action, even lightweight, can add a lot of overhead to the blockchain and impact its performance if repeated millions of times.


We experienced this firsthand when Alien Worlds started to gain momentum. Realizing how burdensome the inline token transfers could be prompted us to redesign the mining rewards distribution mechanism, removing token transfers altogether.


Now, the contract lets mining rewards accumulate internally, allowing users to claim them in batches rather than after every mine action and transfer the mined tokens to their wallets themselves.


By this change alone, the number of actions in each mining transaction dropped from over 10 to 2, increasing the overall smart contract’s efficiency by a staggering 25%.

Breaking it up

Sometimes efficiency gains can be achieved by breaking up the smart contract, especially if this allows us to reduce the number of times it gets called.


For example, many smart contracts – including our mining contract – send out messages with game details, allowing off-chain APIs to record and store them. This logging action is often a loopback, sent outside the contract and then calling back to it again (a valid action must call an existing smart contract action, and calling itself is often the easiest option). However, each time an action is called on any smart contract, even in a callback, it must first generate a new instance of that smart contract, including all its memory storage and instance variables.


In our effort to increase efficiency, we have isolated the logging, creating a tiny new notification contract with just a few lines of code to simply receive and log that action in the blockchain history. Now, instead of instantiating a whole new instance of the heavy mining contract, the logging action calls this new lightweight contract, reducing the temporary memory load on the blockchain.


This fairly small engineering change allows us to save valuable blockchain resources on every mine.


As the game scales up, the importance of small details increases exponentially. A tiny performance gain becomes a sizable efficiency boost when multiplied by 13,000,000 and an important contribution to the blockchain’s stability when multiplied by 13,000,000,000.


With an unrelenting focus on smart contract efficiency, we are ready to welcome Alien Worlds’ next millions of users.


Play Alien Worlds now or visit Galactic Hubs to apply for a development grant.