Blockchain application development (use the application development scaffolding SmartDev to quickly start an application)

5. Use the application development scaffolding SmartDev to quickly start an application

1. Experiment overview

In the experiment in the previous section, we completed the development of the smart contract, but the complete blockchain application development, in addition to the smart contract development, background development is also indispensable.

Background development also includes steps such as project establishment, dependency introduction, configuration code writing, model class writing, service class writing, and business logic writing. These steps are relatively cumbersome, which affects the efficiency of development. For example, we need to write transaction construction and push codes for each contract function; even, we need to write corresponding model classes for the input parameters of each contract function.

Analyzing the above process, there is a certain regularity in the above steps, which can reduce the repetitive workload through scaffolding and help developers develop smart contract applications quickly and agilely. FISCO BCOS provides smart contract scaffolding SmartDev for one-click generation of DAPP application development projects, thereby reducing the difficulty of application development. Users import their contracts into the scaffolding to generate corresponding application development template projects, including corresponding POJO classes, service classes, etc. Users can directly develop dapp web projects based on this.

This experiment will use the smart contract scaffolding SmartDev to generate DAPP application development projects with one click, thereby reducing the difficulty of application development. It mainly includes the download, configuration and operation of SmartDev and the development process of DAPP.

2. Experimental objectives

  1. Understand the working principle of SmartDev application development scaffolding;
  2. Master the download and configuration methods of SmartDev;
  3. Master the operation method of scaffolding.
  4. Master the process of using scaffolding for DAPP development.

3. Experimental environment and suggestions

  1. Familiar with Java development language;
  2. Understand certain basic knowledge of front-end and back-end;
  3. Pre-dependence, before using this component, please confirm that the relevant dependent software has been installed, the list is as follows:
dependent software illustrate Remark
Java >= JDK[1.8] 64bit
Solidity 0.4.25.1 0.5.2.0 0.6.10.0 0.8.11.0
Git Downloading the installation package requires the use of Gi
Gradle greater than 6 less than 7 Using gradle7 will report an error
Maven Choose one with Gradle If you want to generate a maven project, you need

4. Experimental steps

4.1 Download scaffolding

According to the requirements of the experimental environment, before using the scaffolding, you need to download the dependent tool gradle, and the version is preferably v6.3;

cd ~/fisco/ && wget https://services.gradle.org/distributions/gradle-6.3-bin.zip

Unzip to the current folder;

unzip -d ./ gradle-6.3-bin.zip

Configure environment variables;

vi /etc/profile

Click i to enter insert mode and write the following at the end of the file:

export PATH=$PATH:/root/fisco/gradle-6.3/bin

Save and exit, and enter the following command to make the configuration take effect.

source /etc/profile && gradle -v

Then download the scaffolding from github SmartDev-Scaffold:

cd ~/fisco/ && curl -LO https://github.com/WeBankBlockchain/SmartDev-Scaffold/releases/download/V1.0.1/SmartDev-Scaffold-V1_0_1.zip

After the download is successful, decompress the compressed package manually or with the command line:

unzip SmartDev-Scaffold-V1_0_1.zip

annotation

If you cannot download for a long time due to network problems, please try: git clone https://gitee.com/WeBankBlockchain/SmartDev-Scaffold.git

Enter the directory:

cd SmartDev-Scaffold/tools/
tree ./

Install without treecommand usingyum install tree -y

The tools directory contains the execution environment, and its structure is:

├── tools
│   ├── contracts
│   ├──|── HelloWorld.sol
│   ├── config.ini
│   ├── run.sh
│   ├── run.bat

in:

  • The contracts directory is used to store solidity contract files, and the scaffolding will subsequently read the contracts in this directory to generate corresponding business projects. Please delete the default contract in this directory, and copy your own business contract to this directory.
  • config.ini is the startup related configuration.
  • run.sh and run.bat are startup scripts under unix and windows respectively.

4.2 Configure scaffolding

Contract configuration

This experiment takes the default contract HelloWorld.solas an example to introduce.

Note: In the actual development process, please delete the default contract in the contracts directory HelloWorld.sol, and copy your own business contract to this directory.

build configuration

