# solidity本地编译环境配置 **Published by:** [xrshao](https://paragraph.com/@shaozhang/) **Published on:** 2022-08-07 **URL:** https://paragraph.com/@shaozhang/solidity ## Content 一般情况都是直接使用remix,除非solidity的项目文件多,相互调用,才需要使用hardhat来自己编译部署! remix 打开 solidity的文件来 测试简单的! 复杂的还是需要 hardhat 来编译运行!1. 先安装solidity的解释器使用brew 安装就可以brew search solidity 可以看到有的brew install solidity 安装最后使用 solc --version 看下版本号Version: 0.8.11+commit.d7f03943.Darwin.appleclang2. 再安装nodejs, 使用nvm来管理nodejs的版本,所以先按照nvmbrew install nvm mkdir ~/.nvm 有可能提示存在了, vim ~/.bash_profile 复制下面2行到这个文件:wq保存export NVM_DIR=~/.nvm source $(brew --prefix nvm)/nvm.sh 最后让生效: source ~/.bash_profile3. 简单测试,使用remix, 我这边统一使用vscode编辑, 然后加上remix插件,基本上和remix差不多!有差异的话。就直接用remix打开本地文件来运行!安装 配置hardhat。npm install --save-dev hardhat ## 先安装hardhat npx hardhat ## 选择创建 简单例子 然后默认安装就行! contracts目录下,就是存放的sol文件 scripts目录下,就是部署的js文件 test目录下,就是测试的js文件, 可以使用web3.js 或者 ethers.js 来调用合约测试 可以先跑一下测试: npx hardhat test ## 可以看到,1 passing (995ms) 表示测试通过。 正常流程 ,第一步,编译 npx hardhat compile ##默认就是编辑contracts目录下的sol文件 可以在hardhat.config.js 中 设置solidity的版本号 module.exports = { solidity: "0.8.4", }; 配置hardhat.config.js 的网络,然后部署module.exports = { solidity: "0.8.4", networks:{ ganache:{ url:'http://localhost:8545', }, } }; 然后运行:这里默认的部署js是: npx hardhat run --network ganache scripts/sample-script.js 然后对应下ganache的transactions 就可以看到对应的合约部署地址是一致的 获得abi1.hardhat编译部署之后,artifacts/contracts/Greeter.sol/Greeter.json 这个json里面就是对应的abi文件 2.或者手工remix编译,也可以获得abi文件 ## Publication Information - [xrshao](https://paragraph.com/@shaozhang/): Publication homepage - [All Posts](https://paragraph.com/@shaozhang/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@shaozhang): Subscribe to updates