1 of 67

2 of 67

Agenda

  • Recap of the previous session on DApps
  • History of Smart Contracts
  • Solidity Language Basics
  • Smart Contract Development Philosophy
  • Hands-On: Write your own ERC20 Token!
  • Summary
  • Next Session Preview

3 of 67

About Yos

4 of 67

🙋🏻‍♂️🙋🏻‍♀️

5 of 67

Session #00

Recap

6 of 67

Decentralized Applications

ÐApps are applications built on top of a decentralized blockchain platform, implemented as smart contracts.

7 of 67

How are Ðecentralized Apps different?

  • You own your data, stored permanently on a public blockchain.

Competition through permissionless innovation with open source smart contracts and forking.

  • Shared ownership encourage cooperation through cryptographic tokens.

8 of 67

Workflow of launching a DApp

  • Write smart contracts as the backend / ‘base layer’
  • Build a GUI frontend app to interact with the smart contracts
  • Deploy smart contracts onto the blockchain (testnet / mainnet)
  • Deploy frontend to a cloud hosting provider

9 of 67

The Rise of Decentralized

Finance

  • Decentralized lending & borrowing
  • Decentralized exchanges
  • Margin Trading
  • Derivatives

10 of 67

11 of 67

History of

Smart Contracts 📆

12 of 67

What are Smart Contracts? 🙋🏻‍♂️🙋🏻‍♀️

13 of 67

“Code as Law”

14 of 67

Smart Contracts

A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract.

Smart contracts allow the performance of credible transactions without third parties. These transactions are trackable and irreversible.

15 of 67

16 of 67

Before Ethereum: Colored Coins

While originally designed to be a currency, Bitcoin's scripting language allows to store small amounts of metadata on the blockchain, which can be used to represent asset manipulation instructions.

The term "Colored Coins" loosely describes a class of methods for representing and managing real world assets on top of the Bitcoin Blockchain.

17 of 67

Encoding transfers within Bitcoin transactions

18 of 67

2013

2018

19 of 67

20 of 67

“While we have been one of the first in the area of blockchain tokens, long before Ethereum was even released, the ecosystem has since shifted towards ERC-20, which is more flexible and more powerful than bitcoin-based systems,”

https://www.coindesk.com/blockchain-startup-coinprism-to-shut-down-in-2-days

21 of 67

Ethereum & Solidity

22 of 67

Ethereum is a State Machine

A state machine reads a series of inputs, and based on those inputs, will change to a new state.

*

23 of 67

Ethereum is a State Machine

The Ethereum blockchain is a transaction-based state machine.

24 of 67

Ethereum is a Smart Contract platform

Ethereum is also a distributed virtual computing network that stores and enforces smart contracts.

When it launched, it offered more flexible and sophisticated scripting capabilities compared to Bitcoin’s Colored Coins.

25 of 67

What’s a ‘Contract’ on Ethereum?

Ethereum smart contracts are code stored on the Ethereum blockchain. When the contract is triggered by a transaction, it runs on the Ethereum Virtual Machine (EVM).

The EVM runs code and produces cryptographically verified changes to the state of the Ethereum blockchain as its result.

26 of 67

Ethereum Refresher

  • Address: Your public username on the Ethereum network
  • Private Key: Your private password only you know
  • Ether: The native currency of the Ethereum network
  • Transactions: Signed actions that change state on the network
  • Gas: Cost associated with a Transaction, compensates Miners
  • Externally Owned Account: Combination of Address + Private Key
  • Contract Account: Smart contracts have an Address, but no Private Key

27 of 67

28 of 67

29 of 67

EVM code is Turing complete

30 of 67

31 of 67

Computationally universal:

You can build whatever you can imagine.

32 of 67

How are Smart Contracts written?

You can write smart contracts in a number of languages, such as Solidity. These languages compile to EVM code.

A Turing complete language lets you write code that can evaluate any computable function. This puts Solidity in the same class of languages as Python and Java.

33 of 67

The Solidity

Programming Language

34 of 67

35 of 67

36 of 67

Solidity: Contract

Similar to any object in an object-oriented (OO) language, the contract is a container that includes data and methods.

contract MyToken {

// data and methods

}

37 of 67

Solidity: Types

Solidity is statically typed.

Boolean (bool): Boolean value, true or false.

Integer (int, uint): Signed (int) and unsigned (uint) integers.

Address: A 20-byte Ethereum address.

