当前位置:网站首页>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
边栏推荐
- sql server 2008 的导入导出步骤
- vim的安装以及常用命令
- Pta: self TEST-1 print Hourglass (20 points)
- Introduction to Eureka
- ssm中的文件上传和下载
- [LDA] rough version notes of EM variational reasoning [to be improved
- Application of postman-rest client plug-in
- Differences between microservice architecture and SOA Architecture
- 2021-06-20
- Explanation of socket principle (where, what and how to use it)
猜你喜欢

Pta: self test -3 array element cyclic right shift problem (20 points)

Increase the maximum number of MySQL connections
![[LDA] basic knowledge notes - mainly AE and VAE](/img/1c/ccc073cac79b139becd5de0b9a91b1.png)
[LDA] basic knowledge notes - mainly AE and VAE

Solving multithreading security problems

C语言 分割bin文件程序

Use of multithreading

The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP

C operator

Explanation of socket principle (where, what and how to use it)

New关键字、引用&与指针的学习记录
随机推荐
Codeforces Round 797 (Div. 3,CF1690)全题解
Idea Encyclopedia (Reprinted)
Tcp/ip three handshakes and four waves (interview questions)
Array related content
2021-06-27
CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small
Acwing summer daily question (sexy prime number on June 10)
Codeworks round 797 (Div. 3, cf1690)
Rust tip - running the tensorrt model through FFI programming
Acwing暑期每日一题(6月10日性感素数)
Method reference instance method reference
POSTMAN-REST Client插件的应用
h3c GR5200路由器上如何设置公网ip可以访问
Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11
File uploading and downloading in SSM
org. xml. sax. SAXParseException; lineNumber: 63; columnNumber: 10; The definition of "mapper" in the related type must be matched with "(CAC
Introduction to Eureka
ssm中的文件上传和下载
vim的安装以及常用命令
Wild pointer understanding



