updates to smart contract content
|
@ -4,15 +4,13 @@
|
|||
|
||||
A smart contract is a program stored inside of a blockchain. Smart contracts extend blockchain from data to code. They represent an agreement between parties. The agreement is coded and when an action happens, the code is executed and provides a response.
|
||||
|
||||
All of the terms and conditions of the contracts are programmatically defined. The definition specifies the rules, requirements and rewards for those who participate in the blockchain, and how digital assets are transferred between parties. The code is assigned an address (just like your wallet has an address, addresses look like: 0x8E698aeA7d456d4A8D8d0485BDC390EFCebC56FA)
|
||||
All of the terms and conditions of the contracts are programmatically defined. The definition specifies the rules, requirements and rewards for those who participate in the blockchain, and how digital assets are transferred between parties. Each smart contract is assigned a 20 byte address that is used to uniquely identify it.
|
||||
|
||||
They execute on their own, send events, call functions and more. They are perfect for blockchain technology because they allow parties that do not know one another to conduct business in a securely specified way without the use of a middleman.
|
||||
|
||||
The most prominent use is with Ethereum, the worlds first programmable blockchain that allows for smart contracts to be defined to help facilitate the transfer of digital assets like Ether.
|
||||
The most prominent use for smart contracts is with Ethereum, the worlds first programmable blockchain, that allows for smart contracts to be defined to help facilitate the transfer of digital assets like Ether.
|
||||
|
||||
They can also generate their own currency and tokens to transfer. And the use cases are expanding well beyond cryptocurrency.
|
||||
|
||||
The language to write contracts in is Solidity. Smart contracts are Turing complete which means you can write complicated contracts that are defined and coded in a clear way.
|
||||
The language to write contracts in is Solidity. Solidity is Turing complete which means you can write complicated contracts that are defined and coded in a clear way.
|
||||
|
||||
Since every state transition is logged and immutable it is imperative that you thoroughly test the contract before going to production, because bug fixes can be very costly and even cause critical damage to the system.
|
||||
|
||||
|
@ -20,7 +18,7 @@ The key properties and advantages of smart contracts are that they are:
|
|||
|
||||
- **Transparent**: It is publicly readable by others on the blockchain and accessible via APIs
|
||||
- **Immutable**: Once a smart contract is created it cannot be changed again.
|
||||
- **Distributed**: Output of the contract is validated / verified by nodes on the network. Contract states can be publicly visible, and in some cases even “private” variables are visible.
|
||||
- **Distributed**: Output of the contract is validated and verified by nodes on the network. Contract states can be publicly visible, and in some cases even “private” variables are visible.
|
||||
|
||||
## Use cases
|
||||
|
||||
|
|
|
@ -4,55 +4,81 @@
|
|||
|
||||
[Blockchain Development Kit for Ethereum](https://marketplace.visualstudio.com/items?itemName=AzBlockchain.azure-blockchain) is an extension you can use in Visual Studio Code. The extension makes it easy to create, build, and deploy smart contracts on Ethereum ledgers.
|
||||
|
||||
Simply go to extensions and search for Blockchain Development Kit and install it. Make sure you have the correct system requirements before installing it.
|
||||
Simply go to **Extensions** and search for **Blockchain Development Kit** and install it.
|
||||
|
||||
![install image](../media/bdk-install.png)
|
||||
:::image type="content" source="media\bdk-install.png" alt-text="Install Blockchain Development Kit":::
|
||||
|
||||
## Dependencies to install
|
||||
|
||||
- Python
|
||||
- Node
|
||||
- Git
|
||||
To use the Blockchain Development Kit, you will need to make sure you have the following installed:
|
||||
|
||||
### Python
|
||||
|
||||
- Need version 2.7.15 or greater
|
||||
- Please note, you must have Python installed in your `PATH` variable.
|
||||
- Note: Most computers will come with Python pre-installed, however it might not be up-to-date. To check if you do have Python installed, open up a terminal and type `python`
|
||||
- If installed, it will print out the version of Python that you have on your computer
|
||||
- To install Python, visit[the Python downloads page](https://www.python.org/downloads/) and click on the **Download Python X.X.X** button. Note that the image shows the download option for a Mac computer. If you have a Linux or Windows computer you will be presented with that download option.
|
||||
|
||||
:::image type="content" source="media\python-download.png" alt-text="Download Python":::
|
||||
|
||||
- Follow the prompts to download Python, and make sure to select the option to **Add Python to PATH** if that's presented to you.
|
||||
|
||||
### Node
|
||||
|
||||
- Need version 10.15.x or greater
|
||||
- To install Node, visit the [Node.js downloads page](https://nodejs.org/download/). Select the installer for your platform
|
||||
|
||||
:::image type="content" source="media\node-download.png" alt-text="Download Node":::
|
||||
|
||||
- To confirm Node.js is installed, open your terminal and type: `node`. If installed successfully, the terminal will return the version of Node.js installed. You can also confirm that npm is installed by typing `npm` in the terminal.
|
||||
|
||||
### Git
|
||||
|
||||
- Need version 2.10.x or greater
|
||||
- Visit the [Git downloads page](https://git-scm.com/downloads) and select your platform. Follow the instructions to download Git.
|
||||
|
||||
:::image type="content" source="media\git-download.png" alt-text="Download Git":::
|
||||
|
||||
- Note that the image shows the download option for a Mac computer. If you have a Linux or Windows computer you will be presented with that download option.
|
||||
- Once installed, open your terminal and type `git`. If installed successfully, the terminal will return a list of git commands available.
|
||||
|
||||
## Create a New Solidity Project with the extension
|
||||
|
||||
1. Add a new empty folder to your computer. `mkdir myproj`
|
||||
Open Visual Studio Code.
|
||||
2. Go to View -> Command Palette, or use ctrl+shift+P. In the search box type: Blockchain New Solidity Project
|
||||
3. Use the UI file explorer pop up to find the folder you created in step1
|
||||
4. If you have all the requirements needed you should be able to choose: **Create basic project**
|
||||
Once you have all the dependencies installed, you can now use the Blockchain Development Kit to create your first project.
|
||||
|
||||
1. Start by adding a new empty directory to your computer for the project. Note: You can open your terminal and type `mkdir newSolidityProject` to create a new directory. Keep note of where this new empty directory is, but you don't need it yet.
|
||||
2. Open Visual Studio Code.
|
||||
3. Go to View -> Command Palette. In the search box type: `Blockchain: New Solidity Project`. You'll notice that as you begin to type "Blockchain" a list of commands will be presented.
|
||||
|
||||
:::image type="content" source="media\new-solidity-project.png" alt-text="New Solidity project":::
|
||||
|
||||
1. For the type of solidity project, select **Create basic project**.
|
||||
1. Use the UI file explorer pop up to find the folder you created in step 1. Select the folder and in the bottom right-hand of the window you'll see **Project creating...**
|
||||
1. Sit back and wait for your new Solidity project to be created.
|
||||
|
||||
Once it is, open the Explorer and take a look at all the files that were created in the project.
|
||||
|
||||
In this project, you will have a boiler plate of Solidity code that you can use. Notice that you have directories for:
|
||||
|
||||
- **contracts:** Contains the **HelloBlockchain.sol** and **Migrations.sol** contracts
|
||||
- **migrations:** Contains an initial migration and a deploy contract.
|
||||
- **test:** Contains a test for the HelloBlockchain contract written in JavaScript
|
||||
|
||||
You'll also have some other config files:
|
||||
|
||||
- **package.json:** To define project details and dependencies
|
||||
- **truffle-config.json:**. To define dependencies and configuration for Truffle
|
||||
|
||||
For now we will only focus on the **contracts/** directory.
|
||||
|
||||
## Compile the project
|
||||
|
||||
We will start by using the **HelloBlockchain.sol** smart contract inside of the contracts folder.
|
||||
|
||||
1. Go to contracts/HelloBlockchain.sol
|
||||
Right click HelloBlockchain.sol
|
||||
2. Click on Build Contracts to compile the smart contract
|
||||
In Output you can see information about the compiled contract
|
||||
2. Click on **Build Contracts** to compile the smart contract. Note that you might need to change the pragma directive at the top to use `solidity ^0.7.0;`
|
||||
3. Select View -> Output to see information about the compiled contract
|
||||
|
||||
## Deploy the contract locally
|
||||
|
||||
1. Go to contracts/HelloBlockchain.sol
|
||||
2. Right click HelloBlockchain.sol
|
||||
3. Click on Deploy Contracts and select local
|
||||
4. In the output you can see information about the deployed contract
|
||||
|
||||
```solidity
|
||||
contract address: 0x11c89ee4Da94151fEB1700444DE8d95F1c7E5C30
|
||||
> block number: 3
|
||||
> block timestamp: 1599552199
|
||||
> account: 0x962562e7b71529119f2fd5daeCCFcaCa98A9fC17
|
||||
> balance: 99.9845291
|
||||
> gas used: 505945 (0x7b859)
|
||||
> gas price: 20 gwei
|
||||
> value sent: 0 ETH
|
||||
> total cost: 0.0101189 ETH
|
||||
```
|
||||
|
||||
|
||||
|
||||
Here you see some key information / meta data about the contract you just deployed:
|
||||
The address of the contract (for you to interact with using a API or a frontend using web3.js … stay tuned for Module2)
|
||||
Time stamp of the block that the contract creation tx was a part of
|
||||
The account that deployed the contract
|
||||
This contract deployment had no msg.value (value sent = 0)
|
||||
Gas related settings / data and cost in Eth
|
||||
:::image type="content" source="media\build-contracts.png" alt-text="Build contracts":::
|
||||
|
|
|
@ -6,27 +6,13 @@ Truffle is the most popular development framework for Ethereum. It can easily be
|
|||
|
||||
With Truffle, you get:
|
||||
|
||||
- Built-in smart contract compilation, linking, deployment and binary management.
|
||||
- Automated contract testing for rapid development.
|
||||
Scriptable, extensible deployment & migrations framework.
|
||||
- Built-in smart contract building, compilation, deployment, testing and more.
|
||||
- Network management for deploying to any number of public & private networks.
|
||||
- Package management with EthPM & NPM, using the ERC190 standard.
|
||||
- Package management for dependencies
|
||||
- Interactive console for direct contract communication.
|
||||
Configurable build pipeline with support for tight integration.
|
||||
- Configurable build pipeline with support for tight integration.
|
||||
- External script runner that executes scripts within a Truffle environment.
|
||||
|
||||
## Confirm that Node.js is installed
|
||||
|
||||
Truffle requires that you have Node.js v8.9.4 or greater. With Node.js, you automatically get the Node package manager, referred to as npm, installed on your computer.
|
||||
|
||||
To check if you have Node.js installed, run this command in your terminal:
|
||||
|
||||
`node -v`
|
||||
|
||||
To confirm that you have npm installed you can run this command in your terminal:
|
||||
|
||||
`npm -v`
|
||||
|
||||
## Install Truffle
|
||||
|
||||
From your terminal, you can install Truffle with the help of npm. Type:
|
||||
|
|
|
@ -1 +1,48 @@
|
|||
# Exercise: Write a smart contract with the Blockchain Development Kit for Ethereum
|
||||
|
||||
In the project you created earlier, under the **contracts/** directory, select to create a new file called **Shipping.sol**.
|
||||
|
||||
Copy over the contents of this contract below:
|
||||
|
||||
```solidity
|
||||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Shipping
|
||||
{
|
||||
// Our predefined values for shipping listed as enums
|
||||
enum ShippingStatus { Pending, Shipped, Delivered }
|
||||
ShippingStatus public status;
|
||||
|
||||
// This initializes our contract state (sets enum to Pending once the program starts)
|
||||
constructor() public {
|
||||
status = ShippingStatus.Pending;
|
||||
}
|
||||
|
||||
// Function to change to Shipped
|
||||
function Shipped() public {
|
||||
status = ShippingStatus.Shipped;
|
||||
}
|
||||
|
||||
// Function to change to Delivered
|
||||
function Delivered() public {
|
||||
status = ShippingStatus.Delivered;
|
||||
}
|
||||
|
||||
// Function to get the status of the shipping
|
||||
function getStatus(ShippingStatus _status) internal pure returns (string memory) {
|
||||
|
||||
// Check the current status and return the correct name
|
||||
if (ShippingStatus.Pending == _status) return "Pending";
|
||||
if (ShippingStatus.Shipped == _status) return "Shipped";
|
||||
if (ShippingStatus.Delivered == _status) return "Delivered";
|
||||
|
||||
}
|
||||
|
||||
// Get status of your shipped item
|
||||
function Status() public view returns (string memory) {
|
||||
ShippingStatus _status = status;
|
||||
return getStatus(_status);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
|
Двоичные данные
ethereum-blockchain-dev/smart-contracts/media/bdk-install.png
До Ширина: | Высота: | Размер: 531 KiB После Ширина: | Высота: | Размер: 487 KiB |
После Ширина: | Высота: | Размер: 513 KiB |
После Ширина: | Высота: | Размер: 259 KiB |
После Ширина: | Высота: | Размер: 75 KiB |
После Ширина: | Высота: | Размер: 112 KiB |
После Ширина: | Высота: | Размер: 98 KiB |
После Ширина: | Высота: | Размер: 276 KiB |