This commit is contained in:
meaghanlewis 2020-09-14 16:36:22 -07:00
Родитель 377c76d948
Коммит 0d469a33c6
6 изменённых файлов: 23 добавлений и 77 удалений

Просмотреть файл

@ -14,4 +14,4 @@ Through these modules, you will:
- Previous experience with any programming language like C, Python, or JavaScript
- Basic knowledge of programming concepts
- Familiarity with the command line to create new directories
- Visual Studio Code installed
- Visual Studio Code installed

Просмотреть файл

@ -5,8 +5,9 @@ Learning Path Title:
Module Titles:
1. Introduction to blockchain on Azure
2.
3.
4.
5.
6.
2. Learn how to use Solidity
3. Use Solidity to write Ethereum smart contracts
4. Create a bank smart contract with exchange rate
5. Create a decentralized application
6. Learn how to deploy to Ethereum networks

Просмотреть файл

@ -42,7 +42,7 @@ For all variable definitions you must specify the type as well as the variable n
Additionally, you can specify the visibility of a state variable as:
- **public:** part of the contract interface
- **public:** part of the contract interface and can be accessed from other contracts
- **internal:** only accessed internally from the current contract
- **private:** only visible for the contract they are defined in

Просмотреть файл

@ -4,10 +4,10 @@ In this unit you'll learn about the main value types in Solidity. They are calle
## Integers
Integers are used in every Solidity source file. They represent whole numbers and can either be signed or unsigned and range from 8 up to 256 bits.
Integers are used in every Solidity source file. They represent whole numbers and can either be signed or unsigned and range from 8 up to 256 bits that they can store.
- Signed: Include negative and positive numbers. Can represent as `int8`
- Unsigned: Includes positive numbers only. Can represent as `uint256`.
- Signed: Include negative and positive numbers. Can represent as **int**
- Unsigned: Includes positive numbers only. Can represent as **uint**
If a number of bits is not specified, the default value is 256 bits.
@ -30,7 +30,7 @@ price % 2; // 0
## Booleans
Booleans are defined using the keyword `bool` and always have the values **true** or **false**.
Booleans are defined using the keyword **bool** and always have the values `true` or `false`.
They can be defined as:

Просмотреть файл

@ -8,9 +8,9 @@ When using a reference type, you must explicitly provide the data area where the
Every reference type specifies a data location to where the data is stored. The three options for specifying the data area where the type is stored are:
- **memory** the type has a lifetime limited to external function call
- **storage** the type location where state variables are stored
- **calldata** the special data location that contains the function arguments
- **memory:** the type has a lifetime limited to external function call
- **storage:** the type location where state variables are stored
- **calldata:** the special data location that contains the function arguments
They always create an independent copy.
@ -36,7 +36,7 @@ contract C {
## Arrays
Arrays are a way to store similar data in a set data structure. Arrays in Solidity are similar to arrays in other programming languages. They can either have a fixed or dynamic size. Their indices start at 0.
Array elements can be of any type like **uint**, **memory**, or **bytes**, and can also include mappings or structs.
Array elements can be of any type like **uint**, **memory**, or **bytes**, and can also include **mappings** or **structs**.
Examples:
@ -61,12 +61,13 @@ Examples:
```solidity
// Create a dynamic byte array
bytes32[] itemNames;
itemNames.push(bytes32("computer"));
itemNames.push(bytes32("computer")); // adds "computer" to the array
itemNames.length; // 1
```
## Structs
Structs are custom types that a user can define to represent real world objects. These are typically used as schema or represent records.
Structs are custom types that a user can define to represent real world objects. These are typically used as schema or represent records.
Examples:
@ -82,11 +83,11 @@ struct Items_Schema {
## Mapping types
Mappings are key value pairs that are encapsulated (packaged together) ; these are closest to dictionaries or Objects in JavaScript. We typically use mapping to model real world objects and do faster lookups. The values could take on various types (including complex types like structs) making this flexible and human friendly to work with (i.e you can access values by mapping_instance.key).
Mappings are key value pairs that are encapsulated, or packaged together. These are closest to dictionaries or Objects in JavaScript. We typically use mappings to model real-world objects and do faster lookups of data. The values could take on various types, including complex types like structs, making this type flexible and human readable to work with.
Here is a smart contract that uses the struct Items_Schema and saves a list of items represented by the Items_Schema as a dictionary. This somewhat mimics a database.
Here is a code example that uses the struct Items_Schema and saves a list of items represented by the Items_Schema as a dictionary. This somewhat mimics a database.
Notice the mapping signature `uint256 => Items_Schema` => this indicates that the keys are of type unsigned integer and the values are Items_Schema struct.
Notice the mapping signature `uint256 => Items_Schema`. This indicates that the keys are of type unsigned integer and the values are Items_Schema struct.
```solidity
contract Items {

Просмотреть файл

@ -39,7 +39,7 @@ contract Marketplace {
}
```
I'll explain the main components of this smart contract:
Let's dig into the main components of this smart contract:
- There are 3 state variables: buyer, seller, and balances
- There are 2 events: `ListItem` and `PurchasedItem`
@ -47,62 +47,6 @@ I'll explain the main components of this smart contract:
- The constructor will assign the seller user as msg.sender, and set the initial state to ItemAvailable. This constructor is called when the contract is created.
- The `buy` function takes 3 parameters: `seller`, `buyer`, and `price`. It has a requirement that the buyer has enough money for the purchase. Then it transfers money from the buyer to the seller, and finally a message is emitted.
## More complex marketplace example
```solidity
pragma solidity >0.7.0 <0.8.0;
contract Marketplace {
address public seller;
address public buyer;
mapping (address => uint) public balances;
event ListItem(address seller, uint price);
event PurchasedItem(address seller, address buyer, uint price);
enum StateType {
ItemAvailable,
ItemBought
}
StateType public State;
constructor() public {
seller = msg.sender;
State = StateType.ItemAvailable;
}
function buy(address seller, address buyer, uint price) public {
require(price <= balances[sender], "Insufficient balance");
State = StateType.ItemBought;
balances[buyer] -= price;
balances[seller] += price;
emit PurchasedItem(msg.sender, buyer, msg.value);
}
modifier onlySeller() {
require(
msg.sender == seller,
"Only seller can put an item up for sale."
);
_;
}
struct Items_Schema {
uint256 _id:
uint256 _price:
string _name;
}
function listItem(uint 256 memory _price, string memory _name) public {
item_id += 1;
item[vehicle_id] = Items_Schema(item_id, _price, _name);
emit ListItem(address seller, uint price);
}
```
## Challenge
Go to [Remix IDE](https://remix.ethereum.org/) to explore more smart contract examples in Solidity. Remix is an in-browser IDE that is instant to get started without having to create an account or sign in. You can immediately write, test, compile, and deploy contracts.