A Walk around the Block

Mike Haggerty: Blockchain - A Walk Around the Block

“A Walk Around the Block” – Miners and smart contracts and hard forks, OH MY! The explosion of cryptocurrency has brought with it a host of new technology and terminology. The first half of this presentation will focus on giving a rudimentary introduction to blockchain technology in general: how it works and why it matters. In the second half, a specific blockchain software system – EOS.IO – will be used to standup a new blockchain network and deploy a simple smart contract written in C++.

Part 1: Overview of Blockchain Technology

Part 2: Demo

1. Create and Fund Test Account

Generate keypair

Create account

Confirm creation

2. Install EOSIO

https://developers.eos.io/eosio-home/docs/setting-up-your-environment

MacOS

brew tap eosio/eosio
brew install eosio

brew tap eosio/eosio.cdt
brew install eosio.cdt

Ubuntu 16.04

wget https://github.com/eosio/eos/releases/download/v1.6.0/eosio_1.6.0-1-ubuntu-16.04_amd64.deb
sudo apt install ./eosio_1.6.0-1-ubuntu-16.04_amd64.deb

wget https://github.com/EOSIO/eosio.cdt/releases/download/v1.4.1/eosio.cdt-1.4.1.x86_64.deb
sudo apt install ./eosio.cdt-1.4.1.x86_64.deb

3. Check if cleos is working

cleos -u https://jungle2.cryptolions.io get info
  • Compare the data from cleos and bloks: https://jungle.bloks.io/chain
  • Head block is the one just produced; 2 blocks per second.
  • The last irreversible block lags behind because 15/21 block producers must have produced a derived block.

4. Create a local wallet

cleos wallet create --to-console
cleos wallet unlock
cleos wallet import
  • What is a wallet? Does it store crypto assets? No. Just stores keypairs.

5. Test transfer on Jungle

cleos -u https://jungle2.cryptolions.io transfer mphfoofoo123 mphfoofoo321 "27 EOS" 

6. Write HelloEOS

cd ~
mkdir contracts
cd contracts
mkdir hello
cd hello
touch hello.cpp

7. Compile HelloEOS

eosio-cpp -o hello.wasm hello.cpp --abigen

Did you get an Error: `GLIBCXX_3.4.22' not found (Ubuntu 16.04)?

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9
sudo apt-get upgrade libstdc++6

Check output

cat hello.abi
cat hello.wasm

8. Deploy HelloEOS

cleos -u https://jungle2.cryptolions.io set contract mphfoofoo123 ~/dev/walk-block/contracts/hello -p mphfoofoo123@active

Did you get an insufficient ram error?

cleos -u https://jungle2.cryptolions.io system buyram mphfoofoo123 mphfoofoo123 "10 EOS"
  • EOS requires accounts to buy RAM to store their smart contracts

9. Run HelloEOS

cleos -u https://jungle2.cryptolions.io push action mphfoofoo123 hi '["mphfoofoo123"]' -p mphfoofoo123@active
  • The user argument can be any text that conforms to EOS account name specifications. It doesn't have to be a valid account.
  • Try passing a fake account - the contract will still say hello.

10. Validate account

eosio_assert( is_account( user ), "user account does not exist");
  • Add the above line to hello.cpp
  • Recompile and deploy
  • Now only valid accounts will be greeted.

11. Get user's balance

wget https://raw.githubusercontent.com/EOSIO/eosio.contracts/master/eosio.token/include/eosio.token/eosio.token.hpp
wget https://raw.githubusercontent.com/EOSIO/eosio.contracts/master/eosio.token/src/eosio.token.cpp
  • Edit eosio.token.cpp. Change line 6 to
#include "eosio.token.hpp"
  • Copy contents of hello2.cpp to local file.
  • Recompile and deploy

12. Spurn the rich man

  • Add below lines to contract method
  • Recompile and deploy
if(b.amount > 5000000) {
  print(" || You are rich! Go away, ", user);  
} else {
  print(" || Hello, ", user);
}