create account

Day 4 of Building an Equipment System with Smart Contracts (Solidity) by therealwolf

View this thread on: hive.blogpeakd.comecency.com
· @therealwolf · (edited)
$6.72
Day 4 of Building an Equipment System with Smart Contracts (Solidity)
Day 4 of "Building an Equipment System with Smart Contracts" for @ethgardlegends.  

Imagine you find a cool sword and want to equip it to your hero to gain benefits in the game. That's what I'm building.

In technical terms, the goal is to allow NFTs (ERC1155/ERC721) to be equippable on other NFTs (ERC721 => Heroes).

---  

### Today's progress:  

Woke up today and instantly knew how to cut down more contract size. While I was using a shared item metadata mapping for common attributes, I also defined separate metadata mappings for each item type (weapon, armor, accessory) to set exclusive values like `armor = 10` or `damage = 15`.


The fix: I merged these into the existing custom stats struct in the common metadata. Since I was already using this struct for irregular attributes like strength and agility, it made sense to extend it for properties like minDamage and attackSpeed. This small change allowed me to remove more code, freeing up another 4kb, bringing the contract size down to around 20kb.

```
struct ItemStat {
  Stat stat;
  uint8 value;
}

/// Before

struct ItemMetadata {
  uint32 itemId;
  uint16 durability;
  bool isSoulbound;
  ItemStat[] stats; // i.e. 8 strength, 5 agility
}

struct WeaponMetadata {
  uint32 itemId;
  uint16 minDamage; // i.e. 10
  uint16 attackSpeed; // i.e. 12
  ...
}

/// After

struct ItemMetadata {
  uint32 itemId;
  uint16 durability;
  bool isSoulbound;
  ItemStat[] stats; // i.e. 8 strength, 5 agility, 10 minDamage, 12 attackSpeed
}

```

I also renamed and refactored the contract from InventorySystem to EquipmentSystem - an important realization I had after finishing yesterday’s post. Since the code is currently focused purely on equipping and unequipping, it makes sense to be specific with the naming; especially with contract size limits in mind.

Next, I added logic to unsoulbound items. In the game, all items usually start as soulbound, meaning free-to-play players can earn items just like anyone else. But to trade them on the market, they need to be unsoulbound - which costs X amount of Y currency. The contract is flexible enough to support any currency, but in this case, it will be Ethershards - a soulbound currency earned only by owning assets while winning games.

With all tests updated and passing, I was finally able to deploy and initialize the contracts on testnet. That led me to the indexer - a system that listens for smart contract events and transforms them into database tables. Seeing the items I had defined in code now appearing in a structured database - completely automated via onchain events - was pretty satisfying. There’s still more logic to add, but some of it relies on actually minting and equipping items.

So, I started working on an early UI prototype for the Equipment System. Still very, very early, but it might give you a better insight into what I'm building. This pattern is also just how I like to work - switching between different layers of the stack. For example, I might start with smart contracts → backend → frontend → back to backend → more smart contract work → then frontend again, and so on. Keeps things fresh and helps me see the bigger picture.

