Slides I used for FinTech - Financial Innovation and the Internet 2022 Fall at Graduate School of Business and Finance, Waseda University on December 9, 2022.
Innovation and the Internet 2022 Fall Lecture 10 : Smart Contracts Kenji Saito, Graduate School of Business and Finance, Waseda University Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.1/44
be online only. When online, Camera ON is recommended, but not required You do need to speak often anyway (we are going to have a lot of dialogue) We will use breakout rooms a lot, but those won’t be recorded unless you do it yourselves (need to be allowed) Keep your Zoom client updated! We might use latest features The recordings could be used for research on (online) learning Transcribed for use and anonymized Will let you know when the necessity arises Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.2/44
and chat text will be posted at Moodle and Discord Trial automatic transcriptions will be posted at Discord Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.3/44
Lecture 2 10/7 Overview of FinTech (2) • Lecture 3 10/14 Internet Technology and Governance (1) — online • Lecture 4 10/21 Internet Technology and Governance (2) • Lecture 5 10/28 The World of Apps (1) — online (close enough to Halloween) • Lecture 6 11/11 The World of Apps (2) — online • Lecture 7 11/18 Basics of Cryptography and Blockchain — online • Lecture 8 11/25 Blockchain • Lecture 9 12/2 Blockchain and Smart Contracts — online • Lecture 10 12/9 Smart Contracts — online • Lecture 11 12/16 Further Smart Contracts Lecture 12 12/23 Cyber-Physical Society and Future of Finance Lecture 13 1/13 FinTech Ideathon (1) — online Lecture 14 1/20 FinTech Ideathon (2) Lecture 15 1/27 Presentations and Conclusions Lecture 10 has been changed to be online and lecture 11 to be in classroom! Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.4/44
Validity/Existence/Uniqueness layers Applicability of Blockchain Impossibility and Challenges of Blockchain Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.5/44
and Ethereum — Brief Introduction Demonstration of Smart Contracts We will switch a lot of content with the next week, and go with a lot of demonstrations today Next week, we will cover issues of blockchain in more depth Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.6/44
on or disguised as the thoughts and ideas of science and technology The world with existing science and technology is the real world Ex1 : Medical drama Ex2 : Economic novels The world with science and technology unknown to the real world → Fiction Ex1 : Medical drama with nano-machines Ex2 : Drama with digital currency that depreciates (my book “NEO in Wonderland”) Designing new media and putting it into society = living science fiction To work out plans for that = to write science fiction Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.8/44
of the word “smart contract” to fantasize a specific application example, and state the application briefly That is, go ahead and write a very short science fiction Deadline and how to submit November 29, 2022 at 17:59 JST From Moodle (mandatory) (Q&A forum) So that your classmates can read your report, refer to it, and comment on it Optionally, you can also post to #assignments channel at Discord So that anyone in our Discord can read your report, refer to it, and comment on it Just plain text, and be concise, please You may want to apply Kent Beck style for abstracts (4 sentences) (problem) (why it is a problem) (startling sentence) (consequences) of a story Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.10/44
Measures . . . how to improve the class 21 out of 28 students submitted (always better late than never) Your fictions : smart wills / (health) insurance / rent contract (that explodes when broken) / smart loan / inheritance / legal business / means against climate change / education / job market / flying-car parts / flying-car dealer / sharing clothes / metaverse and physical bodies / religion / DAO / poverty alleviation funds / car rental / border control / programmed daily lives / NFT to represent properties Had a lot of fun to read! Many of you have set the story in the distant future If you use this assignment for a new business idea, the near future is better, but sometimes putting it in the far future can reveal underlying problems of a new idea If we were to make them look more like the current smart contracts . . . Use the contracts (programs on blockchain) where a proof is needed (like the last will) Conditions to run the code need to be checked outside the contracts Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.11/44
alive, it is necessary to specify the wallet address of the heir through the smart contract, which can be multiple heirs, and can be divided into multiple inheritance priorities 2. When the user passes away, multiple signatures are required to trigger the smart contract to initiate the inheritance behavior The signing party here needs a government organization (to prove that the user has passed away), the signature of the heir, and possibly the signature of the designated lawyer 3. When the required multi-person signatures are completed (or the minimum number of signatures specified in advance is reached), the smart contract will transfer all assets on the user chain to the address of the heir ⇒ I would think the same about the signatures Because I think death is a social rather than a physical phenomenon Sensing the stop of heartbeats (cf. TENET), for example, would not work Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.12/44
a world where cars no longer exist because they were polluting too much People are having trouble to move and travel On the market, there are new forms of electrics cars but some of them are not working and only scams People cannot be sure to buy the right one In the blockchain people would be able to find secure data and have a certificate for the car they are buying The smart contract will enable people to trust which car they are buying and also be reassured about their payment ⇒ If the number of automobiles is to be reduced, I feel that it would be better to introduce self-driving cars They would not be bought (owned) but rented out as J-san suggested BTW, do you think money will still exist in year 2100? Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.13/44
allocate poverty alleviation funds through smart contract of digital coins, and set payment contracts according to the requirements of poverty alleviation funds use (such as carrying out medical assistance), and the payment contracts are executed automatically by the digital currency system, and the digital currency can only enter and exit the fund channel limited by the rules of use specified in the payment contracts The civil affairs department can independently control the use of subsequent poverty alleviation funds to achieve dedicated and precise control of poverty alleviation funds More complex payment contracts can also be developed to achieve more precise use of funds, such as limiting the usage rules to specific groups of people (those eligible for poverty relief), specific industries (can only be used in hospitals designated for medical aid), and specific rules (whether the payment settlement list meets the conditions for medical aid) ⇒ It shows the concept of programmable money CBDC (Central Bank Digital Currency) is feared by some for this reason Because people’s consumption behaviors can be controlled by the central bank (≃ government) Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.14/44
in a broad sense Smart contracts and vending machines What is a contract? Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.16/44
system for automatically transferring and transitioning digitally represented assets according to predetermined rules Or more mechanically, To write program code and data to a blockchain, Or to a ledger that can pass the last will test (censorship-resistent in a broad sense) And run the program blockchain-wise to update data Run when miners/validators verify transactions They do not agree with the feeling of the word But there is a meaning to this narrow sense Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.17/44
for implementing contracts mechanically Or, contracts implemented as such ↑ Definition in future dictionaries A vending machine is a primitive form of smart contracts Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.18/44
written or spoken agreement, especially one concerning employment, sales, or tenancy, that is intended to be enforceable by law ⇒ Well, code may be law Essential requirement for its establishment Coherence of manifestation of intention between the parties And the medium for that, Oral, handshake, carving/baking on clay tablets and breaking them, paper (handwriting), paper (print) What can a machine do? cf. “The medium is the message.” by McLuhan cf. “Gutenberg Galaxy” by McLuhan Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.19/44
(meaningless concept unless texts are copied word for word without difference) Scientific methodology (publish papers and have results tested) Nationalism (one standard language, one country) Prepared “movies” (series of photos from one viewpoint) Clear distinction between finished and unfinished “products” (printed copies are the products, handwritten manuscripts are not) Individualism (can carry knowledge personally) Unification of popular culture (massive copies) . . . ⇒ Prepared the industrial society Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.20/44
of “authors” (social media time lines are like mosaic media) Overturned scientific methodology (automated hypothesis-testing loops for discoveries) Global perspective (connected across borders) Everyone becomes a moviemaker (YouTubers, TikTokers) Obscured finished and unfinished products ⇒ open design Promotion of collaboration (sharing by default) Diversification of cultural events (long tail) . . . ⇒ Preparing for the next society Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.21/44
GENERATION SMART CONTRACT & DECENTRALIZED APPLICATION PLATFORM” Applying blockchain technology Block interval : at most 1 block in a 12-second time slot Put a programming language on it Turing complete = can emulate a universal Turing machine (well that’s almost a definition of a programming language) Foundation for DApps (applications that automate the center) An attempt to make the current financial and monetary system obsolete That’s what smart contracts are all about Automate digital asset transfers and their state transitions Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.23/44
DPOUSBDUDPEF NFTTBHFPSBOFXBVUPOPNPVTPCKFDU EBUBFYDIBOHFEBNPOHBDDPVOUTPS&UIFS TUPSBHF TUBUF` TFUPG USBOTBDUJPOT EJHJUBMTJHOBUVSF &7. IVNBO GPSFYBNQMF Blockchain = Run of a state machine (state transition system) = Operation of a computer Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.24/44
External Actor A real person/entity who can digitally sign, having an account EOA : Externally-Owned Account Autonomous Object (internal actor) Operates autonomously within the system, and has an account That said, if you don’t send a message, it won’t work (and validators run them) Account Has an Ether balance, and can have storage (state) and EVM code EVM Code Smart contract program Smart contract = application program on Ethereum = intelligent contract Authenticity is ensured by being written in blockchain Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.25/44
JOWPLF DPOUSBDUDPEF NFTTBHFPSBOFXBVUPOPNPVTPCKFDU EBUBFYDIBOHFEBNPOHBDDPVOUTPS&UIFS TUPSBHF TUBUF` TFUPG USBOTBDUJPOT EJHJUBMTJHOBUVSF &7. IVNBO GPSFYBNQMF BVUPOPNPVT PCKFDU Triggered when an autonomous object receives a message, performs a contract, and changes state Gas (computing resource) must be bought (to avoid infinite loops and compensate the block proposer) (mostly burnt) Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.26/44
or interpreters) EVM is a stack machine (stack calculator) (see Lecture 6) Programmers don’t usually program in bytecode or machine languages Requires compilers for other, high-level languages High-level language : human-readable/writable language On the other hand, languages close to machines are called low-level languages . . . We may be scolded by artificial intelligence in the near future ;) Examples : Solidity — JavaScript-like language Current primary language Vyper — Python-like language LLL — Lisp-like language Fe — Rust-like features with reference to Vyper? ← NEW! Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.28/44
to, uint256 value) public returns (bool) { balances[msg.sender] = balances[msg.sender] - value; balances[to] = balances[to] + value; return true; } . . . This can be the core of a (not-so-secure) token contract Without considering overflow/underflow A token contract typically manages the balances of all users Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.30/44
as a template (type or class) Constructor is called during deployment Deploy here means to deploy the contract to the blockchain Decides what parameters to pass to the constructor upon deployment Conforms to Ethereum programming model Deployed contract is a specific entity (instance) Has an account (identified by the address, just like a human user) Has storage and ETH balance Can send messages to other contracts Model where deployed contracts are manipulated by sending messages On the assumption that the authenticity of the code that responds to the message is guaranteed Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.31/44
validation process, and reflect the results in the “world state” Redundant verifiers Closed within the state of blockchain (maintained by the verifiers) This design is consistent in the Ethereum system Challenges I/O commands cannot be issued from within the program Not affected by the outside world other than external actors who send signed messages ⇒ ex. Data from sensors cannot be read directly (problem of an oracle) Not able to directly affect the outside world ⇒ ex. Cannot send commands to the motor to turn it on/off Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.32/44
and Legal Affairs Bureau (or judicial scrivener) Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.33/44
SFUVSOMBOE 1VSDIBTF$POUSBDU -BOE"TTFU MBOE EFQPTJU USBOTGFSMBOESJHIUTUPCVZFS USBOTGFSQBZNFOUUPTFMMFS QBZNFOU EFQPTJU %JHJUBM5PLFO DSFBUFBOEpYJOUIFBJS FJUIFSDBOEPUIJT DSFBUFBOEpYJOUIFBJS DBOGSFFMZKPJOBOEMFBWF DBOGSFFMZKPJOBOEMFBWF 4FMMFS #VZFS JOUFSOBM TUBUF SFUVSONPOFZ TFUUMF USBOTGFS JOUFSOBM TUBUF USBOTGFS JOUFSOBM TUBUF 1. Purchase contract is fixed in the air to prevent taking away of land or money (both parties can verify the contract) 2. Deposit land rights and purchase money in the contract (if they change their minds, they can take them back) 3. When settled (anyone can do it if both right and money are deposited), the rights and money for the property are transferred simultaneously Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.34/44
framework for smart contracts on EVM https://eth-brownie.readthedocs.io/en/stable/ Today, we will use the Python interpreter environment it provides And infura.io that provides accesses to Ethereum (main and test) networks https://infura.io Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.35/44
clone https://github.com/ks91/sample-smart-contracts.git Follow README 1. Setup a sample token project with brownie Brownie is a Python-based Ethereum programming and test environment Javascript-based one is called Truffle, and an Ethereum remote procedure call client is called Ganache See? Engineers are fun people to work with! 2. Copy the content of contracts, scripts and tests directories of this sample project into the corresponding directories of the token project 3. Compile 4. Test Today, I will demonstrate manually the test for automated escrow Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.36/44
>= _price_); /* ’this’ means this contract */ require(_asset_.getOwner() == address(this)); _token_.transfer(_seller_ , _price_); _asset_.transfer(_buyer_); emit Settled(); /* event log */ return true; } Just to introduce the settle() part from the sample code Only if both the price and the asset are deposited with the contract, It transfers money to the seller and asset to the buyer Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.37/44
console >>> network.disconnect() # disconnect from the default, simulated environment of Ethereum >>> network.connect(’goerli’) >>> len(accounts) 0 >>> accounts.add(paste the hexadecimal string of the private key here) <LocalAccount object ’hexadecimal string of the account’> >>> accounts[0].balace() Here is the ETH balance of the account We set up two accounts Once you have created multiple accounts, you can try to transfer ETH, for example as follows >>> accounts[0].transfer(accounts[1], "1 ether") But before that, you may want to feed the accounts with Göerli ETH from some public faucet Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.38/44
test_deploy_and_settle(Token, IndivisibleAsset, OneTimeEscrow): asset = accounts[0].deploy(IndivisibleAsset, "5322 Endo", "mˆ2", 300) token = accounts[0].deploy(Token, "Test Token", "TEST", 18, "1000 ether") Deploying token and asset contracts The above is just the test code as it is, so in the console we will just type the inside of the function First, let’s do seller = accounts[0] and buyer = accounts[1] for readability Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.39/44
token, accounts[1], asset, accounts[0], 300) Sending 300 coins from seller (accounts[0]) to buyer (accounts[1]) to prepare Because the buyer has to cast a transaction that pays 300 coins Deploying Escrow Contracts Buyer is accounts[1], seller is accounts[0], and the price is 300 coins Since we are using the sample arguments of the baked Token as they are, 300 coins is actually a very small unit, like wei in ETH Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.40/44
assert token.balanceOf(accounts[0]) == 999999999999999999700 assert token.balanceOf(accounts[1]) == 0 assert token.balanceOf(escrow) == 300 assert asset.getOwner() == escrow Buyer (accounts[1]) deposits 300 coins in escrow, and Seller (accounts[0]) deposits the asset in escrow assert tests a statement, and fails the test if it turns out to be false But we are in console (not doing an automated test), so we just see the values Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.41/44
token.balanceOf(accounts[1]) == 0 assert token.balanceOf(escrow) == 0 assert asset.getOwner() == accounts[1] Calling settle() and closing the transaction In this code, it is called by seller (accounts[0]), but it is OK to call from either side Let’s see some transactions on an Ethereum Göerli testnet explorer Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.42/44
contract execution as a verifier ⇒ Everyone has access to the contract code It is in principle verifiable that the correct contract is being executed I/O is outside blockchain → this is possible without revealing the entirety of the agreement True worth of smart contract is that everyone can confirm that “the correct contract was executed”? Program code and its execution results as “record whose contents and existence cannot be denied by anyone” But Ethereum’s method should not be the only one There are many issues including how to manage the contracts’ life cycles We have already talked about The DAO Incident Lecture 10 : Smart Contracts — FinTech — Financial Innovation and the Internet 2022 Fall — 2022-12-09 – p.43/44