Introduction to

Smart Contracts

with Ethereum

A little about me.

A little about you.

Blockchain Primer

What is a blockchain?

https://www.youtube.com/watch?v=bBC-nXj3Ng4

Components of a blockchain

Database (Shared Ledger)

Node Application

Consensus Algorithm

Type of Blockchains

Public (Permissionless)

Private (Central)

Consortium (Central Group)

Ethereum Primer

What is Ethereum?

Blockchain based distributed computing platform.

Two types of accounts...

Personal Accounts

Contract Accounts

Lets look at the blockchain:

etherscan.io

Types of applications (Dapps)

Tokens (ICOs)... ERC20

Asset titles... ERC721

Digital Identity

stateofthedapps.com

Enter

Smart Contracts

Applications are built, distributed and ran as a contract on the blockchain

We write these applications using the Solidity programming language

Hello World Example

                
// Our version pragma
pragma solidity ^0.4.6;


// Define our contract.
contract HelloWorld {
  // Single simple method.
  function displayMessage() constant returns (string m) {
    return "Hello from a smart contract";
  }
}
          

How do we run this?

Compile, using the solc compiler

Deploy, by sending a transaction to the network

Execute, by calling the contract with from a client program

Lets tool up!

Solc

Web3.js

Ganache (TestRPC)

Lets make this more interesting...

Lets build a simple lotto

We will call our lotto

Yolanda

Yolanda.sol

              
            pragma solidity ^0.4.6;

contract Yolanda {
  // All our contracts code lives here...
}

Lets add some variables to keep track of the lotto.

              
mapping(address => uint) usersBet;
mapping(uint => address) users;
uint nbUsers = 0;
uint totalBets = 0;
             
address owner;

Next we will define our contructor

              
function Yolanda() public {
  // Register the owner of the contract to who created it.
  owner = msg.sender;
}
              
            

We are cooking

Lets define some functionality

We will define 3 methods:

bet() - Which will allow us to send ether to it

getJackpot() - Which will return the total value of jackpot

endLottery() - Which will only be callable from the owner and will select a winner.

bet()

  
function bet() public payable {
  if (msg.value > 0) {
    if (usersBet[msg.sender] == 0) {
      users[nbUsers] = msg.sender;
      nbUsers += 1;
    }
    usersBet[msg.sender] += msg.value;
    totalBets += msg.value;
  }
}

getJackpot()

  
function getJackpot() public view returns (uint t) {
  return this.balance;
}

endLottery()

  
function endLottery() public {
  if (msg.sender == owner) {
    uint sum = 0;
    uint winningNumber = uint(block.blockhash(block.number-1)) % totalBets + 1;

    for (uint i = 0; i < nbUsers; i++) {
      sum += usersBet[users[i]];
      if (sum >= winningNumber) {
        selfdestruct(users[i]);
        return;
      }
    }
  }
}

Ok... lets deploy this bad boy.

How do we interact with this?

So much more to learn!

Some interesting contract addresses on the MainNet:

EtherDelta Exchange

Cryptokitties Core

EtherDelta Exchange

Thank you!

Questions?

Twitter: @direct

https://www.linkedin.com/in/nickgs/