1

I'm new to solidity and I have a struct that takes an array of Traction[], I'm trying to create the array inside a function createItem but I'm getting an invalid implicit conversion.

I have added the entire contract incase if I'm doing something else wrong.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.21 <0.7.0;
pragma experimental ABIEncoderV2;

contract SupplyChain {
    // Identify the role of an owner
    enum OwnerType {Any, Manufacturer, Ditributor, Retailer, Consumer}

    struct Owner {
        string id; // documentId of a mongo database
        string name;
        OwnerType ownerType;
    }

    // A trasaction is event in the supply chain
    // mostly a change of owner
    struct Transaction {
        string txid; // A generated hash of the transaction
        string previous;
        uint256 timestamp; // Unix timestamp
        bool allowAny; // Anyone can receive/consume the product
        address owner;
        address destination;
        OwnerType destinationType; // Mentions what should be the role of Owner
        string documentId; // documentId that's stored in a mongo database
    }

    struct Item {
        string name;
        uint256 price;
        Transaction[] transactions;
    }
    mapping(uint256 => Item) items;
    mapping(address => Owner) owners;

    function createOwner(
        address account,
        string memory documentId,
        string memory name,
        uint8 roleType
    ) public returns (bool) {
        Owner memory owner = Owner(documentId, name, getRoleType(roleType));
        owners[account] = owner;
        return true;
    }

    function getOwner(address account) public returns (Owner memory) {
        Owner memory owner = owners[account];
        return owner;
    }
    
    function getRoleType(uint256 roleType)private returns (OwnerType){
        if(roleType==0){
            return OwnerType.Any;
        }else if(roleType == 1){
            return OwnerType.Manufacturer;
        }else if(roleType == 2){
            return OwnerType.Ditributor;
        }else if(roleType == 3){
            return OwnerType.Retailer;
        }else if(roleType == 4){
            return OwnerType.Consumer;
        }
    }

    function createItem(
        string memory itemName,
        uint256 itemPrice,
        string memory txid,
        uint256 timestamp,
        bool allowAny,
        address ownerId,
        address destination,
        uint256 destinationType,
        string memory documentId
    ) public {
        // Get owner
        Owner memory owner = getOwner(ownerId);
        // Create inital transaction
        Transaction memory transaction = Transaction(
            txid,
            "", // no previous transaction
            timestamp,
            allowAny,
            ownerId,
            destination,
            getRoleType(destinationType),
            documentId
        );
        // ERROR HERE
        // Here I get an error
        // TypeError: Invalid type for argument in function call. Invalid implicit conversion from struct SupplyChain.Transaction memory[1] memory to struct SupplyChain.Transaction memory[] memory requested. Item memory item = Item(itemName, itemPrice, transactionArray);
        Transaction[1] memory transactionArray =  [transaction];
        Item memory item = Item(itemName, itemPrice, transactionArray);
        // If I change the size of the array to dynamic I get the same invalid conversion but on the assignment and not on function call
        
        
        uint256 itemId = 1;
        // Add item to the chain
        items[itemId] = item;
    }
}

2

The types Transaction[1] and Transaction[] are different:

  • Type Transaction[1] is a statically-allocated array of Transaction objects
  • Type Transaction[] is a dynamically-allocated array of Transaction objects

Therefore, you should either change this:

Transaction[1] memory transactionArray = [transaction];

To this:

Transaction[] memory transactionArray = new Transaction[](1);
transactionArray[0] = transaction;

Or this:

struct Item {
    ...
    Transaction[] transactions;
}

To this:

struct Item {
    ...
    Transaction[1] transactions;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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