Intro to Smart Contracts in
Ethereum
Smart Contracts
● From Wikipedia: “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”
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)
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)
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 (more) IDE for contracts - Mix was discontinued
● A couple of options - Atom with atom-ethereum-interface (supports compilation)
or Visual Studio Code (just syntax highlighting)
Caveats
● 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”
Contract Deployment
● Without any external dependencies : from clients like Mist & Ethereum Wallet
Contract Deployment
After Deployment
This is where you get the ABI interface
Tools
● Infura.io (so you don’t have to run your own node/s)
● Truffle (w/ local testRPC) https://github.com/ConsenSys/truffle. 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
-cloud9-2/ )
● Embark https://github.com/iurimatias/embark-framework
● (in the future) https://openzeppelin.org/
More tools
● Online compiler https://ethereum.github.io/browser-solidity/
● Another online compiler https://etherchain.org/solc
● Online tools https://testnet.etherscan.io https://etherscan.io (block explorer, tx
submit)
● https://etherchain.org/
● http://ether.fund/contracts/ (contract repository w/ source code)

Smart contracts in Solidity

  • 1.
    Intro to SmartContracts in Ethereum
  • 2.
    Smart Contracts ● FromWikipedia: “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”
  • 3.
    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)
  • 4.
    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)
  • 5.
    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 (more) IDE for contracts - Mix was discontinued ● A couple of options - Atom with atom-ethereum-interface (supports compilation) or Visual Studio Code (just syntax highlighting)
  • 6.
    Caveats ● Everything youuse 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”
  • 7.
    Contract Deployment ● Withoutany external dependencies : from clients like Mist & Ethereum Wallet
  • 8.
  • 9.
    After Deployment This iswhere you get the ABI interface
  • 10.
    Tools ● Infura.io (soyou don’t have to run your own node/s) ● Truffle (w/ local testRPC) https://github.com/ConsenSys/truffle. 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 -cloud9-2/ ) ● Embark https://github.com/iurimatias/embark-framework ● (in the future) https://openzeppelin.org/
  • 11.
    More tools ● Onlinecompiler https://ethereum.github.io/browser-solidity/ ● Another online compiler https://etherchain.org/solc ● Online tools https://testnet.etherscan.io https://etherscan.io (block explorer, tx submit) ● https://etherchain.org/ ● http://ether.fund/contracts/ (contract repository w/ source code)