Step 1— Create the factory contract

In the same Marriage.sol file as your Certificate contract, create a MarriageNotary (or MarriageFactory) contract:

pragma solidity ^0.4.19; contract MarriageNotary {

// TODO

} contract Marriage {...}

Specifically, this factory contract will:

Create new Certificate smart contract

Keep track of a list of previously created Certificates

2. Let’s store the list of previously created contracts in an array of addresses. This array variable should be publicly viewable by anyone, i.e. a front-end wrapper that will render previous certificates.

address [] public registeredMarriages;

Best practice tips:

Arrays are good for indexed lookups. In this case, it’s enough for our simple use case.

If you need to loop over your records, arrays are expensive and not the best practice. If you need a standard key-value pair look up, replace your arrays with with mappings instead.

your records, arrays are expensive and not the best practice. If you need a standard key-value pair look up, replace your arrays with with instead. Lastly, in Solidity, you cannot get array.length. So if you need to count the total certificates users created, you should create a new global storage uint variable to track the length of arrays.

3. Next, declare a function to create future marriage certificates. Users will call this function themselves, with details like the couple’s names and vows.

function createMarriage(string _leftName, string _leftVows, string _rightName, string _rightVows, uint _date) public { }

4. Instantiate your new Marriage contract and save the address of the newly generated smart contract. Remember to push the address to your array of registeredMarriages.

function createMarriage(...) public { address newMarriage = new Marriage(msg.sender, _leftName, _leftVows, _rightName, _rightVows, _date);



// saving the address so a front-end client can find it

registeredMarriages.push(newMarriage); }

Let’s think about the front-end interaction for a minute here. When you create a new marriage contract, it takes time for the new certificate contract to be written to the blockchain.

Note: createMarriage is a send() to the blockchain that “promises” to return true or false, depending on the transaction’s success. It won’t return any values, like the new contract address, to the front-end client. Thus, your web client won’t instantly know the address after sending the createMarriage request. Since we want to redirect users to this newly created contract (via its address), we’ll need some (cheap) way for the factory contract to communicate to your client once this “promise” is resolved.

5. Let’s make this contract emit an event to relay this message.

event ContractCreated(address contractAddress); function createMarriage(...) public {

...

emit ContractCreated(newMarriage);

...

}

Best practice tips:

Use events as a “cheap” form of storage. This is helpful in Ethereum, where storage is costly.

This is helpful in Ethereum, where storage is costly. Use events to track user history or notable moments, like the outcome of a bid, or the instance some record is changed on the blockchain.

Events are logged on the network and freely viewable by the front-end. I.e. see this forevermore event where someone rang my wedding bell.

Though contracts emit events, events are not readable from inside the contract. It’s meant for an external client.

6. Lastly, provide a helper function so your web client can easily retrieve all certificates created by this factory.

function getDeployedMarriages() public view returns (address[]) {

return registeredMarriages;

}

Notice:

Solidity gives you view functions for static data-types, for free. This is why you can view a global uint without a getUint() getter function.

This doesn’t apply to arrays. You have to declare your own retrieval function in this case. This should also raise flags that getArrays() may not be a best practice for arrays that will grow exponentially.

You’ve completed the smart contract programming portion of this tutorial! Well done.