add loom-js + contract interaction
This commit is contained in:
parent
319fcf7352
commit
7fa2acb3c4
|
@ -0,0 +1,45 @@
|
|||
import {
|
||||
NonceTxMiddleware, SignedTxMiddleware, Client,
|
||||
Contract, Address, LocalAddress, CryptoUtils, LoomProvider
|
||||
} from 'loom-js'
|
||||
|
||||
import Web3 from 'web3'
|
||||
|
||||
export default class ContractClient {
|
||||
constructor() {}
|
||||
|
||||
async createContract() {
|
||||
const privateKey = CryptoUtils.generatePrivateKey()
|
||||
const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey)
|
||||
|
||||
const client = new Client(
|
||||
'default',
|
||||
'ws://127.0.0.1:46657/websocket',
|
||||
'ws://127.0.0.1:9999/queryws',
|
||||
)
|
||||
|
||||
const from = LocalAddress.fromPublicKey(publicKey).toString()
|
||||
const web3 = new Web3(new LoomProvider(client, privateKey))
|
||||
const ABI = [{"constant":false,"inputs":[{"name":"_tileState","type":"string"}],"name":"SetTileMapState","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"GetTileMapState","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"state","type":"string"}],"name":"OnTileMapStateUpdate","type":"event"}]
|
||||
|
||||
const loomContractAddress = await client.getContractAddressAsync('TilesChain')
|
||||
const contractAddress = CryptoUtils.bytesToHexAddr(loomContractAddress.local.bytes)
|
||||
|
||||
this.contract = new web3.eth.Contract(ABI, contractAddress, {from})
|
||||
|
||||
this.contract.events.OnTileMapStateUpdate({}, (err, event) => {
|
||||
if (err) return;
|
||||
if (this.onEvent) {
|
||||
this.onEvent(event)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async setTileMapState(data) {
|
||||
await this.contract.methods.SetTileMapState(data).send()
|
||||
}
|
||||
|
||||
async getTileMapState() {
|
||||
return await this.contract.methods.GetTileMapState().call()
|
||||
}
|
||||
}
|
53
app/dapp.js
53
app/dapp.js
|
@ -11,6 +11,7 @@ import Web3Render from './components/standard/Web3Render';
|
|||
import fetchIdeas from './utils/fetchIdeas';
|
||||
import { getPolls, omitPolls } from './utils/polls';
|
||||
import DrawField from './components/draw/DrawField';
|
||||
import ContractClient from './contract_client'
|
||||
window['SNT'] = SNT;
|
||||
|
||||
import './dapp.css';
|
||||
|
@ -28,13 +29,12 @@ class App extends React.Component {
|
|||
EmbarkJS.onReady((err) => {
|
||||
if (err) this.setState({ web3Provider: false });
|
||||
else {
|
||||
this._getPolls();
|
||||
//this.contractClient = new ContractClient()
|
||||
this._setAccounts();
|
||||
}
|
||||
web3.eth.net.getId((err, netId) => {
|
||||
//if (netId !== MAINNET) this.setState({ web3Provider: false})
|
||||
})
|
||||
fetchIdeas().then(ideaSites => { this.setState({ ideaSites })});
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -42,14 +42,6 @@ class App extends React.Component {
|
|||
this.setState({account: _account});
|
||||
}
|
||||
|
||||
_getPolls = async () => {
|
||||
this.setState({ loading: true })
|
||||
const { nPolls, poll } = PollManager.methods;
|
||||
const polls = await nPolls().call();
|
||||
if (polls) getPolls(polls, poll).then(omitPolls).then(rawPolls => { this.setState({ rawPolls, loading: false })});
|
||||
else this.setState({ rawPolls: [], loading: false });
|
||||
}
|
||||
|
||||
_setAccounts() {
|
||||
const { fromWei } = web3.utils;
|
||||
web3.eth.getAccounts(async (err, [address]) => {
|
||||
|
@ -58,50 +50,11 @@ class App extends React.Component {
|
|||
})
|
||||
}
|
||||
|
||||
updatePoll = async (idPoll) => {
|
||||
const { poll, nPolls } = PollManager.methods;
|
||||
const { rawPolls } = this.state;
|
||||
const npolls = await nPolls().call();
|
||||
// This check needs to be done because of a bug in web3
|
||||
if (npolls !== rawPolls.length) return this._getPolls();
|
||||
const newPolls = [...rawPolls];
|
||||
const updatedPoll = await poll(idPoll).call();
|
||||
newPolls[idPoll] = { ...updatedPoll };
|
||||
this.setState({ rawPolls: newPolls });
|
||||
}
|
||||
|
||||
appendToPoll = (idPoll, data) => {
|
||||
const { rawPolls } = this.state;
|
||||
const newPolls = [...rawPolls];
|
||||
newPolls[idPoll] = { ...newPolls[idPoll], ...data }
|
||||
this.setState({ rawPolls: newPolls });
|
||||
}
|
||||
|
||||
setPollOrder = pollOrder => { this.setState({ pollOrder }) }
|
||||
|
||||
_renderStatus(title, available) {
|
||||
let className = available ? 'pull-right status-online' : 'pull-right status-offline';
|
||||
return <Fragment>
|
||||
{title}
|
||||
<span className={className}></span>
|
||||
</Fragment>;
|
||||
}
|
||||
|
||||
render(){
|
||||
const { admin, web3Provider, loading } = this.state;
|
||||
const { _getPolls, updatePoll, setPollOrder, appendToPoll } = this;
|
||||
const toggleAdmin = () => this.setState({ admin: true });
|
||||
const votingContext = { getPolls: _getPolls, toggleAdmin, updatePoll, appendToPoll, setPollOrder, ...this.state };
|
||||
const { web3Provider, loading } = this.state;
|
||||
return (
|
||||
<Web3Render ready={web3Provider}>
|
||||
<DrawField />
|
||||
{false && <VotingContext.Provider value={votingContext}>
|
||||
<Fragment>
|
||||
{admin ?
|
||||
<AdminView setAccount={this.setAccount} /> :
|
||||
<Voting />}
|
||||
</Fragment>
|
||||
</VotingContext.Provider>}
|
||||
</Web3Render>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
"formik": "^0.11.11",
|
||||
"jquery": "^3.3.1",
|
||||
"lodash": "^4.17.10",
|
||||
"loom-js": "^1.14.0",
|
||||
"react": "^16.3.2",
|
||||
"react-blockies": "^1.3.0",
|
||||
"react-bootstrap": "^0.32.1",
|
||||
|
|
Loading…
Reference in New Issue