pragma solidity >=0.4.21 <0.6.0; contract Casino { address payable public owner; event Play(address payable indexed player, uint256 betSize, uint8 betNumber, uint8 winningNumber); event Payout(address payable winner, uint256 payout); constructor() public { owner = msg.sender; } function kill() external { require(msg.sender == owner, "Only the owner can kill this contract"); selfdestruct(owner); } function fund() external payable {} function bet(uint8 number) external payable { require(msg.value <= getMaxBet(), "Bet amount can not exceed max bet size"); require(msg.value > 0, "A bet should be placed"); uint8 winningNumber = generateWinningNumber(); emit Play(msg.sender, msg.value, number, winningNumber); if (number == winningNumber) { payout(msg.sender, msg.value * 10); } } function getMaxBet() public view returns (uint256) { return address(this).balance / 100; } function generateWinningNumber() internal view returns (uint8) { return uint8(block.number % 10 + 1); // Don't do this in production } function payout(address payable winner, uint256 amount) internal { assert(amount > 0); assert(amount <= address(this).balance); winner.transfer(amount); emit Payout(winner, amount); } }
测试代码:
const Casino = artifacts.require('Casino'); const assert = require("chai").assert; const truffleAssert = require('truffle-assertions'); contract('Casino', (accounts) => { let casino; const fundingAccount = accounts[0]; const bettingAccount = accounts[1]; const fundingSize = 100; // build up and tear down a new Casino contract before each test beforeEach(async () => { casino = await Casino.new({ from: fundingAccount }); await casino.fund({ from: fundingAccount, value: fundingSize }); assert.equal(await web3.eth.getBalance(casino.address), fundingSize); }); afterEach(async () => { await casino.kill({ from: fundingAccount }); }); it("should lose when bet on the wrong number", async () => { let betSize = 1; // we know what the winning number will be since we know the algorithm let betNumber = (await web3.eth.getBlock("latest")).number % 10 + 1; let tx = await casino.bet(betNumber, { from: bettingAccount, value: betSize }); // player should be the same as the betting account, and the betted number should not equal the winning number truffleAssert.eventEmitted(tx, 'Play', (ev) => { return ev.player === bettingAccount && !ev.betNumber.eq(ev.winningNumber); }); // there should be no payouts truffleAssert.eventNotEmitted(tx, 'Payout'); // check the contract's balance assert.equal(await web3.eth.getBalance(casino.address), fundingSize + betSize); }); it("should win when bet on the right number", async () => { let betSize = 1; // we know what the winning number will be since we know the algorithm let betNumber = ((await web3.eth.getBlock("latest")).number + 1) % 10 + 1; let tx = await casino.bet(betNumber, { from: bettingAccount, value: betSize }); // player should be the same as the betting account, and the betted number should equal the winning number truffleAssert.eventEmitted(tx, 'Play', (ev) => { return ev.player === bettingAccount && ev.betNumber.eq(ev.winningNumber); }); // player should be the same as the betting account, and the payout should be 10 times the bet size truffleAssert.eventEmitted(tx, 'Payout', (ev) => { return ev.winner === bettingAccount && ev.payout.toNumber() === 10 * betSize; }); // check the contract's balance assert.equal(await web3.eth.getBalance(casino.address), fundingSize + betSize - betSize * 10); }); });
相关推荐
区块链模式茶场游戏源码带商城 虚拟农场+在线商城 带系统交易,茶叶种植系统,带交易大厅,带游戏玩家游戏获得的茶叶可以与商城结合的系统 区块链模式 游戏规则: 佣金规则 茶场分五个级别: 1、茶工级别:免费...
Cocos creator 2.X 数字合十 消消乐 游戏源码下载,完整可运营版本,可二次开发做区块链游戏,代码是TS的代码,方便阅读,容易修改。 Cocos creator 2.X 数字合十 消消乐 游戏源码下载,完整可运营版本,可二次开发...
小游戏 游戏系统设计、开发,供相关人员学习参考,提供说明材料+源代码 小游戏 游戏系统设计、开发,供相关人员学习参考,提供说明材料+源代码 小游戏 游戏系统设计、开发,供相关人员学习参考,提供说明材料+源代码...
养蘑菇区块链技术投资理财游戏源码可随便二开源代码介绍
Mage Duel 是作为 LINE 区块链示例服务实现的 TCG(交易卡游戏)。玩家互相决斗,使用他们的卡片组。法师决斗由服务器和客户端组成。此存储库仅包含服务器端代码。
基于tendermint区块链的宠物游戏源码+全部资料(毕业设计).zip基于tendermint区块链的宠物游戏源码+全部资料(毕业设计).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心...
区块链 基于以太坊的水果机游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于以太坊的水果机游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于以太坊的水果机游戏的设计与实现+详细文档...
区块链 基于以太坊的健身养成游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于以太坊的健身养成游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于以太坊的健身养成游戏的设计与实现+...
区块链 基于web3.js 以太坊的投注游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于web3.js 以太坊的投注游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于web3.js 以太坊的投注游戏的...
贝壳云科技开发的游戏红包项目代码,php语言,支持安装附安装教程,注:有部分加密的文件不过不影响安装,可用于学习研究。
区块链 基于以太坊的共享画布游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于以太坊的共享画布游戏的设计与实现+详细文档+全部资料(优秀项目).zip 【备注】 1、该项目是个人高分毕业设计项目源码...
区块链 基于以太坊的三连棋游戏的设计与实现+详细文档+全部资料(优秀项目).zip区块链 基于以太坊的三连棋游戏的设计与实现+详细文档+全部资料(优秀项目).zip 【备注】 1、该项目是个人高分毕业设计项目源码,已...
水浒传xlbe 水浒服务端 水浒客户端资源
目录自动格式化代码更改页面<title> 安装依赖项导入组件代码分割添加样式表后处理CSS 添加CSS预处理器(Sass,Less等) 添加图像,字体和文件使用public文件夹更改HTML 在模块系统之外添加资产何时使用public...
涉及到开发者领域有游戏开发、移动应用、前端开发、后端开发、人工智能、大数据、物联网、信息安全、物联网、运维管理、云技术、区块链等。涉及到的开发语言有Javascript、Java、SQL、PHP、Python、C#、C++、C、...
火柴人大逃亡HTML5游戏源码,运行需要服务器环境,已经反复测试,放心使用。
链游玩家|出品 2020年的开端并不是很...在这种玩家看来,玩链游的最终目的就是“赚token”,他们会研究区块链游戏所有的规则和玩法,甚至能够做到比游戏策划还懂规则的地步,并运用自己的高深见解,为自己赚取代币。
Blochchain-Cryptocurrencies:有关CIS-600区块链和加密货币的代码具有实验室经验。 储存库由准则和一些游戏代码以及适当的文档组成
Libplanet Libplanet是一个.NET库,用于以分散方式创建多人在线游戏,这意味着... 同构:Libplanet是一个.NET库,因此每个游戏逻辑都可以用相同的语言C#编写,并可以在区块链上运行。 不需要胶水代码或“智能合约”
了解为什么区块链在技术领域会改变游戏规则 使用基本的Hyperledger Fabric部署设置区块链网络 了解创建去中心化应用程序的注意事项 学习将业务网络与现有系统集成 使用Hyperledger Composer快速编写智能合约 如果您...