Cover photo

Skybreach: Technical details powering onchain game on Base

Or what it takes to build an onchain game in 2024

Last week we launched a mini game called Skybreach Dungeons on Base. The game is part of a bigger game called Skybreach that is under development. While Dungeons might seem simple enough on the surface, it’s far from it, so I wanted to write down how we built it and with this also show what it takes to build somewhat complex onchain game these days.

Smart Contracts

In the last year popularity of FOCG (Fully On Chain Games) increased dramatically, however when we started working on first drafts of Skybreach, FOCG wasn't anything big and MUD framework was still in very early days, we heard of MUD at a time, but didn't feel comfortable enough to use it. However, our passion for decentralised applications and vision for autonomous worlds was already in-line with the current FOCG trends, so we wanted to put as much of game logic on-chain as possible. Since none of the frameworks were mature enough, we went with a custom solution which turned out to be much more "on-chain" than the majority of web3 games, It is still not Fully on-chain, so we are careful in calling Skybreach it.

Most of the Skybreach contracts use Upgradable Proxy pattern (for agile development), and consist of over 21 smart contracts (including erc20 resources)

I'll summarise the main purpose of each contract bellow

Lands and Settlements

Skybreach world consists of 25k land plots. Each plot is an NFT based on modular RMRK ERCs. By owning multiple adjacent plots, user can combine them into Settlements. Individual Plots are staked into a contract, and user is given a settlement NFT in return. When settlement is created the contract stores some key information on adjacency bonus, plot rarities, resource abundance and biome types. This simplifies a lot of contract logic that would have been too expensive if we had to iterate through all adjacent plots to get these stats.

Skybreach Resources

Skybreach resources are the raw resources that can be gathered from your land once the settlement is built. Resources consist of wood, clay, coal, copper, diamond, gold, iron and stone. Each Resource is a separate ERC20 contract and will be used when producing various structures. Resource contracts are based on Axelar's Interchain Token Service that allows them to travel between EVM chains easily. We use ERC20 instead of ERC1155, to make use of ERC-7590 on Skybreach Game Wallet, which gives NFTs the ability to hold ERC20s (more on Game Wallet below).

Buildings

By combining resources and building blueprints, user will be able to send his Chunky workers to construct a buildings on his settlement. Buildings depend on resources and settlements to be in place, so they are coming a bit later. They will improve the resources you gather, reduce costs across skybreach, increase your RMRK staking limit, allow you to craft and upgrade items and more.

Chunkies

Chunkies are the main inhabitants of Skybreach Lands and are used for the most of the actions, including building, gathering resources, surveying land, battling in the dungeons, expeditions and more. Chunkies are also NFTs based on RMRK ERCs but with addition of On-Chain Attributes (ERC-7508) which is used for their progression system. By fighting in the Skybreach Dungeons (and other future activities), they gain on-chain experience that will be used for variety of actions later on.

One notable part of Chunkies contract is that the default mint is set in $RMRK tokens, but the contract allows users to mint them in ETH and WETH as well and swaps it into $RMRK through a Uniswap V3 on the fly.

Quests

We can configure a variety of quest types that are assigned to a Skybreach Land Plot. Each quest has flexible configuration like start/end time, limits per user or nft, recurring or one-time rewards, etc. This allows us to not rely on any third party quest services and have a constant flow of things player can do as the game progresses. For example Skybreach Dungeons is a type of quest too. Each week we configure 4 new dungeon quests with a duration period of 7 days and dedicated rewards.

Skybreach Wallet

Skybreach has an in-game wallet that is an NFT that can hold ERC-20 tokens (ERC-7590). User can withdraw and deposit assets between this NFT wallet and his EOA, this allows our smart contract to use user owned resources (wood, clay, stone, etc) without an extra approval transaction to sign, and adds other quality of life improvements like batch transfers.

Misc

Some other logic that is all handled on-chain that is worth mentioning is Leaderboard rewards logic for Skybreach Dungeons that allows players to claim their leaderboard rewards in a permissionless way.
The list of smart contracts in Skybreach ecosystem doesn't end there, with other connected NFT contracts like Kanaria and Skybreach Items, both of which use Equippable and Nestable NFTs and play important role in the Skybreach universe. And there's also $SILA and $RMRK tokens that will act as an important in-game currency.

If you are curious to explore Skybreach Smart contracts, you can find the list if addresses deployed on Base here

Indexer and Backend

