0

Can anyone please update the listUser Function or make a new function to update the address of the User of type struct UserAddress. Push it in the array in the struct User.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;

contract Unknown {
    struct User{
        string name;
        address walletAddress;
        uint256 phoneNumber;
        UserAddress[] addresses;
        uint256 addressCount;
    }

    User[] public allUsers;
    Products[] public allProducts;

    struct Products{
        string name;
        string description;
        string price;
        string[] images;
    }

    struct UserAddress{
        string name;
        string streetName;
        string city;
        string state;
        string country;
        uint256 pincode;
    }
    
    mapping(address => User) public walletAddressToUser;
    mapping(address => Products[]) walletAddressToUserBoughtProducts;
    mapping(address => Products[]) walletAddressToUserOnSaleProducts;
    mapping(address => Products[]) ProductsInCard;

    function listUser(string memory _name, uint256 _phoneNumber) public {
        User storage user = allUsers.push();
        user.name = _name;
        user.phoneNumber= _phoneNumber;
        user.walletAddress = msg.sender;
        allUsers.push(user);
        walletAddressToUser[msg.sender] = user;
    }

}

1 Answer 1

1

What you're asking for is kind of unclear. You want to create an array of UserAddresses in listUser() and add it to your User struct? Short answer is, you can't do that, you'd have to find a workaround. An obvious one would be to add an (address => UserAddress[]) walletToAddresses; mapping, im sure you can figure out other ones too. Exact implementation is down to you because i have no idea how you're getting that data, but if it's passed as an arg to the function you just have to copy it.

By the way, your current code is adding the User you're creating 2 times in the array, i assume that's not intended. Do


    function listUser(string memory _name, uint256 _phoneNumber, UserAddress[] memory _addresses) public {
        User memory user;
        user.name = _name;
        user.phoneNumber= _phoneNumber;
        user.walletAddress = msg.sender;
        user.addresses = _addresses;
        allUsers.push(user);
        walletAddressToUser[msg.sender] = user;
    }

or

    function listUser(string memory _name, uint256 _phoneNumber, UserAddress[] memory _addresses) public {
        User storage user = allUsers.push();
        user.name = _name;
        user.phoneNumber= _phoneNumber;
        user.walletAddress = msg.sender;
        user.addresses = _addresses;
        walletAddressToUser[msg.sender] = user;
    }

instead

1
  • Thank you @foxxxey, Ya I did push it twice by mistake sry there. I got your point. I want to know if l took all the parameters of UserAddress struct in the listuser function as an argument then how can I push it in that array of address (type UserAddress) in the user struct? Can I do it, if yes can you please show me its code. Sorry for being Unclear. Jun 6, 2023 at 17:04

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.