Head over to the top left dropdown “Environment” and make sure “Javascript VM” is selected. This means that all the code will run as a standalone, and not interact with the actual Ethereum network in any way. Click the + icon in the top left corner to create a new file, and call it “will.sol”. A tab will appear at the top which represents which file is currently being edited.

2. Define the Version

In solidity, the first line must always state its current version. Is should look like this:

Version at the time of writing: 0.5.7

3. First Contract

Skip a line and type the following:

Each new contract must be prepended with “contract”, then the name with the first letter always capitalized, followed by open/close curly brackets to contain the logic. Use a double forward slash to write a comment, which is useful for describing for what the code does without altering it.

4. Global Variables and the Constructor

Before we start writing code, we should already have the terms of the will clearly defined. Let’s say gramps is leaving behind a fortune of 50 Ethers. 20 will go to his son Conrad and the remaining 30 to his wife Lisa. Realistically, when gramps passes away, an external program would call a function inside the contract to distribute the funds, but we will take care of that ourselves for the purposes of this tutorial. Check the comments section for discussion on this!

Let’s start by declaring:

The owner of the contract The amount of fortune left behind A switch that tells us if gramps is still alive A constructor function that sets these values.

Line 5 declares the owner. When declaring variables in solidity, they must be prepended with their type. Owner in this case is of type “address” which is unique to solidity and represents an Ethereum wallet address. This will be useful later on when calling specific functionality only meant for the owner (gramps).

Line 6 will hold the value of the fortune gramps is leaving behind. Its type is a uint or “unsigned integer” which means it can only be a positive number. Solidity has many types, but we will not cover all of them here. They can be found in the official documentation for further reading.

Line 7 tells us whether gramps is deceased or not in the form of a boolean value, true or false. It is set to false by default.

Lines 9–13 is the constructor function. This special function will execute automatically upon the contract’s deployment.

The “public” keyword is what’s known as a “visibility modifier” which tells the contract who is allowed to call the function. Public means that the function can be called within the contract and outside of it by someone else or another contract.

The “payable” keyword is what makes solidity truly unique. It allows the function to send and receive ether. The constructor has this modifier so that when we deploy the contract we can initialize it with an ether balance, in this case 50. When the contract receives ether, it will store it in its own address.

Here, we set the owner to “msg.sender”, which is a built-in global variable representative of the address that is calling the function. In this case, it will be us (gramps).

The fortune is set to “msg.value”, which is another built-in variable that tells us how much ether has been sent.

Even though isDeceased is set to false by default, it’s set manually here to provide clarity.

5. Modifiers

Modifiers are add-ons to functions that contain conditional logic. For example, I have a function that turns off a light switch and a modifier that states the light switch must be “on”. I add the modifier to function so that it can only be called if the switch is “on” and will throw an error if the switch is “off”.

Line 15 declares the “onlyOwner” modifier. If added to a function, it can only be called if the caller (msg.sender) is equivalent to the owner variable as stated above (remember how we set the owner in the constructor). We will need this to allow the distribution of funds, which will be implemented later.

The “require” keyword states that everything written within the following parenthesis must be equal to true or else solidity will throw an error and the execution will stop.

The “_;” at the end tells the execution to shift to the actual function after it finishes reading the modifier.

Line 20 declares the “mustBeDeceased” modifier. Once added to a function, it can only be called if the value of “isDeceased” is true. We will also use this to allow for the distribution of funds. There is currently no way to set it to true, but we will fix that later.

As an aside, we could just write “require” underneath each function declaration, but using modifiers is a great way to avoid repetition and to reuse code.

6. Inheritances

Now we must declare how the loot is divided amongst the family members. We will need their public wallet keys (addresses) and their desired allotments.

As we stated before, Conrad will receive 20 ETH and Lisa will inherit 30. Let’s create a list to store their wallet addresses and a function that sets the inheritance for each address.

Line 25 declares an empty array called “familyWallets” for storing the family members’ wallet addresses. This is a list-like data structure in which any element can be indexed and subsequently accessed within it. Notice the keyword “payable” before the brackets. This allows the address(es) to receive Ether. The square brackets after “address” indicate it’s an array of items rather than a single variable.