We index all Skybreach events using Subsquid SDK. We have been using Subsquid for two years now for our NFT Marketplace where we index across 7 EVM chains. We love SQD because it is very flexible and allows us to structure the code the way we want to, instead of forcing us to follow a specific pattern. For Singular we self-host it on Kubernetes stack, but for Skybreach we decided to go with the cloud option for simpler deployment process. Subsquid cloud comes packaged with a simple graphql server, but you can connect your own and there's even Hasura config available. If graphql is not your thing, subsquid allows direct PostgreSQL connection to use with tools like Drizzle ORM or direct SQL queries. From the indexer, we use a custom event-bus to send all events to our queue system through self-hosted Redis db, that is then ingested by our other services hosted separately: Self hosted Typesense search indexer for a faster client-side searches, Elasticsearch for some further data wrangling, and NOVU Notification service (not in use at the moment).

On the game client's backend we use Drizzle ORM, trpc, and GraphQL powered by gql.tada and react-query, to get data from our subsquid indexer and apis for non-crucial data that would be inefficient to fetch from the contracts directly.

Client side (web)

Web2

I won't go into much details in regards to web2 stack, which is a pretty standard and consists of Nextjs, Chakra-ui and Typescript and other smaller libraries.

Web3

We use viem and wagmi heavily throughout the web app and it's our bread-and-butter of working with onchain from anywhere where Typescript is used.

For user authentication and onboarding we just recently switched to Privy which allowed us to add Guest Login and reduce the friction for anyone that wants to try the Demo Mode without worrying about wallets and funding. Guest Login creates and embedded wallet for the user and opens up some possibilities for us like sponsored transactions that will abstract the clunky web3 interactions even further for the players. Privy also has a great built-in Wallet Funding/Onramp experience that we use, allowing users to fund their wallet with an ease when ETH is required to pay for gas. The on-ramp options are Coinbase Commerce and Moonpay. And Lastly the built-in SIWE allows us to have an additional layer of security when validating user's game session.

Whenever user needs and ERC20 token swap instead of a fiat on-ramp, we use SquidRouter widget that uses Axelar under the hood for cheap and fast cross-chain swaps.

Client side (game client)

Skybreach Dungeons

Currently the main playable part of Skybreach is Skybreach Dungeons, which is a mini-game built using Godot Engine and then compiled to WASM for the web embed. The contract interactions are happening on the web/javascript side of the client side, with Javascript and WASM communicating between each other and the game server to validate the game session during the run. Finally at the end of the quest, the final game result is pre-signed by the game server and passed to the client for user to send to the contract alongside the "finish quest" transaction.


When finishDungeonQuest transaction is sent, the player immediately gets any eligible rewards due for completion, his Chunky's state goes back to available and can be used in other quests, and Game result is saved on-chain for the Leaderboard rewards. Each week there are 4 new Dungeon Quests configured, each with it's own unique seed that is used to procedurally generate the dungeon layout, Leaderboard and completion rewards. When previous batch of Dungeons ends, anyone on the winning position of the Leaderboard can claim their rewards, all on chain!

Skybreach Lands

All the Land based mini-games, including current Land management and upcoming Settlement management and resource gathering are visualised using Phaser. This is the same engine that is powering a highly successful web3 game Pixels.

Future tooling Wishlist

Depending on the success of the game, like any dev team, we have a variety of tools that we would love to try and integrate. Some we never tried yet, others were deprioritised due to time constrains.

Coinbase Account Abstraction Kit and particularly Sponsored transactions to reduce the number of transactions to sign for the user.

We already have 4+ tasks in our backlog for Farcaster Frames to check the status of your settlement, building, quests and leaderboards.

Much further down the line, but as we have Social Hub area in the backlog, we are looking for good decentralised solutions for chats. Farcaster is great as a social protocol, but not so good for real-time chats it seems.

Passkeys for signing transactions with an ease.

Closing thoughts

As you can see, as the game gets semi-complex, the amount of different techniques, workarounds and tools that we need to use is considerably large. This gets amplified when a team has strong decentralisation principles and integrity. Would it be easier to build this with with fake web 2.5 or even with no web3? Of course yes, but there's something thrilling in this challenge working under constraints while trying to make the user experience as close to web2 as possible.

Loading...
highlight
Collect this post to permanently own it.
yurinondual.eth logo
Subscribe to yurinondual.eth and never miss a post.
#gamefi#web3#base#solidity