SlideShare a Scribd company logo
1 of 20
Download to read offline
Smart Contract Programming
Anatoly Ressin
anatoly@assistunion.com
Senior Software Developer at C.T.Co
board member of Latvian Blockchain Association
co-founder at AssistUnion
…an idea
• Bitcoin script language allows
some sophisticated calculations
(verifying conditions) though it
is not Turing-Complete…
• Blockchain system with
embedded Turing-Complete
language would allow to verify
any [computable] condition!
Vitalik Buterin
2013
Why do we need custom logic
on blockchain?
• Auctions;
• Lotteries;
• Voting;
• Different kinds of ledgers (tax-statements, land-books…)
• Obligations and rights management;
• Custom crypto-currencies connected to arbitrary
business-processes;
• … [try to imagine more - maybe it’ll be your fresh startup]
Most important papers
• Ethereum White paper (by Vitalik Buterin)
• Ethereum Yellow paper (by Gavin Wood)
• formal description of the semantics
Accounts
• External — controlled by Private/Public keys
• Public address + Private Key
• Balance
• Contract — controlled by code. Contract has:
• Public address
• Code
• Persistent storage [mapping (256bit => 256bit)]
• Balance
Ethereum Virtual Machine
	0:	PUSH1				
	1:	0	
	2:	CALLDATALOAD	
	3:	SLOAD	
	4:	NOT	
	5:	PUSH1	
	6:	9	
	7:	JUMPI						[!]	
	8:	STOP	
	9:	JUMPDEST			[!]	
	0:	PUSH1		
11:	32	
12:	CALLDATALOAD	
13:	PUSH1		
14:	0	
15:	CALLDATALOAD	
16:	SSTORE
• Stack based
• Has it’s own
• STACK
• MEMORY
• Has access to the
• message data
• contract storage & balance
• block & transaction contexts
Classical problem
• Undecidability
• Time: how many steps are needed to complete
an execution (and whether it completes at all?)
• Space: how many memory it would consume for
the execution.
Gas Economics
• Gas is a unit for measuring execution complexity.
• Every OpCode costs well-defined amount of Gas.
• To initiate a transaction, you need to specify
• gas: Amount of Gas you allow to spend for execution;
• gasPrice: how many ethers (wei) you are agree to pay
per 1 gas unit;
• Miners are free to to include transactions into blocks if
gasPrice combined with gas amount is profitable for
them.
Meet Solidity
• Object Oriented (Multiple inheritance with
linearization)
• JavaScript inspired
• Statically Typed
• Targets to EVM
by Gavin Wood (!)
Example: Mortal.sol
contract mortal {
address owner;
function mortal() {
owner = msg.sender;
}
function kill() {
if (msg.sender == owner) {
selfdestruct(owner);
}
}
} greeter.sol
source code example
globals: block, msg, tx
• block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most
recent blocks excluding current
• block.coinbase (address): current block miner’s address
• block.difficulty (uint): current block difficulty
• block.gaslimit (uint): current block gaslimit
• block.number (uint): current block number
• block.timestamp (uint): current block timestamp
• msg.data (bytes): complete calldata
• msg.gas (uint): remaining gas
• msg.sender (address): sender of the message (current call)
• msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)
• msg.value (uint): number of wei sent with the message
• now (uint): current block timestamp (alias for block.timestamp)
• tx.gasprice (uint): gas price of the transaction
• tx.origin (address): sender of the transaction (full call chain)
Basic data types
• address: account identifier, similar to a 160-bit hash type.
• bool: two-state value.
• uint: 256-bit unsigned integer, operable with bitwise and
unsigned arithmetic operations.
• int: 256-bit signed integer, operable with bitwise and signed
arithmetic operations.
• hash: 256-bit, 32-byte data chunk, indexable into bytes and
operable with bitwise operations.
• string32: zero-terminated ASCII string of maximum length 32-
bytes (256-bit).
Structs, Arrays, Mappings
struct	RGBWallet	{	
						uint	redCoins;	
						uint	greenCoins;	
						uint	blueCoins;	
}	
RGBWallet	myWallet;	
uint[]	arr	=	new	uint[10];
mapping	(address	=>	RGBWallet)	wallets;
Reference type location
modifiers
• memory
• storage
• calldata
function	(uint[8]	memory	args)	{	
			…	
}
Affects how assignments are performed
(copy if needed to change location)
Function modifiers
function-modifiers.sol
source code example
Security considerations
• Avoid sending money from the contract. Allow
users to withdraw it!
• Preferred execution flow on inter-contract
communication:
1. Check preconditions;
2. Modify state;
3. Only then call other contracts.
Simple Auction
simple-auction.sol
source code example
And many-many more…
• Full Solidity language labs;
• ERC20: token standard;
• Different kinds of Voting Schemes;
• DAO;
• Contract verification;
• Web3 and DApps
Thank you!
Your questions…
If you are interested in a full Solidity course — drop
me a line: anatoly@assistunion.com
Useful sources
• https://github.com/ethereum/wiki/wiki/White-
Paper
• http://solidity.readthedocs.io/
• https://ethereumbuilders.gitbooks.io/guide/
content/en/solidity_tutorials.html
• https://drive.google.com/file/d/
0ByMtMw2hul0EN3NCaVFHSFdxRzA/view

