0

I am on an hardhat project, and get this error when i want to compile the solidity code

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

contract Marketplace {
    event UserCreated(
        address indexed userAddress,
        string userId,
        string username,
        uint8 accountType
    );
    event StoreCreated(
        address indexed sellerAddress,
        string storeName,
        uint256 latitude,
        uint256 longitude
    );
    event RequestCreated(
        string indexed requestId,
        address indexed buyerAddress,
        string requestName,
        uint256 latitude,
        uint256 longitude
    );
    event OfferCreated(
        string indexed offerId,
        address indexed sellerAddress,
        string storeName,
        uint256 price,
        string requestId
    );

    event RequestAccepted(
        string indexed requestId,
        string indexed offerId,
        address indexed sellerAddress
    );

    event OfferRemoved(string indexed offerId, address indexed sellerAddress);

    enum AccountType {
        BUYER,
        SELLER
    }
    enum RequestLifecycle {
        PENDING,
        ACCEPTED_BY_SELLER,
        ACCEPTED_BY_BUYER,
        REQUEST_LOCKED,
        COMPLETED
    }

    struct Location {
        uint256 latitude;
        uint256 longitude;
    }

    struct Store {
        string name;
        string description;
        Location location;
    }

    struct User {
        string id;
        string username;
        string phone;
        Location location;
        uint256 createdAt;
        AccountType accountType;
        Store[] stores;
    }

    struct Request {
        string id;
        string name;
        string buyerId;
        uint256 sellersPriceQuote;
        string[] sellerIds;
        string lockedSellerId;
        string description;
        string[] images;
        uint256 createdAt;
        RequestLifecycle lifecycle;
        Location location;
        uint256 updatedAt;
    }

    struct Offer {
        string id;
        uint256 price;
        string[] images;
        string requestId;
        string storeName;
        string sellerId;
        bool isAccepted;
        uint256 createdAt;
        uint256 updatedAt;
    }

    // Custom errors with Marketplace__ prefix
    error Marketplace__OnlySellersAllowed();
    error Marketplace__OnlyBuyersAllowed();
    error Marketplace__OfferAlreadyAccepted();
    error Marketplace__InvalidAccountType();
    error Marketplace__OfferAlreadyExists();
    error Marketplace__UnauthorizedRemoval();
    error Marketplace__OfferNotRemovable();

    mapping(address => User) public users;
    mapping(string => Request) public requests;
    mapping(string => Offer) public offers;
    mapping(address => mapping(string => bool)) public buyerOffers; // Tracks offers created by each buyer for each request

    uint256 constant TIME_TO_LOCK = 900;

    function createUser(
        string memory _id,
        string memory _username,
        string memory _phone,
        uint256 _latitude,
        uint256 _longitude,
        AccountType _accountType
    ) public {
        if (
            _accountType != AccountType.BUYER &&
            _accountType != AccountType.SELLER
        ) {
            revert Marketplace__InvalidAccountType();
        }

        Location memory userLocation = Location(_latitude, _longitude);

        users[msg.sender] = User(
            _id,
            _username,
            _phone,
            userLocation,
            block.timestamp,
            _accountType,
            new Store[](0)
        );
        emit UserCreated(msg.sender, _id, _username, uint8(_accountType));
    }
}

error

UnimplementedFeatureError: Copying of type struct Marketplace.Store memory[] memory to storage not yet supported.


Error HH600: Compilation failed
1
  • Just remove code until you find the line that causes this error. The error message is also quite clear, in my opinion Commented Aug 12 at 12:12

1 Answer 1

1

The problem is that you're trying to assign a memory-allocated struct (specifically, one with an array inside) to a storage-allocated mapping. Solidity doesn't directly support this

To solve the error "UnimplementedFeatureError: Copying of type struct Marketplace.Store memory[] memory to storage not yet supported," follow these steps:

1. Replace Array with Mapping:

  • Instead of using an array to store Store structs, use a mapping to store Store data for each user.

2. Use Mapping for Stores:

  • Define a mapping for Store data and store IDs associated with each user.

3. Adjust Struct and Function:

  • Remove the Store[] stores array from the User struct.
  • Adjust the user creation function to use the mapping for storing Store data.

Add this code:

mapping(address => mapping(string => Store)) public userStores;
mapping(address => string[]) public userStoreIds;

struct User {
    string id;
    string username;
    string phone;
    Location location;
    uint256 createdAt;
    AccountType accountType;
}

function registerUserAndStore(
    string memory _id,
    string memory _username,
    string memory _phone,
    uint256 _latitude,
    uint256 _longitude,
    AccountType _accountType,
    string memory _storeId,
    string memory _storeName,
    string memory _storeDescription
) public {
    Location memory userLocation = Location(_latitude, _longitude);
    users[msg.sender] = User(
        _id,
        _username,
        _phone,
        userLocation,
        block.timestamp,
        _accountType
    );
    
    if (_accountType == AccountType.SELLER) {
        Store memory newStore = Store(
            _storeName,
            _storeDescription,
            userLocation
        );
        userStores[msg.sender][_storeId] = newStore;
        userStoreIds[msg.sender].push(_storeId);
    }
}

You can see the image, by doing the above changes its compiling at my end now:

enter image description here

4
  • thank you it worked.
    – Davy King
    Commented Aug 13 at 10:13
  • Perfect @DavyKing Commented Aug 13 at 10:14
  • @AwaisAhmad Good work Commented Aug 13 at 10:35
  • Thanks Brother @TalhaSajidChaudhary Commented Aug 13 at 11:03

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.