project screenshot 1
project screenshot 2
project screenshot 3

Hex War

Conquer a virtual world on Optimism. Grow your holdings by aggressively minting property, forming foreclosures, and outmanuevering your opponents.

Hex War

Created At

ETHSanFrancisco 2022

Project Description

The game world is represented as a hexagonal grid. Every vertex in the hexagonal grid is represented by a NFT implementing the ERC-721 interface (VertexERC721.sol). Moreover, while every NFT has a unique tokenId, every NFT is also uniquely represented by a set of three coordinates, corresponding to a position in space.

A user can expand their holdings. NFT holders are able to mint new NFTs only in hex locations directly adjacent to an NFT they currently own. However users are not able to continously mint - a randomized cooldown period prevents constant expansion.

Controlling a hex is valuable. The more NFTs someone owns that are located on the same hex, the lower cooldown periods exist for minting new NFTs. Thus if someone else owns controls the entire hex, they have a much faster ability to move and expand. Moving fast is important, because it enables users to prevent competitors from controlling their own hexes, or blocking competitors from expanding into new realms.

How it's Made

Sponsor Technology: Optimism-Goerli (smart contracts are deployed here, and live app is built on top of this network)

Techstack: Smart Contracts: Solidity, Hardhat, Node.js Frontend: React/Next.js, ethers.js, Styled-Components

The FE makes RPC calls to the smart contract to read data and render it for the user. The user is able to interact with the contract by sending transactions to mint an NFT. The resulting change in state is polled by the FE and rendered in hexagonal grid, built from a unique tuple of 3 coordinates associated with each NFT.

A hexagonal grid is rendered using React/plain SVG, defined by a cubic based coordinate system residing on-chain via ethers.js. A unique hex color code is generated for each ETH address. The experience feels very much like playing a board game, but verified on-chain.

The ERC-721 smart contract implementation was very unique, and initially based on Art-Gobblers, though the idea eventually veered away from having a paired ERC-20 due to time constraints. Each ERC-721 must maintain not just a token id, but a unique set of hex coordinates. Each ERC-721 must additionally be able to easily check the state of all the other tokens located on adjacent hexagons. This aggregates into a multiplier that is used determine the delay until a user may mint again. Aggregating many stateful interactions in a gas-efficient way was key on the smart contract side of things.

background image mobile

Join the mailing list

Get the latest news and updates