More Related Content

What's hot

Ethereum Web3.js - Some tips for the developer
Ethereum Web3.js - Some  tips  for  the developer Ethereum Web3.js - Some  tips  for  the developer
Ethereum Web3.js - Some tips for the developer
炫成 林
 
Algorithm (MAC/GCM/GMAC)
Algorithm (MAC/GCM/GMAC)Algorithm (MAC/GCM/GMAC)
Algorithm (MAC/GCM/GMAC)
Nam Yong Kim
 

What's hot (11)

Introduction into blockchains and cryptocurrencies
Introduction into blockchains and cryptocurrenciesIntroduction into blockchains and cryptocurrencies
Introduction into blockchains and cryptocurrencies
 
Blockchain for creative content - What we do in LikeCoin
Blockchain for creative content - What we do in LikeCoinBlockchain for creative content - What we do in LikeCoin
Blockchain for creative content - What we do in LikeCoin
 
Bitcoins Math
Bitcoins MathBitcoins Math
Bitcoins Math
 
以太坊代幣付款委託 @ Open Source Developer Meetup #12
以太坊代幣付款委託 @ Open Source Developer Meetup #12以太坊代幣付款委託 @ Open Source Developer Meetup #12
以太坊代幣付款委託 @ Open Source Developer Meetup #12
 
Ethereum bxl
Ethereum bxlEthereum bxl
Ethereum bxl
 
Ethereum Web3.js - Some tips for the developer
Ethereum Web3.js - Some  tips  for  the developer Ethereum Web3.js - Some  tips  for  the developer
Ethereum Web3.js - Some tips for the developer
 
Introducing Blockchains
Introducing BlockchainsIntroducing Blockchains
Introducing Blockchains
 
Algorithm (MAC/GCM/GMAC)
Algorithm (MAC/GCM/GMAC)Algorithm (MAC/GCM/GMAC)
Algorithm (MAC/GCM/GMAC)
 
Introduction to Lightning Network
Introduction to Lightning NetworkIntroduction to Lightning Network
Introduction to Lightning Network
 
Bitcoin in general - presentation
Bitcoin in general - presentationBitcoin in general - presentation
Bitcoin in general - presentation
 
Blockchain and Smart Contracts
Blockchain and Smart ContractsBlockchain and Smart Contracts
Blockchain and Smart Contracts
 

Similar to "Programming Smart Contracts on Ethereum" by Anatoly Ressin from AssistUnion at CryptoCurrency focused 54th DevClub.lv

Similar to "Programming Smart Contracts on Ethereum" by Anatoly Ressin from AssistUnion at CryptoCurrency focused 54th DevClub.lv (20)

