Skip to content
This repository has been archived by the owner on Jan 9, 2019. It is now read-only.

get latest changes to dev branch #48

Open
wants to merge 26 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/*
DApps/*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ publisher.json
/dist
/build
/builds
/DApps/new_dice
/DApps/FindTheEthereum

# misc
npm-debug.log
Expand Down
1 change: 0 additions & 1 deletion DApps/FindTheEthereum
Submodule FindTheEthereum deleted from a944ed
150 changes: 150 additions & 0 deletions DApps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@

# 🌐 Decentralized applications

DApps based on protocol **dao.casino** consist of a distributed network of backend-applications called "[bankroller app](https://github.com/DaoCasino/BankRollerApp)", [smart contracts](https://github.com/DaoCasino/Protocol/tree/master/contracts) and frontend part. The [DC.js](https://github.com/DaoCasino/DCLib) library needs to connect all this parts and provide tools for create p2p games.

## Table of Contents

- [General Workflow](#-general-workflow)
- [Offchain](#️-offchain)
- [File structure](#-file-structure)
- [dapp.manifest](#dappmanifest)
- [dapp_logic.js](#dapp_logicjs)
- [index.html](#indexhtml)
- [bankroller.js](#bankrollerjs)
- [Launch examples](#-launch-examples)

## :construction: General Workflow

1. At the beginning the player performs the function `approve` to the `ERC20` contract which allows the contract of the game to deposit funds from the player's account in the amount of the selected deposit [read more about ERC20 approve](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md#approve)
2. The library [DC.js](https://github.com/DaoCasino/DCLib) which is launched in the player's browser finds a suitable bankroller (filter by bankrollers balance and keccak256(logic.js)) and connect to him. We use WebRTC as transport.
3. The player's frontend sends a request that includes [the data (more details here) and the signature by its private key](https://github.com/DaoCasino/Protocol/tree/master/contracts#openchannel) to the bankroller.
4. Bankroll checks the data and opens the channel [открытие канала](https://github.com/DaoCasino/Protocol/tree/master/contracts#openchannel).
5. The contract of the game freezes the player and the bankroller's funds (the bankroller is freezing much more funds to cover the player's winnings).
6. A player and a bankroll play [in offchain](#️-offchain).
7. The player ends the game session.
8. The bankroller checks the data and calls [close channel](https://github.com/DaoCasino/Protocol/tree/master/contracts#openchannel).
9. A smart game contract unfreezes and distributes funds between the player, the bankroll, the operator, the referrer and the game developer in accordance with the set parameters.

![scheme](./scheme.jpg "main scheme")

### multisig

The contract of the channels is the realization of the multi-sig contract. The player signs the obligation and passes it to the bankroller. Participant can send to the contract only the data signed by another participant.

![multisig](./multisig_scheme.png "multisig scheme")

### Disputes

We have implemented a mechanism for solving controversial situations(disputes) for our games smart contracts. In case of fraud, cheated party can send a request to [open a dispute](https://github.com/DaoCasino/Protocol/tree/master/contracts#opendispute). After dispute is opened, other side has a temporary window, to [provide evidence of fair play](https://github.com/DaoCasino/Protocol/tree/master/contracts#closedispute). In case of failure to provide proof, the game ends in favor of the deceived party.

## ⚡️ Offchain

To scale decentralized applications, increase the speed and reduce the cost of transactions, **dao.casino** uses [game channels technology](https://medium.com/@dao.casino/dao-casino-charges-up-dice-game-with-gc-technology-46f6a4bb5df9). It is based on [payment channels](https://en.bitcoin.it/wiki/Payment_channels).[DC.js library](https://github.com/DaoCasino/DCLib) has all the necessary methods for working with *game channels* and *payment channels*.
> ### *Game channels*

*Player and bankroller deciding to start game. Player sends signed hash, bankroller check it and creates transaction to open a channel. When game begins, player sends game state with seed (which is needed for Signidice algorithm) to bankroller. Bankroller signs seed and sends it back — that’s a game process. Depending on game results, participants refresh channel state.
Channel can be closed at any time. To do this, player sends the last state of the channel to the bankroller with a request for closure, after which the bankroller closes the channel.*

## 📁 File structure

|name|description|
|---|---|
|`manifest.json`|Configuration file **bankroller app**|
|`dapp_logic.js`|Basic logic of the aaplication. Must have for both sides (clients and bankroller [bankroller app](https://github.com/DaoCasino/BankRollerApp))|
|`index.html`|Frontend of the game where [DC.js](https://github.com/DaoCasino/DCLib) is connected |
|`bankroller.js`|The part executed inside the [bankroller app](https://github.com/DaoCasino/BankRollerApp)|

### manifest.json

The root folder for each application must contain the manifest.json file

Required fields:

|name|description|
|---|---|
|`name`|title of application|
|`slug`|unique namespace|
|`index`|path to the frontend file (ex. index.html)|
|`logic`|path to the logic.js|
|`run`|path to the backend.js (witch runs on the [bankroller app](https://github.com/DaoCasino/BankRollerApp))|

Example:

```js
{
"name" : "Dice DApp Example",
"code" : "dicedapp_v2",
"index" : "./index.html",
"logic" : "./dapp_logic.js",
"run" : "./bankroller.js"
}
```

### dapp_logic.js

Basic logic of the aaplication. Must have for both sides (clients and bankroller)

Example:

```js
DCLib.defineDAppLogic('dicedapp_v2', function(){
const _self = this
var ping = function(){
return "pong";
}
return _self;
})
````

### index.html

This code executed on the gamer's side (in a browser)

Example:

```html
<script src="https://platform.dao.casino/api/lib/v2/DC.js?v=2"></script> <!-- connect library DC.js -->
<script src="dapp_logic.js"></script> <!-- connect logic file -->
<script>
window.App = new DCLib.DApp({code :'pinpong'})
App.connect({bankroller : "auto"}, function(connected){
if (connected) {
var randomHash = DCLib.Utils.makeSeed();
App.call('ping', [], console.log); // return "pong"
}
});
<script>
```

### bankroller.js

This code executed in the [bankroller app](https://github.com/DaoCasino/BankRollerApp).

Example:

```js
window.MyDApp_debug = (function(){
var myDApp = new DCLib.DApp({code : 'dicedapp_v2'})

// Banroller side code
// console.log(myDApp)

return myDApp
})()
```

## 🔌 Launch examples

1. Download and install [Bankroller app](https://github.com/DaoCasino/BankRollerApp/releases)
2. In **DEV** tab, you find DApp example download it and view sources.
3. Click **Open in browser**.

[Watch video](https://www.youtube.com/watch?v=vD2kI_4IEFA)

## ⛓ Links

* [Minimum viable game](https://daocasino.readme.io/docs)
* [DC Library](https://github.com/DaoCasino/DClib)
* [Bankroller application](https://github.com/DaoCasino/BankRollerApp/releases)
* [Contracts](https://github.com/DaoCasino/Protocol/tree/master/contracts)
11 changes: 7 additions & 4 deletions DApps/dicedapp_v2/bankroller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

//(function(){
window.MultDApp = (function(){
let DApp = new DCLib.DApp({slug : 'dicegame_v3'})

return DApp
window.MyDApp_debug = (function(){
var myDApp = new DCLib.DApp({slug : 'dicetest_v11'})

// Banroller side code
// console.log(myDApp)

return myDApp
})()

4 changes: 2 additions & 2 deletions DApps/dicedapp_v2/dapp.manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "Dice Game v.2",
"slug" : "dicegame_v3",
"name" : "Dice Test v11",
"slug" : "dicetest_v11",

"index" : "./index.html",
"logic" : "./dapp_logic.js",
Expand Down
28 changes: 15 additions & 13 deletions DApps/dicedapp_v2/dapp_logic.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
/**
* Define our DApp logic constructor,
* Define our DApp logic constructor,
* for use it in frontend and bankroller side
*/
DCLib.defineDAppLogic('dicegame_v3', function(){

/* global DCLib */
DCLib.defineDAppLogic('dicetest_v11', function () {
const _self = this

const MAX_RAND_NUM = 65536
const MAX_RAND_NUM = 65535
const HOUSEEDGE = 0.02 // 2%

let history = []

var Roll = function(user_bet, user_num, random_hash){
var Roll = function (user_bet, user_num, random_hash) {
// convert 1BET to 100000000
user_bet = DCLib.Utils.bet2dec(user_bet)

// generate random number
const random_num = DCLib.numFromHash(random_hash, 0, 65536)
const random_num = DCLib.numFromHash(random_hash, 0, MAX_RAND_NUM)

let profit = -user_bet
// if user win
if (user_num >= random_num) {
profit = (user_bet * (MAX_RAND_NUM - MAX_RAND_NUM*HOUSEEDGE) / user_num) - user_bet
profit = (user_bet * (MAX_RAND_NUM - MAX_RAND_NUM * HOUSEEDGE) / user_num) - user_bet
}

// add result to paychannel
_self.payChannel.addTX( profit )
_self.payChannel.addTX(DCLib.Utils.dec2bet(profit), true)
_self.payChannel.printLog()

// push all data to our log
// just for debug
// just for debug
const roll_item = {
timestamp : new Date().getTime(),
user_bet : user_bet,
profit : profit,
user_num : user_num,
balance : _self.payChannel.getBalance(),
random_hash : random_hash,
random_num : random_num,
random_num : random_num
}
history.push(roll_item)

Expand All @@ -45,6 +47,6 @@ DCLib.defineDAppLogic('dicegame_v3', function(){

return {
roll : Roll,
history : history,
history : history
}
})
})
12 changes: 8 additions & 4 deletions DApps/dicedapp_v2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// Create our DApp
DCLib.on('ready', function(){
window.MyDApp = new DCLib.DApp({slug : 'dicegame_v3'})
window.MyDApp = new DCLib.DApp({slug : 'dicetest_v11'})
})


Expand All @@ -44,9 +44,9 @@

function startGame(deposit){
MyDApp.connect({
bankroller : '0x146c5e3b9395738eb67feceb5e37cd5a56d63342',
paychannel : { deposit : deposit } ,
gamedata : {type:'uint', value: [0, 0, 0]}
bankroller : 'auto',
paychannel : { deposit : deposit },
gamedata : {type:'uint', value:[1, 2, 3]}
},
function(connected, info){
console.log('connect result:', connected)
Expand Down Expand Up @@ -147,6 +147,10 @@
var user_num = $('#user_num').val()

callbacks.onRoll(user_bet, user_num)

setTimeout(function(){
$('form.step-2').removeClass('disabled')
},1500)
})

$('form.step-3').on('submit', function(e){
Expand Down
2 changes: 1 addition & 1 deletion DApps/dicedapp_v2/lib/DC.js

Large diffs are not rendered by default.

Binary file modified DApps/example.zip
Binary file not shown.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@
[![Github All Releases](https://img.shields.io/github/downloads/DaoCasino/BankRollerApp/total.svg)]()
[![Github All Releases](https://img.shields.io/github/release/DaoCasino/BankRollerApp/all.svg)]()

Download app from the release section https://github.com/DaoCasino/BankRollerApp/releases

BankrollerApp is a basic part of Dao.Casino protocol. This is a desktop app, for bankrollers and gamedevelopers the execution environment for [DApps](https://github.com/DaoCasino/BankRollerApp/tree/master/DApps).

App includes:
* Ethereum wallet with BET token support
* Developer tools, for run, testing and deploy [DApps](https://github.com/DaoCasino/BankRollerApp/tree/master/DApps).

# Get it
Download app from the release section https://github.com/DaoCasino/BankRollerApp/releases
<img src="https://raw.githubusercontent.com/DaoCasino/BankRollerApp/master/public/assets/img/mac_screenshot.png">

## Develop
You need nodejs 6.9+
# Develop
## run
You need nodejs 8.9+
Xcode for MacOS
<pre>npm install && npm start</pre>
```
git clone --depth=1 https://github.com/DaoCasino/BankRollerApp && cd BankRollerApp
npm install && npm start
```

## build
```
npm run build
npm run build_electron_mac
npm run build_electron_windows
npm run build_electron_linux
```


# Contribute
As an open-source project we welcome any kind of community involvement. Whether that is by contributing code, reporting issues or engaging in insightful discussions.
64 changes: 64 additions & 0 deletions node_modules/pull-ws/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading