SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 

Tendances (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
 

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

Similaire à "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
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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?
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

"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