Ethereum in Practice - Building a Basic Environment (Local Version)

Ethereum smart contract development in practice - building a basic environment (local version)

install truffle

truffleIt is a development tool for Ethereum smart contracts. truffleThe basic environment is nodejs, so we need to download and install it on the system first nodejs, http://nodejs.cn/download/.

image-20211218092332947

Execute the following command, if the output can be normal, it can be determined that the nodejsinstallation has been successful.

node -v

will be installed nodejsat the same time as the installation npm. npmIs nodejsthe package manager of the nodejsworld, managing most of the open source packages in the world. For example, when we execute the command below, it can be installed truffle.

npm install -g truffle

By default, npmthe package needs to be downloaded from a foreign server, and the speed is often very slow. Therefore, I recommend executing the following instructions to npmconfigure the mirror server as Alibaba Cloud to improve the download speed of the package.

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

After we have configured the mirror resources, we can use the following command to check whether the configuration is successful.

npm config get registry

Below we formally execute the installation command to install truffle.

npm install -g truffle

If the following problem is encountered during the installation process, it means that the current system is not installed pythonor there is no configured pythonenvironment path.

image-20211129160947479

You can download python through the official website, https://www.python.org/downloads/, because the official website is relatively slow, we recommend a download link in the Huawei mirror, https://mirrors.huaweicloud.com/python/, you can download it according to The current time, download the latest or next-new version, the version I downloaded is python-3.10.0a2.exe.

When installing, pay attention to tick to add the Python execution path to the environment variable, as shown in the following figure.

After the installation is complete, you can use the following command to determine whether python has been installed successfully:

python --version
# Python 3.10.0a2

Re-execute npm install truffle -gto install truffle, and use the following command to determine whether the entire installation is successful.

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 the following three commands to initialize the project, the first two create an empty folder, and the last one initialize the truffleproject.

$ 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 the project is initialized, we can see the directory as shown below:

image-20211217215149242

Next, we create a contract and a test against this contract

$ truffle create contract Metacoin
$ truffle create test Metacoin

At this point, look at the directory structure again, and add two new files, which Metaconin.solrepresent the contract file and metacoin_test.jsthe test file of the contract:

image-20211217215547579

Install Ganache

Ganache is used to simulate the Ethereum network and runs locally. Smart contracts developed locally can be directly executed in the Ethereum network simulated by Ganache running locally, which is convenient for programmers to debug smart contracts.

We log on to the website: http://trufflesuite.com/ganache/ and download a Windows version emulator.

image-20211218085908358

Install and run the simulator, a virtual Ethereum network will be created immediately after normal startup, and 10 virtual Ethereum accounts will be created randomly.

image-20211214221620528

There are a few points to note:

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

  • Network ID is5777

  • The contents displayed in each row of the list are: account address, 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 each private key of an account.

The content mentioned above will be used later, and will have a deeper understanding of these content at that time. It does not matter if it is not clear now.

Install VScode

You can download VScode through the official website, https://code.visualstudio.com/. This software is a general code editor developed by Microsoft. The installation method is relatively simple, so it is no longer meaningful to describe it here.

image-20211218090846391

If you simply use the native one VScode, you will find that this editor does not support the official language of Ethereum smart contracts Solidityin place, and all the buttons are in English, which is inconvenient for us to use. VScodeTherefore, we have to install some plugins for the native ones to solve the problems mentioned above.

In the opened interface, click the Apply Extensions button as shown in the figure.

image-20211218091122972

The picture below shows some of the plug-ins I installed. Readers can install them according to their own preferences. Note that the solidityplug-ins marked in red must be installed.

image-20211218092139305

In this way, through the above three steps, we have completed the development environment of Ethereum, and now we will enter the formal development of Ethereum.

Guess you like

Origin blog.csdn.net/u012331525/article/details/122008558