Skip to content
Permalink
Browse files

Changes for Ebakus network

  • Loading branch information
ziogaschr committed Apr 2, 2019
1 parent 0356a7d commit 6fe8ecce30cca567d58ee3b30ce9df3978c03cee
@@ -26,6 +26,8 @@ import NiftyAlert from './NiftyAlert';
class App extends Component {
state = {
web3: null,
ebakus: null,
ebakusWallet: null,
brand: [],
brandItem: [],
isLoading: true,
@@ -42,6 +44,7 @@ class App extends Component {
constructor(props) {
super(props);
this.setWeb3 = this.setWeb3.bind(this);
this.setEbakusWallet = this.setEbakusWallet.bind(this);
}

handleAlertOpen = errmag => {
@@ -81,6 +84,9 @@ class App extends Component {
setWeb3(web3) {
this.setState({ web3 });
}
setEbakusWallet(ebakusWallet) {
this.setState({ ebakusWallet });
}

Elffn = e => {
let wElf = document.querySelector('.Elf0').clientWidth;
@@ -243,7 +249,12 @@ class App extends Component {
<a className="showTutorial" onClick={this.handleOpenTutorial} />
</div>
<IndexUi />
<Ebakus {...this.props} {...this.state} setWeb3={this.setWeb3} />
<Ebakus
{...this.props}
{...this.state}
setWeb3={this.setWeb3}
setEbakusWallet={this.setEbakusWallet}
/>
<Warning {...this.props} />
{this.state.isErrorOpen && (
<NiftyAlert
@@ -30,7 +30,7 @@ import user11 from '../../images/user/user11.png';
import user12 from '../../images/user/user12.png';
import user13 from '../../images/user/user13.png';
import user14 from '../../images/user/user14.png';
import { getCryptoHerosGameAddress } from '../../lib/web3Service';
import { getRPCProvider, getCryptoHerosGameAddress } from '../../lib/web3Service';
import {
doCreateSingleGame,
doGetUserSingleGames,
@@ -169,67 +169,94 @@ export default class extends React.Component {
const tx = {
from: account,
to: getCryptoHerosGameAddress(network),
value: this.props.web3.utils.toWei(String(betEth), 'ether'),
value: web3.utils.toWei(String(betEth), 'ether'),
data: byteData,
};

web3.eth.sendTransaction(tx, (err, response) => {
if(err) {
this.handleAlertOpen("Sorry, transaction failed");
try {
const gas = await web3.eth.estimateGas(tx);
tx.gas = gas;
} catch (e) {
console.error(e);
this.handleAlertOpen('Sorry, transaction failed');
this.setState({
isLoading: false,
});
return;
}

this.props.ebakusWallet
.sendTransaction(tx)
.then(response => {
let t = setInterval(async () => {
const result = await axios.post(getRPCProvider(network), {
method: 'eth_getTransactionReceipt',
params: [response.transactionHash],
id: 1,
jsonrpc: '2.0',
});

if (result.data.result.status === '0x1') {
const gameChecker = window.setInterval(async () => {
const games = await doGetUserSingleGames(network, account);
//FIXME: 透過 user 戰鬥場數來判斷此役戰鬥在合約中是否已確實完成
// 但礙於組建設計不良, 生命週期混亂, 只能透過在 App.js 取得的 historyGamesCount
// 以及本地取得的 historyGames 做雙重判斷, 之後需要更改
if (
games.length <= historyGames.length ||
games.length <= historyGamesCount
) {
return;
}
window.clearInterval(gameChecker);
const gamePromises = games.map(cur =>
getSingleGame(network, cur, account)
);
const gameDetails = await Promise.all(gamePromises);
const thisGame = gameDetails[gameDetails.length - 1];
const userPointer = thisGame[1];
const contractPointer = thisGame[2];
const userBet = thisGame[3];
const gameType = thisGame[4]; // 0 = small win 1 = big win
const isWin = thisGame[5]; // 0 win, 1 lost, 2 平手
const isUserSmall = userPointer < contractPointer;

const battleResult = {
userPointer,
contractPointer,
userBet,
isUserSmall,
gameType,
isWin,
};

window.setTimeout(() => {
this.setState({
isLoading: false,
isShowResult: true,
isShowHistory: false,
hasBattleResult: true,
battleResult,
});
}, 0);
}, 500);
window.clearInterval(t);
}
}, 500);
})
.catch(err => {
let errmsg = 'Sorry, transaction failed'
if (err === 'no_funds') {
errmsg ='Not enough funds. Please add some funds and try again.'
}

this.handleAlertOpen(errmsg);
this.setState({
isLoading: false,
});
return;
}

let t = setInterval(async () => {
const result = await axios.get(`https://api-ropsten.etherscan.io/api?module=transaction&action=gettxreceiptstatus&txhash=${response}&apikey=RAADZVN65BQA7G839DFN3VHWCZBQMRBR11`)
if (result.data.status === "1") {

const gameChecker = window.setInterval(async () => {
const games = await doGetUserSingleGames(network, account);

//FIXME: 透過 user 戰鬥場數來判斷此役戰鬥在合約中是否已確實完成
// 但礙於組建設計不良, 生命週期混亂, 只能透過在 App.js 取得的 historyGamesCount
// 以及本地取得的 historyGames 做雙重判斷, 之後需要更改
if(games.length <= historyGames.length || games.length <= historyGamesCount) {
return;
}
window.clearInterval(gameChecker);
const gamePromises = games.map(cur => getSingleGame(network, cur, account));
const gameDetails = await Promise.all(gamePromises);
const thisGame = gameDetails[gameDetails.length - 1];
const userPointer = thisGame[1];
const contractPointer = thisGame[2];
const userBet = thisGame[3];
const gameType = thisGame[4]; // 0 = small win 1 = big win
const isWin = thisGame[5]; // 0 win, 1 lost, 2 平手
const isUserSmall = userPointer < contractPointer;

const battleResult = {
userPointer,
contractPointer,
userBet,
isUserSmall,
gameType,
isWin,
};

window.setTimeout(() => {
this.setState({
isLoading: false,
isShowResult: true,
isShowHistory: false,
hasBattleResult: true,
battleResult,
});
}, 0);
}, 1234);
window.clearInterval(t);
}
}, 3000);
});
}
});
};

// 看歷史戰鬥
handleShowHistory = async e => {
@@ -2,7 +2,7 @@ import React, { Component } from 'react';
import classnames from 'classnames/bind';
import cardtitle from '../../images/cardtitle.png';
import style from './Card.css';
import { getCryptoHerosTokenAddress } from '../../lib/web3Service';
import { getRPCProvider, getCryptoHerosTokenAddress } from '../../lib/web3Service';
import axios from 'axios';
import LoadingCoin from '../LoadingCoin';
import Button from 'material-ui/Button';
@@ -63,7 +63,7 @@ class Card extends Component {
});
};

handleSubmitEbakus = doMintTx => {
handleSubmitEbakus = async doMintTx => {
const { account, network } = this.props.ebakus;
const { web3 } = this.props;

@@ -74,30 +74,49 @@ class Card extends Component {
data: doMintTx,
};

web3.eth.sendTransaction(msk, this.handleEbakusCallBack);
const gas = await web3.eth.estimateGas(msk);
msk.gas = gas;

this.props.ebakusWallet
.sendTransaction(msk)
.then(res => {
this.handleEbakusCallBack(null, res);
})
.catch(err => {
this.handleEbakusCallBack(err, null);
});
};

handleEbakusCallBack = (err, result) => {
const { network } = this.props.ebakus;

if (err) {
this.setState({ errmsg: 'Sorry, transaction failed' }, () =>
let errmsg = 'Sorry, transaction failed'
if (err === 'no_funds') {
errmsg ='Not enough funds. Please add some funds and try again.'
}
this.setState({ errmsg }, () =>
this.setState({ isOpenAlert: true })
);
console.error('Ebakus Error:', err.message);
console.error('Ebakus Error:', err);
this.setState({ isLoading: false });
return;
}

const tx = result;
let t = setInterval(async () => {
const result = await axios.get(
`https://api-ropsten.etherscan.io/api?module=transaction&action=gettxreceiptstatus&txhash=${tx}&apikey=RAADZVN65BQA7G839DFN3VHWCZBQMRBR11`
);

if (result.data.result.status === '1') {
const result = await axios.post(getRPCProvider(network), {
method: 'eth_getTransactionReceipt',
params: [tx.transactionHash],
id: 1,
jsonrpc: '2.0',
});

if (result.data.result.status === '0x1') {
this.ReloadDataFn();
window.clearInterval(t);
}
}, 3000);
}, 500);
};

ReloadDataFn = () => {

1 comment on commit 6fe8ecc

@bakhanh

This comment has been minimized.

Copy link

bakhanh commented on 6fe8ecc Sep 28, 2019

Hay....

Please sign in to comment.
You can’t perform that action at this time.