In this world nothing can be said to be certain, except…

EthernalElves
4 min readJan 14, 2022

--

In this world nothing can be said to be certain, except…

Death, Taxes and Gas

Gas has been a hot topic recently in the NFT space and appears to be the make it or break it for projects and with Ethernal Elves is right around the corner, I wanted to talk to you all about what we’ve done to take this into consideration. We finished writing the contract a few days ago and have been obsessed with reducing gas on contract functions.

Before I explain what we’ve done to make gameplay actions cost-effective (relatively), let me put some things in perspective. 21,000 is the minimum amount of gas an operation on Ethereum will use. That's about $11 with where gas is today. Writing to the blockchain costs 20,000 per 256 bit/32byte storage slot and those updating values cost 5,000. Mathematical operations, calling functions, executing logic cost a varying amount of gas on the EVM.

The core game engine in Ethernal Elves is essentially if/then conditions, randomness, and time. It costs 15,000 to run. Summing up the operations we just spoke about- 21,000 + 20,000 + 15,000 and you’re already sitting at about 65,000 or $35. To change a simple true/false boolean value costs 28,000 or $15.

Contract functions and gas usage. Gas was at 160 for this snapshot.

The other constraint is the contract size. You cannot deploy a contract larger than 24kb to Ethereum. So trying to make on-chain art, game logic, and game state storage fit into blocks of 24kb is another very interesting challenge.

When Shakespeare said “Brevity is the soul of wit”, I’m pretty sure he had solidity in mind.

Variable and NFT Metadata Storage

Storing data in the blockchain is expensive. Solidity likes to store data in 32byte “memory slots”. Each 32-byte slot can store 256 bits. Variables that determine the traits your character has, such as its physical attributes, like hair and weapons, or stats like attack points or level are stored on-chain in these slots. Additionally, we need to store other data to create the game mechanics, like your wallet address and timestamp for staking-related game mechanics. Remember, each slot costs gas to create, update and read from.

One of the ways in which we optimize for gas is struct packing, which basically means fitting together a bunch of different traits and stats into one 256 bit/32-byte storage slot. We managed to fill all player traits AND game state into one slot. We did this by adapting what Nova Blitz did here. Solidity likes to use 256-bit integers for calculations. An unsigned integer 256 bits in length is a very big number, 2²⁵⁶ to be precise. Other than storing token values in WEI, it's unlikely you’ll need a number greater than 65,000 to store some values. In our case, we don't really need more than 256 (which is ²⁸ or 8 bits) to store 4 unique body types or 15 unique weapons, or 200 levels.

Storing 8 binary values in one 256-bit address.

We managed to fit 8 variables, including a timestamp and address that used up 200/256 bits we had available. But it gets better. We used two of those 8-bit values to store another six traits.

Storing 13 values in one 256-bit address.

You can store 3 indices in the number 256 using the units, tens, and hundreds places. The hundreds can store three numbers, zero, one, two. The tens can store 6 and units can store 7. Using this indexing technique, we were able to pack a lot more into one data storage slot. To decode the number:

Example: Using the number 145 to store three traits
1. units: 145%10 = 5
2. tens: (145/10) = 14.5, solidity only uses integers, so 14. 14 % 10 = 4
3. hundreds: same as above but divide by 100. Gives you 1.

Bank Balances

The in-game currency is an ERC20 token called MIREN, with the symbol $REN. MIREN is expelled when you kill a creature and can be used within the Ethernal Elves ecosystem. You need MIREN to mint Sentinel Elves after the initial mint is over. When you earn a MIREN reward, the reward is added to your account balance on the smart contract. You have the option of leaving the MIREN in the contract or transferring it to your wallet. When you mint a Sentinel Elf, the mint function first uses MIREN in your contract balance, then checks to see if you have some in your wallet. This saves a little bit of gas and is convenient (you don’t have to do another transaction to claim).

*shout out to 0xInuarashi who implemented this feature in one of his projects.

Other gas optimizations involve inlining code, reusing variables, simplifying math, using circuit breakers, and a lot of reiterations. While it is super fun and exciting optimizing transactions on Ethereum, you can do so much with gas optimizations. This is why a L2 or side-chain solution is necessary with more interactive gameplay which is part of our long-term vision and we are excited for what is to come.

0xHusky

--

--

EthernalElves

Ethernal Elves is a new on-chain RPG. Launching in 2022.