Skip to content

Commit

Permalink
Changes for Ebakus network
Browse files Browse the repository at this point in the history
  • Loading branch information
ziogaschr committed Apr 2, 2019
1 parent 0356a7d commit 6fe8ecc
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 217 deletions.
13 changes: 12 additions & 1 deletion dapp/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import NiftyAlert from './NiftyAlert';
class App extends Component {
state = {
web3: null,
ebakus: null,
ebakusWallet: null,
brand: [],
brandItem: [],
isLoading: true,
Expand All @@ -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 => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
137 changes: 82 additions & 55 deletions dapp/src/components/Arena/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down
41 changes: 30 additions & 11 deletions dapp/src/components/Card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,7 +63,7 @@ class Card extends Component {
});
};

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

Expand All @@ -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 = () => {
Expand Down
Loading

1 comment on commit 6fe8ecc

@bakhanh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hay....

Please sign in to comment.