0

I'm working on the following code in Remix IDE. What I'm trying to do is create an object of a structure that is mapped to an address. Inside the struct, there is a dynamic array. How can I put a value in this case? I checked other answers on stack exchange but I can't find anything that works for my case. Help please.

pragma solidity ^0.5.0; 

contract Register{
    
    //state variables - stored permanently in contract storage 
    
    // goverment entity - municipality 
    address public govermentEntity; 
    
  
    //seller - sorting facility 
    struct Seller {
        address sellerAddress; 
        string sellerLocation;
        address [] sortingMachineAddress; // dynamic array 
        bool isExist;
    }
    

    
    //constructor - initilize state variables
    constructor() public{
     govermentEntity = msg.sender; 
    }
    
    // Mappings 
  
    mapping (address => Seller) registeredSellers; 
    
    modifier onlyGovermentEntity{
        require(msg.sender == govermentEntity, "Entity not authorized to register stakeholders.");
        _;
    }
    
    // register seller if it doesn't exist 
    function registerSeller (address addr, string memory sellerLocation, address sortingMachineAddress) public onlyGovermentEntity {
        require(registeredSellers[addr].isExist == false , "Seller is registered already."); 
        
        registeredSellers[addr] = Seller(addr, sellerLocation, sortingMachineAddress, true);  // WHAT IS A SORTING MACHINE ADDRESS AND HOW TO SAVE IT DYNAMICALLY???
    }
    
   
    
}
0

Change this:

registeredSellers[addr] = Seller(addr, sellerLocation, sortingMachineAddress, true);

To this:

registeredSellers[addr].sellerAddress = addr;
registeredSellers[addr].sellerLocation = sellerLocation;
registeredSellers[addr].sortingMachineAddress.push(sortingMachineAddress);
registeredSellers[addr].isExist = true;

BTW, the sellerAddress field seems redundant, as it essentially maps a key to itself.


I would actually go one step further and change the function prototype from this:

function registerSeller(
    address addr,
    string memory sellerLocation,
    address sortingMachineAddress
)

To this:

function registerSeller(
    address addr,
    string memory sellerLocation,
    address[] memory sortingMachineAddress
)

And then do this:

registeredSellers[addr].sellerAddress = addr;
registeredSellers[addr].sellerLocation = sellerLocation;
registeredSellers[addr].isExist = true;
for (uint256 i = 0; i < sortingMachineAddress.length; i++)
    registeredSellers[addr].sortingMachineAddress.push(sortingMachineAddress[i]);
2
  • Thank you for the reply! I'm testing it out now but I don't know how to pass an array of addresses in remix IDE. Is it something like ["0x12....", "0x34..."]? – Iman Dec 21 '20 at 16:34
  • @Iman: I believe so. – goodvibration Dec 21 '20 at 17:00

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.