/ contracts / MasterFactory.sol
MasterFactory.sol
 1  // SPDX-License-Identifier: MIT
 2  pragma solidity ^0.8.0;
 3  
 4  import "./UserProfile.sol";
 5  import "hardhat/console.sol";
 6  
 7  contract MasterFactory {
 8  
 9      mapping(address => UserProfile) private userProfiles;
10      address[] private allUsers;
11  
12      function addUserProfile(address _userContractAddress) external {
13          userProfiles[_userContractAddress] = UserProfile(_userContractAddress);
14          allUsers.push(_userContractAddress);
15      }
16  
17      function getUserNameByAddress(address _userContractAddress) external view returns (string memory) {
18          UserProfile profile = userProfiles[_userContractAddress];
19          return profile.getName();
20      }
21  
22      function getAllUserAddresses() external view returns (address[] memory) {
23          return allUsers;
24      }
25  
26      function getAllUserNames() external view returns (string[] memory) {
27          uint256 arrayLength = allUsers.length;
28          string[] memory users = new string[](arrayLength);
29  
30          for (uint256 i = 0; i < arrayLength; i++) {
31              UserProfile profile = userProfiles[allUsers[i]];
32              users[i] = profile.getName();
33          }
34  
35          return users;
36      }
37  
38  }