You are on page 1of 45

COMP 3320

ELECTRONIC COMMERCE TECHNOLOGY


LECTURE 7: BLOCKCHAIN AND SMART CONTRACT

Dr John T. H.Yuen
CYC 306, thyuen@cs.hku.hk 1
OVERVIEW

• Compare different types of blockchain

• Understand smart contract


Blockchain and Ø Concept
smart contract
Ø Technology in Ethereum

• Applications with smart contract

Ø NFT
2
BLOCKCHAIN: REVOLUTION OF VALUE TRANSFER

Internet lowered the


cost of data transfer

Blockchain lowered the


cost of value transfer
?

New business model to be developed for the revolution of Blockchain


3
TYPES OF BLOCKCHAIN

Public Consortium Private


No centralized management Multiple Organizations Single Organization

Permissionless Permissioned Permissioned


Ledger writer • Could be malicious • Identified, trusted • Identified, trusted
• Require economic incentive • No incentive required • No incentive required
Proof of Work, Proof of Stake... PBFT-type (~voting) PBFT/Paxos-type
• No/slow finality • Lighter, faster • Fastest
Consensus
• 51% attack • Enable finality • Enable finality
• PoW: Large energy consumption • Low energy consumption • Low energy consumption
Latency Long: Bitcoin: 10 min or more Short: 10+ ms Short: 10+ ms
Throughput 10x-1000x tps 1000+ tps 10000+ tps 4

Disruptive new business model with no intermediate Cost cutting for existing business model
• e.g. sharing economy • e.g. finance Figure from: SAP
CONSORTIUM BLOCKCHAIN ECOSYSTEM

R3 Consortium blockchain Open source consortium Use Ethereum’s technology to


formed by 60+ financial blockchain hosted by The Linux develop consortium blockchain.
institution in 2015 Foundation since 2016. Started in 2017.
Members: IBM, Intel Members: Microsoft, JP Morgan

Do you think blockchain applications are all decentralized?


PROOF OF OWNERSHIP
THE FIRST WAVE OF BLOCKCHAIN APPLICATIONS

6
PROOF OF OWNERSHIP
¢ One of the first application of blockchain is to use the
immutable and integrity properties of blockchain
§ Once it is written, it cannot be changed

Use case: Decentralized land titles


¢ Problems:
§ Flawed paperwork, forged signatures, unclear documents
§ Lacking central government authority
§ Corrupt officials accepting bribes, tampering with records
§ Government cannot support land record authority
¢ Blockchain: a simple mechanism to transfer ownership, like making
a transaction on Bitcoin 7

§ Testing in UK, Sweden, Russia, Brazil, India, Ukraine


USING THE INTEGRITY OF BLOCKCHAIN

¡ Other use cases for the proof of ownership


§ Education certificate (fight against fake education background)
§ Diamond (fight against fake/stolen/blood diamond)
§ Antiques

¡ Blockchain fight against censorship


§ #MeToo activists posted on Ethereum after post being deleted
§ “There is no 404 on blockchain.”

https://etherscan.io/tx/0x2d6a7b0f6adeff38423d4c62cd8b6ccb708ddad85da5d3d06756ad4d8a04a6a2
8
FROM BLOCKCHAIN TO SMART CONTRACT
TOWARDS BLOCKCHAIN 2.0

9
FROM BLOCKCHAIN TO SMART CONTRACT

The restrictions of ¢ Bitcoin is “coin centric.”


¢ Primary Purpose: Alternative to existing
Bitcoin’s script can It cannot store other currency
only perform limited data except those
transaction logic transaction data

¢ Ethereum is a decentralized Turing-complete


Turing-complete virtual machine to execute smart contract
Support data storage using network nodes, that uses its coin ether
scripting language can
completely controlled
perform arbitrary as “fuel”.
by code
transaction logic
¢ Primary Purpose: Platform for decentralized 10

applications + Smart Contracts


CONTRACTS

q Contract q Smart contract

§ a written or spoken § code that facilitates, verifies, or


agreement ... that is intended enforces the negotiation or
to be enforceable by law. execution of a digital contract.
• Trusted entity must run this code

