Smart Contracts
using web3.js
Felix Crisan, NETOPIA (mobilPay, web2sms, btko.in)
@fixone / felix@netopia.ro
TGE?
ICO?
Ethereum?
Cryptocurrencies?
Bitcoin?
What all the ICO’s have in
common?
Smart Contracts
● Wikipedia says: “Smart contracts are computer protocols that facilitate, verify, or
enforce the negotiation or performance of a contract, or that make a contractual clause
unnecessary”
● Usually also have a user interface
● Can be verified, enforced, proven by computers
● In MVC terms a smart contract is an environment where the Model is stored on the
blockchain, the View is accessible to anyone by calling public methods or looking at
public variables and the Controller is the contract code itself
● “Code is (the) Law”
There are 3 eras of
currency: Commodity
based, politically based,
and now, math based.
- Chris Dixon
Ethereum support for Smart Contracts
● Contract = code (i.e. functions) and data (its state) that resides at a specific address on
the Ethereum blockchain
● EVM is the runtime for Smart Contracts on Ethereum
● Accounts (thus contracts as well) have a persistent memory area which is called
storage: key-value store that maps 256-bit words to 256-bit words
● Contracts can neither read nor write to any storage apart from its own
● Contracts also have access to memory - linear and can be addressed at byte level
○ Memory is expanded by a 256bits when reading or writing & expansion must be paid in gas
(quadratic scale)
Not all the contracts
deployed on a blockchain
are be smart.
Some are actually dumb.
Ethereum support for Smart Contracts
● Contracts are defined through a transaction sent to 0x000.....000
● Data of the transaction is the compiled contract
● High level languages (for which compilers exist)
○ Solidity (Javascript like)
○ Serpent (Python like)
○ LLL (Lisp like)
● No (real) IDE for contracts - Mix was discontinued, reborn as web-based ReMix
● A couple of options - Atom with atom-ethereum-interface (supports compilation) or
Visual Studio Code (just syntax highlighting)
Ethereum support for Smart Contracts
● Contracts can call other contracts (they can even call themselves)
● 1024 max call stack depth
● call (new contexts) vs delegatecall (same context, e.g. same msg.sender + msg.value)
● Events
● Contracts can purge themselves from the blockchain (OPCODE selfdestruct)
Good to know
● Everything you use in a smart contract is publicly visible, even local variables and state
variables marked private
● If a contract receives Ether (without a function being called), the fallback function is
executed
○ If no fallback function, the Ether is rejected
● Don’t use tx.origin for authorization (use msg.sender)
● Always have a “circuit-breaker”
On the blockchain,
nobody knows you're a
fridge.
-Richard Brown
web3.js
● Official Ethereum JavaScript API
● Wrapper over JSON RPC functionality (available in go-ethereum - geth, parity,
cpp-ethereum, pyethapp)
○ Some RPC modules require explicit activation in order to be available!
● Other API/clients
○ web3j Java https://github.com/web3j/web3j
○ Nethereum C# .NET https://github.com/Nethereum/Nethereum
○ ethereum-ruby Ruby https://github.com/DigixGlobal/ethereum-ruby
● Available
○ on npm as a node module
○ for bower and component as an embeddable js
○ as a meteor.js package.
Web3.js - continued
● Managing accounts
● Signing transactions
● Getting notifications (about certain events)
● Sending of client requests (both asynchronously and synchronously)
● Interaction with Ethereum clients over JSON-RPC
● Auto-generation of Java smart contract function wrappers from Solidity ABI files
web3.js Installation/Usage
Installation
>npm install web3
Usage
var Web3 = require('web3');
var web3 = new Web3();
And then
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3.js
● After web3 instantiation all geth modules (specifically enabled over RPC) can be
accessed
● For instance
web3.eth.getBalance(web3.eth.coinbase).toNumber()
web3.personal.listAccounts
console.log(“sha3 is”,web3.sha3(“some string”))
Deployment using a
wallet with GUI
Contract Deployment
● Without any external dependencies : from clients like Mist & Ethereum Wallet
Contract Deployment
After Deployment
This is where you get the ABI interface
Free advice of the day:
not your keys, not your
crypto
… although you don’t
really have private keys,
you merely have
permission from Chuck
Norris to use his (as he
brute-forced all 2256
of
them)
Tools
● Infura.io (so you don’t have to run your own node/s)
● Truffle (w/ local testRPC) http://truffleframework.com/ You can test it without any local
install, from the browser (see e.g.
http://joeysprogrammingblog.com/2015/12/16/ethereum-truffle-and-testrpc-with-clou
d9-2/ )
● Embark https://github.com/iurimatias/embark-framework
● https://openzeppelin.org/
● Online compiler https://remix.ethereum.org/
● Block Explorers
○ Mainnet
■ https://etherscan.io
■ https://etherchain.org
○ Testnet (multiple testnets available ! )
■ https://ropsten.io (POW)
■ https://kovan.etherscan.io/ (POA)
■ https://rinkeby.etherscan.io/ (also POA)
More tools
Get more info
● The Bible: https://ethereum.github.io/yellowpaper/paper.pdf
● But, if you’re an atheist: https://github.com/ethereum/wiki/wiki/JavaScript-API
● http://www.ethdocs.org/en/latest/
● http://solidity.readthedocs.io/en/develop/index.html
● https://crypto.stanford.edu/cs251/syllabus.html (not only on Ether, but great
nonetheless)
All the code for today’s demo
https://github.com/fixone/ethdemo
Smart contracts using web3.js

