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);            

    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 (       

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?

  • 1
    Why not implement search off-chain? Is it important that the search itself be trustless? – user19510 Jul 6 '18 at 22:34
  • 1
    @smarx The search itself doesn't have to be trustless but I would like to avail of the censorship resistance that ethereum provides. If I do searches off chain on a VPS or something, the VPS could be taken offline. That would break the search functionality temporarily. Of course I could do backups. But then any new contracts that where added in the time the VPS was offline would't be stored in a database. – Dean Brown Jul 6 '18 at 23:12
  • Right, if users are concerned about censorship by the operator of the database, then I agree that's not a good solution. As to catching up after being offline, that's what events are for! – user19510 Jul 6 '18 at 23:16

In my opinion, you should reformulate the way you separate concerns so that the search function is performed by software clients which can be servers, browsers, something else or all of those.

Have a look at this opinion piece about this exact concern for a more detailed explanation: https://medium.com/solidified/the-joy-of-minimalism-in-smart-contract-design-b67fb4073422

Hope it helps.

| improve this answer | |

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.