snt-gas-relay/test-dapp/app/components/callgasrelayed.js

360 lines
13 KiB
JavaScript
Raw Normal View History

2018-08-07 02:30:16 +00:00
import React, {Component} from 'react';
import StatusGasRelayer, {Contracts, Functions} from '../status-gas-relayer';
2018-08-07 02:30:16 +00:00
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';
2018-08-07 02:30:16 +00:00
import Grid from '@material-ui/core/Grid';
import MySnackbarContentWrapper from './snackbar';
2018-08-07 02:30:16 +00:00
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';
2018-08-07 02:30:16 +00:00
const styles = theme => ({
root: {
width: '100%',
backgroundColor: theme.palette.background.paper
},
card: {
marginBottom: theme.spacing.unit * 3
}
});
2018-08-07 02:30:16 +00:00
window.TestContract = TestContract;
class CallGasRelayed extends Component {
2018-08-07 02:30:16 +00:00
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,
2018-08-24 19:21:26 +00:00
relayer: '',
2018-08-07 02:30:16 +00:00
transactionError: '',
messagingError: '',
submitting: false
};
}
componentDidMount(){
EmbarkJS.onReady(() => {
this.setState({
gasToken: STT.options.address
});
});
}
2018-07-20 16:05:12 +00:00
2018-08-07 02:30:16 +00:00
handleChange = name => event => {
this.setState({
2018-08-07 02:30:16 +00:00
[name]: event.target.value
});
2018-08-07 02:30:16 +00:00
};
2018-09-01 04:45:28 +00:00
sign = async (event) => {
2018-08-07 02:30:16 +00:00
if(event) event.preventDefault();
this.setState({
msgSent: false,
transactionError: ''
});
2018-08-07 02:30:16 +00:00
try {
2018-09-01 04:45:28 +00:00
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)
.setContractFunction(Functions.Identity.call)
2018-09-01 04:45:28 +00:00
.setTransaction(this.state.to, this.state.value, this.state.data)
.setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit);
2018-09-01 11:23:24 +00:00
2018-09-01 04:45:28 +00:00
const signature = await s.sign(web3);
this.setState({signature});
2018-08-07 02:30:16 +00:00
} catch(error){
this.setState({transactionError: error.message});
}
}
obtainRelayers = async event => {
2018-08-07 02:30:16 +00:00
event.preventDefault();
const {web3, kid, skid} = this.props;
2018-08-07 02:30:16 +00:00
this.setState({
messagingError: '',
submitting: true
});
this.props.clearMessages();
2018-08-24 19:21:26 +00:00
try {
const s = new StatusGasRelayer.AvailableRelayers(Contracts.Identity, this.props.identityAddress, web3.eth.defaultAccount)
.setRelayersSymKeyID(skid)
.setAsymmetricKeyID(kid)
.setGas(this.state.gasToken, this.state.gasPrice);
await s.post(web3);
console.log("Message sent");
this.setState({submitting: false});
2018-08-24 19:21:26 +00:00
} catch(error){
this.setState({messagingError: error.message, submitting: false});
}
}
2018-09-01 11:23:24 +00:00
sendTransaction = async event => {
2018-08-24 19:21:26 +00:00
event.preventDefault();
const {web3, kid} = this.props;
let relayer = this.state.relayer;
if(relayer == '' && this.props.relayers.length == 1){
relayer = this.props.relayers[0];
}
this.setState({
messagingError: '',
submitting: true
});
this.props.clearMessages();
2018-08-07 02:30:16 +00:00
try {
2018-09-01 11:23:24 +00:00
const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount)
.setContractFunction(Functions.Identity.call)
2018-09-01 11:23:24 +00:00
.setTransaction(this.state.to, this.state.value, this.state.data)
.setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit)
.setRelayer(relayer)
.setAsymmetricKeyID(kid);
await s.post(this.state.signature, web3);
this.setState({submitting: false});
console.log("Message sent");
2018-08-07 02:30:16 +00:00
} catch(error){
this.setState({messagingError: error.message, submitting: false});
}
}
2018-08-07 02:30:16 +00:00
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});
}
2018-08-07 02:30:16 +00:00
testContractDataCall = () => {
TestContract.methods.val().call().then(value => console.log({message: "TestContract.val(): " + value}));
}
2018-08-07 02:30:16 +00:00
render(){
const {classes} = this.props;
return <div>
2018-08-10 18:37:50 +00:00
<Card className={classes.card}>
<CardContent>
<b>This functionality is used when a Identity will invoke a contract function or transfer ether without paying fees </b>
</CardContent>
</Card>
2018-08-07 02:30:16 +00:00
{ this.state.transactionError && <MySnackbarContentWrapper variant="error" message={this.state.transactionError} /> }
<Card className={classes.card}>
<CardHeader title="1. Transaction Data" />
<CardContent>
<form noValidate autoComplete="off">
<Grid container spacing={24}>
<Grid item xs={5}>
<TextField
id="to"
label="To"
value={this.state.to}
onChange={this.handleChange('to')}
margin="normal"
fullWidth
/>
</Grid>
<Grid item xs={2}>
<TextField
id="value"
label="Value"
value={this.state.value}
onChange={this.handleChange('value')}
margin="normal"
fullWidth
/>
</Grid>
<Grid item xs={5}>
<TextField
id="data"
label="Data"
value={this.state.data}
onChange={this.handleChange('data')}
margin="normal"
fullWidth
/>
</Grid>
<Grid item xs={2}>
<TextField
id="nonce"
label="Nonce"
value={this.props.nonce}
margin="normal"
fullWidth
InputProps={{
readOnly: true
}}
/>
</Grid>
<Grid item xs={6}>
<TextField
id="gasToken"
label="Gas Token"
value={this.state.gasToken}
onChange={this.handleChange('gasToken')}
margin="normal"
fullWidth
select
SelectProps={{
native: true
}}
>
<option key={STT.options.address} value={STT.options.address}>
{STT.options.address} (STT)
</option>
<option key="0x0000000000000000000000000000000000000000" value="0x0000000000000000000000000000000000000000">
0x0000000000000000000000000000000000000000 (ETH)
</option>
</TextField>
</Grid>
<Grid item xs={2}>
<TextField
id="gasPrice"
label="Gas Price"
value={this.state.gasPrice}
onChange={this.handleChange('gasPrice')}
margin="normal"
fullWidth
/>
</Grid>
<Grid item xs={2}>
<TextField
id="gasLimit"
label="Gas Limit"
value={this.state.gasLimit}
onChange={this.handleChange('gasLimit')}
margin="normal"
fullWidth
/>
</Grid>
</Grid>
</form>
</CardContent>
<CardActions>
<Button color="primary" onClick={this.sign}>
Sign Message
</Button>
<Button size="small" onClick={this.testContractDataSend}>TestContract.methods.test().send()</Button>
<Button size="small" onClick={this.testContractDataCall}>TestContract.methods.test().call()</Button>
2018-08-07 02:30:16 +00:00
</CardActions>
</Card>
2018-08-07 02:30:16 +00:00
{ this.state.messagingError && <MySnackbarContentWrapper variant="error" message={this.state.messagingError} /> }
2018-08-24 19:21:26 +00:00
2018-08-07 02:30:16 +00:00
<Card className={classes.card}>
2018-08-24 19:21:26 +00:00
<CardHeader title="2. Find Available Relayers" />
2018-08-07 02:30:16 +00:00
<CardContent>
<TextField
id="symKey"
label="Symmetric Key"
value={config.relaySymKey}
margin="normal"
fullWidth
InputProps={{
readOnly: true
}}
/>
<TextField
id="topic"
label="Whisper Topic"
value={this.state.topic}
margin="normal"
InputProps={{
readOnly: true
}}
/>
</CardContent>
<CardActions>
2018-08-24 19:21:26 +00:00
<Button size="small" color="primary" onClick={this.obtainRelayers} disabled={this.state.submitting}>
Send &quot;availability&quot; Message
</Button>
</CardActions>
</Card>
<Card className={classes.card}>
<CardHeader title="3. Generate Transaction" />
<CardContent>
<TextField
id="relayer"
label="Relayer"
value={this.state.relayer}
onChange={this.handleChange('relayer')}
margin="normal"
fullWidth
select
SelectProps={{
native: true
}}
>
{
this.props.relayers.length > 0 ?
this.props.relayers.map((r, i) => <option key={i} value={r}>Relayer #{i+1}: {r}</option>)
:
<option></option>
}
</TextField>
<TextField
id="signature"
label="Signed Message"
value={this.state.signature}
margin="normal"
fullWidth
InputProps={{
readOnly: true
}}
/>
</CardContent>
<CardActions>
<Button size="small" color="primary" onClick={this.sendTransaction} disabled={this.state.submitting}>
Send &quot;transaction&quot; Message
2018-08-07 02:30:16 +00:00
</Button>
</CardActions>
</Card>
</div>;
}
}
2018-08-07 02:30:16 +00:00
CallGasRelayed.propTypes = {
classes: PropTypes.object.isRequired,
nonce: PropTypes.string.isRequired,
identityAddress: PropTypes.string,
web3: PropTypes.object,
kid: PropTypes.string,
skid: PropTypes.string,
2018-08-24 19:21:26 +00:00
clearMessages: PropTypes.func,
relayers: PropTypes.array.isRequired
2018-08-07 02:30:16 +00:00
};
export default withStyles(styles)(CallGasRelayed);