Smart contracts using web3.js

  • 1.
    Smart Contracts using web3.js FelixCrisan, NETOPIA (mobilPay, web2sms, btko.in) @fixone / felix@netopia.ro
  • 2.
  • 3.
  • 4.
  • 5.
  • 7.
  • 8.
    What all theICO’s have in common?
  • 9.
    Smart Contracts ● Wikipediasays: “Smart contracts are computer protocols that facilitate, verify, or enforce the negotiation or performance of a contract, or that make a contractual clause unnecessary” ● Usually also have a user interface ● Can be verified, enforced, proven by computers ● In MVC terms a smart contract is an environment where the Model is stored on the blockchain, the View is accessible to anyone by calling public methods or looking at public variables and the Controller is the contract code itself ● “Code is (the) Law”
  • 10.
    There are 3eras of currency: Commodity based, politically based, and now, math based. - Chris Dixon
  • 11.
    Ethereum support forSmart Contracts ● Contract = code (i.e. functions) and data (its state) that resides at a specific address on the Ethereum blockchain ● EVM is the runtime for Smart Contracts on Ethereum ● Accounts (thus contracts as well) have a persistent memory area which is called storage: key-value store that maps 256-bit words to 256-bit words ● Contracts can neither read nor write to any storage apart from its own ● Contracts also have access to memory - linear and can be addressed at byte level ○ Memory is expanded by a 256bits when reading or writing & expansion must be paid in gas (quadratic scale)
  • 12.
    Not all thecontracts deployed on a blockchain are be smart. Some are actually dumb.
  • 13.
    Ethereum support forSmart Contracts ● Contracts are defined through a transaction sent to 0x000.....000 ● Data of the transaction is the compiled contract ● High level languages (for which compilers exist) ○ Solidity (Javascript like) ○ Serpent (Python like) ○ LLL (Lisp like) ● No (real) IDE for contracts - Mix was discontinued, reborn as web-based ReMix ● A couple of options - Atom with atom-ethereum-interface (supports compilation) or Visual Studio Code (just syntax highlighting)
  • 14.
    Ethereum support forSmart Contracts ● Contracts can call other contracts (they can even call themselves) ● 1024 max call stack depth ● call (new contexts) vs delegatecall (same context, e.g. same msg.sender + msg.value) ● Events ● Contracts can purge themselves from the blockchain (OPCODE selfdestruct)
  • 15.
    Good to know ●Everything you use in a smart contract is publicly visible, even local variables and state variables marked private ● If a contract receives Ether (without a function being called), the fallback function is executed ○ If no fallback function, the Ether is rejected ● Don’t use tx.origin for authorization (use msg.sender) ● Always have a “circuit-breaker”
  • 16.
    On the blockchain, nobodyknows you're a fridge. -Richard Brown
  • 17.
    web3.js ● Official EthereumJavaScript API ● Wrapper over JSON RPC functionality (available in go-ethereum - geth, parity, cpp-ethereum, pyethapp) ○ Some RPC modules require explicit activation in order to be available! ● Other API/clients ○ web3j Java https://github.com/web3j/web3j ○ Nethereum C# .NET https://github.com/Nethereum/Nethereum ○ ethereum-ruby Ruby https://github.com/DigixGlobal/ethereum-ruby ● Available ○ on npm as a node module ○ for bower and component as an embeddable js ○ as a meteor.js package.
  • 18.
    Web3.js - continued ●Managing accounts ● Signing transactions ● Getting notifications (about certain events) ● Sending of client requests (both asynchronously and synchronously) ● Interaction with Ethereum clients over JSON-RPC ● Auto-generation of Java smart contract function wrappers from Solidity ABI files
  • 19.
    web3.js Installation/Usage Installation >npm installweb3 Usage var Web3 = require('web3'); var web3 = new Web3(); And then web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  • 20.
    web3.js ● After web3instantiation all geth modules (specifically enabled over RPC) can be accessed ● For instance web3.eth.getBalance(web3.eth.coinbase).toNumber() web3.personal.listAccounts console.log(“sha3 is”,web3.sha3(“some string”))
  • 21.
  • 22.
    Contract Deployment ● Withoutany external dependencies : from clients like Mist & Ethereum Wallet
  • 23.
  • 24.
    After Deployment This iswhere you get the ABI interface
  • 25.
    Free advice ofthe day: not your keys, not your crypto
  • 26.
    … although youdon’t really have private keys, you merely have permission from Chuck Norris to use his (as he brute-forced all 2256 of them)
  • 27.
    Tools ● Infura.io (soyou don’t have to run your own node/s) ● Truffle (w/ local testRPC) http://truffleframework.com/ You can test it without any local install, from the browser (see e.g. http://joeysprogrammingblog.com/2015/12/16/ethereum-truffle-and-testrpc-with-clou d9-2/ ) ● Embark https://github.com/iurimatias/embark-framework ● https://openzeppelin.org/
  • 28.
    ● Online compilerhttps://remix.ethereum.org/ ● Block Explorers ○ Mainnet ■ https://etherscan.io ■ https://etherchain.org ○ Testnet (multiple testnets available ! ) ■ https://ropsten.io (POW) ■ https://kovan.etherscan.io/ (POA) ■ https://rinkeby.etherscan.io/ (also POA) More tools
  • 29.
    Get more info ●The Bible: https://ethereum.github.io/yellowpaper/paper.pdf ● But, if you’re an atheist: https://github.com/ethereum/wiki/wiki/JavaScript-API ● http://www.ethdocs.org/en/latest/ ● http://solidity.readthedocs.io/en/develop/index.html ● https://crypto.stanford.edu/cs251/syllabus.html (not only on Ether, but great nonetheless)
  • 30.
    All the codefor today’s demo https://github.com/fixone/ethdemo