Agenda
About Yos
🙋🏻♂️🙋🏻♀️
Session #00
Recap
Decentralized Applications
ÐApps are applications built on top of a decentralized blockchain platform, implemented as smart contracts.
How are Ðecentralized Apps different?
Competition through permissionless innovation with open source smart contracts and forking.
Workflow of launching a DApp
The Rise of Decentralized
Finance
History of
Smart Contracts 📆
What are Smart Contracts? 🙋🏻♂️🙋🏻♀️
“Code as Law”
Smart Contracts
A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract.
Smart contracts allow the performance of credible transactions without third parties. These transactions are trackable and irreversible.
Before Ethereum: Colored Coins
While originally designed to be a currency, Bitcoin's scripting language allows to store small amounts of metadata on the blockchain, which can be used to represent asset manipulation instructions.
The term "Colored Coins" loosely describes a class of methods for representing and managing real world assets on top of the Bitcoin Blockchain.
Encoding transfers within Bitcoin transactions
2013
2018
“While we have been one of the first in the area of blockchain tokens, long before Ethereum was even released, the ecosystem has since shifted towards ERC-20, which is more flexible and more powerful than bitcoin-based systems,”
https://www.coindesk.com/blockchain-startup-coinprism-to-shut-down-in-2-days
Ethereum & Solidity
Ethereum is a State Machine
A state machine reads a series of inputs, and based on those inputs, will change to a new state.
*
Ethereum is a State Machine
The Ethereum blockchain is a transaction-based state machine.
Ethereum is a Smart Contract platform
Ethereum is also a distributed virtual computing network that stores and enforces smart contracts.
When it launched, it offered more flexible and sophisticated scripting capabilities compared to Bitcoin’s Colored Coins.
What’s a ‘Contract’ on Ethereum?
Ethereum smart contracts are code stored on the Ethereum blockchain. When the contract is triggered by a transaction, it runs on the Ethereum Virtual Machine (EVM).
The EVM runs code and produces cryptographically verified changes to the state of the Ethereum blockchain as its result.
Ethereum Refresher
EVM code is Turing complete
Computationally universal:
You can build whatever you can imagine.
How are Smart Contracts written?
You can write smart contracts in a number of languages, such as Solidity. These languages compile to EVM code.
A Turing complete language lets you write code that can evaluate any computable function. This puts Solidity in the same class of languages as Python and Java.
The Solidity
Programming Language
Solidity: Contract
Similar to any object in an object-oriented (OO) language, the contract is a container that includes data and methods.
contract MyToken {
// data and methods
}
Solidity: Types
Solidity is statically typed.
Boolean (bool): Boolean value, true or false.
Integer (int, uint): Signed (int) and unsigned (uint) integers.
Address: A 20-byte Ethereum address.
Mapping: Hash lookup tables for key => value pairs: mapping(KEY_TYPE => VALUE_TYPE) NAME.
And others...
Solidity: Variables
Contracts can include data as variables.
contract MyToken {
string public name = “GeekCoin”;
bool private totalSupply = 1000;
}
Solidity: Visibility Modifiers
public: Public is the default; such functions can be called by other contracts or EOA transactions, or from within the contract.
external: External functions are like public functions, except they cannot be called from within the contract unless explicitly prefixed with the keyword this.
internal: Internal functions are only accessible from within the contract—they cannot be called by another contract or EOA transaction. They can be called by derived contracts (those that inherit this one).
private: Private functions are like internal functions but cannot be called by derived contracts.
Remix Demo
https://remix.ethereum.org
Compiling Solidity
Compiling Solidity generates two artifacts:
Solidity: Constructors
When a contract is created, it also runs the constructor function if one exists, to initialize the state of the contract.
The constructor is run in the same transaction as the contract creation. The constructor function is optional.
contract MyToken {
constructor(address _minter) {
// This is the constructor
}
}
Solidity: Functions
Within a contract, we define functions that can be called by an EOA transaction or another contract.
function hello() returns (bool) {
return true;
}
Solidity: Function Modifiers
view: A function marked as a view are getter functions that promises not to modify any state.
pure: A pure function is one that neither reads nor writes any variables in storage.
payable: A payable function is one that can accept incoming Ether payments. Functions not declared as payable rejects incoming payments.
Solidity: Assertions
Assertions are used to validate inputs such as function arguments and expected values.
require(withdraw_amount <= 0.1 ether);
require(msg.sender == owner, “Only the contract owner can call this function.”);
Solidity: Error Handling
A successful transaction means that the program executed without an error and reached the end of execution.
If execution fails due to an error, all of its effects (changes in state) are “rolled back” as if the transaction never ran.
A failed transaction is still recorded as having been attempted, and the ether spent on gas for the execution is still deducted, but it otherwise has no other effects on contract or account state.
Solidity: Events
contract ERC20 {
event Transfer(address from, address to, uint amount);
function transfer(address to, uint amount) {
...
emit Transfer(msg.sender, to, amount);
}
}
Solidity: Other Language Constructs
We’ll learn the rest in the next session:
Smart Contract
Development Philosophy
Imagine somebody reported a bug...
Smart Contracts ≠
Traditional Software
Keeps Contracts Simple 💡
Prepare for Failure 👩🚒🔥
Building an ERC20 Token
What is ERC20?
The ERC20 token standard defines a common interface for contracts implementing a token, such that any compatible token can be accessed and used in the same way.
ERC20 is a standard for fungible tokens, meaning that different units of an ERC20 token are interchangeable and have no unique properties.
ERC20 Demo / Hands-On
Final code: https://bit.ly/2GFxdwP
Summary
Next Session