9. 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”
10. There are 3 eras of
currency: Commodity
based, politically based,
and now, math based.
- Chris Dixon
11. 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)
12. Not all the contracts
deployed on a blockchain
are be smart.
Some are actually dumb.
13. 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)
14. 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)
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”
17. 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.
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
20. 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”))
26. … 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)
27. 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/
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 code for today’s demo
https://github.com/fixone/ethdemo