I am new to Solidity, trying to store a product in the purchaser wallet and retrieve the information from the owner wallet. I have some ideas but didn't know which one will be best.

  1. To store an individual product in a random address
  2. Store products in owners address
  3. Store product in an array of products in owners address
  4. Storing product data as a transaction in a owner's wallet

Appreciate if you can share sample code. Thanks in advance.

pragma solidity >=0.4.25 <0.7.0;

contract ItemBill{

    enum ProductState {Shipped, Store, Owned, Sale}
    address owner;
    bytes32 public name;
    uint public productCount = 0;

    struct Product 
            uint id;
            bytes32 name;
            bytes32 serlialNo;
            uint price;
            address UpdatedBy;
            bytes10 pro_state; //later change to ProductState

    mapping(address => Product[]) OwnerOfProducts;

    //address[] public ProductAccts; //Array address of products
    //Product[] prod; //Array of products

    event ProductCreated
        uint id,
        bytes32 name,
        bytes32 serlialNo,
        uint price,
        address owner,
        bytes10 pro_state //later change to ProductState

    constructor() public {
            owner = msg.sender;

    function createProduct(address _OwnerAddress, bytes32 _name, bytes32 _serialNo, uint _price, bytes10 _pro_state) public {
        // Require a valid name
        require(_name.length > 0);
        // Require a valid price
        require(_price >= 0);
        // Require a valid serlialNo
        require(_serialNo.length > 0);
        //Create new product
        Product memory p;
        //assign value to products
        p.id = productCount++;
        p.name = _name;
        p.serlialNo = _serialNo;
        p.price = _price;
        p.UpdatedBy = msg.sender;
        p.pro_state = _pro_state;


        // Trigger an event
        emit ProductCreated(productCount, _name, _serialNo, _price, msg.sender, _pro_state );

    function getProduct(address _address) view public returns(uint, bytes32, bytes32, uint, address, bytes10) {

        returns(OwnerOfProducts[_address].id, OwnerOfProducts[_address].name, OwnerOfProducts[_address].serlialNo, OwnerOfProducts[_address].price, OwnerOfProducts[_address].owner, OwnerOfProducts[_address].pro_state); 

**//Getting ParseError: Expected primary expression. //In Remix**


    function getproductcount() view public returns(uint){
        returns OwnerOfProducts.length;

1 Answer 1


As always, it depends on what the dApp is supposed to do. From your example implementation, I see serialNumber and that leads me to surmise that "product" as you understand it, is an individual item and not an inventory item that can exist in quantity or be in stock and owned by many people at the same time. But think that through and be sure the latter isn't more like what you have in mind.

I fixed the last two functions that are causing you trouble.

    function getProduct(address _address, uint index) view public returns(uint, bytes32, bytes32, uint, bytes10) {
        Product storage p = OwnerOfProducts[_address][index];
        return(p.id, p.name, p.serlialNo, p.price, p.pro_state); 

    function getproductcount(address _address) view public returns(uint){
        return OwnerOfProducts[_address].length;

As described above, I'm not sure this data structure fits what you want to accomplish, but the data structure implies these functions have to be a certain way.

This bit:

mapping(address => Product[]) OwnerOfProducts;

Says, for every owner address, there is an array of products.

So, getProduct will need the address and the row to inspect. It does not return the owner's address because the caller needs to already know that. They pass it in.

getproductcount needs to know which owner array you wan to count.

It's possible that what you're really after is:

  1. A master table of product info - the items.
  2. Instances of items manufactured and sold to owners, with serial numbers, etc.
  3. Instinctively, I would start with a flat organization of products I can use to discover the owners (array of addresses or orders), and an array of keys in the owners I can use to discover the products.

You are into a fairly advanced structure for a beginner project and not enough is known about the proper way to normalize the structure. As a general rule, when the function signatures and processes seem awkward, it usually means the data structure doesn't really map to the real universe and the problems start there.

To store an individual product in a random address

There is a way to model serializable products as NFTs using ERC721. In this case, product == address, but you shouldn't force that to happen. Only if it's natural, otherwise addresses for keys is a bad idea.

Store products in owners address

Given the next question, this sounds like one product per owner and that is probably not sufficient.

Store product in an array of products in owners address

Only if you are okay with only being able to enumerate the products with knowlege of the owners who have them.

Storing product data as a transaction in a owner's wallet

Everything is a transaction. Most will involve the owner's wallet. There are advanced patterns that optimize for gas but I don't think any apply here. As a general hueristic, anything that is relied on for contract logic must be in the contract state layout.

As a tip, I would optimize storage by removing descriptive info that is not required for contract logic as there are other ways to store it at less cost.

Hope it helps.

  • Thanks @rob-hitchens-b9lab for the response. you are right I have a good experience in program but new to solidity. Wanted to store history of the product/Item from supplier to customer. Using NFT's ERC721 seems to be a good choice but it will cost lot of gas. I am think to use 2 structures ` Structure (Transaction){ ...} and structure(Product){ transaction, ....} ` Commented Jan 27, 2020 at 5:28
  • This library might help so you can more easily think in terms of relationships. There are no indexes, joins, etc., in Solidity so you have to build out what you will need for access and avoid unbounded iteration inside a contract. github.com/rob-Hitchens/UnorderedKeySet Commented Jan 27, 2020 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.