The generation configuration can be done in config.ini as follows. In the actual development, appropriately modify the name as needed

cd ~/fisco/SmartDev-Scaffold/tools && vi config.ini
# 指定项目的名称
artifact=demo
# 组名称
group=org.example
### 所支持的合约列表,默认为空表示选择所有合约
selector=
# solidity编译器版本,可选0.4.25.1, 0.5.2.0, 0.6.10.0, 0.8.11.0
compiler=0.4.25.1
# 工程生成类型,可以设置为gradle或maven
type=gradle
# gradle版本,支持5.6.1、gradle 6各版本。暂不支持gradle7。如果您选择了maven项目,系统会自动忽略此选项
gradleVersion=6.3

Note: Regarding the selector, if you need to compile and output only for the specified contract, you can enter the list of required contracts separated by commas (ignored in this experiment). For example, in the following code, there are only two contracts, AccountController and RoleController:

selector=AccountController,RoleController

4.3 Running Scaffolding

Give the script run.sh executable permission, then start the script, and generate the SpringBoot project according to the above configuration;

chmod +x run.sh && bash run.sh

insert image description here

After running successfully, you will get a SpringBoot-based project project in the tools directory:

├─contracts
├─run.sh
├─run.bat
└─demo # 和前面config.ini中定义项目名一致

The specific content of the generated project is as follows (take the gradle project as an example)

cd ~/fisco/SmartDev-Scaffold/tools/demo/ && tree ./
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── settings.gradle
└── src
    ├── main
    │   ├── contracts
    │   │   └── HelloWorld.sol
    │   ├── java
    │   │   └── org
    │   │       └── example
    │   │           └── demo
    │   │               ├── Application.java
    │   │               ├── config
    │   │               │   ├── BcosConfig.java
    │   │               │   ├── ContractConfig.java
    │   │               │   ├── SdkBeanConfig.java
    │   │               │   └── SystemConfig.java
    │   │               ├── constants
    │   │               │   └── ContractConstants.java
    │   │               ├── model
    │   │               │   ├── bo
    │   │               │   │   └── HelloWorldSetInputBO.java
    │   │               │   └── CommonResponse.java
    │   │               └── service
    │   │                   └── HelloWorldService.java
    │   └── resources
    │       ├── abi
    │       │   └── HelloWorld.abi
    │       ├── application.properties
    │       ├── bin
    │       │   ├── ecc
    │       │   │   └── HelloWorld.bin
    │       │   └── sm
    │       │       └── HelloWorld.bin
    │       └── conf
    └── test
        └── java
            └── org
                └── example
                    └── demo
                        └── Demos.java

in:

  • The config directory contains Bean configuration classes
  • The service directory contains the Service class accessed by the smart contract, and a class corresponds to a contract
  • The bo directory contains the encapsulated POJO class of the input parameters of the contract function
  • The src/main/resource/conf directory is used to store certificate information
  • Demos.java contains sample codes such as private key generation and contract deployment

If a maven project is generated, there will be no gradle-related content, but pom, mvnw, mvnw.cmd and other files will be included.

4.4 DAPP development

Here is an introduction to the DAPP development process, taking the demo project generated earlier as an example.

4.4.1 Deployment contract

Use the console or Webase IDE to deploy the HelloWorld contract. This experiment takes the console as an example;

Enter the ~/fisco/ directory, first start all nodes of the bcos chain, and then start the console;