Mapping: Hash lookup tables for key => value pairs: mapping(KEY_TYPE => VALUE_TYPE) NAME.

And others...

38 of 67

Solidity: Variables

Contracts can include data as variables.

contract MyToken {

string public name = “GeekCoin”;

bool private totalSupply = 1000;

}

39 of 67

Solidity: Visibility Modifiers

public: Public is the default; such functions can be called by other contracts or EOA transactions, or from within the contract.

external: External functions are like public functions, except they cannot be called from within the contract unless explicitly prefixed with the keyword this.

internal: Internal functions are only accessible from within the contract—they cannot be called by another contract or EOA transaction. They can be called by derived contracts (those that inherit this one).

private: Private functions are like internal functions but cannot be called by derived contracts.

40 of 67

Remix Demo

https://remix.ethereum.org

41 of 67

Compiling Solidity

Compiling Solidity generates two artifacts:

  • Bytecode (Low-level EVM instructions) executed by Ethereum nodes.
  • ABI (Application Binary Interface) which describes a contract and its functions.

42 of 67

43 of 67

Solidity: Constructors

When a contract is created, it also runs the constructor function if one exists, to initialize the state of the contract.

The constructor is run in the same transaction as the contract creation. The constructor function is optional.

contract MyToken {

constructor(address _minter) {

// This is the constructor

}

}

44 of 67

Solidity: Functions

Within a contract, we define functions that can be called by an EOA transaction or another contract.

function hello() returns (bool) {

return true;

}

45 of 67

Solidity: Function Modifiers

view: A function marked as a view are getter functions that promises not to modify any state.

pure: A pure function is one that neither reads nor writes any variables in storage.

payable: A payable function is one that can accept incoming Ether payments. Functions not declared as payable rejects incoming payments.

46 of 67

Solidity: Assertions

Assertions are used to validate inputs such as function arguments and expected values.

require(withdraw_amount <= 0.1 ether);

require(msg.sender == owner, “Only the contract owner can call this function.”);

47 of 67

Solidity: Error Handling

A successful transaction means that the program executed without an error and reached the end of execution.

If execution fails due to an error, all of its effects (changes in state) are rolled back” as if the transaction never ran.

A failed transaction is still recorded as having been attempted, and the ether spent on gas for the execution is still deducted, but it otherwise has no other effects on contract or account state.

48 of 67

Solidity: Events

contract ERC20 {

event Transfer(address from, address to, uint amount);

function transfer(address to, uint amount) {

...

emit Transfer(msg.sender, to, amount);

}

}

49 of 67

Solidity: Other Language Constructs

We’ll learn the rest in the next session:

  • Interfaces
  • Inheritance
  • External Contract Calls
  • Libraries
  • EVM Assembly

50 of 67

Smart Contract

Development Philosophy

51 of 67

Imagine somebody reported a bug...

52 of 67

53 of 67

54 of 67

Smart Contracts

Traditional Software

55 of 67

56 of 67

Keeps Contracts Simple 💡

  • Re-use audited, well-tested code.
  • Simplify contract logic whenever possible.
  • Clarity > Performance
  • Modularize into multiple, smaller contracts.

57 of 67

Prepare for Failure 👩‍🚒🔥

  • Get comprehensive security audits from external auditors.
  • Organize bug bounty programs and public testnets. Roll out carefully.
  • Contracts should be pausable should things go awry.
  • Be able to upgrade contracts for bugfixes and improvements.

58 of 67

Building an ERC20 Token

59 of 67

What is ERC20?

The ERC20 token standard defines a common interface for contracts implementing a token, such that any compatible token can be accessed and used in the same way.

ERC20 is a standard for fungible tokens, meaning that different units of an ERC20 token are interchangeable and have no unique properties.

60 of 67

61 of 67

62 of 67

63 of 67

ERC20 Demo / Hands-On

Final code: https://bit.ly/2GFxdwP

64 of 67

Summary

  • Recap of the previous session on DApps
  • History of Smart Contracts
  • Solidity Language Basics
  • Smart Contract Development Philosophy
  • Hands-On: Write your own ERC20 Token!
  • Summary
  • Next Session Preview

65 of 67

Next Session

  • More Solidity: Interfaces, Inheritance, Events, External Contract Calls, and more
  • Smart Contract Design Patterns
  • Smart Contract Security Best Practices
  • Smart Contract Upgradeability
  • Smart Contract Development Tools
  • Testing your Smart Contracts

66 of 67

67 of 67