Ethereum Overview and what are Smart Contracts and what are their limitations. A Solidity programming language Crash Course. How to create an ERC20 token. What is an ICO on Ethereum.
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Programming smart contracts in solidity
1. PROGRAMMING SMART CONTRACTS WITH
SOLIDITY
Navigate : Space / Arrow Keys | - Menu | - Fullscreen | - Overview | - Blackout | - Speaker | - HelpM F O B S ?
1 / 44
2. EMANUEL MOTA - @EMOTA7
Founder of Yari Labs
emanuel@yarilabs.com
@yarilabs
Braga Blockchain
2 / 44
3. ABOUT THE TALK
Intro
Ethereum Overview
Smart Contracts
Solidity Crash Course
What is an ERC20 token
ICO on Ethereum
Questions
Braga Blockchain
3 / 44
5. SOME STATS ABOUT THE AUDIENCE
Developers: 70% of the audience
Knowledge of Solidity programming language?
Never heard of it before - 53%
I'm a Beginner - 26%
I've tryed it a couple of times - 16%
I'm an Intermediate user - 5%
I'm a Solidity Guru - 0%
Braga Blockchain
5 / 44
6. Ethereum Network
“Each node of the Ethereum
network hosts a blockchain database
and a node client capable of
executing application code stored on
blockchain. Nodes communicate
through Wire protocol and expose
same interface but can be
implemented in different languages.”
Excerpt From: Roberto Infante. “Building Ethereum ÐApps”
ETHEREUM
Braga Blockchain
6 / 44
9. ETHEREUM
Two kinds of accounts
External Accounts (wallets controlled by humans)
Contract Accounts (controlled by code)
every account has a balance
Braga Blockchain
9 / 44
10. ETHEREUM
Code execution costs GAS
Transaction is a message sent from one account to
another and can have a data payload
Braga Blockchain
10 / 44
13. SMART CONTRACTS
Contract = code (i.e. functions) + data (i.e. state) and
resides on the blockchain
EVM is the runtime for Smart Contracts on
Ethereum
Accounts have a persistent memory area which is
called storage
Contracts can neither read nor write to any storage
apart from their own
Braga Blockchain
13 / 44
14. SMART CONTRACTS
Contracts can call other contracts
1024 max call stack depth
Support Events
Contracts can purge themselves from the blockchain
(OPCODE selfdestruct)
Braga Blockchain
14 / 44
16. SOLIDITY
Solidity is a statically typed, contract programming
language that has similarities to Javascript, Java and C.
Braga Blockchain
16 / 44
17. SOLIDITY
Has some contract-speci c features like:
modi er (guard) clauses
event noti ers for listeners
custom global variables.
Braga Blockchain
17 / 44
18. SOLIDITY
Hello World
version pragma — declares the version of the compiler to be used to avoid breaking changes
introduced on future versions
pragma solidity ^0.4.19;
contract HelloWorld {
}
Braga Blockchain
18 / 44
19. SOLIDITY
Statically typed language
uint data type is an unsigned integer (non-
negative number)
int data type is used for signed integers
uint has 256 bits we can also have uint8, uint16,
uint32
contract Example {
// This will be stored permanently in the blockchain
uint myUnsignedInteger = 100;
string name;
}
Braga Blockchain
19 / 44
20. SOLIDITY
More complex data types - Structs
struct TokenHolder {
uint age;
string obs;
}
// Arrays
string[5] stringArray;
// a dynamic Array - has no fixed size, can keep growing:
uint[] dynamicArray;
// Public array
TokenHolder[] public shareHolders;
Braga Blockchain
20 / 44
21. SOLIDITY
Mappings and data type address
rst example, the key is an address and the value is a uint
second example, the key is a uint and the value is a string
// For a financial app, storing a uint that holds the user's account balance:
mapping (address => uint) public accountBalance;
// Or could be used to store / lookup usernames based on userId
mapping (uint => string) userIdToName;
Braga Blockchain
21 / 44
22. SOLIDITY
Function declarations
uint[] scores;
function addNewScore(string _clientId, uint _score) public {
...
_updateScores(_score);
}
function _updatesScores(string _clientId, uint _number) private {
...
scores.push(_number) {
...
}
Braga Blockchain
22 / 44
23. SOLIDITY
More about functions
string greeting = "Whazaaa ?";
function sayHello() public returns (string) {
return greeting;
}
Braga Blockchain
23 / 44
24. SOLIDITY
Function Modi ers
function sayHello() public view returns (string) {
function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}
// Functions can return many arguments, and by specifying returned arguments
// names we don't need to explicitly return
function increment(uint x, uint y) returns (uint x, uint y) {
x += 1;
y += 1;
}
// when a function returns multiple values we need to parallel assign
(x1, y1) = increment(1,2);
Braga Blockchain
24 / 44
25. SOLIDITY
More on functions Modi ers
modifier onlyAfter(uint _time) { require (now >= _time); _; }
modifier onlyOwner { require(msg.sender == owner) _; }
// Append right after function declaration
function changeOwner(newOwner) onlyAfter(someTime) onlyOwner() {
owner = newOwner;
}
Braga Blockchain
25 / 44
26. SOLIDITY
Payable function Modi er
// All functions that receive ether must be marked 'payable'
function depositEther() public payable {
balances[msg.sender] += msg.value;
}
Braga Blockchain
26 / 44
27. SOLIDITY
Events
Events are a way for a contract to communicate that something happened on the blockchain
to a front-end client that is 'listening' for events
A javascript implementation would look something like:
// declare the event
event IntegersAdded(uint x, uint y, uint result);
function add(uint _x, uint _y) public returns(uint) {
uint result = _x + _y;
// fire an event to let the app know the function was called:
IntegersAdded(_x, _y, result);
return result;
}
YourContract.IntegersAdded(function(error, result) {
// do something with result
}
Braga Blockchain
27 / 44
28. SOLIDITY
Important global variables
this; // address of contract
this.balance; // often used at end of contract life to transfer balance
// ** msg - Current message received by the contract ** **
msg.sender; // address of sender
msg.value; // amount of eth sent to contract (in wei) function should be "payabl
msg.data; // bytes, complete call data
msg.gas; // remaining gas
now; // current time (approximately) - uses Unix time
Braga Blockchain
28 / 44
29. IMPORTANT DESIGN NOTES
Obfuscation: All variables are publicly viewable on
blockchain, so anything that is private needs to be
obfuscated
Storage optimization: Writing to blockchain is
expensive, as data is stored forever
Braga Blockchain
29 / 44
30. IMPORTANT DESIGN NOTES
Cron Job: Contracts must be manually called to
handle time-based scheduling
Cost of Gas: the fuel Ethereum DApps run on
Braga Blockchain
30 / 44
33. SOME USEFULL LINKS
(so you don’t have to run your own
node/s)
Truf e .
Embark
Open Zeppelin
https://etherchain.org/
http://infura.io
https://github.com/ConsenSys/truf e
https://github.com/iurimatias/embark-
framework
https://openzeppelin.org/
https://metamask.io/
Braga Blockchain
33 / 44
34. WHAT IS AN ERC20 TOKEN?
Braga Blockchain
34 / 44
35. ERC20 TOKEN
ERC stands for Ethereum Request for Comments
contract MyToken {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* Initializes contract with initial supply tokens to the creator */
function MyToken(
uint256 initialSupply
) {
// Give the creator all initial tokens
balanceOf[msg.sender] = initialSupply;
}
Braga Blockchain
35 / 44
36. ERC20 TOKEN (CONTINUATION)
(from ethereum.org)
/* Send coins */
function transfer(address _to, uint256 _value) {
// Check if the sender has enough
require(balanceOf[msg.sender] >= _value);
// Check for overflows
require(balanceOf[_to] + _value >= balanceOf[_to]);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
}
}
Braga Blockchain
36 / 44
37. ERC20 TOKEN
An ERC20 token implements the following API
name
symbol
decimals
transfer(to, value)
transferFrom(from, to, value)
approve(spender, value)
approveAndCall(spender, value, extraData)
burn(value)
burnFrom(from, value)
plus trigger a set of events
a complete spec of a ERC20 Token check andhttps://ethereum.org/token
https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
Braga Blockchain
37 / 44
38. ERC20 TOKEN
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner)
public constant returns (uint balance);
function allowance(address tokenOwner, address spender)
public constant returns (uint remaining);
function transfer(address to, uint tokens)
public returns (bool success);
function approve(address spender, uint tokens)
public returns (bool success);
function transferFrom(address from, address to, uint tokens)
public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint toke
}
Braga Blockchain
38 / 44
39. HOW TO MAKE AN ICO (CROWDSALE)
CONTRACT
Braga Blockchain
39 / 44
40. WHAT REALLY IS AN ICO ?
An ICO - Initial Coin Offer should really be called:
token generation event (TGE) as ICO is a term
initially coined for currencies and now its being used
for selling ERC20 (ethereum tokens).
And ICO should be done automatically via a
SmartContract that implements the rules of the
token sale.
Braga Blockchain
40 / 44
41. ICO
To buy tokens from a smart contract participants
should transfer ethereum directly from their wallets to
the smart contract address so that the smartcontract
assigns them the tokens automatically.
To see an example of a crowdsale contract check:
https://ethereum.org/crowdsale
Braga Blockchain
41 / 44