EOS博彩合约设计
集中博彩游戏合约设计
一、功能接口
1. 质押deposit
由用户发起,用户将个人账户中token质押给平台,从而可以进入平台去参与平台活动。
2. 赎回withdraw
由用户发起,在用户结束平台活动需要离开时,发起赎回曾质押给平台的token到个人账户。
3. 开启一期下注openbet
由平台发起,平台启动一期下注,玩家可以参与下注。
4. 结束一期下注 closurebet
由平台发起,平台关闭一期下注,所以玩家的下注被锁定。
5. 下注offerbet
由用户发起,用户参与平台开启的下注,需要在一期下注开启之后执行。
6. 取消下注canneloffer
由用户发起,用户取消曾参与的下注,需要在该期下注结束之前执行。
7. 开奖reveal
由平台发起,平台在一期下注上进行结果操作。
二、数据存储
1. 质押赎回账户表account_index
个人账户token质押给平台和从平台赎回token需要一个账户表来管理个人账户token信息,账户表数据结构如下:
a. 个人账户名称
b. 资产额
2. 下注期数记录表g_bet_index
游戏从第1期开始,随后每开启一期游戏,期数自动加1,游戏期数记录表记录了总的下注期数,同时也记录了当前正在进行或者要开启的下一期下注的期数,下注期数记录表结构如下:
a. 记录id
b. 当前正在进行的下注期数
c. 当前正在进行的下注期名称
d. 当前下注开启关闭状态
e. 当前下注结算状态
3. 下注记录表bet_index
在一期下注开启时间窗口,平台用户可以自由下注以及取消下注,下注记录表则记录了用户的下注情况,下注记录表数据结构如下:
a. 记录id
b. 下注期数
c. 个人账户名称
d. 下注资产额
e. 下注信息
三、接口实现设计
a. 质押赎回
1. 用户发起质押操作,参数包括质押资产额
2. 从user账户转账token到dice账户,dice.xxx合约将调用eosio.token的transfer action执行转账操作,将user个人账户中token转账到合约账户dice
3. 修改质押赎回账户表,记录user个人用户的质押信息,添加新记录或者修改记录
4. 用户发起赎回操作
5. 从dice账户转账token到user个人账户,dice.xxx合约将调用eosio.token的transfer action执行转账
6. 修改质押赎回账户表,修改user个人用户的质押信息,修改记录或者删除记录
b. 开启下注、结束下注以及开奖
1. 平台发起一次openbet
2. 检查新一期下注是否合法,检查是否有正在进行的下注,如果没有则当前期数+1,同时下注开启
3. 平台发起一次closurebet
4. 检查关闭的下注是否合法,检查是否有正在进行的下注,如果有,则关闭下注
5. 平台发起一次开奖
6. 计算改期开奖结果,计算池中各用户的下注信息并给出各个用户的开奖结果
7. 开奖结果兑现,将开奖结果兑现到各用户的质押上
c. 下注及取消下注
1. 用户发起下注,参数包括下注资产额、下注信息
2. 检查该期下注是否在开放窗口期,如果不在开放窗口期则不能下注
3. 修改用户质押资产额
4. 修改用户下注资产额以及下注信息
5. 用户发起取消下注
6. 检查该期下注是否在开放窗口期,如果不在开放窗口期则不能取消下注
7. 修改用户下注资产额及下注信息
8. 修改用户质押资产额
四、部署合约
1. 创建合约账户
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 create account eosio dice.xxx EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
2. 部署合约
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 set contract dice.xxx /home/kingnet/tangy/eos/mycontracts/dice.xxx
五、平台接口使用
平台接口需要合约账户
1. 开启下注
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx openbet '{}' -p dice.xxx
2. 关闭下注
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx closurebet '{}' -p dice.xxx
3. 开奖
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx reveal '{}' -p dice.xxx
六、用户接口使用
1. 质押
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx deposit '{"from":"alice", "quantity":"100.0000 EOS"}' -p alice
2. 赎回
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx withdraw '{"to":"alice", "quantity":"100.0000 EOS"}' -p alice
以下用户接口需要在下注开启窗口才能被执行
3. 用户下注
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx offerbet '{"bet":"10.0000 EOS", "player":"alice","info":0}' -p alice
4. 取消下注
cleos --wallet-url http://localhost:9800 --url http://localhost:9800 push action dice.xxx canceloffer '{"player":"alice"}' -p alice
七、合约代码
/**
- @file
- @copyright defined in eos/LICENSE.txt
*/
include
include
include
include <eosiolib/eosio.hpp>
include <eosiolib/asset.hpp>
include <eosiolib/contract.hpp>
include <eosiolib/crypto.h>
using eosio::key256;
using eosio::indexed_by;
using eosio::const_mem_fun;
using eosio::asset;
using eosio::permission_level;
using eosio::action;
using eosio::print;
using eosio::name;
class dice : public eosio::contract {
public:
dice(account_name self) : eosio::contract(self),
bets(_self, _self),
gbet(_self, _self),
accounts(_self, _self) {
}
//@abi action
void offerbet(const asset& bet, account_name player, const uint64_t betinfo) {
eosio_assert( bet.symbol == S(4,EOS) , "only EOS token allowed" );
eosio_assert( bet.is_valid(), "invalid bet" );
eosio_assert( bet.amount > 0, "must bet positive quantity" );
//
require_auth( player );
auto cur_player_itr = accounts.find( player );
eosio_assert(cur_player_itr != accounts.end(), "unknown account");
eosio_assert(cur_player_itr->balance >= bet, "insufficient balance");
auto cur_gbet_itr = gbet.begin();
eosio_assert(cur_gbet_itr != gbet.end(), "bet is not build");
eosio_assert(cur_gbet_itr->open == 1, "bet is not open");
eosio_assert(cur_gbet_itr->reveal == 0, "bet is revealed");
uint64_t betid = cur_gbet_itr->betid;
uint64_t exist_bet = false;
auto bybetid_index = bets.template get_index<N(bybetid)>();
auto cur_bets_itr = bybetid_index.find(betid);
while(cur_bets_itr != bybetid_index.end() && cur_bets_itr->betid == betid) {
if(cur_bets_itr->owner == player) {
exist_bet = true;
break;
}
cur_bets_itr ++;
}
eosio_assert(exist_bet == false, "bet is done");
// Store new offer
auto new_bet_itr = bets.emplace(_self, [&](auto& rbet){
rbet.id = bets.available_primary_key();
rbet.balance = bet;
rbet.owner = player;
rbet.betid = betid;
rbet.info = betinfo;
});
// Update player's accounts
accounts.modify( cur_player_itr, 0, [&](auto& acnt) {
eosio_assert( acnt.balance >= bet, "insufficient balance" );
acnt.balance -= bet;
});
}
//@abi action
void canceloffer(account_name player) {
//
require_auth( player );
auto cur_gbet_itr = gbet.begin();
eosio_assert(cur_gbet_itr != gbet.end(), "bet is not build");
eosio_assert(cur_gbet_itr->open == 1, "bet is closed");
eosio_assert(cur_gbet_itr->reveal == 0, "bet is revealed");
uint64_t betid = cur_gbet_itr->betid;
uint64_t exist_bet = false;
auto bybetid_index = bets.template get_index<N(bybetid)>();
auto cur_bets_itr = bybetid_index.find(betid);
while(cur_bets_itr != bybetid_index.end() && cur_bets_itr->betid == betid) {
if(cur_bets_itr->owner == player) {
exist_bet = true;
break;
}
cur_bets_itr ++;
}
eosio_assert(exist_bet == true, "no bet");
// Update player's accounts
auto cur_player_itr = accounts.find( player );
if( cur_player_itr == accounts.end() ) {
cur_player_itr = accounts.emplace(_self, [&](auto& acnt){
acnt.owner = player;
});
}
accounts.modify( cur_player_itr, 0, [&](auto& acnt) {
acnt.balance += cur_bets_itr->balance;
});
bybetid_index.erase(cur_bets_itr);
//bets.erase(cur_bet_itr);
}
//@abi action
void openbet() {
//
require_auth(_self);
// Create global bet counter if not exists
auto cur_gbet_itr = gbet.begin();
if( cur_gbet_itr == gbet.end() ) {
cur_gbet_itr = gbet.emplace(_self, [&](auto& g_bet){
g_bet.id = gbet.available_primary_key();
g_bet.betid = 0;
g_bet.open = 0;
g_bet.reveal = 1;
});
}
eosio_assert(cur_gbet_itr != gbet.end(), "bet is not build");
eosio_assert(cur_gbet_itr->open == 0, "bet is opened");
eosio_assert(cur_gbet_itr->reveal == 1, "bet is not reveal");
// Increment global bet counter
gbet.modify(cur_gbet_itr, 0, [&](auto& g_bet){
g_bet.betid++;
g_bet.open = 1;
g_bet.reveal = 0;
});
}
//@abi action
void closurebet() {
//
require_auth(_self);
auto cur_gbet_itr = gbet.begin();
eosio_assert(cur_gbet_itr != gbet.end(), "bet is not build");
eosio_assert(cur_gbet_itr->open == 1, "bet is closure");
eosio_assert(cur_gbet_itr->reveal == 0, "reveal is done");
// udpate global bet status
gbet.modify(cur_gbet_itr, 0, [&](auto& g_bet){
g_bet.open = 0;
});
}
//@abi action
void reveal() {
//
require_auth(_self);
auto cur_gbet_itr = gbet.begin();
eosio_assert(cur_gbet_itr != gbet.end(), "bet is not build");
eosio_assert(cur_gbet_itr->open == 0, "bet is open");
eosio_assert(cur_gbet_itr->reveal == 0, "reveal is done");
//
uint64_t bet_result = 0;
uint64_t betid = cur_gbet_itr->betid;
//
{
uint32_t t_now = now();
checksum256 hash;
sha256((char*)(&t_now), sizeof(uint32_t), &hash);
bet_result = (hash.hash[15]) % 2;
}
//
asset total_balance;
asset win_balance;
auto bybetid_index = bets.template get_index<N(bybetid)>();
auto cur_bets_itr = bybetid_index.find(betid);
while(cur_bets_itr != bybetid_index.end() && cur_bets_itr->betid == betid) {
total_balance += cur_bets_itr->balance;
if(cur_bets_itr->info == bet_result)
win_balance += cur_bets_itr->balance;
cur_bets_itr++;
}
if(win_balance.amount == 0) {
bybetid_index = bets.template get_index<N(bybetid)>();
cur_bets_itr = bybetid_index.find(betid);
while(cur_bets_itr != bybetid_index.end() && cur_bets_itr->betid == betid) {
auto use_balance = cur_bets_itr->balance;
auto cur_player_itr = accounts.find( cur_bets_itr->owner );
if( cur_player_itr == accounts.end() ) {
cur_player_itr = accounts.emplace(_self, [&](auto& acnt){
acnt.owner = cur_bets_itr->owner;
});
}
accounts.modify( cur_player_itr, 0, [&]( auto& acnt ) {
acnt.balance += use_balance;
});
cur_bets_itr ++;
}
} else {
bybetid_index = bets.template get_index<N(bybetid)>();
cur_bets_itr = bybetid_index.find(betid);
while(cur_bets_itr != bybetid_index.end() && cur_bets_itr->betid == betid) {
if(cur_bets_itr->info == bet_result) {
//auto use_balance = (cur_bets_itr->balance / win_balance) * total_balance;
auto use_balance = (cur_bets_itr->balance * total_balance.amount) / win_balance.amount;
auto cur_player_itr = accounts.find( cur_bets_itr->owner );
if( cur_player_itr == accounts.end() ) {
cur_player_itr = accounts.emplace(_self, [&](auto& acnt){
acnt.owner = cur_bets_itr->owner;
});
}
accounts.modify( cur_player_itr, 0, [&]( auto& acnt ) {
acnt.balance += use_balance;
});
}
cur_bets_itr ++;
}
}
// udpate global bet status
gbet.modify(cur_gbet_itr, 0, [&](auto& g_bet){
g_bet.reveal = 1;
});
}
//@abi action
void deposit( const account_name from, const asset& quantity ) {
require_auth( from );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must deposit positive quantity" );
auto itr = accounts.find(from);
if( itr == accounts.end() ) {
itr = accounts.emplace(_self, [&](auto& acnt){
acnt.owner = from;
});
}
action(
permission_level{ from, N(active) },
N(eosio.token), N(transfer),
std::make_tuple(from, _self, quantity, std::string(""))
).send();
accounts.modify( itr, 0, [&]( auto& acnt ) {
acnt.balance += quantity;
});
}
//@abi action
void withdraw( const account_name to, const asset& quantity ) {
require_auth( to );
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must withdraw positive quantity" );
auto itr = accounts.find( to );
eosio_assert(itr != accounts.end(), "unknown account");
accounts.modify( itr, 0, [&]( auto& acnt ) {
eosio_assert( acnt.balance >= quantity, "insufficient balance" );
acnt.balance -= quantity;
});
action(
permission_level{ _self, N(active) },
N(eosio.token), N(transfer),
std::make_tuple(_self, to, quantity, std::string(""))
).send();
if( itr->is_empty() ) {
accounts.erase(itr);
}
}
//@abi action
void reset() {
//
require_auth( _self );
auto cur_gbet_itr = gbet.begin();
while(cur_gbet_itr != gbet.end()) {
cur_gbet_itr = gbet.erase(cur_gbet_itr);
}
auto cur_bet_itr = bets.begin();
while(cur_bet_itr != bets.end()) {
cur_bet_itr = bets.erase(cur_bet_itr);
}
}
private:
//@abi table bet i64
struct bet {
uint64_t id;
account_name owner;
asset balance;
uint64_t betid;
uint64_t info;
uint64_t primary_key()const { return id; }
uint64_t by_betid()const { return betid; }
account_name by_owner() const { return owner; }
EOSLIB_SERIALIZE( bet, (id)(owner)(balance)(betid)(info) )
};
typedef eosio::multi_index< N(bet), bet,
indexed_by< N(bybetid), const_mem_fun<bet, uint64_t, &bet::by_betid > >,
indexed_by< N(byowner), const_mem_fun<bet, account_name, &bet::by_owner > > > bet_index;
//@abi table gbet i64
struct gbet {
uint64_t id;
uint64_t betid;
uint64_t betname;
uint64_t open = 0;
uint64_t reveal = 0;
uint64_t primary_key()const { return id; }
EOSLIB_SERIALIZE( gbet, (id)(betid)(betname)(open)(reveal) )
};
typedef eosio::multi_index< N(gbet), gbet> gbet_index;
//@abi table account i64
struct account {
account( account_name o = account_name() ):owner(o){
}
account_name owner;
asset balance;
bool is_empty()const { return !( balance.amount); }
uint64_t primary_key()const { return owner; }
EOSLIB_SERIALIZE( account, (owner)(balance) )
};
typedef eosio::multi_index< N(account), account> account_index;
bet_index bets;
gbet_index gbet;
account_index accounts;
};
EOSIO_ABI( dice, (offerbet)(canceloffer)(openbet)(closurebet)(reveal)(deposit)(withdraw)(reset) )
八、合约ABI
{
"____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-03-29T02:09:11",
"types": [],
"structs": [{
"name": "bet",
"base": "",
"fields": [{
"name": "id",
"type": "uint64"
},{
"name": "owner",
"type": "account_name"
},{
"name": "balance",
"type": "asset"
},{
"name": "betid",
"type": "uint64"
},{
"name": "info",
"type": "uint64"
}
]
},{
"name": "gbet",
"base": "",
"fields": [{
"name": "id",
"type": "uint64"
},{
"name": "betid",
"type": "uint64"
},{
"name": "betname",
"type": "uint64"
},{
"name": "open",
"type": "uint64"
},{
"name": "reveal",
"type": "uint64"
}
]
},{
"name": "account",
"base": "",
"fields": [{
"name": "owner",
"type": "account_name"
},{
"name": "balance",
"type": "asset"
}
]
},{
"name": "offerbet",
"base": "",
"fields": [{
"name": "bet",
"type": "asset"
},{
"name": "player",
"type": "account_name"
},{
"name": "info",
"type": "uint64"
}
]
},{
"name": "canceloffer",
"base": "",
"fields": [{
"name": "player",
"type": "account_name"
}
]
},{
"name": "openbet",
"base": "",
"fields": [
]
},{
"name": "closurebet",
"base": "",
"fields": [
]
},{
"name": "reveal",
"base": "",
"fields": [
]
},{
"name": "deposit",
"base": "",
"fields": [{
"name": "from",
"type": "account_name"
},{
"name": "quantity",
"type": "asset"
}
]
},{
"name": "withdraw",
"base": "",
"fields": [{
"name": "to",
"type": "account_name"
},{
"name": "quantity",
"type": "asset"
}
]
},{
"name": "reset",
"base": "",
"fields": [
]
}
],
"actions": [{
"name": "offerbet",
"type": "offerbet",
"ricardian_contract": ""
},{
"name": "canceloffer",
"type": "canceloffer",
"ricardian_contract": ""
},{
"name": "openbet",
"type": "openbet",
"ricardian_contract": ""
},{
"name": "closurebet",
"type": "closurebet",
"ricardian_contract": ""
},{
"name": "reveal",
"type": "reveal",
"ricardian_contract": ""
},{
"name": "deposit",
"type": "deposit",
"ricardian_contract": ""
},{
"name": "withdraw",
"type": "withdraw",
"ricardian_contract": ""
},{
"name": "reset",
"type": "reset",
"ricardian_contract": ""
}
],
"tables": [{
"name": "bet",
"index_type": "i64",
"key_names": [
"id"
],
"key_types": [
"uint64"
],
"type": "bet"
},{
"name": "gbet",
"index_type": "i64",
"key_names": [
"id"
],
"key_types": [
"uint64"
],
"type": "gbet"
},{
"name": "account",
"index_type": "i64",
"key_names": [
"owner"
],
"key_types": [
"account_name"
],
"type": "account"
}
],
"ricardian_clauses": []
}
EOS博彩合约设计的更多相关文章
- 大型博彩公司招聘 .net,DB,tester,android
大型博彩公司招聘 .net,DB,tester,android,ios等. 等拿完年终奖的朋友,可以先发简历给我,先面试,年后上班. emai:sjchen1203@126.com 要求: 1. 全职 ...
- tyvj1519博彩游戏
博彩游戏 From admin 背景 Background Bob最近迷上了一个博彩游戏…… 描述 Description 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到 ...
- tyvj P1519 博彩游戏(AC自动机+DP滚动数组)
P1519 博彩游戏 背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的 ...
- cocos 水果机,老Tiger虎机流水灯,博彩大转盘效果
原(http://www.cnblogs.com/zisou/p/cocos2d-xZhuanpan.html) 博彩大转盘,转盘抽奖的小系统,这是一个很有意思的游戏模块,游戏中增加这样一些趣味的小模 ...
- 买or不买?如何测试博彩公司赔率是否合理?
世界杯期间,烧烤店.酒吧都热闹起来了,柔柔我的朋友圈也热闹起来了,有酱紫的: 还有酱紫的: 然后还有酱紫的: 酱紫的: 当然天台也是一如既然的热闹: 似乎人人都在输钱,那真正的赢家在哪里呢?博彩业的真 ...
- h5博彩webapp项目实例|h5棋牌游戏|h5博彩app案例
html5实现的博彩webapp.h5棋牌app实例,运用h5+css3+zepto+jQ+swiper+layer等技术进行布控开发,750px最大宽度适配手机端设备,采用flex+rem布局样式. ...
- 【cocos2d-x 手游研发----博彩大转盘】
博彩大转盘,转盘抽奖的小系统,这是一个很有意思的游戏模块,游戏中增加这样一些趣味的小模块,我会附上源码: 会增进玩家的粘性,每天都想来抽两把试试手气: 我做的这个是个矩形风格的转盘,不是那种圆形的转盘 ...
- 博彩游戏(tyvj 1519)
背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的子串,那么就中奖了,否 ...
- [JoyOI1519] 博彩游戏
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背景 Bob最近迷上了一个博彩游戏…… 题目描述 这个游戏的规则是这样的:每花一块钱可以 ...
随机推荐
- JS创建类的方法--简单易懂有实例
版权声明:本文为博主原创文章,转载请注明出处 Javascript是一种基于对象的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有Class. ...
- Python pip常用指令
pip listpip list --outdatedpip list --outdated | grep Jinja2pip uninstall pycurlpip show pycurlpip i ...
- SSM框架之关于使用JSP作为视图展示问题解决方案
JSP作为视图层展示数据,已经有很长一段时间了,不管是在校学习还是企业工作,总会或多或少接触这个.特别是对于一些传统中小型或者一些几年前的企业而言,有很多使用JSP作为视图展示层. JSP本质是就是S ...
- Kafka设计解析(三)Kafka High Availability (下)
转载自 技术世界,原文链接 Kafka设计解析(三)- Kafka High Availability (下) 摘要 本文在上篇文章基础上,更加深入讲解了Kafka的HA机制,主要阐述了HA相关各种场 ...
- (转)WebSocket的原理
前言:无聊逛知乎,就逛到H5的栏目去了,正好看到了关于Websocket的东西.个人是比较喜欢看这类风格的,转到博客分享,以便自己以后理解. ---------------------分割线----- ...
- P2196 挖地雷
题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...
- springMVC框架 对BaseCtrl封装,简化开发
让你的项目有对象,你的项目如何才会有面向对象特征呢?没有面向对象特征的项目不是好项目哦.此篇博文会使用到面向对象特征中的封装继承,还有就是枚举类型.这篇博文教你如何让你的项目体现面向对象特征. 最近公 ...
- 使用SSH命令从一台Linux远程登陆到另一台Linux
命令格式: ssh 用户名@IP 示例: ssh root@192.168.1.10 回车,然后根据提示输入登陆密码即可.
- Vmware 下安装linux虚拟机
由于想自己玩玩linux系统,就想着装一个linux的虚拟机,虚拟机vmware很好找,也很好用,但是linux镜像安装老是出问题,然后就找了很多版本的,最后实验成功一种,在这里分享给大家. 一.安装 ...
- HBase基础概念
定义 非关系型分布式列式数据库,支持大数据量查询(百万,上亿行) 概要 数据存储:HDFS 数据计算:MapReduce/Spark 服务协调:Zookeeper 特征 列式存储(列只有一种类型byt ...