2018-08-09 21:23:32 +00:00
|
|
|
import React, {Component} from 'react';
|
2018-09-01 20:35:56 +00:00
|
|
|
import StatusGasRelayer, {Contracts} from '../status-gas-relayer';
|
2018-08-09 21:23:32 +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 Grid from '@material-ui/core/Grid';
|
|
|
|
import MySnackbarContentWrapper from './snackbar';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import SNTController from 'Embark/contracts/SNTController';
|
2018-08-10 18:25:39 +00:00
|
|
|
import STT from 'Embark/contracts/STT';
|
2018-08-09 21:23:32 +00:00
|
|
|
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-09-01 20:35:56 +00:00
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
const styles = theme => ({
|
|
|
|
root: {
|
|
|
|
width: '100%',
|
|
|
|
backgroundColor: theme.palette.background.paper
|
|
|
|
},
|
|
|
|
card: {
|
|
|
|
marginBottom: theme.spacing.unit * 3
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.TestContract = TestContract;
|
2018-09-14 18:48:34 +00:00
|
|
|
window.SNTController = SNTController;
|
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
class TransferSNT extends Component {
|
|
|
|
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
topic: '0x534e5443',
|
2018-10-08 18:14:19 +00:00
|
|
|
account: '',
|
2018-08-09 21:23:32 +00:00
|
|
|
to: '0x0000000000000000000000000000000000000000',
|
|
|
|
amount: 0,
|
|
|
|
gasPrice: 0,
|
|
|
|
signature: '',
|
|
|
|
kid: null,
|
|
|
|
skid: null,
|
|
|
|
msgSent: '',
|
|
|
|
payload: '',
|
|
|
|
message: '',
|
2018-08-24 19:21:26 +00:00
|
|
|
relayer: '',
|
2018-08-09 21:23:32 +00:00
|
|
|
web3js: null,
|
|
|
|
transactionError: '',
|
|
|
|
messagingError: '',
|
|
|
|
submitting: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange = name => event => {
|
2018-09-14 15:35:46 +00:00
|
|
|
if(name == 'relayer'){
|
|
|
|
this.props.updateRelayer(event.target.value);
|
|
|
|
}
|
2018-08-09 21:23:32 +00:00
|
|
|
this.setState({
|
|
|
|
[name]: event.target.value
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-10 18:25:39 +00:00
|
|
|
getBalance = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
STT.methods.balanceOf(this.state.to)
|
|
|
|
.call()
|
|
|
|
.then(balance => {
|
|
|
|
console.log("Balance of " + this.state.to + ": " + balance + " STT");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-01 20:35:56 +00:00
|
|
|
sign = async (event) => {
|
2018-08-09 21:23:32 +00:00
|
|
|
if(event) event.preventDefault();
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
msgSent: false,
|
|
|
|
transactionError: ''
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2018-10-08 18:14:19 +00:00
|
|
|
this.setState({account: web3.eth.defaultAccount});
|
|
|
|
|
|
|
|
const s = new StatusGasRelayer.SNTController(SNTController.options.address, web3.eth.defaultAccount)
|
2018-09-01 20:35:56 +00:00
|
|
|
.transferSNT(this.state.to, this.state.amount)
|
|
|
|
.setGas(this.state.gasPrice);
|
|
|
|
|
|
|
|
const signature = await s.sign(web3);
|
|
|
|
|
|
|
|
this.setState({signature});
|
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
} catch(error){
|
|
|
|
this.setState({transactionError: error.message});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 18:14:19 +00:00
|
|
|
obtainRelayers = async (event) => {
|
2018-08-09 21:23:32 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const {web3, kid, skid} = this.props;
|
|
|
|
|
2018-08-24 19:21:26 +00:00
|
|
|
this.setState({
|
|
|
|
messagingError: '',
|
|
|
|
submitting: true
|
|
|
|
});
|
|
|
|
this.props.clearMessages();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2018-10-08 18:14:19 +00:00
|
|
|
const s = new StatusGasRelayer.AvailableRelayers(Contracts.SNT, SNTController.options.address, this.state.account)
|
2018-09-01 20:35:56 +00:00
|
|
|
.setRelayersSymKeyID(skid)
|
|
|
|
.setAsymmetricKeyID(kid)
|
2018-12-03 13:24:55 +00:00
|
|
|
.setGasToken(STT.options.address)
|
|
|
|
.setGas(this.state.gasPrice);
|
2018-09-01 20:35:56 +00:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendTransaction = async event => {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const {web3, kid} = this.props;
|
|
|
|
|
|
|
|
let relayer = this.state.relayer;
|
2018-09-14 15:35:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
let relayers = [];
|
|
|
|
for (var key in this.props.relayers) {
|
|
|
|
if (this.props.relayers.hasOwnProperty(key)) relayers.push(key);
|
|
|
|
}
|
|
|
|
|
2018-09-14 19:19:10 +00:00
|
|
|
if(relayer == '' && relayers.length >= 1){
|
2018-09-14 15:35:46 +00:00
|
|
|
relayer = relayers[0];
|
|
|
|
}
|
2018-08-24 19:21:26 +00:00
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
this.setState({
|
|
|
|
messagingError: '',
|
|
|
|
submitting: true
|
|
|
|
});
|
|
|
|
this.props.clearMessages();
|
|
|
|
|
|
|
|
try {
|
2018-10-08 18:14:19 +00:00
|
|
|
const s = new StatusGasRelayer.SNTController(SNTController.options.address, this.state.account)
|
2018-09-01 20:35:56 +00:00
|
|
|
.transferSNT(this.state.to, this.state.amount)
|
|
|
|
.setGas(this.state.gasPrice)
|
|
|
|
.setRelayer(relayer)
|
|
|
|
.setAsymmetricKeyID(kid);
|
|
|
|
|
|
|
|
await s.post(this.state.signature, web3);
|
|
|
|
|
|
|
|
this.setState({submitting: false});
|
|
|
|
console.log("Message sent");
|
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
} catch(error){
|
|
|
|
this.setState({messagingError: error.message, submitting: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
|
|
|
const {classes} = this.props;
|
2018-09-14 15:35:46 +00:00
|
|
|
|
|
|
|
let relayers = [];
|
|
|
|
for (var key in this.props.relayers) {
|
|
|
|
if (this.props.relayers.hasOwnProperty(key)) relayers.push(key);
|
|
|
|
}
|
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
return <div>
|
2018-08-10 18:37:50 +00:00
|
|
|
<Card className={classes.card}>
|
|
|
|
<CardContent>
|
2018-08-15 15:51:42 +00:00
|
|
|
<b>This functionality is used for simple wallets to perform SNT transfers without paying eth fees</b>
|
2018-08-10 18:37:50 +00:00
|
|
|
</CardContent>
|
|
|
|
</Card>
|
2018-08-09 21:23:32 +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="amount"
|
|
|
|
label="Amount"
|
|
|
|
value={this.state.amount}
|
|
|
|
onChange={this.handleChange('amount')}
|
|
|
|
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={2}>
|
|
|
|
<TextField
|
|
|
|
id="gasPrice"
|
|
|
|
label="Gas Price"
|
|
|
|
value={this.state.gasPrice}
|
|
|
|
onChange={this.handleChange('gasPrice')}
|
|
|
|
margin="normal"
|
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
</form>
|
|
|
|
</CardContent>
|
|
|
|
<CardActions>
|
|
|
|
<Button color="primary" onClick={this.sign}>
|
|
|
|
Sign Message
|
|
|
|
</Button>
|
2018-08-10 18:25:39 +00:00
|
|
|
<Button size="small" onClick={this.getBalance}>STT.methods.balanceOf(to).call()</Button>
|
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
</CardActions>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
{ this.state.messagingError && <MySnackbarContentWrapper variant="error" message={this.state.messagingError} /> }
|
2018-08-24 19:21:26 +00:00
|
|
|
|
2018-08-09 21:23:32 +00:00
|
|
|
<Card className={classes.card}>
|
2018-08-24 19:21:26 +00:00
|
|
|
<CardHeader title="2. Find Available Relayers" />
|
2018-08-09 21:23:32 +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 "availability" 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
|
|
|
|
}}
|
|
|
|
>
|
2018-09-14 15:35:46 +00:00
|
|
|
{ relayers.length > 0 ? relayers.map((r, i) => <option key={i} value={r}>Relayer #{i+1}: {r}</option>) : <option></option> }
|
|
|
|
|
2018-08-24 19:21:26 +00:00
|
|
|
</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 "transaction" Message
|
2018-08-09 21:23:32 +00:00
|
|
|
</Button>
|
|
|
|
</CardActions>
|
|
|
|
</Card>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TransferSNT.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,
|
2018-09-14 15:35:46 +00:00
|
|
|
updateRelayer: PropTypes.func,
|
|
|
|
relayers: PropTypes.object.isRequired
|
2018-08-09 21:23:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default withStyles(styles)(TransferSNT);
|