2018-07-23 19:21:31 +00:00
|
|
|
import {
|
|
|
|
NonceTxMiddleware, SignedTxMiddleware, Client,
|
|
|
|
Contract, Address, LocalAddress, CryptoUtils, LoomProvider
|
|
|
|
} from 'loom-js'
|
|
|
|
|
|
|
|
import Web3 from 'web3'
|
2018-07-25 19:57:22 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
|
2018-07-26 20:26:36 +00:00
|
|
|
export const createContract = async (tileStateUpdateHandler) => {
|
2018-07-25 19:57:22 +00:00
|
|
|
const privateKey = CryptoUtils.generatePrivateKey()
|
|
|
|
const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey)
|
|
|
|
|
|
|
|
const client = new Client(
|
|
|
|
'default',
|
2018-08-03 14:59:53 +00:00
|
|
|
'wss://draw.status.im/websocket',
|
|
|
|
'wss://draw.status.im/queryws',
|
2018-07-25 19:57:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
const contract = new web3.eth.Contract(ABI, contractAddress, {from})
|
|
|
|
|
|
|
|
|
|
|
|
contract.events.OnTileMapStateUpdate({}, (err, event) => {
|
|
|
|
if (err) return;
|
2018-07-26 20:26:36 +00:00
|
|
|
tileStateUpdateHandler(event);
|
2018-07-25 19:57:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return contract;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-23 19:21:31 +00:00
|
|
|
|
|
|
|
export default class ContractClient {
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
async createContract() {
|
|
|
|
const privateKey = CryptoUtils.generatePrivateKey()
|
|
|
|
const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey)
|
|
|
|
|
|
|
|
const client = new Client(
|
|
|
|
'default',
|
2018-08-03 13:27:24 +00:00
|
|
|
'wss://127.0.0.1:46657/websocket',
|
|
|
|
'wss://127.0.0.1:9999/queryws',
|
2018-07-23 19:21:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2018-07-25 19:57:22 +00:00
|
|
|
|
|
|
|
return this.contract;
|
2018-07-23 19:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async setTileMapState(data) {
|
|
|
|
await this.contract.methods.SetTileMapState(data).send()
|
|
|
|
}
|
|
|
|
|
|
|
|
async getTileMapState() {
|
|
|
|
return await this.contract.methods.GetTileMapState().call()
|
|
|
|
}
|
|
|
|
}
|