current position:Home>Ethereum smart contract - building the basic environment

Ethereum smart contract - building the basic environment

2022-02-03 15:41:02 xiyan0033

Building the infrastructure

install truffle

truffle It is the development tool of Ethereum smart contract ,truffle The basic environment is nodejs, Therefore, we need to download and install... On the system first nodejs,http://nodejs.cn/download/.

image-20211218092332947

Execute the following command , If it can output normally , It can be determined that nodejs Installed successfully .

node -v

In the installation nodejs Will be installed at the same time npm.npm yes nodejs Package manager in the world , Manages the nodejs Most open source packages in the world . for example , When we execute the following command , Can install truffle.

npm install -g truffle

By default ,npm You need to download packages from foreign servers , The speed is often very slow . therefore , I suggest the following instructions , take npm The image server is configured as alicloud to improve the download speed of packages .

npm config set registry https://registry.npm.taobao.org --global

When we configure the image resources , We can query whether the configuration is successful through the following command .

npm config get registry

Now let's officially execute the installation command to install truffle.

npm install -g truffle

If you encounter the following problems during installation , Indicates that the current system is not installed python Or no configuration python The environmental path of .

image-20211129160947479

You can download it through the official website python,https://www.python.org/downloads/, Because the official website is relatively slow , Recommend a download link in Huawei image ,https://mirrors.huaweicloud.com/python/, According to the current time , Download the latest or next version , The version I downloaded is python-3.10.0a2.exe.

During installation , Note that the check box will Python Add the execution path to the environment variable , As shown in the figure below .

After installation , You can judge by the following command python Has it been installed successfully :

python --version
# Python 3.10.0a2

Re execution npm install truffle -g To install truffle, And judge whether the whole installation is successful through the following command .

truffle version

# Truffle v5.4.22 (core: 5.4.22)
# Solidity v0.5.16 (solc-js)
# Node v10.24.1
# Web3.js v1.5.3

Use these three commands to initialize the project , The first two create an empty folder , The latter one initializes truffle project .

$ mkdir metacoin
$ cd metacoin
$ truffle init

Starting init...
================

> Copying project files to D:\Program Files\metacoin

Init successful, sweet!

Try our scaffold commands to get started:
  $ truffle create contract YourContractName # scaffold a contract
  $ truffle create test YourTestName         # scaffold a test

http://trufflesuite.com/docs

After project initialization , We can see the directory shown in the figure below :

image-20211217215149242

below , We create a contract and test it

$ truffle create contract Metacoin
$ truffle create contract MetacoinTest

here , Let's look at the directory structure , Two new files ,Metaconin.sol It means the contract document 、metacoin_test.js It represents the test file of the contract :

image-20211217215547579

install Ganache

Ganache Used to simulate Ethereum network , Run locally , Locally developed smart contracts can be run directly locally Ganache In the simulated Ethereum network , It is convenient for programmers to debug smart contracts .

We log on to the website :http://trufflesuite.com/ganache/, Download a Windows Version Simulator .

image-20211218085908358

Install and run the simulator , A virtual Ethereum network will be created immediately after normal startup , And randomly create 10 A virtual Ethereum account .

image-20211214221620528

There are several points to pay attention to :

  • The network created by the simulator and the listening port are http://127.0.0.1:7545

  • The Internet ID by 5777

  • Each line in the list displays : Account address , The account balance , Number of transactions in the account , Account index and the most important account private key , That is, the small key displayed in the last column of each row , Click the small key to see the private key of each account .

The content mentioned above , Will be used later , At that time, we will have a deeper understanding of these contents , It doesn't matter if you don't know now .

install VScode

You can download it through the official website VScode,https://code.visualstudio.com/, The software is a general code editor developed by Microsoft , The installation is relatively simple , There is no more meaningful description here .

image-20211218090846391

If you simply use native VScode, You will find that this editor is the official language of Ethereum smart contract Solidity Not enough support , And all buttons are in English , Inconvenient for us to use . therefore , We're going to give the original VScode Install some plug-ins to solve the problems mentioned above .

In the open interface , Click the application extension button as shown in the figure .

image-20211218091122972

The following figure shows some plug-ins I installed , Readers can install... According to their preferences , Be careful , Marked red solidity Plug in must be installed .

image-20211218092139305

such , Through the above three steps , We have built the development environment of Ethereum , Now we will enter the formal development of Ethereum .

copyright notice
author[xiyan0033],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202031540588534.html

Random recommended