contracts/app/dapp.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

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';
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';
import ContractClient, { createContract } from './contract_client'
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);
}
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 {
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
}
async createContract() {
this.contractClient = await createContract();
this.createContractEventListener();
this.requestUpdateTilesOnCanvas();
}
createContractEventListener = async () => {
// THIS CURRENTLY DOES NOT WORK DUE POSSIBLE LOOM PROVIDER BUG
this.contractClient.onEvent = tileData => {
console.log('tiledata', tileData)
}
}
async requestUpdateTilesOnCanvas() {
const tileMapState = await this.contractClient.methods.GetTileMapState().call()
if (tileMapState) {
const canvasState = JSON.parse(tileMapState);
this.setState({ canvasState });
}
}
setTileMapState = async (data) => {
await this.contractClient.methods.SetTileMapState(data).send()
}
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(){
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}>
<DrawField setTileMapState={setTileMapState} canvasState={canvasState} />
2018-07-04 18:46:52 +00:00
</Web3Render>
);
2018-05-13 04:31:01 +00:00
}
}
ReactDOM.render(<App></App>, document.getElementById('app'));