机器环境
- win10
- nodev8.9.4
- npm install -g truffle
- npm install -g ganache-cli
Github地址
规则
- 初始化账号有10个ETH
- 每次游戏输了会减1个ETH,赢了则加2个ETH
- 注:有两个相同则不扣不增
效果
初始化项目
mkdir eth-slot-machine
cd eth-slot-machine
truffle init
编写游戏合约
- contracts/slotMachine.sol
pragma solidity ^0.4.19;
contract SlotMachine {
mapping (address => uint) public balanceOf;
function getBalanceOf(address _address) public view returns (uint) {
return balanceOf[_address];
}
function admission(address _address) public {
balanceOf[_address] = 10;
}
function checkGameResult(string _result) public {
require(keccak256(_result) == keccak256("win") || keccak256(_result) == keccak256("lose"));
if (keccak256(_result) == keccak256("win")) {
balanceOf[msg.sender] += 2;
} else {
balanceOf[msg.sender]--;
}
}
}
编写迁移脚本
- migrations/2_deploy_contract.js
var SlotMachine = artifacts.require("./slotMachine.sol");
module.exports = function(deployer) {
deployer.deploy(SlotMachine);
};
配置truffle.js文件
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*"
}
}
};
启动ganache-cli
迁移合约
编辑app/index.html
打开index.html