![image.png](https://files.peakd.com/file/peakd-hive/therealwolf/Eo1vWZJvbyq2wu9abp4KLvkEfPJZ2vdKp7vQRWxDECbvzrZQLKLGjENgTPf8zQ8o3sA.png)  

---  

### Today's lesson:  

_To quote one of my favourite songs: ["And I need to get some rest -- yo, where's the cess?](https://www.youtube.com/watch?v=P8JEm4d6Wu4)_

Take some rest.

I’m usually relentless when it comes to bug-hunting. I enjoy my work as a code-detective, and I find it hard to call it a night when a problem isn't solved. But after a long day, even if my motivation and persistence are there, my brain just isn’t at full power.  

And yesterday/today was a perfect example of this. After a good night’s rest, I almost instantly saw the best solution for the contract size issue and a better solution in general. That of course not always the case, sometimes it takes longer to find the solution to an issue than one night, but taking some rest is a great start.

---  

_PS: If you're having trouble understanding certain parts but want to learn more, try the AI feature on Peakd or ChatGPT/Grok to have it explained 👍._
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 42 others
properties (23)
authortherealwolf
permlinkday-4-of-building-an-equipment-system-with-smart-contracts-solidity
categorybuildinginpublic
json_metadata"{"app":"peakd/2025.3.5","description":"Eat. Sleep. Code. Repeat. Eat. Sleep. Code. Repeat. Eat. Sleep. Code. Repeat","format":"markdown","image":["https://files.peakd.com/file/peakd-hive/therealwolf/Eo1vWZJvbyq2wu9abp4KLvkEfPJZ2vdKp7vQRWxDECbvzrZQLKLGjENgTPf8zQ8o3sA.png"],"tags":["buildinginpublic","ethgardlegends","evm","smartcontracts","ethgard","hive"],"users":["ethgardlegends."]}"
created2025-03-27 22:48:54
last_update2025-03-29 22:29:42
depth0
children4
last_payout2025-04-03 22:48:54
cashout_time1969-12-31 23:59:59
total_payout_value3.364 HBD
curator_payout_value3.353 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,268
author_reputation581,693,011,827,252
root_title"Day 4 of Building an Equipment System with Smart Contracts (Solidity)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,723,091
net_rshares20,806,885,259,646
author_curate_reward""
vote details (106)
@meno ·
$0.21
hey brother... I've been meaning to ask. Are you doing all this on your own? Did you get a team together? 
👍  
properties (23)
authormeno
permlinkre-therealwolf-stt3b0
categorybuildinginpublic
json_metadata{"tags":["buildinginpublic"],"app":"peakd/2025.3.5","image":[],"users":[]}
created2025-03-27 23:45:00
last_update2025-03-27 23:45:00
depth1
children2
last_payout2025-04-03 23:45:00
cashout_time1969-12-31 23:59:59
total_payout_value0.106 HBD
curator_payout_value0.107 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length106
author_reputation525,533,153,064,630
root_title"Day 4 of Building an Equipment System with Smart Contracts (Solidity)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,724,200
net_rshares663,473,523,896
author_curate_reward""
vote details (1)
@therealwolf ·
$0.08
Meet the team:


![image.png](https://files.peakd.com/file/peakd-hive/therealwolf/23uFKd8yfGsnk1c74d1X8RDAp2VwA9fdNYf58dtw3acFcMqYaenS1pjYMJB4H4eVG5sZV.png)


😄
👍  , ,
properties (23)
authortherealwolf
permlinkre-meno-stuwkx
categorybuildinginpublic
json_metadata{"tags":["buildinginpublic"],"app":"peakd/2025.3.6"}
created2025-03-28 23:14:57
last_update2025-03-28 23:14:57
depth2
children1
last_payout2025-04-04 23:14:57
cashout_time1969-12-31 23:59:59
total_payout_value0.042 HBD
curator_payout_value0.042 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length160
author_reputation581,693,011,827,252
root_title"Day 4 of Building an Equipment System with Smart Contracts (Solidity)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,749,516
net_rshares270,729,818,168
author_curate_reward""
vote details (3)
@meno ·
HAHAHAHHAHA
properties (22)
authormeno
permlinkre-therealwolf-stuxem
categorybuildinginpublic
json_metadata{"tags":["buildinginpublic"],"app":"peakd/2025.3.6","image":[],"users":[]}
created2025-03-28 23:32:48
last_update2025-03-28 23:32:48
depth3
children0
last_payout2025-04-04 23:32:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length11
author_reputation525,533,153,064,630
root_title"Day 4 of Building an Equipment System with Smart Contracts (Solidity)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,749,739
net_rshares0
@mercurial9 ·
$0.43
Love the art work of the card!
👍  ,
properties (23)
authormercurial9
permlinkre-therealwolf-sttif2
categorybuildinginpublic
json_metadata{"tags":["buildinginpublic"],"app":"peakd/2025.3.5","image":[],"users":[]}
created2025-03-28 05:11:27
last_update2025-03-28 05:11:27
depth1
children0
last_payout2025-04-04 05:11:27
cashout_time1969-12-31 23:59:59
total_payout_value0.214 HBD
curator_payout_value0.214 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation287,212,886,021,633
root_title"Day 4 of Building an Equipment System with Smart Contracts (Solidity)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,727,807
net_rshares1,354,068,727,928
author_curate_reward""
vote details (2)