Take the following code for example:
pragma solidity ^0.4.23;
import "./MyContract.sol";
contract MyFactory {
address[] public myContracts;
function createContract(string stringOne, string stringTwo, string stringThree) public {
address sender = msg.sender;
address newContract = new MyContract(sender, stringOne, stringTwo, stringThree);
myContracts.push(newContract);
}
function getAllContracts() public view returns(address[]) {
return myContracts;
}
}
And then also this:
pragma solidity ^0.4.23;
contract MyContract {
address public createdBy;
string public stringOne;
string public stringTwo;
string public stringThree;
constructor(address sender, string one, string two, string three) public {
createdBy = sender;
stringOne = one;
stringTwo = two;
stringThree = three;
}
function getContractSummary() public view returns(address, string, string, string) {
return (
createdBy,
stringOne,
stringTwo,
stringThree
);
}
}
The best way I can think to search through all the contracts that have been created by the factory would be to create a mapping in the factory:
mapping(string => address) public contractNames;
When creating a new contract we could push stringOne to the mapping along with the address that the contract was created at.
Then we could call the public getter that this would create and pass the users search term. If the search term was found we could get back the address that the contract was deployed at.
The only problem with this approach is that we would only receive back results that matched the search term exactly.
Is there a better way to implement this functionality? Or is this as good as it gets?