当前位置:网站首页>Classic case of solidity - Smart games
Classic case of solidity - Smart games
2022-06-12 15:28:00 【Rookie counter attack】
Refer to https://ke.qq.com/webcourse/2379017/103111126#taid=10365401060101385&vid=5285890802017890711
1. Case function introduction
- Platform parties deploy smart contracts , The user selects the next big or the next small .
- Smart contracts specify the rules of the game , Using simple random numbers to achieve .
2. The specific code is as follows
pragma solidity ^0.6.1;
struct User{
address payable addr;
uint256 amount;
}
contract bocai_demo{
User[] bigs;// Xiada
User[] smalls;// Next little
address admin;// Address of Platform party
bool isFinshed;// End flag
uint256 outtimes;// By the time
uint256 bigTotalAmount;// Bet a large sum of money
uint256 smallTotalAmount;// Bet on a small total amount
uint256 result;// The lottery results
constructor() public{
admin = msg.sender;
isFinshed = false;
outtimes = now+120;// Bet for a minute
bigTotalAmount=0;
smallTotalAmount=0;
}
// Bets
function bet(bool flag) public payable{
require(msg.value>0,"msg.value must >0");
require(!isFinshed,"game must not finshed");
require(now <= outtimes,"time not out");
if(flag){
// Xiada
User memory user = User(msg.sender,msg.value);
bigs.push(user);
bigTotalAmount+=msg.value;
}else{
// Next little
User memory user = User(msg.sender,msg.value);
smalls.push(user);
smallTotalAmount+=msg.value;
}
}
// A prize
function open() public payable{
require(!isFinshed,"only open once");
require(outtimes<=now,"time must ok");
// Calculate whether the bet is big or small , Make rules of the game
isFinshed=true;
// Random generation 0-18 Number within ,0-9: Small 10-17: Big
result = uint256(keccak256(abi.encode(msg.sender,now,outtimes,admin,smalls.length)))%18;
// Pie Award
User memory user;
if(result<9){
// Bet a small win
for(uint256 i = 0;i < smalls.length;i++){
user=smalls[i];
// Win a large sum of 90% The money
uint256 amount = bigTotalAmount * user.amount / smallTotalAmount *90 /100 + user.amount;
user.addr.transfer(amount);
}
}else{
// Make a big bet
for(uint256 i=0;i<bigs.length;i++){
user=bigs[i];
// Win a small sum of 90% The money
uint256 amount = smallTotalAmount * user.amount / bigTotalAmount *90 /100 +user.amount;
user.addr.transfer(amount);
}
}
}
// Get the total amount of money and the remaining money to deploy the contract
function getBalance() external view returns(uint256,uint256,uint256){
return (bigTotalAmount,smallTotalAmount,address(this).balance);
}
// Get the result of the final game
function getResult() external view returns(string memory){
require(isFinshed,"bet must finshed");
if(result<9){
return "small";
}else{
return "big";
}
}
}
3. The test steps are as follows
- selection 7 An account address ,1 User deployment contracts , Act as the platform side , rest 6 As a betting user .
- Enter the bet amount and amount as shown in the figure above (true)/ Small (false)
- This is a screenshot of the account address balance transformation during each test
- After the game , Conduct lottery , The ultimate winner is the big bet , Will get a bet for a small total amount of 90% Reward
边栏推荐
- Use of thread communication
- 【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
- Design concept of ORM framework
- Acwing summer daily question (sexy prime number on June 10)
- IGMP报文(TCP/IP详解卷1/卷2)
- MySQL development considerations (Alibaba development manual)
- C operator
- MySQL开发注意事项(阿里巴巴开发手册)
- Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11
- Kinect2.0+ORBSLAM2_ with_ pointcloud_ map
猜你喜欢

Multi thread knowledge induction

Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)

Acwing暑期每日一题(6月10日性感素数)

Left aligned, right aligned, random number, goto, compare output bool

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

C语言 分割bin文件程序

IMU的学习记录

Swap numbers, XOR, operator correlation

Function recursion example

The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP
随机推荐
Village to village communication (and collective search)
[jvm learning] class loading subsystem
使用CSDN-markdown编辑器
Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11
Differences between microservice architecture and SOA Architecture
Learning is an inhumane thing (becoming an expert's internal mind skill)
Use of boost:: bind() in ROS
Install RHEL 7/8 (red hat) virtual machine (Reprint)
Explanation of socket principle (where, what and how to use it)
Remote control of other computers -- detailed tutorial
[LDA] basic knowledge notes - mainly AE and VAE
5G新方案!升级现有的基站和UE模拟器至5G毫米波频段
Conversion between sparse array and array and file reading and writing
Kinect2.0+ORBSLAM2_with_pointcloud_map
Import and export steps of SQL Server 2008
Apprendre est une chose contre la nature humaine
The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP
RARP summary (tcp/ip explanation volume 1/2)
Socket原理讲解(在哪、是什么、怎么用)
CUDA out of memory 或 BrokenPipeError: [Errno 32] Broken pipe 或 OSError: [WinError 1455] 页面文件太小的解决办法



