- [15m] ☀️ Warm Up: DApp Developer Roadmap
- [30m] 💬 TT: Introduction to Solidity
- [10m] 🌴 BREAK
- [30m] 💻 Demo: Danicoin
- [15m] 🔄 Introduce Homework: Crypto Pet Shop
- Review Solidity syntax with examples of two contracts.
- Learn how to mint a sample token similar to Ether or Dogecoin.
- Introduce the Truffle Pet Shop tutorial.
Review the DApps developer roadmap with students.
Ask students to identify where we are as a class on the roadmap.What are the technologies we've learned so far? Circle each as students call them out, and use the remaining time to discuss and respond to student's answers.
- Programming language for writing smart contracts on the Ethereum blockchain
- Contract-oriented language
- Runs on the Ethereum Virtual Machine (EVM)
- Ethereum Nodes run the EVM
- These nodes are connected to the Ethereum blockchain
- Statically typed
- Supports inheritance
- Can use third party libraries and inherit from external contracts
- Generally considered the best way to develop for the Ethereum blockchain
- The backend of an Ethereum app (
dApp= Distributed App) - Where all the logic lives
- Files end in
.sol - Allow us to execute code on the Ethereum blockchain
- Think of it like a microservice or mini API out on the web
- Anyone can see the code for our smart contracts
- Anyone connected to the network can call functions on our smart contracts
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
Let's break it down line by line!
- Line 01: The first line tells us that the source code is written for Solidity version
0.4.16, or a newer version of the language up to, but not including version0.9.0.- The
pragmakeyword is used to enable certain compiler features or checks. - The first line is an example of a version pragma.
- You have to add the pragma to all your files if you want to enable it in your whole project.
- Question: Why do you think specifying a minimum and maximum version might be important?
- The
- Line 03: Defines a Smart Contract --- a collection of code (functions) and data (state) that resides at a specific address on the Ethereum blockchain.
- Line 04: Delares a state variable called
storedDataof typeuint(unsigned integer of 256 bits)- Like a single slot in a database you can query / alter by calling functions of the code that manages the database
- Lines 06 - 12: The contract defines the functions
setandgetthat can be used to modify or retrieve the value of thestoredDatavariable.- Question: If this contract existed on the blockchain, who would be able to call the
setfunction?
- Question: If this contract existed on the blockchain, who would be able to call the
The following contract demonstrates how to develop a simple ERC-20 token contract similar to Ethereum, Dogecoin, etc. There's some new stuff in here that we haven't talked about yet, so let's break this one down line by line, too!
contract Coin {
// The keyword "public" makes variables
// accessible from other contracts
address public minter;
mapping (address => uint) public balances;
// Events allow clients to react to specific
// contract changes you declare
event Sent(address from, address to, uint amount);
// Constructor code is only run when the contract
// is created
constructor() {
minter = msg.sender;
}
// Sends an amount of newly created coins to an address
// Can only be called by the contract creator
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
// Errors allow you to provide information about
// why an operation failed. They are returned
// to the caller of the function.
error InsufficientBalance(uint requested, uint available);
// Sends an amount of existing coins
// from any caller to an address
function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert InsufficientBalance({
requested: amount,
available: balances[msg.sender]
});
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
Let's learn how to develop these contracts on our own machines using the Crypto Pet Shop Tutorial!
Our asyncronous classwork this week will have you complete another tutorial that gets you familiar with the tools and framework we'll use in the remainder of the course.
The instructor should walk through the tutorial step by step and answer any questions that may arise.
We'll be learning lots more about truffle next class period, and continue working our way through the roadmap!