Line 27 creates a mapping from an address type to a uint type named “inheritance” for stashing the value of each address’ inheritance. This is a key/value pair data structure in which the key can be called to obtain the value. It’s the equivalent of a “dictionary” in other languages such as Python and Javascript.

Line 29 declares the function that adds an address to the array we just created, then sets the inheritance given the address. When we call it, we must provide it with one family member’s wallet address along with their allotted spoils. Notice the “onlyOwner” modifier we added to this function. Can you guess why we stuck that in there? (Hint: look at the “public” keyword.)

Line 30 simultaneously creates a wallet address and appends it to our “familyWallets” array given the input parameter “wallet” with “.push”.

Line 31 takes the value of the key “wallet” (a parameter) from the “inheritance” mapping we created earlier and sets it to the inheritance given the other parameter, “inheritanceAmount”.

Notice how logically these steps connect starting from the first. We created a spot for the wallets and the inheritance to live. The function would then populate those data fields with the information we supply it!

7. Show me the Money

Let’s recap. So far we’ve learned about global variables and their types, the constructor, special keywords such as “payable” and “public”, built-in variables like “msg.sender”, modifiers and “require”, arrays, mappings and functions. We’ve built the framework, now let’s tie it all together so we can complete our contract.

For the last coding part of the tutorial, we’ll implement a way the family can get paid by looking through each of their addresses and sending them their respective dough.

Line 34 declares the “payout()” function. Note the “private” keyword. This visibility modifier is the opposite of “public” as we’ve seen earlier on. It permits this function to be called only within the contract as demonstrated on line 42. Security is the primary purpose as it doesn’t allow anyone or any other contract to interact with it. Notice the “mustBeDeceased” modifier tagged on at the end. It’s currently still impossible for this function to execute because of it.

Line 35 is a for loop that iterates over the “familyWallets” array. The syntax is as follows: Declare a counter variable (i), state the loop’s condition and increment (i) every time one cycle is complete. The code within the block will execute up until the counter is less than the length of the “wallets” array. (i) will start at 0 as stated, the logic will execute, (i) will increment to 1 and the logic will execute again. The cycle repeats until the condition is satisfied. Can you guess how many times it will complete the task? (Hint: look at the array’s length)

Line 36 is the actual task we’ve been ranting about the entire lesson. “familyWallets[i]” refers specifically to the element located at the i’th position in the “familyWallets” array. During the first iteration, we know that (i) is equal to 0. We can thus surmise that we want the first address in the list, as arrays always start at index 0. We then introduce a new call to “.transfer()” on the address we just captured. This is one relatively straightforward global method to transfer value that’s embedded within Solidity. It only takes one argument, the amount we want to send. Being the savvy programmers we are, we came prepared with a data structure that contains the information in question. Can you remember where we kept it? “inheritance[familyWallets[i]]” refers directly to the amount of inheritance we specified in the “inheritance” mapping for again, each element located in the i’th position in the “familyWallets” array. Since i= 0 on the first iteration, we want the inheritance of the first address in the list.

To summarize, this single line of code transfers funds from the contract’s address to a receiver’s address which is pulled from a list with an amount equivalent to that same address’ inheritance allocation.

Lines 40–42 is the function that gets called when gramps passes away. Here, we set the value of the variable we declared right at the beginning, “isDeceased”, to true. Notice the repeated use of “public” and “onlyOwner” keywords. Bearing that in mind, we can finally call “payout()” to send forth the capital.

For the keen eyed readers, you may be thinking “isDeceased” is redundant. Technically you’re correct. We could eliminate “deceased()” all together and change “private” to “public” & “mustBeDeceased” to “onlyOwner” in “payout()”. It would produce the exact same results. This switch simply aims to explore another means of implementation in which we practice utilizing modifiers. It’s common practice to make functions private and have them called by a public function elsewhere for security purposes.

And we’re done!

Actually, not quite…

This smart contract is complete, but how do we actually use it? Now we can harvest the fruits of our labour.

8. Contract Deployment & Interaction

Your screen should look like this: