入门的话可以通过Remix开发工具完成solidity项目的编写、编译、部署等操作。专业点的开发工具有 Truffle 和 Hardhat,先看一看hardhat的简介:
Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.
开发工具的话,常用的还有一个本地模拟节点的Ganache(Ganache属于truffle),因为链上包括主网和测试网调试比较慢,可以先在本地测试。
常用开发模式:
npm (npx) version 8.5.5
node version 16.13.1
新手第一次如下:
npm init -y
,此时目录中会生成package.json
文件。npm install --save-dev hardhat
。npx hardhat
,会出现终端选项自行选择。如果安装过hardhat,以上不用执行,直接运行 npx hardhat init
npm install --save-dev hardhat@^2.9.3 @nomiclabs/hardhat-waffle@^2.0.0 ethereum-waffle@^3.0.0 chai@^4.2.0 @nomiclabs/hardhat-ethers@^2.0.0 ethers@^5.0.0
;npm install --save dotenv@^16.0.0
npm install --save @openzeppelin/contracts
安装以上包的package.json
文件如下:
{
"name": "yourdappname",
"version": "version",
"description": "",
"main": "yourmain.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-waffle": "^2.0.0",
"chai": "^4.2.0",
"ethereum-waffle": "^3.0.0",
"ethers": "^5.0.0",
"hardhat": "^2.9.3"
},
"dependencies": {
"dotenv": "^16.0.0",
"@openzeppelin/contracts": "^4.7.3"
}
}
生成目录结构如下:
.
├── README.md
├── contracts
├── hardhat.config.js
├── node_modules
├── .env 手动创建,用于放置privateKey,需安装dotenv包
├── package-lock.json
├── package.json
├── scripts
└── test
通过js代码连接以太坊有两个框架 Web3js和Ethers.js,一般要通过http或者wss代理去连接,比如通过alchemy或者infura等节点供应商提供的api去连接。
npx hardhat compile
编译soliditynpx hardhat test
测试tests目录下js文件npx hardhat run
或者npx hardhat run scripts/deploy.ts
运行js ,默认在本地网络运行。注意指定网络:npx hardhat run scripts/deploy.js --network mumbai