11
SMART CONTRACT
¢ Contract ¢ Smart contract (Distributed applications DAPP):
• The code stored and synchronized in blockchain, which
is triggered by user initialized special transaction, and
executed concurrently by consensus nodes.
• It can update the data stored in blockchain and the
modified data remains synchronized among all nodes.
!"#": % → '
An option contract A triggering event like an Smart
between parties is written expiration date and strike contract
DAPP
as code into the price is hit and the contract
blockchain. The contract is executes itself according to !"#": % → ' !"#": % → '
in the public ledger. the coded terms. Smart
Smart
contract contract
DAPP DAPP

!"#": % → '
12
Smart
contract
DAPP
ETHEREUM
THE FIRST SMART CONTRACT PLATFORM

13
ETHEREUM SMART CONTRACT
¢ History
• Late 2013: Ethereum described in whitepaper by Vitalik Buterin
• July 2015: Ethereum blockchain launched
• March 2017, various blockchain start-ups, research groups, and Fortune 500 companies announced the creation of
the Enterprise Ethereum Alliance (EEA).
§ It has >150 members now, including Microsoft, Intel, JP Morgan, Deloitte, CME, DTCC, Mastercard, Cisco.

¢ Smart Contracts in Ethereum:


§ React to external world when "poked" by transactions (which call specific functions)
§ Have direct control over:
• internal ether balance
• internal contract state 14

Demo: https://ethfiddle.com/
BITCOIN VS ETHEREUM

¡ The “Gold Standard” of blockchains ¡ Smart contract blockchain platform

¡ Asset: bitcoin ¡ Asset: ether


¡ Primary purpose of the Bitcoin blockchain ¡ Fund computation
¡ Simple and robust ¡ Align incentives

¡ Stack-based, primitive scripting language, not Turing- ¡ Complex and feature-rich


complete ¡ Turing-complete scripting language
¡ UTXO-based ¡ Account-based
¡ Will likely remain Proof-of-Work ¡ Uses a blend of PoW and PoS
¡ Block creation time: 10 min ¡ Block creation time: 15 sec 15
ETHEREUM ACCOUNTS

Externally Owned Accounts Contract Accounts


¡ Owned by some external entity ¢ “Owned” by contract
(person, corporation, etc.) ¢ Code execution triggered by
¡ Can send transactions to transfer transactions or function calls (msg)
ether or trigger contract code ¢ Contains:
¡ Contains: § Address
§ Address § Associated contract code
16
§ Ether Balance § Persistent storage
ACCOUNTS VS UTXO MODEL

Alice owns private keys to a Bob owns private keys to


set of UTXOs accounts

Tx#12: à $5 to Alice address: “0xbc126...”


Tx#17: à $1 to Alice
balance: $10
Tx#22: à $6 to Alice code: c := a + b

17
ETHEREUM SMART CONTRACTS

Ethereum contracts generally serve four purposes:


¢ Store and maintain data
§ Data represents something useful to users or other contracts
§ E.g.: a token currency or organization’s membership
¢ Manage contract or relationship between untrusting users
§ E.g.: financial contracts, escrow, insurance
¢ Provide functions to other contracts
§ Serving as a software library
¢ Complex Authentication Demo: https://ethfiddle.com/
§ E.g.: M-of-N multi-signature access
18
MINING IN ETHEREUM

¡ Quite similar to Bitcoin:


1. Download the entire Ethereum blockchain
2. Verify incoming transactions and run Smart
Contract code invoked by transactions
3. Create a block
4. Find a valid nonce
5. Broadcast your block
6. Profit!

19
ETHEREUM VIRTUAL MACHINE

Solidity Contract
contract Bidding { Solidity
/* ... */ EVM Code
Compiler
}
PUSH1 0x60 PUSH1 0x40
MSTORE CALLDATASIZE
ISZERO
...
PUSH2 0x1b4c POP
JUMP
Vyper Contract
def register(key, value): Vyper
/* … */ Compiler

20
ETHEREUM VIRTUAL MACHINE

¡ The EVM (Ethereum Virtual Machine) is a “mini computer” that runs contract code
¡ Contract code that actually gets executed on every node is EVM code
§ EVM code: low-level, stack based bytecode language (i.e. JVM bytecode)
¡ Every Ethereum node runs EVM

