操作系统:centos 7.6
当前目录:$HOME/workspace/
npm install -g truffle
mkdir eth-lottery
cd eth-lottery
truffle unbox react
cd contracts
vim Lottery.sol
Lottery.sol
pragma solidity ^0.5.0;
contract Lottery {
address payable public manager;
address payable[] public playersSlice;
mapping(address => bool) public playersMap;
uint256 public round = 0;
address payable public winner;
constructor() public {
manager = msg.sender;
}
modifier onlyManager() {
require(manager == msg.sender);
_;
}
function play() public payable {
require(msg.value == 1 ether);
require(!playersMap[msg.sender]);
playersMap[msg.sender] = true;
playersSlice.push(msg.sender);
}
function getBalance() public view returns(uint256) {
return address(this).balance;
}
function getPlayersSlice() public view returns(address payable[] memory) {
return playersSlice;
}
function getPlayersCnt() public view returns(uint256) {
return playersSlice.length;
}
function randomUInt256(uint256 seed) private view returns(uint256) {
bytes memory v1 = abi.encodePacked(block.timestamp, block.difficulty, seed);
bytes32 v2 = keccak256(v1);
return uint256(v2);
}
function calBonusAndIncome(uint256 balance) private pure returns(uint256, uint256) {
uint256 bonus = balance * 9 / 10;
uint256 income = balance - bonus;
return (bonus, income);
}
function drawPrize() onlyManager public {
uint256 len = playersSlice.length;
require(len > 0);
uint256 idx = randomUInt256(len) % len;
winner = playersSlice[idx];
uint256 bonus;
uint256 income;
(bonus, income) = calBonusAndIncome(getBalance());
manager.transfer(income);
winner.transfer(bonus);
++round;
for (uint256 i = 0; i < len; ++i) {
delete(playersMap[playersSlice[i]]);
}
}
function withdrawPrize() onlyManager public {
for (uint256 i = 0; i < playersSlice.length; ++i) {
address payable player = playersSlice[i];
delete(playersMap[player]);
player.transfer(1 ether);
}
delete(playersSlice);
}
}
remixd -s $HOME/workspace/eth-lottery/constacts --remix-ide http://remix.ethereum.org
remixd的安装
npm install remixd -g
cd ../migrations
vim 2_deploy_contracts.js
2_deploy_contracts.js
var Lottery = artifacts.require("./Lottery.sol");
module.exports = function(deployer) {
deployer.deploy(Lottery);
};
npm install -g ganache-cli
ganache-cli
记住输出的助记词( Mnemonic )和监听节点
Mnemonic: improve ripple network feature legal holiday always awkward seek enforce quick drip
...
...
Listening on 127.0.0.1:8545
8.2. 假如想部署外网,例如主网或者Ropsten测试网络,需要一个仿真节点。
infura有免费提供这个服务。上去它的官网 https://infura.io/ ,并注册,开启一个项目。这样就获得节点
9. 修改模版的配置文件
根据官方文档 http://truffleframework.com/docs/advanced/configuration 我们把网络添加到truffle-config.js
cd ..
vim truffle-config.js
truffle-config.js
const path = require("path");
const HDWalletProvider = require("truffle-hdwallet-provider")
module.exports = {
// See
// to customize your Truffle configuration!
contracts_build_directory: path.join(__dirname, "client/src/contracts"),
networks: {
ropsten: {
provider: function() {
const mnemonic = 'inject stock easy learn repair fringe damage crawl cruise junior enable remember';
const endpoint = 'https://ropsten.infura.io/v3/ba7794faf4cd4b27894283a1ded74631';
return new HDWalletProvider(mnemonic, endpoint);
},
network_id: '3',
},
test: {
provider: function() {
const mnemonic = 'improve ripple network feature legal holiday always awkward seek enforce quick drip';
const endpoint = 'http://127.0.0.1:8545/';
return new HDWalletProvider(mnemonic, endpoint);
},
network_id: '*',
},
}
};
帮记词和节点根据你们的配置,还要安装npm install truffle-hdwallet-provider模块
npm init
npm install truffle-hdwallet-provider --save
truffle compile
合约的abi等信息都会导出在 ./client/src/contracts/ 下
11. 部署合约
11.1. 部署在本地测试节点网络
truffle migrate --network test
输出结果
⚠️ Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.
Starting migrations...
======================
> Network name: 'development'
> Network id: 1549381217260
> Block gas limit: 6721975
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x99fd7645bdb7db4f4e6ea803e281ca06af0f4a36fc059f78ab712389a9898fae
> Blocks: 0 Seconds: 0
> contract address: 0x27D8F0326C4C6C60b080B1b21b7EAcEd4FF467D6
> account: 0xa9a981e813d6d6658738220dD5F2fAa078B62aFA
> balance: 99.99430184
> gas used: 284908
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00569816 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00569816 ETH
2_deploy_contracts.js
=====================
Deploying 'Lottery'
-------------------
> transaction hash: 0x32a6518bd03ae38805a6a0cce25e502eefd294dd91243ae486606a21c1a91280
> Blocks: 0 Seconds: 0
> contract address: 0xc65c364Fa2E7C8C684834c726636DBFd19938825
> account: 0xa9a981e813d6d6658738220dD5F2fAa078B62aFA
> balance: 99.976873
> gas used: 829408
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.01658816 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.01658816 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.02228632 ETH
11.2. 部署在ropsten测试网络
truffle migrate --network ropsten
输出结果
⚠️ Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.
Starting migrations...
======================
> Network name: 'ropsten'
> Network id: 3
> Block gas limit: 8000029
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xb488e762a06315eca3eddc94e67cf73b34e8f5d50364f8ec503df0306a1c0b6f
> Blocks: 0 Seconds: 4
> contract address: 0x479cc2fA1cbE27d00e09fC3a477b08D42680f10C
> account: 0xcf961578df24ECB997929dbA39F6a05bA14c6bAd
> balance: 15.05442234499999
> gas used: 284908
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00569816 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00569816 ETH
2_deploy_contracts.js
=====================
Deploying 'Lottery'
-------------------
> transaction hash: 0xe5aed3122b0127d7e30919ec43d73df0b1ed2d71d0a591af12a091ccf7bc5c57
> Blocks: 2 Seconds: 20
> contract address: 0x8c2216C695836EF98cd0cE470e0c7e95d80b8f06
> account: 0xcf961578df24ECB997929dbA39F6a05bA14c6bAd
> balance: 15.03689750499999
> gas used: 834208
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.01668416 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.01668416 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.02238232 ETH
部署得太慢得话,有一个gas的项,多费一点gas,部署可以快很多。
合约信息
https://ropsten.etherscan.io/address/0x8c2216C695836EF98cd0cE470e0c7e95d80b8f06
部署完,应该要在ropsten测试网络提交源码的,但是我用VPN连上也是提交不成功,人机检测还是不刷出来,所以算了。
cd client
npm install semantic-ui-css --save
npm install semantic-ui-react --save
npm run start