Introduction to Smart Contracts

Smart contracts are what the blockchain hype is really about.

NOTE: This article/talk was aimed at programmers, but it is probably also useful for anyone generally interested in blockchain stuff. It contain some references to Swedish words, content, or law.

NOTE: You probably want to watch this great video in addition to reading this.

Disclaimers

Disclaimer 1: I’m not an expert. I’ve been in the blockchain space since 2012 but I have no self-written smart contracts running in production (I’ll explain why later). Despite having none running in production, I know how to write them and how they work, so I’m not a complete fraud.

Disclaimer 2: I have a financial interest in cryptocurrency assets like Bitcoin or Ethereum. None of this is investment advice. Always do your due diligence before investing and never invest more than you can afford to lose.

Intro

TODO: Give very short intro and overview of how the talk is structured (to make it easier to follow). A ToC would be nice.

Questions to ask the audience:

  • How many know about Bitcoin?
  • How many own or have used Bitcoin?

  • How many know about Ethereum?
  • How many have used Ethereum?

When we talk about “smart contracts” here, we will be referring to the smart contracts specifically on Ethereum. But most of it also holds true for the general term.

What makes smart contracts “smart”?

  • Decentralized
  • “Unstoppable”
  • Can be used to eliminate third parties

What makes smart contracts not so smart?

  • Decentralized
    • Harder to keep secrets (but not impossible)
  • “Unstoppable”
    • If a contract does something you don’t want (eg. something illegal), tough luck
  • Can be used to eliminate third parties
    • Third parties are sometimes smarter than smart contracts

Terminology

TODO: Perhaps move much of this section until after showing off a contract

Use and meaning of the terminology varies between individuals and communities. This is my attempt at .

  • Blockchain
    • Simply put: A chain of blocks or a ‘decentralized ledger’
      • Each block contains a set of transactions
      • A block with new transactions is added by miners/stakers every T seconds
    • The protocol sets the rules
    • Two species of blockchains (this might be unnecessary and confusing)
      • Specific: Supports a specific feature set, embedded in the protocol
        • Example: Bitcoin/Litecoin/Dogecoin (money)
        • Example: Monero/Zcash (untraceable money using RingCT and zk-SNARKs, respectively)
      • General: Using a type of VM to run “Turing complete” code
        • Example: Ethereum, the major topic we will spend time on
  • Consensus rules: how the network is secured (might be unnecessary)
    • Proof of Work (PoW)
      • Interesting factoid: The originator of the term Proof of Work, Markus Jakobsson, got his masters degree at Lund University (Datateknik).
    • Others exist and are being researched/developed
      • PoS - Proof of Stake (to replace PoW on Ethereum)
      • PoR - Proof of Replication (to be used by Filecoin, not sure if technically a consensus rule)
      • PoST - Proof of Spacetime (to be used by Filecoin, not sure if technically a consensus rule)
  • Tokens (or “cryptotokens”)
    • A type of transferable asset
    • Examples:
      • Money (Bitcoin, Ethereum)
      • Securities (The DAO)
      • Something needed in order to use a service (utility tokens)
  • Smart contracts
    • The type of code that runs on general blockchains like Ethereum

Writing smart contracts

Writing smart contracts is pretty easy, but writing ones that are correct and bugfree is not only hard but also costly.

Start with showing what they enable

Examples:

  • Tokens
  • Voting
  • Crowdsales
  • Multisig
    • Useful when you want to have collective ownership of a wallet. Examples are 2-of-3 multisigs where two keys are needed to move tokens from a wallet.

More advanced examples:

  • Prediction markets
  • Futarchy

Work through an example contract

Sending out personal tokens using such a contract would be nice.

  • Show how to use
    • Use MyEtherWallet and etherscan.io
  • Show how to deploy
    • Use testnet (need testnet ETH)
    • You could deploy private testnet (How-to)
  • Explain code (how?)

Best practices

Most popular applications

Ethereum has sometimes been called “a solution in search of a problem”. Smart contracts can be used to do a lot of things, but it’s not always clear if a smart contract solution would improve upon the status quo (although sometimes it is).

DAOs

A Decentralized Autonomous Organization is a form of organization that exist outside of any particular legal framework or jurisdiction. They can be run by investors/stakeholders (holders of some specific token) or possibly even AI.

ICOs

An Initial Coin Offerings is a form of decentralized crowdfunding for decentralized applications (usually applications running on blockchains).

Some are complete money-grabs, others end up being highly overfunded startups with a overhyped product, or worse: no product. A few might actually turn out successful (I have my guesses), but we’re still holding our breaths to see what unfolds.

Words of caution: Be very careful about ICOs and don’t buy into the hype. Do your own due diligence, crunch the numbers with your expectations, and be the best sceptic you can be. At this point, Bitcoin and Ethereum are relatively low-risk assets compared to many ICOs, so those might be better investments if you don’t have the time to be as careful as you would need to be with ICOs.

Many types of ICOs might now be illegal in the US, the SEC (Securities and Exchange Commission) has sent out warning letters and new ICOs in the US now tend to be held for accredited investors only.

Problems

  • Regulatory
  • Scaling (on-chain vs off-chain)
    • Blocksize
    • The fee market
    • Full blocks => High transaction fees
    • Want hints for solutions? I’m afraid you’ll have to look elsewhere as I’m staying out of it. Sorry.
  • Proof of Work
    • Protocol rule by miners
    • Energy inefficient (bad for the environment)
    • Some argue its not a problem (I’m not so sure)

The Anarchy of Blockchains

Blockchains enables extreme privacy (especially untraceable chains like Monero and Zcash). The impact this has on society is significant.

Examples of undesirable effects:

  • Black markets such as Silk Road
    • Many have been shut down by law enforcement, others have voluntarily shut down (some by exit scam).
    • OpenBazaar is an example of a next-generation marketplace that is fully decentralized and therefore a potential future challenge for law enforcement.
  • Ransomware

Taxation on cryptocurrency

NOTE: I am not a lawyer/tax person.

In Sweden all profits earned by trading cryptocurrency are taxed as capital gains. This does not apply when directly trading cryptocurrency pairs.

The Swedish Tax Authority now believes that capital gains on cryptocurrency have been underreported. See this reporting by TV4: “Skatteverket misstänker omfattande skattefusk med Bitcoins”.

In Sweden, the capital gains tax is normally 30% (“genomsnittsmetoden”). But when gains go above 400% one can use a different method when calculating taxes that instead of taxing 30% of gains taxes 20% of the whole sale (“schablonmetoden”).

If you want to buy Bitcoin only as an investment and expect gains over ~2% year (the tax on assets held in an ISK), then the by far best option is to buy Bitcoin certificate and keep them in an ISK.

Top Blockchain Projects

Note: My own biases lie herein, and my biases are a bit conservative.

  • Bitcoin
  • Ethereum
  • Filecoin

What I’m the most excited about

  • Decentralized systems
  • Programmable money
  • Fewer third parties
  • Prediction markets
    • The Prediction Market Graveyard
    • No money prediction markets
    • The hypothesis goes that prediction markets will effectively aggregate available information to a probability that an event will occur (
      • Better than current information sources since the market-aspect would make it self-calibrating
    • Problem: Assassination markets
      • A partial (naive) solution would be to make it illegal to participate in such markets, but criminals could hide well.
    • Decentralized PMs in development: Augur, Gnosis, STOX

Learn more

Other fantastic things in cryptospace

  • IPFS, IPNS and Filecoin
  • Syncthing (not really crypto, but people who don’t know about it probably should)