21
SMART CONTRACT PROBLEMS
¢ What if our contract has an infinite loop?
¢ Every node on the network will get stuck executing the loop forever!
§ It is impossible to determine ahead of time whether the contract will ever terminate
§ ⇒ Denial of Service Attack!

Ethereum’s solution:
¢ Every contract requires “gas”, which “fuels” contract execution
¢ Every op-code requires some gas in order to execute
¢ If the contract successfully executes, the remaining gas is refunded to the sender
¢ If the contract execution runs out of gas before it finishes, the execution reverts and the gas are not refunded
¢ Purchasing gas == purchasing distributed, trustless computational power
22

¢ An attacker looking to launch a DoS attack will need to supply enough ether to fund the attack
QUESTION

¡ Can you think of other potential software security of smart contract?

mapping (address => uint256) public balanceOf;


// INSECURE
function transfer(address _to, uint256 _value) {
/* Check if sender has balance */
require(balanceOf[msg.sender] >= _value);
/* Add and subtract new balances */
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
}
23
ETHEREUM CONCLUSIONS

¡ Ethereum is not about optimising efficiency of computation


¡ Its parallel processing is redundantly parallel
§ way to reach consensus on the system state without needing trusted third parties

¡ Contract executions are redundantly replicated across nodes


§ ⇒ expensive, slow, memory-intensive
§ creates an incentive not to use the blockchain for computation that can be done off chain

24
BLOCKCHAIN 2.0 ECOSYSTEM
25
ICO

¡ ICOs - Initial Coin Offerings


§ Way for people to invest Ether into startups of companies being
built on top of Ethereum
§ Permissionless, effortless way to invest in a good company
(think Kickstarter)

¡ Tezos ICO $200 million


¡ Filecoin ICO $253 Million
¡ EOS ICO $4.2 Billion
¡ $7 billion was raised via ICO from January–June 2018 26

Source: https://blockchainhub.net/ico-initial-coin-offerings/
ICO

¡ Beware about ICOs


§ prone to scams and securities law violations
§ almost half of ICOs sold in 2017 failed by February
2018
¡ Following a speculative boom in cryptocurrency
prices that peaked in December 2017, regulation
of cryptocurrencies has been rapidly changing

27
ICO

¡ Source: PwC 6 th ICO / STO Report, Spring 2020.


28
29
BLOCKCHAIN APPLICATIONS IN DIFFERENT INDUSTRIES
Global payment, clearing and settlement,
trade finance, insurance claim, Know Your
Customer (KYC)…

Finance

Supply chain
management, Supply Personal health record,
Health health management
food traceability Chain

Blockchain
Applications

Car/room rental, Sharing Digital Rights Video/music copyright,


storage sharing economy Management Music streaming platform

IoT

30
Device management and
authentication, data exchange
CENTRAL BANK DIGITAL CURRENCY (CDBC)
• A currency established as money by government regulation, monetary authority or law)

Ø Firstly discussed by the Bank of England in 2015. Major countries with testing: Sweden, China

• Potential advantages:
Ø Technological efficiency: Faster clearing and settlement
Ø Financial inclusion
Ø Prevent crimes such as tax evasion or money laundering
Ø Monetary policy transmission: a new channel for monetary policy transmission (helicopter money)

Ø Prototype of CBDC cross-border payments with Bank of Thailand and ten participating banks from HK and Thailand

35

https://www.hkma.gov.hk/media/eng/doc/key-functions/financial-infrastructure/Report_on_Project_Inthanon-LionRock.pdf
36
NFT
NON-FUNGIBLE TOKEN

38
NON-FUNGIBLE TOKEN (NFT)

Idea/ concept:
It is a way to represent ownership of
anything that is unique.

More technical term:


A unique token that exist on a blockchain
which you can prove ownership of

39
NFT

¡ Non-fungible: Not interchangeable (Unique)

For instance:
¡ Fungible: Banksnotes, Cryptocurrency (Bitcoin)
¡ “100HKD” Banknote can be exchanged with 2 “50HKD” Banknotes and they worth the same

