A static, marriage contract that just shows text is no fun. Let’s encode some logic and automation.

Wouldn’t it be cool if your marriage contract can also be a wedding registry?

Let people gift ethers to the contract

Earlier, we mentioned that smart contracts can store money and handle transactions. Let’s put this to good use. You want to let anyone who publicly views your marriage contract to gift it Ether. Let’s call this feature “ringing a wedding bell”.

Create a function called ringBell(). Ethereum offers a special ‘payable’ type function to handle such monetary transactions:

// ringBell is a payable function that allows people to celebrate the couple's marriage by sending Ethers to the marriage contract function ringBell() public payable {...}

Behind the scenes, Solidity functions receives important variables:

msg.value: contains the designed amount from the sender. A “payable” type function automatically stores this msg.value in its own ledger.

contains the designed amount from the sender. A “payable” type function automatically stores this msg.value in its own ledger. msg.sender: is the address of whoever sent you money.

2. Let’s be greedy and use our knowledge of msg.value to impose a minimum gift value. How about a minimum gift of .0001 ether (~5 cents)?

// ringBell is a payable function that allows people to celebrate the couple's marriage by sending Ethers to the marriage contract function ringBell() public payable {

require(msg.value > .0001 ether);

}

Notice:

require() is used to validate user inputs and other conditional checks that might often fail.

is used to validate user inputs and other conditional checks that might often fail. If the conditions are not met, the user’s transaction is immediately halted.

This is nice, because all remaining gas (i.e. transaction fees) are immediately refunded to the sender if his request fails.

Let contract owners withdraw their money

Declare a collect() function that transfers the ethers from a smart contract to a user’s wallet.

function collect() {

owner.transfer(address(this).balance);

}

Notice:

‘This’ refers to the contract itself. So ‘owner.transfer(…)’ moves the contract’s entire balance into the owner’s wallet.

Let’s take a moment here to think about security. You want to ensure that only the contract’s owner can withdraw.

One way to handle this is to “require(owner == msg.sender)”. But you might have to perform this “require owner” check often — since you want to add more features for owners. The solution?

Refactor conditional checks into function modifiers

It is considered best practice to refactor require() checks into their own modifier functions.

Create a function modifier called “onlyOwner”:

// Reusable modifier function modifier onlyOwner() {

require(msg.sender == owner);

_;

} // To use a modifier, append it to the end of the function name function collect() external onlyOwner {

owner.transfer(address(this).balance);

}

Notice:

“_” is a placeholder for the contents of the feature-function you are modifying with onlyOwner().

for the contents of the feature-function you are modifying with onlyOwner(). We use the external function modifier to save the owner some transaction fees.

Finally, create a helper function for our front-end:

Create a function that only lets the contract’s owners check how much ether his contract contains:

// Allow only owners the check the balance of the contract function getBalance() public view onlyOwner returns (uint) {

return address(this).balance;

}

That’s it! You’ve built your first smart contract class.

You can now use Remix to instantiate smart marriage contracts on the blockchain. All that’s left is creating the factory contract and a simple UI.

Next steps