If (this) then: pay me! | First looks at Ethereum smart contracts

Smart contracts are considered to be “trustless agreements” to exchange goods or services without requiring a centralized authority such as a banking institution or a government.

Using the blockchain’s built-in method of validating transactions, payment is only made when conditions to the contract have been fulfilled

Terms of the agreement are instead encoded in a software platform that two (or more!) parties can agree to by digitally signing a contract with the public address of their cryptocurrency wallet.

Funds are held in escrow by the platform itself until conditional statements are met, meaning that until a task is completed or the contract expires the funds are unavailable to either party. Once conditions have been met, nodes on the blockchain validate the transaction and the money is available to the designee.

Smart contracts help you exchange money, property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman or central authority. Using a public blockchain (such as the Bitcoin or Ethereum network) provides a record of transaction history that anyone can audit and is backed up redundantly across the world.

Contracts can be as simple as printing “Hello World” to a terminal by interacting with a Decentralized App (DAPP) or infinitely complex by chaining conditions to fire other smart contracts.

but why tho?

Let’s consider a few use cases.

Fundraising

Invite anyone anywhere to donate money, incentivize donors to donate $X.xx by a certain date or all money will be returned to donors. Charitably match all funds donated on Giving Tuesday, etc.

Real Estate

Rent out a tiny-house with a digital key pad: so long as rent is paid by the first of the month the door will open for your digital key.

Transfer Money

Send your grandson $5.00 on his birthday every year for the next 100 years. The contract will continue being fulfilled as long as it is programmed.

Multi-signature accounts

A smart contract can function as a ‘multi-signature’ account so that funds can only be spent when a required percentage of people agree. Conditional logic can allow for voting! Imagine a worker-owned cooperative that required 2/3rds of members to vote to approve any spending from a certain account or perhaps a shared business account that prevented one partner from fraudulently siphoning funds for personal use — unlike a bank account all transactions on the blockchain are public.


Why Ethereum

If we can use any blockchain-based currency to fulfill a smart contract, why bother with Ethereum? Because Ethereum was designed as a smart contract platform for decentralized applications (DAPPs).

At the heart of it is the Ethereum Virtual Machine (“EVM”), which can execute code of arbitrary algorithmic complexity. Developers can create applications that run on the EVM using high-level programming languages syntactically similar to JavaScript and Python [Solidity and Serpent, respectively.] The EVM is running simultaneously on thousands of nodes across the network to provide redundant copies of any change in state.

Developers can create DAPPs of their choosing for users to interact with. A DAPP serves as the intermediary platform for smart contracts, allowing users to enter smart contracts with each other.

Consider this example of a voting DAPP from Remix, a popular Ethereum developer IDE, written in Solidity.

/// Code snipped from https://remix.ethereum.org
pragma solidity >=0.4.22 <0.6.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
/// Create a new ballot with $(_numProposals) different proposals.
constructor(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public view returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}

For deeper reference, consider the following sources.

The Hitchhiker’s Guide to Smart Contracts in Ethereum by Manuel Araoz

A step by step guide to developing a smart contract in Solidity using the Truffle Dev Framework. (Built with npm obvi!)

https://blog.zeppelin.solutions/the-hitchhikers-guide-to-smart-contracts-in-ethereum-848f08001f05

Building a smart contract using the command line

Another walkthrough to set up an Ethereum development environment and build blockchain-based bot to say “Hello World!”

https://www.ethereum.org/greeter

The Docs!

http://ethdocs.org/en/latest/index.html