Building a Simple Inventory Management Smart Contract in Solidity

Harsh RajHarsh Raj
3 min read

Introduction

In this blog post, I'll walk you through the process of building a basic inventory management smart contract using Solidity. This contract allows users to add items to an inventory, view the details of items, and update the stock of existing items. It's a great example of how to use Solidity's 'struct' and 'mapping' features effectively.

Prerequisites

Before diving into the code, ensure you have a basic understanding of the following:

  • Solidity programming language.

  • Ethereum smart contract development.

  • Tools like Remix IDE for writing and deploying smart contracts.

Step-by-Step Breakdown

Let's break down the Solidity code for the Inventory Management Smart Contract.

1. Setting Up the Smart Contract

We begin by declaring the Solidity version and the contract name:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Inventory {

Here, the pragma directive ensures compatibility with Solidity version 0.8.10. The Inventory contract is where all our logic will reside.

2. Defining the Item Structure

We use a struct to define the properties of an item in the inventory:

struct Item {
string name;
uint price;
uint stock;
}

The Item struct contains three properties:

  • name: The name of the item (string).

  • price: The price of the item in wei (unsigned integer).

  • stock: The available quantity of the item (unsigned integer).

3. Storing Items with a Mapping

Next, we store the items in a mapping, which associates a unique ID with each Item:

mapping(uint => Item) public items;
uint public itemcount;

Here:

  • items: A mapping that uses a uint as the key and an Item struct as the value.

  • itemcount: A counter that tracks the number of items added to the inventory.

4. Adding Items to the Inventory

The addItem function allows users to add a new item to the inventory:

function addItem(string memory _name, uint _price, uint _stock) public {
itemcount++;
items[itemcount] = Item(_name, _price, _stock);
}

This function increments the itemcount and assigns a new Item to the items mapping. The item's name, price, and stock are passed as parameters to this function.

5. Retrieving Item Information

To view the details of a specific item, we use the getItem function:

function getItem(uint _itemId) public view returns(string memory, uint, uint) {
Item memory item = items[_itemId];
return (item.name, item.price, item.stock);
}

This function takes an item ID as an argument and returns the name, price, and stock of the corresponding item.

6. Updating Stock of an Item

Finally, the addstock function allows us to update the stock of an existing item:

function addstock(uint itemid, uint newstock) public {
items[itemid].stock = newstock;
}

By passing the item ID and the new stock value, this function updates the item's stock in the items mapping.

Conclusion

In this blog post, we created a simple inventory management system using Solidity. We explored the use of structs and mappings to store and manage data on the blockchain. You can build upon this smart contract by adding more features like removing items, setting different prices, or even integrating it with a front-end interface.


Deploying the Smart Contract

You can deploy this contract using Remix IDE:

  1. Copy the code into Remix.

  2. Compile the contract.

  3. Deploy it on the Ethereum test network.

  4. Interact with the functions through the Remix interface.

Further Improvements

As a next step, consider enhancing this contract by adding more functions like deleting items, adjusting prices, or integrating it with an ERC-20 token for payments. This will make your inventory system more robust and feature-rich.


I hope this guide helped you understand how to build a basic inventory management smart contract in Solidity. Feel free to explore more complex use cases and share your progress!

Happy coding!

0
Subscribe to my newsletter

Read articles from Harsh Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harsh Raj
Harsh Raj

Blockchain Developer | Smart Contract Auditor | Solidity Enthusiast Passionate about decentralized applications and blockchain technology. I specialize in developing smart contracts, creating innovative solutions on Ethereum, and exploring the potential of Web3. Currently documenting my journey in Solidity to share insights and help others in the blockchain community. Let's connect and build the future of decentralized tech together!