【区块链】以太坊编程环境构建

以太坊编程环境构建

以太坊编程环境介绍

以太坊编程涉及语言

  • Solidity:类JavaScript,合约语言
  • web3.js:javaScript,合约调用
  • React等:js框架,用户交互,webUI等
  • Nodejs:js框架,后台逻辑
  • JS/html,基础语言

各类以太坊网络

  • Mainnet:以太坊主网
  • Ropsten,以太坊主测试网络
  • Ganache/testrpc:自建测试节点
  • Geth客户端自建私有链或联盟链

基本编程测试环境要求

  • 标准Mac/Windows环境即可
  • 内存4G以上
  • Mac下最好有管理员权限

Npm/nodejs环境

  • 随同NodeJS一起安装的包管理工具
  • 安装nodejs即可,会同时安装npm
  • 允许用户从NPM服务器下载别人编写的第三方包到本地使用

Npm基本使用

基本语法:

npm install <Module Name>

其中:

  • npm -v #显示版本
  • npm install <module> -g # 全局安装
  • npm uninstall <module> # 卸载
  • npm update <module> # 更新
  • npm list <module> # 显示模块版本号
    注意:在package.json所在目录下使用npm.install

Ganache环境

ganache-cli基于JS编写,通过npm安装
ganache是在本地使用内存模拟的一个以太坊环境
为测试提供很多便捷功能,如自动生成账号等
开发测试专用。一般都是在ganache调式完成后,再部署到真实以太坊节点

Ganache基本使用

安装命令:

npm install -g ganache-cli #node的版本需要>6.11.5

ganache-cli命令:

ganache-cli <options>

Options参数同真实节点geth的参数基本一致,可以自定义端口等,一般无需设置。

Truffle环境

truffle是Solidity语言的一套开发框架,本身基于js。
客户端做了深度集成。开发,测试,部署一行命令都可以搞定
提供自动化项目构建机制
提供了合约抽象接口,对web3.js进行了进一步封装,简化开发流程

Truffle基本使用

安装:

sudo npm install -g truffle

truffle init # 初始化代码环境
truffle compile # 编译
truffle deploy # 部署
truffle test # 测试
truffle console # 命令行工具

编辑器IDE

Remix,基本web的编程环境
SublimeText
Emacs Solidity
Atom Solium Linter
Visual Studio Code
这里使用VS Code。

Linux环境开发

  • Shell环境
  • Nodejs安装调试:https://nodejs.org/en/
    node安装完成后,应当同时安装了node.js和npm
  • npm -v
  • node -v

Ganache环境安装调试

安装:

sudo npm install -g ganache-cli

启动调试:

ganache-cli

Truffle安装调试

安装:

sudo npm install -g truffle

调试:

truffle init

编辑器下载

VS code : https://code.visualstudio.com/
安装solidity插件。

安装过程

安装node.js和npm

参考链接:https://phoenixnap.com/kb/install-latest-node-js-and-nmp-on-ubuntu
(直接阅读英文就好)

引言

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast and scalable network applications. In addition to offering various JS modules, it is also lightweight, efficient and supports consistent and integrated development.
Node Package Manager (NPM) is Node’s official package manager, used for installing and managing package dependencies.
In this tutorial, learn three (3) different options for installing Node.js and NPM on Ubuntu 18.04.

3种方式在Ubuntu上安装NPM

方法1:从Ubuntu仓库安装Node.js和NPM
sudo apt update
sudo apt install nodejs
nodejs -v
sudo apt install npm
npm -v
方法2:使用NVM安装Node.js和NPM

Another way to install Node.js and NPM is with the Node Version Manager (NVM). NVM is a tool practical for managing multiple Node.js versions.

sudo apt install curl
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

这一步结束之后需要根据提示重启终端,然后将NVM导入到环境变量,即执行:

export NVM_DIR="$HOME/.nvm"

然后检查是否安装成功:

nvm --version

一旦安装完成nvm,就可以从所有可用的Node.js版本中选择安装:

nvm ls-remote
nvm ls
node -v
nvm use 8.11.1
方法3:从NodeSource仓库中安装Node.js
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_11.x | sudo bash -
sudo apt install nodejs
nodejs --version
npm --version
安装开发工具

Once you have Node.js and NPM setup, install the development tools. This automation tool kit allows compiling and installing native add-ons from the NPM.

sudo apt install gcc g++ make

安装ganache

这个步骤一般不会出现问题。

sudo  npm install -g ganache-cli
ganache-cli

启动调试之后会出现127.0.0.1:8545的URL,说明安装正常。使用ctrl+C关闭调试

安装truffle

这个过程中问题较多。
正常步骤:

sudo npm install -g truffle
truffle init

可能出现的问题:

  1. 问题:Error: EACCES: permission denied, open ‘/root/.config/truffle/config.json’
    解决方案:使用root用户安装
    具体参考:https://stackoverflow.com/questions/57031399/error-eacces-permission-denied-open-root-config-truffle-config-json

2 问题:Error: Cannot find module spawn-sync/postinstall
解决方案:

npm install -g try-thread-sleep
npm install -g serverless --ignore-scripts spawn-sync

具体参考:https://github.com/serverless/serverless/issues/4319
3 问题:npm ERR! [email protected] postinstall: node ./scripts/postinstall.js
解决方案:使用–unsafe-perm=true
具体参考:https://stackoverflow.com/questions/48298361/npm-install-failed-at-the-node-sass4-5-0-postinstall-script

发布了279 篇原创文章 · 获赞 169 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/ARPOSPF/article/details/104463424
今日推荐