2018-06-26 16:46:06 +00:00
|
|
|
import React, { Fragment } from 'react';
|
2018-05-13 04:31:01 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
2018-06-28 20:24:28 +00:00
|
|
|
import web3 from "Embark/web3"
|
2018-05-13 04:31:01 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
2018-05-24 12:11:33 +00:00
|
|
|
import SNT from 'Embark/contracts/SNT';
|
2018-07-12 19:31:47 +00:00
|
|
|
import Web3Render from './components/standard/Web3Render';
|
2018-07-19 21:28:32 +00:00
|
|
|
import DrawField from './components/draw/DrawField';
|
2018-08-11 21:52:45 +00:00
|
|
|
//import ContractClient, { createContract } from './contract_client'
|
2018-08-12 15:04:54 +00:00
|
|
|
import ContractClient from './client_contractgo';
|
|
|
|
import Events from './chain_client/events';
|
2018-05-24 12:11:33 +00:00
|
|
|
window['SNT'] = SNT;
|
2018-05-13 04:31:01 +00:00
|
|
|
|
|
|
|
import './dapp.css';
|
|
|
|
|
2018-07-05 18:31:51 +00:00
|
|
|
const MAINNET = 1;
|
|
|
|
|
2018-05-13 04:31:01 +00:00
|
|
|
class App extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
2018-07-18 10:29:59 +00:00
|
|
|
state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: true, loading: true };
|
2018-05-13 04:31:01 +00:00
|
|
|
|
2018-05-21 18:29:57 +00:00
|
|
|
componentDidMount(){
|
2018-07-04 18:46:52 +00:00
|
|
|
EmbarkJS.onReady((err) => {
|
|
|
|
if (err) this.setState({ web3Provider: false });
|
|
|
|
else {
|
2018-07-25 19:57:22 +00:00
|
|
|
this.createContract();
|
2018-07-04 18:46:52 +00:00
|
|
|
this._setAccounts();
|
|
|
|
}
|
2018-07-05 18:31:51 +00:00
|
|
|
web3.eth.net.getId((err, netId) => {
|
2018-07-19 21:28:32 +00:00
|
|
|
//if (netId !== MAINNET) this.setState({ web3Provider: false})
|
2018-07-05 18:31:51 +00:00
|
|
|
})
|
2018-07-04 18:46:52 +00:00
|
|
|
})
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 19:57:22 +00:00
|
|
|
async createContract() {
|
2018-07-26 20:26:36 +00:00
|
|
|
const { tileStateUpdateHandler } = this;
|
2018-08-11 21:52:45 +00:00
|
|
|
this.contractClient = await new ContractClient();
|
2018-08-12 15:04:54 +00:00
|
|
|
this.events = new Events();
|
2018-08-11 21:52:45 +00:00
|
|
|
await this.contractClient.createContract();
|
2018-08-12 15:04:54 +00:00
|
|
|
this.events.onEvent = tileData => this.tileStateUpdateHandler(tileData)
|
2018-07-25 19:57:22 +00:00
|
|
|
this.requestUpdateTilesOnCanvas();
|
|
|
|
}
|
|
|
|
|
2018-07-26 20:26:36 +00:00
|
|
|
tileStateUpdateHandler = tileData => {
|
|
|
|
console.log('tile state update event received');
|
2018-08-12 15:04:54 +00:00
|
|
|
this.setState({ canvasState: tileData });
|
2018-07-25 19:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async requestUpdateTilesOnCanvas() {
|
2018-08-11 21:52:45 +00:00
|
|
|
const tileMapState = await this.contractClient.getTileMapState()
|
|
|
|
const tileData = tileMapState.getData()
|
|
|
|
if (tileData) {
|
|
|
|
const canvasState = JSON.parse(tileData);
|
2018-07-25 19:57:22 +00:00
|
|
|
this.setState({ canvasState });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTileMapState = async (data) => {
|
2018-08-11 21:52:45 +00:00
|
|
|
await this.contractClient.setTileMapState(JSON.stringify(data))
|
2018-07-25 19:57:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 13:49:22 +00:00
|
|
|
setAccount(_account){
|
|
|
|
this.setState({account: _account});
|
|
|
|
}
|
2018-05-13 04:31:01 +00:00
|
|
|
|
2018-06-28 20:24:28 +00:00
|
|
|
_setAccounts() {
|
|
|
|
const { fromWei } = web3.utils;
|
|
|
|
web3.eth.getAccounts(async (err, [address]) => {
|
|
|
|
const balance = await SNT.methods.balanceOf(address).call();
|
|
|
|
this.setState({ snt: { balance: fromWei(balance) }});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-13 04:31:01 +00:00
|
|
|
render(){
|
2018-07-25 19:57:22 +00:00
|
|
|
const { setTileMapState } = this;
|
|
|
|
const { web3Provider, loading, canvasState } = this.state;
|
2018-05-17 19:53:23 +00:00
|
|
|
return (
|
2018-07-04 18:46:52 +00:00
|
|
|
<Web3Render ready={web3Provider}>
|
2018-08-11 21:52:45 +00:00
|
|
|
<DrawField setTileMapState={setTileMapState} canvasState={canvasState} request={this.requestUpdateTilesOnCanvas.bind(this)}/>
|
2018-07-04 18:46:52 +00:00
|
|
|
</Web3Render>
|
2018-06-26 16:46:06 +00:00
|
|
|
);
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App></App>, document.getElementById('app'));
|