¡ Non-fungible: Art, Paintings, License


¡ There can only be 1 Monalisa
¡ NFT: unique identifying information written in their smart contracts

40
PROOF-OF-OWNERSHIP

¡ Does taking a screenshot of someone’s NFT Digital Avatar


means you own it?
¡ No, this is because you don’t have the anything to prove that
ownership e.g. a certificate

¡ Each NFT can used to prove ownership of, this is


because of the Digital Signature which is associated with
the address in the blockchain

41
CURRENT ADOPTIONS

CyptoKitty: “Dragon” CryptoPunk #5822


¡ As mentioned, NFTs can be associated
with anything that is unique
¡ Hence it could include physical
assets like:
¡ Deed to real estate, clothes, sneakers

¡ However at this point majoring of


NFTs are linked to digital
collective, arts, avatars, images USD 172000
USD 23 million
and videos
42
CRYPTOKITTIES

¡ a blockchain-based game that allows players to purchase, collect,


breed and sell various types of virtual cats.
¡ one of the earliest blockchain-based game
¡ CryptoKitties' popularity in December 2017 congested the
Ethereum network, causing it to reach an all-time high in
transactions and slow down significantly.
¡ The virtual cats are breedable and carry a unique number and
256 bit distinct genome with DNA and different attributes that
can be passed to offspring.
§ Each CryptoKitty is represented in the form of a non-fungible ERC-
721 token
§ ERC-721: Every token is unique: similar to serial number of a bank
note 43
WHY IS NFT SUCH A GAME CHANGER?

¡ The Idea of Real Ownership

¡ Example:
¡ You bought an in-game item say a new skin, a weapon in
Minecraft
¡ This game is owned by a company - Microsoft.
¡ If one day the publisher stops supporting the game, the
items you bought vanish along with the game. Gone.
¡ But because NFTs are minted on a blockchain, they
confirm real ownership, no one actually owns your NFT
but you.
44
NFT DIGITAL ARTWORK AND COLLECTIBLES

¡ NFT shot to prominence in these 2 years, with


multi-million dollar sales of NFT artwork grabbing
headlines
¡ Twitter has been overrun by NFT avatars like Bored
Ape Yacht Club, CryptoPunks and Pudgy Penguins.
¡ In 2021, NFTs blowed into the mainstream, Digital artist
Beeple grabbed headlines with USD 69million
sale
¡ made him the third most valuable living artist overnight
“Everydays: The First 5000 Days”
45
CELEBRITIES WITH NFT AVATARS
NFT IN METAVERSE

¡ The metaverse is a seamless convergence of our


physical and digital lives, creating a unified, virtual
community where we can work, play, relax, transact
and socialize

¡ Metaverses such as Decentraland and Somnium


Space already show the beginnings of true
society, with individuals settling land,
interacting socially and exchanging goods
and asserting ownership rights.
¡ And these are done through NFTs as they are the
ideal tool to represent ownership of virtual
assets that are unique and can be proved of
ownership. 47
DIFFERENT INDUSTRIES JOINING THE NFT-REALM

Visa acquired CryptoPunk for USD Adidas and Prada announced they JPMorgan opens a virtual lounge in
$150,000 as they believe “NFTs will will be collaborating to release a metaverse saying that it has an
play an important role in the future of NFT project “estimated at over $1 trillion” market
retail, social media, entertainment, opportunity
and commerce 48
• Compare different types of blockchain

• Understand smart contract


Ø Concept
Blockchain and
smart contract Ø Technology in Ethereum

• Be familiar with smart contract ecosystem

Smart Contract
• Analyse blockchain and smart contract
applications, e.g. NFT
49

¡ Reference:

1. “Bitcoin: A Peer-to-Peer Electronic Cash System”, Satoshi Nakamoto.


2. https://medium.com/@VitalikButerin
3. Why Bitcoin Matters. https://dealbook.nytimes.com/2014/01/21/why-bitcoin-matters/

¡ ********
¡ Run the smart contract demo code in: https://blog.zeppelin.solutions/the-hitchhikers-guide-to-
smart-contracts-in-ethereum-848f08001f05
¡ Test it in: https://ethfiddle.com/

50

You might also like