Smart Contracts 101
Polkadot Smart Contracts
About Me
- Kian Paimani aka.
@kianenigma
- Engineering Lead @ Parity Technologies
- Polkadot Core Fellow
- blog.kianenigma.com (<-- slides)
What is a Smart Contract
Blockchain models that we can think of:
- Database 🤮
- State Machine ♾️
- Computer 💻
Note:
First is utter BS
Second is accurate, but a bit to math-y
Third one is accurate, and one that most people relate to, we will use this one
--
What is a Smart Contract
- Computer has
code
andmemory
. - Users can interact with the
code
(trigger a transfer) - In a blockchain, we call these the
stf
/runtime
, andstate
respectively.
Aside: The main novelty of a computer is that it will execute
code
correctly, and therefore you can TRUST thememory
. 🪄
--
What is a Smart Contract
- First generation blockchains had a fixed
code
. - What is the
code
of the Bitcoin computer?- Transfer of BTC
Note:
First blockchains were computers with a (almost) fixed code
. Bitcoin's code
is ONLY how to do transfer of BTC, and therefore the State of the Bitcoin computer was only user balances. Nothing more.
--
What is a Smart Contract
- Smart Contracts are the first step into making blockchain computers programmable.
--
What is a Smart Contract
- Ethereum is a programmable blockchain
- What is the
code
of the Ethereum computer?- Transfer of ETH
- Upload a contract
- Call into an existing contract, which might alter state
--
What is a Smart Contract
--
What is a Smart Contract
- Given: blockchains are computers with
code
andmemory
- Smart contracts are essentially a way to extend the
code
andmemory
--
What is a Smart Contract
- Execution of these contracts is as secure as the execution of the main blockchain's
code
. 🪄 - If you can trust Ethereum and its ability to correctly transfer ETH, you should also trust that it can correctly execute whatever is the code of any smart contract.
Ethereum Contracts
A Smart Contract has:
- code
- address (at which it can be invoked)
- state/memory
- balance: money owned by the contract
Note:
Bringing it all together
Case Study
Simple Escrow Contract
- Buyer deposits funds
- Both buyer and seller approve
- Transfer funds to seller
- Timeout
- Return funds to buyer
--
Case Study
Web3 Roulette
- Deposit Phase
- Anyone can deposit money into the contract
- Roulette Phase
- One participants get nothing back, their money split among everyone else
How to Write a Contract
- Ethereum Contracts are written in a language Solidity.
- Part of the Ethereum
code
is to execute Solidity code. - This happens in a virtual machine known as EVM
Note:
EVM: Ethereum Virtual Machine
How to Execute a Contract
If Ethereum wants to execute a totally untrusted code, what challenges does it have?
- Deterministic cost (gas)
- Deterministic output (EVM)
--
How to Execute a Contract
- Ability to meter ⏰ the cost of the execution of a contract is an important requirement
- This is why when interacting with any contract, you specify
gas_limit
--
Extending Memory
What about the fact that a contract can add more data to its own memory, which in turn bloats the overall blockchain memory?
- State cost needs to be taken into account
- Part of gas
- Deposit
- State Rent
--
Other Properties of Contracts
- Atomic
- Immutable
Smart Contracts in Polkadot
Polkadot
- Computer who's
code
allows other Blockchains to be executed and validated. - Polkadot always assumed one of these external blockchains (Parachains) would deliver smart contract capability.
Past
- Polkadot's
code
is WASM bytecode (not EVM or similar). - An experiment to compile Rust to WASM as a smart Contract
pallet-contracts
and Ink!.
- Execute EVM compatible Smart Contracts as well
Note:
Both of these initiated in Parity, but are pursued as community projects now.
Now
- A new, much better virtual machine based on RiskV: PolkaVM
- Fork
pallet-contracts
to execute any Smart Contract that compiles to PolkaVM. - Compiler to compile Solidity to PolkaVM bytecode
--
Now
Outcome: Polkadot will have fast Solidity-compatible Smart Contracts in H2 2025
Outro
- Smart Contracts are the most basic form of building an application on top of a secure blockchain computer 🪄.
- Battle tested approach that has been used for almost a decade now
Questions
- Made with 💎 Obsidian-Digital-Garden
- © Kian Paimani 2024. Content on this site is licensed under a Creative Commons Attribution 4.0 International License.