怎样实现的?
  • 怎样实现的?
  • 简介
  • 怎样实现的?
    • 用Mastodon搭建自己的Twitter
    • 用Cumulusclips搭建自己的YouTube
    • 用bookStack搭建团队知识平台
    • 用Duplicati实现计划备份
    • 用FreeNAS搭建网络存储服务器
    • Ovirt4.2搭建流程
    • 使用Rancher光速搭建K8s集群
    • CentOS7不重启扩展根分区磁盘空间
    • 搭建团队文档协作平台(OnlyOffice)
    • 使用TF-Hub进行迁移学习(图像分类)
    • 用EVE-NG做仿真网络实验
    • 用docsite生成企业站点
    • 使用Harbor搭建自己的docker镜像仓库
    • 使用OpenSTF同时操作多台手机
    • 使用LDX创建管理基于容器的虚拟机
    • 调整Linux内核参数
    • ProxmoxVE高可用集群搭建并集成Ceph
    • Ceph集群的搭建与运维
    • Nextcloud搭建自己的云盘
    • Centos7搭建单节点OpenShift(OKD)
    • 使用Fuel快速搭建OpenStack
    • CentOS7搭建DNS服务器
    • 微信的语音段传输
    • 用Python写MT4自动交易策略来炒外汇
    • WebAR
    • 用BadUSB物理入侵别人的电脑
    • 用Tensorflow.js和摄像头玩俄罗斯方块
    • 开发以太坊DAPP--水果机
    • 表达式是怎样被解析的呢
    • Airtest基于图像识别的UI自动化测试工具
    • 滑动拼图验证码的JS实现
    • 内网穿透--TCP打洞
    • 一致性哈希算法的理解与实践
Powered by GitBook
On this page
  • 原理
  • 机器环境
  • 编写智能合约
  • 编写迁移脚本
  • 配置truffle.js文件
  • 启动ganache-cli测试链
  • 迁移部署合约
  • 启动前端
  • GitHub地址
  • 效果展示
  1. 怎样实现的?

开发以太坊DAPP--水果机

原理

  • 使用 truffle 框架,solidity语言进行智能合约的开发和部署

  • 使用 web3.js 来进行页面与智能合约的交互

  • 使用 ganache-cli 作为测试链进行测试

  • 使用metamask作为钱包

机器环境

  • Win10

  • Node.js

编写智能合约

npm isntall -g ganache-cli
npm install -g solc
npm install -g truffle

truffle init

npm install --save truffle-contract
npm install --save web3

cd a dir
touch ./contracts/SlotMachine.sol
SlotMachine.sol
// 声明编译器版本号
pragma solidity ^0.4.4;

// 智能合约类
contract SlotMachine {
    
    address public slotMachineFunds;
    // 每个硬币的价格
    uint256 public coinPrice = 0.1 ether;
    // 合约所有者的地址
    address owner;
    // 游戏开始的事件
    event Rolled(address sender, uint rand1, uint rand2, uint rand3);
    // 存放地址到余额的映射
    mapping (address => uint) pendingWithdrawals;
    // 权限装饰器,只有合约所有者才有权限
    modifier onlyOwner() {
        require(owner == msg.sender);
        _;
    }

    function SlotMachine() {
        owner = msg.sender;
    }

    //游戏逻辑,payable表示每次游戏都要给coinPrice的钱
    function oneRoll() payable {
        require(msg.value >= coinPrice);

        uint rand1 = randomGen(msg.value);
        uint rand2 = randomGen(msg.value + 10);
        uint rand3 = randomGen(msg.value + 20);

        uint result = calculatePrize(rand1, rand2, rand3);

        Rolled(msg.sender, rand1, rand2, rand3);

        pendingWithdrawals[msg.sender] += result;

    }
    // 查看合约的余额
    function contractBalance() constant returns(uint) {
        return this.balance;
    }
    // 计算游戏结果
    function calculatePrize(uint rand1, uint rand2, uint rand3) constant returns(uint) {
        if(rand1 == 5 && rand2 == 5 && rand3 == 5) {
            return coinPrice * 30;
        } else if (rand1 == 6 && rand2 == 5 && rand3 == 6) {
            return coinPrice * 20;
        } else if (rand1 == 4 && rand2 == 4 && rand3 == 4) {
            return coinPrice * 15;
        } else if (rand1 == 3 && rand2 == 3 && rand3 == 3) {
            return coinPrice * 12;
        } else if (rand1 == 2 && rand2 == 2 && rand3 == 2) {
            return coinPrice * 10;
        } else if (rand1 == 1 && rand2 == 1 && rand3 == 1) {
            return coinPrice * 5;
        } else if ((rand1 == rand2) || (rand1 == rand3) || (rand2 == rand3)) {
            return coinPrice;
        } else {
            return 0;
        }
    }
    // 退票出钱
    function withdraw() {
        uint amount = pendingWithdrawals[msg.sender];

        pendingWithdrawals[msg.sender] = 0;

        msg.sender.transfer(amount);
    }
    // 查看用户余额
    function balanceOf(address user) constant returns(uint) {
        return pendingWithdrawals[user];
    }
    // 设置游戏硬币的价格
    function setCoinPrice(uint _coinPrice) onlyOwner {
        coinPrice = _coinPrice;
    }

    function() onlyOwner payable {
    }

    function cashout(uint _amount) onlyOwner {
        msg.sender.transfer(_amount);
    }
    // 生成随机数
    function randomGen(uint seed) private constant returns (uint randomNumber) {
        return (uint(sha3(block.blockhash(block.number-1), seed )) % 6) + 1;
    }
}

编写迁移脚本

migrations/2_deploy_contracts.js
var SlotMachine = artifacts.require('./SlotMachine');

module.exports = (deployer) => {
    deployer.deploy(SlotMachine);
}

配置truffle.js文件

truffle.js
module.exports = {
  networks: {  
    development: {  
      host: "localhost",  
      port: 8545,  
      network_id: "*"  
    }  
  }  
};

启动ganache-cli测试链

  • ganache-cli

  • 记录账号信息

迁移部署合约

  • truffle migrate

启动前端

  • npm install && npm run dev

GitHub地址

效果展示

Previous用Tensorflow.js和摄像头玩俄罗斯方块Next表达式是怎样被解析的呢

Last updated 6 years ago

https://github.com/ns2250225/ethereum-slot-machine