Blockchain for Developers
Blockchain for DevelopersBlockchain for Developers
Blockchain for Developers
 
Blockchain, Ethereum and Business Applications
Blockchain, Ethereum and Business ApplicationsBlockchain, Ethereum and Business Applications
Blockchain, Ethereum and Business Applications
 
Hello world contract
Hello world contractHello world contract
Hello world contract
 
Node.js Blockchain Implementation
Node.js Blockchain ImplementationNode.js Blockchain Implementation
Node.js Blockchain Implementation
 
BlockChain.pptx
BlockChain.pptxBlockChain.pptx
BlockChain.pptx
 
Blockchain Technology Introduction and Basics
Blockchain Technology  Introduction and BasicsBlockchain Technology  Introduction and Basics
Blockchain Technology Introduction and Basics
 
blockchain class 3.pdf
blockchain class 3.pdfblockchain class 3.pdf
blockchain class 3.pdf
 
Bitcoin 101 - Certified Bitcoin Professional Training Session
Bitcoin 101 - Certified Bitcoin Professional Training SessionBitcoin 101 - Certified Bitcoin Professional Training Session
Bitcoin 101 - Certified Bitcoin Professional Training Session
 
How to not Destroy Millions in Smart Contracts
How to not Destroy Millions in Smart ContractsHow to not Destroy Millions in Smart Contracts
How to not Destroy Millions in Smart Contracts
 
Blockchain - Presentacion Betabeers Galicia 10/12/2014
Blockchain - Presentacion Betabeers Galicia 10/12/2014Blockchain - Presentacion Betabeers Galicia 10/12/2014
Blockchain - Presentacion Betabeers Galicia 10/12/2014
 
20190606 blockchain101
20190606 blockchain10120190606 blockchain101
20190606 blockchain101
 
01 what is blockchain
01 what is blockchain01 what is blockchain
01 what is blockchain
 
Ethereum.pptx
Ethereum.pptxEthereum.pptx
Ethereum.pptx
 
以比特幣為例的區塊鏈技術介紹 ( Intro to Blockchain using Bitcoin as an example)
以比特幣為例的區塊鏈技術介紹 ( Intro to Blockchain using Bitcoin as an example)以比特幣為例的區塊鏈技術介紹 ( Intro to Blockchain using Bitcoin as an example)
以比特幣為例的區塊鏈技術介紹 ( Intro to Blockchain using Bitcoin as an example)
 
Hands on with smart contracts
Hands on with smart contractsHands on with smart contracts
Hands on with smart contracts
 
Ethereum
EthereumEthereum
Ethereum
 
Blockchain Technologies for Data Science
Blockchain Technologies for Data ScienceBlockchain Technologies for Data Science
Blockchain Technologies for Data Science
 
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
The Ethereum Blockchain - Introduction to Smart Contracts and Decentralized A...
 
Blockchain Demystified
Blockchain DemystifiedBlockchain Demystified
Blockchain Demystified
 
Introduction to Blockchain and Ethereum
Introduction to Blockchain and EthereumIntroduction to Blockchain and Ethereum
Introduction to Blockchain and Ethereum
 

Recently uploaded

