truffle 安装
sudo npm install -g truffle --registry=https://registry.npm.taobao.org
sudo npm install -g ganache-cli --registry=https://registry.npm.taobao.org
sudo npm install ethereumjs-testrpc -g --registry=https://registry.npm.taobao.org
启动testrpc
testrpc
//报错可能是因为端口被占用
sudo lsof -n -P | grep :8545
//解决办法 kill掉进程pid
sudo kill -9 pid
创建truffle 项目
sudo truffle init
truffle_demo
├── contracts //开发者编写的智能合约
│ └── Migrations.sol
├── migrations //存放部署脚本
│ └── 1_initial_migration.js
├── test //存放测试文件
├── truffle-config.js //truffle 默认配置文件
└── truffle.js
修改项目网络配置
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: 'localhost',
port: 8545,
network_id: '*' // Match any network id
}
}
};
新建一个智能合约
SimpleStorage.sol
pragma solidity ^0.4.23;
contract SimpleStorage {
uint storedData;
constructor() public {
storedData = 100;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
编译智能合约
sudo truffle compile
build
└── contracts
├── Migrations.json
└── SimpleStorage.json
部署智能合约
sudo truffle migrate --reset
添加测试文件
simple_storage.js
var SimpleStorage = artifacts.require("./SimpleStorage.sol");
contract('SimpleStorage', function(accounts) {
it("Initial SimpleStorage settings should match", function() {
return SimpleStorage.deployed().then(function(instance){
meta = instance;
return meta.get.call();
}).then((old)=>{
console.log("old value:",old);
return meta.set.call(100000000);
}).then((msg)=>{
console.log("update over:",msg);
return meta.get.call();
}).then((newValue)=>{
console.log("new value:",newValue)
})
});
});
//测试例程可能不是很合适
测试合约
truffle test