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

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstep committed Sep 26, 2017
1 parent 6214405 commit 545be8a
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 1,070 deletions.
2 changes: 0 additions & 2 deletions .env
@@ -1,8 +1,6 @@
# Custom app vars
#APP_BUILD_FOR_WINSERVER = true

APP_DICE_GAMECHANNEL = true

# DevServer settings
HTTPS = false
OPEN_BROWSER = true
Expand Down
9 changes: 0 additions & 9 deletions src/app.background.js
Expand Up @@ -8,14 +8,5 @@ setTimeout(()=>{
return
}

// if (process.env.APP_DICE_GAMECHANNEL) {
// return
// }

Games.runUpdateBalance()

Games.checkDeployTasks()

Games.runServerConfirm()
Games.runBlockchainConfirm()
}, 10000)
9 changes: 1 addition & 8 deletions src/app.client.js
Expand Up @@ -25,19 +25,12 @@ document.addEventListener('DOMContentLoaded',()=>{

if (window.App) {
window.App.view = view
window.Games = Games

// if (process.env.APP_DICE_GAMECHANNEL) {
// return
// }
window.Games = Games

setTimeout(()=>{
Games.startMesh()
}, 3000)

setTimeout(()=>{
Games.startChannelsGames()
}, 1500)
}
})

Expand Down
32 changes: 0 additions & 32 deletions src/model/Api.js
Expand Up @@ -19,38 +19,6 @@ class Api {
return response.text()
})
}


/*
* Confirm
*/
getLogs(address, game_code, game_version){
return this.request({
a: 'unconfirmed',
address: address,
game: game_code,
version: game_version,
}, 'proxy.php').then(r => {
let res = {}
try {
res = r.json()
} catch(e) {
}
return res
})
}

sendConfirm(address, seed, confirm){
return this.request({
a: 'confirm',
address: address,
vconcat: seed,
result: confirm,
},'proxy.php').then( response => {
return response.text()
})
}

}

export default new Api( _config.api_url )
33 changes: 29 additions & 4 deletions src/model/Eth/Eth.js
Expand Up @@ -16,6 +16,8 @@ const web3_sha3 = require('web3/packages/web3-utils').sha3
const rpc = new RPC( _config.rpc_url )
const wallet = new Wallet()

let balances_cache = {}

