An Introduction to Ethereum and Smart Contracts


This article intends to serve as an introduction to the blockchain and Ethereum by describing a problem that these technologies could be helpful in solving. It’s secondary aim is to build the scaffolding of a mental model that will guide the reader on a path to futher exploration of these topics.



Alice, Bob, Carl and Dana work in an office with an automatic espresso machine, capable of making lattes and cappuccinos. They enjoy the machine, but they are inconsistent coffee drinkers. In any given week, they consume between 1/4 and 2 gallons of milk, making it difficult to keep a steady supply of fresh milk in the refrigerator without creating waste. The optimal solution is probably to buy smaller quantities of milk more frequently, but would be unfair to the person responsible for purchasing milk.



Alice and her colleagues decide to architect a system where each user of the espresso machine is incentivized to maintain a supply of milk. Here’s how it works:

  1. Alice notices that the milk is getting low. She uses an app on her phone to record the milk outage, which triggers a notification to Bob, Carl and Dana that a verification is necessary.
  2. When Bob looks in the refrigerator and verifies that the milk is low, he signs off on Alice’s claim. Alice and Bob are both rewarded 1 SNToken for their effort.
  3. Alice, Bob, Carl and Dana are now able to volunteer to buy milk. Carl decides to buy some milk the next morning. When he arrives at the office, he scans the barcode of the milk he purchased and puts it in the refrigerator.
  4. Alice, Bob and Dana receive a notification requesting that they verify Carl’s purchase. Dana goes to the refrigerator and scans the barcode of the milk. If the barcode she scans matches the one Carl scanned earlier that morning, then Carl is given 2 SNTokens for his work, plus 5 SNTokens as reimbursement for the cost of the milk. Dana earns 1 SNToken for verifying.

If it is predetermined that 1 SNToken = $0.50, then the cost of purchasing milk in this example is $5.00 (5 SNTokens for work + 5 SNTokens for milk).

Alice and her colleagues have several options for exchanging their tokens for something valuable: redeem them for cash, spend them on the company store website, or exchange them for personal time off.

Given this relatively simple system, it’s reasonable to consider using a standard tech stack. To draw a useful comparison between a traditional solution and one built using a blockchain, we first need a basic understanding of what a blockchain is, and what benefits it offers.



A block can be thought of as a data structure comprised of some data, a hash of its data and some metadata. The blocks are chained together because each block must have a block number, and the hash of the previous block.

Block Chain Example Image

Changing the data in any of the blocks in the chain would require changing its hash value, which would then require changing every block proceeding it in the chain. This gives a blockchain the property of immutability because the computing power necessary to accomplish this makes it economically unviable.



Ethereum is an implementation of a blockchain. Bitcoin is also built on a blockchain. Bitcoin’s blocks store transactional data. For example, Alice sent Bob 1 Bitcoin. Ethereum is unique because it can store objects with internal state. It is a programmable blockchain. Consider this Javascript class:

This could be stored on the Ethereum blockchain as:

Another property of the blockchain is decentralization. Each user that wants to interact with the Ethereum blockchain runs software on their machine that contains a copy of the existing blockchain. When a new block is added to the chain, each node gets a copy of the latest version of the blockchain. This means there is no single source of failure and no single entity storing all the data.

What benefits do immutability and decentralization  provide? They are mechanisms for facilitating trust between parties. Immutability enables trust because it allows each party to verify claims made by other parties. Dana cannot verify Carl’s claim that he purchased milk, and then later take back that claim. In the real world, if Alice sends Bob a check, and Bob cashes the check, Bob can no longer claim that he never received money from Alice. If that were possible, there would be no trust in the financial system and a check would just be a piece of paper.

Decentralization is another mechanism for maintaining trust. Let’s imagine that Alice decides to act maliciously and manages to alter a block in the blockchain. When she tries to sync her blockchain with Bob, Carl and Dana’s copy they will notice that her copy does not match theirs and they will vote on which copy to accept. Compare this to a traditional database where all Alice would need to do is compromise a single machine hosting the database.


Smart Contracts

Smart contracts are how we interact with the blockchain on Ethereum. The language used to write smart contracts is Solidity . We’ll begin by defining some variables and stubbing out the functions we’ll need.

bool public outOfMilk = false;

This line of code initializes a boolean variable named outOfMilk with a value of false. The public between the variable type and variable name means that anyone that knows our contract address can read the value of this variable.

address public milkOutageNotifier;

milkOutageNotifier is a variable of type address . Every user on Ethereum has an account address. In our example, this variable is used to store Alice’s account address.

mapping (address => uint) public snTokenBalances

snTokenBalances is a dictionary like object where the key is an address and the value is an int. This will allow us to store the amount of SNTokens each user has earned.

event Notification (string message)

Here we define an event that we will emit with a payload containing a string. The consumers of our smart contract will be able to listen for Notification events and react accordingly.

Let’s replace our stubbed out methods with some logic.

Next Steps

As this article was intended to serve only as an introduction, there is much left to explore. Here are some learning paths you could take:

Get up and running:

  • Look into testrpc and truffle
  • Read up on web3, the Javascript API for interacting with smart contracts
  • Install the metamask chrome extension


  • Consider the pros and cons of building your app on the public Ethereum network or bootstrapping a private blockchain network
  • Download and install Geth


  • Think about how you might improve this example…would it be possible to use a raspberry pi to monitor the amount of milk?
  • How could you prevent one user from creating two accounts and becoming both the notifier and verifier?
  • What other behavior in the real world could you incentivize?

I hope you are as excited about the possibilities of Ethereum as I am. Thank you for reading.