Blog
Building on a Subnet solution by Avalanche Subnets
š” Background & Problem Statement
In the ever-evolving world of blockchain technology, Avalanche has emerged as a prominent L1 player, driving innovation and pushing the boundaries of what's possible with the introduction of Subnets: Avalancheās answer to the blockchain trilemma. This design, based on horizontal scaling, paves the way for avant-garde applications of blockchain.
With Avalanche Subnets, developers can craft bespoke blockchains tailored to their distinct requirements. Be it a gaming application or a decentralized finance system (DeFi), Avalanche Subnets promise enhancements in performance, cost efficiency, user experience, and security.
Last fall, our team at Codelink embarked on an ambitious endeavor: creating a multiplayer game demo using Avalanche's groundbreaking Subnet technology. Venturing into the uncharted waters of pioneering tech is seldom a smooth sail.
After months of painstaking development and troubleshooting, it became evident that many of our hurdles stemmed from a significant gap in infrastructure tooling. Our perseverance paid off, and we eventually managed to successfully release the game demo. In our quest for solutions, we joined hands with Kurtosis. Their expertise became instrumental as we worked to refine and de-complicate the Avalanche Subnet setup, affirming our belief that there had to be a more streamlined way to approach this technology.
By integrating Kurtosis' solution into the Avalanche Subnet framework, we slashed the intricacy of the subnet initiation ā for instance, trimming down a 440-line instruction to just 300 . We believe Kurtosis can be used as a valuable tool to lower the barriers of adoption for Avalanche subnets & blockchain gaming and hope to share our learnings from the experience in this short blog post.
šØSolution
In this blog post, we share how our team at CodeLink uses Kurtosis, an open-source developer tool, to spin up an Avalanche subnet locally, run a virtual machine binary (e.g. subnet-evm), and deploy a sample game onto it. Using Kurtosis is quicker and simpler than using a Hardhat environment or the Fuji testnet. But donāt take our word for it, read on and see for yourself.
Table of contents:
- How to use Kurtosis to spin up a local Avalanche subnet
- How to set up & play the multiplayer game
- Conclusion & takeaways
ā° Shortcut
For those who wish to dive straight into the code, the source code behind the demo game can be found here. Simply follow the README instructions for getting it all set up. We offer a few ways to deploy the game, including on a local Hardhat environment or the Fuji testnet, but we encourage folks to use Kurtosis because of the incredibly short time it takes to get started. The package (i.e. the environment definition) written by Kurtosis is found here, which gives anyone a reproducible and portable way to spin up an Avalanche subnet of any size or configuration, either locally or in the cloud.
š¢ How to use Kurtosis to deploy a local Avalanche subnet
Kurtosis authored the Avalanche package, which is a portable and parameterized environment definition meant to give developers a reliable and highly-customizable way to spin up an Avalanche subnet for their development, prototyping, and testing needs. This package is meant to help get builders started faster, by abstracting away the complexities of setting up the subnet so that developers can focus on building their virtual machine logic and their decentralized applications on top of the subnet.
Kurtosis is used by Codelink for quick and trivial local deployment of the game for testing and development. There are only 3 steps to deploy everything!
- Install the prerequisite tools: Make sure you have Docker installed & started on your machine, and then install Kurtosis. For MacOS, simply run the below (other installation methods can be found here)
# Install Kurtosis
brew install kurtosis-tech/tap/kurtosis-cli
You can check that youāve installed Kurtosis properly by running the following, which will return both the versions of the CLI and the Kurtosis Engine.
kurtosis version
- Download the game & deployment logic
git clone git@github.com:CodeLinkIO/MoveAndWhap.git && cd MoveAndWhap
- Spin up the subnet
kurtosis run . '{"ephemeral_ports": true,"node_count": 1}' --enclave mnw
Boom! That is it! You now have a local Avalanche subnet running with subnet-evm! The above command is instructing Kurtosis to use the environment definition to spin up a single Avalanche node with the container prots mapped automatically to ephemeral ports on your machine. Weāve also specified that we want our āenclaveā or environment to be named āmnwā.
You should see the following get returned:
As you can see above in the example output, Kurtosis has spun up a single Avalanche (non-staking) node with the subnet-evm binary and exposes an RPC port for you to connect to. In this case, it is 127.0.0.1:57308
Wait, so was that it?
Yes! The command above in Step 3 is actually telling Kurtosis to run this main.star file, which is an environment definition that takes care of spinning up an Avalanche validator node from scratch and runs the VM binary (weāre using subnet-evm by default, but any VM can be used). Itās written in Starlark, a dialect of Python, and is meant to gracefully handle dynamic dependency passing and careful sequencing of steps that are often required for blockchain set up and configurations.
Next, letās check out how to actually play the game and showcase how the blockchain can be used to revolutionize the gaming industry.
šÆ How to set up & play the multiplayer game on an Avalanche subnet
The game demo built by Codelink is a multiplayer game where all user actions get recorded on-chain. This game was originally developed to accomplish two things: (1) help developers understand how to program a web3 game on an EVM chain and to (2) get our hands dirty with cutting edge tech and to see what was possible.
A more thorough description of the game mechanics are here, but essentially, itās like a naval search-and-destroy game where players can move around the ocean and attack other players for points. Any number of players can join and, as explained earlier, every move gets recorded on-chain.
- Obtain the full RPC URL from the locally deployed Avalanche subnet. In your terminal, you should be able to scroll up to see the Starlark output. This output object will return a few fields, with the chain-rpc-url being one of them (highlighted below in Yellow as an example).
- Connect your wallet to the local Avalanche subnet using Metamask: To interact with the game, we need a wallet to be connected, just like any other decentralized application on the blockchain. In this tutorial, weāre going to use Metamask as an example.
To connect your wallet, simply follow the instructions here from Metamask, under the āAdding a network manuallyā section with the following data:
- Network name: maw network
- New RPC URL: http://172.16.2.6:9650/ext/bc/2E4dXs9pQYT4rZNtdUXn7AQ1RLLx8RmppD4pHBeVSp1Gz45FyR/rpc Note: this will be different for you, since this URL is generated dynamically.
- Chain ID: 13123
- Currency symbol: MAW
Once everything is entered, make sure you click āsaveā and switch to the āmaw networkā using the Metamask UI!
- Get some test tokens to play the game with. Using Metamask, follow these instructions, under the āImporting using a private keyā section with the following data:
- Private key string: 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027
Youāve now imported the address 0x8db97c7cece249c2b98bdc0226cc4c2a57bf52fc with 1,000,000 MAW tokens for use in the game!
- Deploy the game contracts & start playing! To do this, run these 3 commands separately from the home directory of MoveAndWhap:
#Python
$ npm run deploy:local_subnet # This deploys the game's smart contracts to the subnet
$ npm run server # This starts the game server
$ npm run ai # This adds a few bots in the game to play with
$ yarn start # This starts the frontend
That is it! You have now configured all you need to start playing a multiplayer game on a local Avalanche subnet! As mentioned earlier, all moves are recorded on-chain so each time that is done, a transaction must be signed via a wallet interaction via Metamask. With ERC4337 though, session keys should enable a more seamless experience for a user down the road.
š Conclusion & takeaways
Subnets are Avalancheās answer to the scalability portion of the blockchain trilemma, allowing for near-infinite horizontal scaling by giving institutions the ability to create their own instances of blockchains with rules and configurations specific to their needs. This means performance, cost, data sovereignty/isolation, and UX can all be calibrated for the game or DeFi application being built!
In this blog post, we shared our experiences from designing & building a sample game on an Avalanche subnet and how Kurtosis was able to play a key role in reducing the complexities and overhead of setting up an Avalanche subnet for local development & deployment of our game.
Our goal with this project was to shed light on how developers can take advantage of Avalanche subnets to pave the way for more builders in the future & to help on-board the next billion users onto web3. If you or your organization are considering building on a subnet, we at codelink.io would love to help you get started!
About Codelink
CodeLink is an exceptional powerhouse in the blockchain industry, known for their groundbreaking contributions to projects like CryptoKitties and NBA Topshot - both of which have garnered immense popularity. Their expertise in cutting-edge technologies such as blockchain development & other technologies sets them apart as innovative trailblazers, continuously pushing the boundaries of what's possible in web3. With their unwavering commitment to overcoming challenges and their relentless pursuit of innovation, CodeLink is revolutionizing the blockchain landscape with their extraordinary creations.
To learn how CodeLink can partner with your team to deliver high-quality releases and get you to market sooner, contact us via hello@codelink.io
About Kurtosis
Kurtosis is an open-source build system for web3 test environments that gracefully handles dynamic dependency passing and sequential set up logic - both of which are often required for bootstrapping complex L1 infrastructure. Environment definitions written using Kurtosis are completely reproducible, portable, and work at any scale (as only limited by the underlying hardware) to empower engineers to test functionalities or scales that they couldnāt before. Spend less time building custom testing harnesses and frameworks - try Kurtosis today by getting in touch with us at hello@kurtosistech.com.
About Avalanche subnets:
Subnets are Avalancheās answer to the scaling portion of the blockchain trilemma. They are a unique scaling solution that enables institutions of any size to use distributed ledger technology for their applications, whether it be for gaming or for a DeFi use case. Subnets are powerful because they allow anyone to establish their own sovereign blockchain and control the levers. This includes being able to fine-tune application specific requirements (compute, speed, etc), data provenance and privacy, and validator sovereignty.
You can read more about Subnets here in Avalancheās docs.