Recently uploaded (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

"Programming Smart Contracts on Ethereum" by Anatoly Ressin from AssistUnion at CryptoCurrency focused 54th DevClub.lv

  • 1. Smart Contract Programming Anatoly Ressin anatoly@assistunion.com Senior Software Developer at C.T.Co board member of Latvian Blockchain Association co-founder at AssistUnion
  • 2. …an idea • Bitcoin script language allows some sophisticated calculations (verifying conditions) though it is not Turing-Complete… • Blockchain system with embedded Turing-Complete language would allow to verify any [computable] condition! Vitalik Buterin 2013
  • 3. Why do we need custom logic on blockchain? • Auctions; • Lotteries; • Voting; • Different kinds of ledgers (tax-statements, land-books…) • Obligations and rights management; • Custom crypto-currencies connected to arbitrary business-processes; • … [try to imagine more - maybe it’ll be your fresh startup]
  • 4. Most important papers • Ethereum White paper (by Vitalik Buterin) • Ethereum Yellow paper (by Gavin Wood) • formal description of the semantics
  • 5. Accounts • External — controlled by Private/Public keys • Public address + Private Key • Balance • Contract — controlled by code. Contract has: • Public address • Code • Persistent storage [mapping (256bit => 256bit)] • Balance
  • 6. Ethereum Virtual Machine 0: PUSH1 1: 0 2: CALLDATALOAD 3: SLOAD 4: NOT 5: PUSH1 6: 9 7: JUMPI [!] 8: STOP 9: JUMPDEST [!] 0: PUSH1 11: 32 12: CALLDATALOAD 13: PUSH1 14: 0 15: CALLDATALOAD 16: SSTORE • Stack based • Has it’s own • STACK • MEMORY • Has access to the • message data • contract storage & balance • block & transaction contexts
  • 7. Classical problem • Undecidability • Time: how many steps are needed to complete an execution (and whether it completes at all?) • Space: how many memory it would consume for the execution.
  • 8. Gas Economics • Gas is a unit for measuring execution complexity. • Every OpCode costs well-defined amount of Gas. • To initiate a transaction, you need to specify • gas: Amount of Gas you allow to spend for execution; • gasPrice: how many ethers (wei) you are agree to pay per 1 gas unit; • Miners are free to to include transactions into blocks if gasPrice combined with gas amount is profitable for them.
  • 9. Meet Solidity • Object Oriented (Multiple inheritance with linearization) • JavaScript inspired • Statically Typed • Targets to EVM by Gavin Wood (!)
  • 10. Example: Mortal.sol contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) { selfdestruct(owner); } } } greeter.sol source code example
  • 11. globals: block, msg, tx • block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for 256 most recent blocks excluding current • block.coinbase (address): current block miner’s address • block.difficulty (uint): current block difficulty • block.gaslimit (uint): current block gaslimit • block.number (uint): current block number • block.timestamp (uint): current block timestamp • msg.data (bytes): complete calldata • msg.gas (uint): remaining gas • msg.sender (address): sender of the message (current call) • msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier) • msg.value (uint): number of wei sent with the message • now (uint): current block timestamp (alias for block.timestamp) • tx.gasprice (uint): gas price of the transaction • tx.origin (address): sender of the transaction (full call chain)
  • 12. Basic data types • address: account identifier, similar to a 160-bit hash type. • bool: two-state value. • uint: 256-bit unsigned integer, operable with bitwise and unsigned arithmetic operations. • int: 256-bit signed integer, operable with bitwise and signed arithmetic operations. • hash: 256-bit, 32-byte data chunk, indexable into bytes and operable with bitwise operations. • string32: zero-terminated ASCII string of maximum length 32- bytes (256-bit).
  • 14. Reference type location modifiers • memory • storage • calldata function (uint[8] memory args) { … } Affects how assignments are performed (copy if needed to change location)
  • 16. Security considerations • Avoid sending money from the contract. Allow users to withdraw it! • Preferred execution flow on inter-contract communication: 1. Check preconditions; 2. Modify state; 3. Only then call other contracts.
  • 18. And many-many more… • Full Solidity language labs; • ERC20: token standard; • Different kinds of Voting Schemes; • DAO; • Contract verification; • Web3 and DApps
  • 19. Thank you! Your questions… If you are interested in a full Solidity course — drop me a line: anatoly@assistunion.com
  • 20. Useful sources • https://github.com/ethereum/wiki/wiki/White- Paper • http://solidity.readthedocs.io/ • https://ethereumbuilders.gitbooks.io/guide/ content/en/solidity_tutorials.html • https://drive.google.com/file/d/ 0ByMtMw2hul0EN3NCaVFHSFdxRzA/view