import React, {Component} from 'react'; import Button from '@material-ui/core/Button'; import Card from '@material-ui/core/Card'; import CardActions from '@material-ui/core/CardActions'; import CardContent from '@material-ui/core/CardContent'; import CardHeader from '@material-ui/core/CardHeader'; import EmbarkJS from 'Embark/EmbarkJS'; import Grid from '@material-ui/core/Grid'; import IdentityGasRelay from 'Embark/contracts/IdentityGasRelay'; import MySnackbarContentWrapper from './snackbar'; import PropTypes from 'prop-types'; import STT from 'Embark/contracts/STT'; import TestContract from 'Embark/contracts/TestContract'; import TextField from '@material-ui/core/TextField'; import config from '../config'; import web3 from 'Embark/web3'; import {withStyles} from '@material-ui/core/styles'; const styles = theme => ({ root: { width: '100%', backgroundColor: theme.palette.background.paper }, card: { marginBottom: theme.spacing.unit * 3 } }); window.TestContract = TestContract; class CallGasRelayed extends Component { constructor(props){ super(props); this.state = { topic: '0x4964656e', to: '0x0000000000000000000000000000000000000000', value: 0, data: '0x00', gasPrice: 0, gasLimit: 0, gasToken: "0x0000000000000000000000000000000000000000", signature: '', kid: null, skid: null, msgSent: '', payload: '', message: '', web3js: null, transactionError: '', messagingError: '', submitting: false }; } componentDidMount(){ EmbarkJS.onReady(() => { this.setState({ gasToken: STT.options.address }); }); } handleChange = name => event => { this.setState({ [name]: event.target.value }); }; sign = (event) => { if(event) event.preventDefault(); this.setState({ msgSent: false, transactionError: '' }); IdentityGasRelay.options.address = this.props.identityAddress; try { IdentityGasRelay.methods.callGasRelayHash( this.state.to, this.state.value, web3.utils.soliditySha3({t: 'bytes', v: this.state.data}), this.props.nonce, this.state.gasPrice, this.state.gasLimit, this.state.gasToken ) .call() .then(message => { return web3.eth.sign(message, web3.eth.defaultAccount); }) .then(signature => { this.setState({signature}); }); } catch(error){ this.setState({transactionError: error.message}); } } sendMessage = event => { event.preventDefault(); const {web3, kid, skid} = this.props; this.setState({ messagingError: '', submitting: true }); this.props.clearMessages(); try { let jsonAbi = IdentityGasRelay._jsonInterface.filter(x => x.name == "callGasRelayed")[0]; let funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, [ this.state.to, this.state.value, this.state.data, this.props.nonce, this.state.gasPrice, this.state.gasLimit, this.state.gasToken, this.state.signature ]); const sendOptions = { ttl: 1000, sig: kid, powTarget: 1, powTime: 20, topic: this.state.topic, symKeyID: skid, payload: web3.utils.toHex({ 'address': this.props.identityAddress, 'encodedFunctionCall': funCall }) }; web3.shh.post(sendOptions) .then(() => { this.setState({submitting: false}); console.log("Message sent"); return true; }); } catch(error){ this.setState({messagingError: error.message, submitting: false}); } } testContractDataSend = () => { let jsonAbi = TestContract._jsonInterface.filter(x => x.name == "test")[0]; let funCall = web3.eth.abi.encodeFunctionCall(jsonAbi, []); this.setState({data: funCall, to: TestContract.options.address}); } testContractDataCall = () => { TestContract.methods.val().call().then(value => console.log({message: "TestContract.val(): " + value})); } render(){ const {classes} = this.props; return
{ this.state.transactionError && }
{ this.state.messagingError && }
; } } CallGasRelayed.propTypes = { classes: PropTypes.object.isRequired, nonce: PropTypes.string.isRequired, identityAddress: PropTypes.string, web3: PropTypes.object, kid: PropTypes.string, skid: PropTypes.string, clearMessages: PropTypes.func }; export default withStyles(styles)(CallGasRelayed);