class Eth {
constructor(){
this.ABI = ABI
Expand Down Expand Up @@ -122,15 +124,32 @@ class Eth {
return web3_sha3(name).substr(2,8)
}

getEthBalance(address, callback){
getEthBalance(address, callback, force=false){
if (!force && balances_cache[address] && balances_cache[address].eth_t > (new Date().getTime()-60*1000) ) {
callback( balances_cache[address].eth )
return
}

this.RPC.request('getBalance', [address, 'latest']).then( response => {
callback( Utils.hexToNum(response.result) / 1000000000000000000 )
if (!balances_cache[address]) {
balances_cache[address] = {}
}

balances_cache[address].eth_t = new Date().getTime()
balances_cache[address].eth = Utils.hexToNum(response.result) / 1000000000000000000

callback( balances_cache[address].eth )
}).catch( err => {
console.error(err)
})
}

getBetsBalance(address, callback){
getBetsBalance(address, callback, force=false){
if (!force && balances_cache[address] && balances_cache[address].bets_t > (new Date().getTime()-60*1000) ) {
callback( balances_cache[address].bets )
return
}

let data = '0x' + this.hashName('balanceOf(address)')
+ Utils.pad(Utils.numToHex(address.substr(2)), 64)

Expand All @@ -141,7 +160,13 @@ class Eth {
'data': data
}, 'latest']
).then( response => {
callback( Utils.hexToNum(response.result) / 100000000 )
if (!balances_cache[address]) {
balances_cache[address] = {}
}
balances_cache[address].bets_t = new Date().getTime()
balances_cache[address].bets = Utils.hexToNum(response.result) / 100000000

callback( balances_cache[address].bets )
}).catch( err => {
console.error(err)
})
Expand Down
8 changes: 7 additions & 1 deletion src/model/Eth/RPC.js
Expand Up @@ -16,10 +16,11 @@ export default class ethRPC {
}

request(method_name=false, params=[], id=1){
console.log('request ', method_name, params)
return new Promise((resolve, reject) => {
try {
let res = this.callMethod(method_name, params, id, (response)=>{
if (response) {
if (response && !response.error) {
resolve( response )
} else {
reject( response )
Expand Down Expand Up @@ -51,10 +52,15 @@ export default class ethRPC {
'params': params
})
}).then( response => {
// console.log(response)
return response.json()
}).then( obj => {
if (obj.error) {
console.error(obj)
}
callback( obj )
}).catch( err => {
console.error(err)
callback( err )
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/Eth/Wallet.js
Expand Up @@ -188,7 +188,7 @@ export default class Wallet {
let options = {
to: contract_address,
nonce: nonce,
gasPrice: '0x'+Utils.numToHex(61000000000),
gasPrice: '0x'+Utils.numToHex(81000000000),
gasLimit: '0x'+Utils.numToHex(gasLimit),
value: 0,
}
Expand Down
31 changes: 22 additions & 9 deletions src/model/dice_gamechannel.js
Expand Up @@ -4,8 +4,15 @@ const GameLogic = function(deposit){

var roll = function(user_bet, user_num, random_hash){
let profit = -user_bet
console.log(random_hash)


if (random_hash.substr(0,2)=='0x') {
random_hash = random_hash.substr(2)
}

const random_num = bigInt(random_hash, 16).divmod(65536).remainder.value

const random_num = bigInt(random_hash,16).divmod(65536).remainder.value
if (user_num > random_num) {
profit = (user_bet * (65536 - 1310) / user_num) - user_bet
}
Expand Down Expand Up @@ -68,7 +75,6 @@ const _channels = {}

class DiceGameChannel {
constructor(){
this.web3 = web3
this.Games = Games

this.setGameContract(contract.address, ()=>{
Expand Down Expand Up @@ -162,17 +168,19 @@ class DiceGameChannel {

let prev_trans = false
const sendTrans = (signedTx, cb, repeat=5)=>{
console.log('TXs:',prev_trans, signedTx)
if (prev_trans==signedTx) return
prev_trans = signedTx

console.log('sendRawTransaction')
Eth.RPC.request('sendRawTransaction', ['0x'+signedTx], 0).then( response => {
repeat--
if (!response || !response.result || response.error) {
if (repeat < 0 || (response && response.error && response.error.code==-32000)) return
setTimeout(()=>{ sendTrans(signedTx, cb) }, 1500)
setTimeout(()=>{ sendTrans(signedTx, cb, repeat) }, 1500)
return
}

prev_trans = signedTx

// Create game logic
Games[data.args.channel_id] = new GameLogic(data.args.player_deposit)

Expand All @@ -182,14 +190,19 @@ class DiceGameChannel {
}

cb( response.result )
}).catch(e=>{
repeat--
console.error('sendTrans Error:', e)
setTimeout(()=>{ sendTrans(signedTx, cb, repeat) }, 1500)
})
}

// open(bytes32 id, address player, uint playerDeposit, uint bankrollDeposit, uint nonce, uint time, bytes sig) {
Eth.Wallet.signedContractFuncTx(
contract.address, contract.abi,
'open', args,
signedTx => { sendTrans(signedTx, callback) }
signedTx => { sendTrans(signedTx, callback) },
900000
)
}

Expand Down Expand Up @@ -287,7 +300,7 @@ class DiceGameChannel {

prepareArgs(args){
if (!args || !args.length) {
return false
return []
}

let new_args = []
Expand Down Expand Up @@ -323,7 +336,7 @@ class DiceGameChannel {
const VRS = Eth.Wallet.lib.signing.signMsgHash(
Eth.Wallet.getKs(),
pwDerivedKey,
this.web3.utils.soliditySha3.apply(this, data2sign),
web3.utils.soliditySha3.apply(this, data2sign),
Eth.Wallet.get().openkey.substr(2)
)

Expand All @@ -333,7 +346,7 @@ class DiceGameChannel {
}

checkMsg(check_data, sig, user_id){
const recover = this.web3.eth.accounts.recover((this.web3.utils.soliditySha3.apply(this, check_data)), sig)
const recover = web3.eth.accounts.recover((web3.utils.soliditySha3.apply(this, check_data)), sig)

return (recover.toLowerCase() == user_id.toLowerCase())
}
Expand Down

0 comments on commit 545be8a

Please sign in to comment.