cd ~/fisco/
bash nodes/127.0.0.1/start_all.sh 
cp -n console/conf/config-example.toml console/conf/config.toml
cp -r nodes/127.0.0.1/sdk/* console/conf/
bash console/start.sh

In actual development, the contract to be deployed needs to be HelloWorld.solcopied to the console directory contracts/solidity/. Since the console already contains the contract by default HelloWorld.sol, this step is omitted in this experiment.

Enter the following command in the console to deploy the contract;

在控制台输入以下指令部署成功则返回合约地址,示例如下,以实际步骤的为准
[group:1]> deploy HelloWorld
transaction hash: 0x01f334001a3bab4aec59451eb45adf969351ef056cf7addf32c4d76db0ceb180
contract address: 0x72b67af62a14a1222471b08d99cc31acd56285fe
currentAccount: 0xea5ae216dd34f5b4bb6e67043c1b38e0aa5aac2d

Record the contract address and user account for future steps. Enter q in the console to exit the console.

4.4.2 Certificate Copy

Copy the configuration file in the bcos node to the conf directory or the src/main/resources/conf directory of the generated project.

cp -r ~/fisco/nodes/127.0.0.1/sdk/* ~/fisco/SmartDev-Scaffold/tools/demo/src/main/resources/conf/
4.4.3 Configure connection nodes

Please modify application.properties, which contains the following information, and fill in the contract address according to the actual situation:

cd ~/fisco/SmartDev-Scaffold/tools/demo/src/main/resources/ && vi application.properties
### Java sdk configuration
cryptoMaterial.certPath=conf
network.peers[0]=127.0.0.1:20200
network.peers[1]=127.0.0.1:20201

### System configuration
system.groupId=1
system.privateKey=

### Contract configuration
contract.helloWorldAddress=0x72b67af62a14a1222471b08d99cc31acd56285fe	# 根据实际情况填写

### Springboot configuration
server.port=8080
server.session.timeout=60
banner.charset=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

in:

  • The java sdk configuration configuration part is consistent with the javasdk configuration.
    • Among them, the user needs to replace network.peers[0]=127.0.0.1:20200 with the actual chain node listening address.
  • System configuration includes group, private key and other configurations.
    • system.hexPrivateKey is the plaintext of the hexadecimal private key. If it is empty, the private key corresponding to the above java sdk configuration will be used. If the above sdk is not configured, a random one will be generated
  • Contract confguration contains contract configuration, and users need to replace it with the previously deployed contract address.
4.4.4 Complementary business

A complete DAPP should contain at least three layers of architecture, and this example completes a Controller. Create a new controller directory under org.example.demo, and then create a HelloController class under this directory, as follows:

cd ~/fisco/SmartDev-Scaffold/tools/demo/src/main/java/org/example/demo/
mkdir controller
vi controller/HelloController.java

Or complete the above steps in the IDEA editor.

Write the following in the HelloController class:

package org.example.demo.controller;

import org.example.demo.model.bo.HelloWorldSetInputBO;
import org.example.demo.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloController {
    
    
    
    @Autowired
    private HelloWorldService service;
    
    @GetMapping("set")
    public String set(@RequestParam("n") String n) throws Exception{
    
    
        HelloWorldSetInputBO input = new HelloWorldSetInputBO(n);
        return service.set(input).getTransactionReceipt().getTransactionHash();    
    }
    
    @GetMapping("get")
    public String get() throws Exception{
    
    
        return service.get().getValues();    
    }
}
4.4.5 Running the jar package

This experiment uses the project generated by Gradle, then execute the following command to generate the Jar package (the generation time is longer, depending on the network speed):

gradle bootJarWhen it is slow, you can add the domestic image in the build.gradle file:

repositories { maven{ url'http://maven.aliyun.com/nexus/content/groups/public/'} }

cd ~/fisco/SmartDev-Scaffold/tools/demo/
gradle bootJar

If you are generating a maven project, execute:

cd ~/fisco/SmartDev-Scaffold/tools/demo/
mvn package
cd target

After the execution is complete, a dist directory is generated in the current directory, and the demo-exec.jar package is generated in the directory. Enter the following command to execute the jar package:

cd ~/fisco/SmartDev-Scaffold/tools/demo/dist/
java -jar demo-exec.jar

Then, you can type in your browser:

http://127.0.0.1:8080/hello/set?n=hello

Return example:

0xb66179f2e5d8edefa638fcaf234061260583a32cfef5dae63f74998827afc9d0
http://127.0.0.1:8080/hello/get

Return example:

["hello"]

5. Practice tasks

  1. Combining front-end knowledge, design a front-end interface for the above DAPP, and cooperate with the back-end project to run successfully.
  2. Please replace the sample contract "HelloWorld.sol" in this experiment with the deposit contract designed in the experiment in the previous section, and try to generate the SpringBoot project.

Guess you like

Origin blog.csdn.net/m0_46635662/article/details/130225982