Obtaining relayer balance and switched method to obtain latest blocks

This commit is contained in:
Richard Ramos 2018-09-14 11:35:46 -04:00
parent b13f42e58e
commit 881c58c8b8
6 changed files with 112 additions and 55 deletions

View File

@ -69,6 +69,10 @@ class Body extends Component {
let relayers = this.state.relayers;
relayers[msgObj.sig] = msgObj.address;
if(this.state.relayerAddress == '0x0000000000000000000000000000000000000000'){
this.setState({relayerAddress: msgObj.address});
}
this.setState({relayers});
}

View File

@ -30,7 +30,8 @@ class Body extends Component {
kid: null,
skid: null,
message: '',
relayers: []
relayerAddress: '0x0000000000000000000000000000000000000000',
relayers: {}
};
}
@ -43,6 +44,14 @@ class Body extends Component {
const web3js = new Web3('ws://localhost:8546');
// Default for devenv
web3js.eth.net.getId().then(netId => {
if(netId != 1 && netId != 3){
this.setState({relayerAddress: config.relayAccount});
}
});
web3js.shh.newKeyPair()
.then((kid) => {
web3js.shh.addSymKey(config.relaySymKey)
@ -58,9 +67,14 @@ class Body extends Component {
if(msgObj.message == Messages.available){
// found a relayer
console.log("Relayer available: " + msgObj.sig);
let relayers = this.state.relayers;
relayers.push(msgObj.sig);
relayers = relayers.filter((value, index, self) => self.indexOf(value) === index);
relayers[msgObj.sig] = msgObj.address;
if(this.state.relayerAddress == '0x0000000000000000000000000000000000000000'){
this.setState({relayerAddress: msgObj.address});
}
this.setState({relayers});
}
@ -79,7 +93,7 @@ class Body extends Component {
web3.eth.getAccounts()
.then(accounts => {
this.setState({walletAddress: accounts[2]});
this.setState({walletAddress: accounts[0]});
});
});
@ -89,6 +103,10 @@ class Body extends Component {
this.setState({tab});
};
updateRelayer = (relayer) => {
this.setState({relayerAddress: this.state.relayers[relayer]});
}
updateNonce = (newNonce) => {
this.setState({nonce: newNonce});
}
@ -98,18 +116,18 @@ class Body extends Component {
}
render(){
const {tab, walletAddress, nonce, web3js, message, kid, skid, relayers} = this.state;
const {tab, walletAddress, nonce, web3js, message, kid, skid, relayers, relayerAddress} = this.state;
return <Fragment>
<Tabs value={tab} onChange={this.handleChange}>
<Tab label="Transfer SNT" />
<Tab label="Execute" />
</Tabs>
{tab === 0 && <Container><TransferSNT clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} relayers={relayers} /></Container>}
{tab === 1 && <Container><Execute clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} relayers={relayers} /></Container>}
{tab === 0 && <Container><TransferSNT clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} relayers={relayers} updateRelayer={this.updateRelayer} /></Container>}
{tab === 1 && <Container><Execute clearMessages={this.clearMessages} web3={web3js} kid={kid} skid={skid} nonce={nonce} relayers={relayers} updateRelayer={this.updateRelayer} /></Container>}
<Divider />
<Container>
<Status message={message} nonceUpdateFunction={this.updateNonce} nonce={nonce} walletAddress={walletAddress} />
<Status relayerAddress={relayerAddress} message={message} nonceUpdateFunction={this.updateNonce} nonce={nonce} walletAddress={walletAddress} />
</Container>
</Fragment>;
}

View File

@ -51,6 +51,9 @@ class Execute extends Component {
}
handleChange = name => event => {
if(name == 'relayer'){
this.props.updateRelayer(event.target.value);
}
this.setState({
[name]: event.target.value
});
@ -67,7 +70,7 @@ class Execute extends Component {
try {
const accounts = await web3.eth.getAccounts();
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[2])
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[0])
.execute(this.state.allowedContract, this.state.data)
.setGas(this.state.gasPrice, this.state.gasMinimal);
@ -104,7 +107,7 @@ class Execute extends Component {
try {
const accounts = await web3.eth.getAccounts();
const s = new StatusGasRelayer.AvailableRelayers(Contracts.SNT, SNTController.options.address, accounts[2])
const s = new StatusGasRelayer.AvailableRelayers(Contracts.SNT, SNTController.options.address, accounts[0])
.setRelayersSymKeyID(skid)
.setAsymmetricKeyID(kid)
.setGas(STT.options.address, this.state.gasPrice);
@ -124,10 +127,16 @@ class Execute extends Component {
const {web3, kid} = this.props;
let relayer = this.state.relayer;
if(relayer == '' && this.props.relayers.length == 1){
relayer = this.props.relayers[0];
}
let relayers = [];
for (var key in this.props.relayers) {
if (this.props.relayers.hasOwnProperty(key)) relayers.push(key);
}
if(relayer == '' && relayers.length == 1){
relayer = relayers[0];
}
this.setState({
messagingError: '',
@ -138,7 +147,7 @@ class Execute extends Component {
try {
const accounts = await web3.eth.getAccounts();
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[2])
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[0])
.execute(this.state.allowedContract, this.state.data)
.setGas(this.state.gasPrice, this.state.gasMinimal)
.setRelayer(relayer)
@ -156,6 +165,12 @@ class Execute extends Component {
render(){
const {classes} = this.props;
let relayers = [];
for (var key in this.props.relayers) {
if (this.props.relayers.hasOwnProperty(key)) relayers.push(key);
}
return <div>
<Card className={classes.card}>
<CardContent>
@ -279,12 +294,7 @@ class Execute extends Component {
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>
}
{ relayers.length > 0 ? relayers.map((r, i) => <option key={i} value={r}>Relayer #{i+1}: {r}</option>) : <option></option> }
</TextField>
<TextField
id="signature"
@ -315,7 +325,8 @@ Execute.propTypes = {
kid: PropTypes.string,
skid: PropTypes.string,
clearMessages: PropTypes.func,
relayers: PropTypes.array.isRequired
updateRelayer: PropTypes.func,
relayers: PropTypes.object.isRequired
};
export default withStyles(styles)(Execute);

View File

@ -86,10 +86,14 @@ class Status extends Component {
this.setState({isDev: netId != 1 && netId != 3});
});
web3.eth.subscribe('newBlockHeaders')
.on('data', (block) => {
this.web3BlockRead();
}
web3BlockRead = () => {
web3.eth.getBlock('latest').then(block => {
this.setState({block: block.number});
this.readChain();
setTimeout(this.web3BlockRead, 10000);
return true;
});
}

View File

@ -52,9 +52,9 @@ class Status extends Component {
constructor(props){
super(props);
this.state = {
'isDev': true,
'addressETHBalance': 0,
'addressSTTBalance': 0,
'relayerAddress': null,
'relayerEthBalance': 0,
'relayerSTTBalance': 0,
'block': 0,
@ -72,19 +72,25 @@ class Status extends Component {
return;
}
this.setState({
relayerAddress: config.relayAccount
});
this.getBlock();
});
}
getBlock = () => {
web3.eth.subscribe('newBlockHeaders')
.on('data', (block) => {
// Default for devenv
web3.eth.net.getId().then(netId => {
this.setState({isDev: netId != 1 && netId != 3});
});
this.web3BlockRead();
}
web3BlockRead = () => {
web3.eth.getBlock('latest').then(block => {
this.setState({block: block.number});
this.readChain();
setTimeout(this.web3BlockRead, 10000);
return true;
});
}
@ -117,12 +123,12 @@ class Status extends Component {
});
}
web3.eth.getBalance(this.state.relayerAddress)
web3.eth.getBalance(this.props.relayerAddress)
.then(relayerEthBalance => {
this.setState({relayerEthBalance});
});
STT.methods.balanceOf(this.state.relayerAddress)
STT.methods.balanceOf(this.props.relayerAddress)
.call()
.then(relayerSTTBalance => {
this.setState({relayerSTTBalance: web3.utils.fromWei(relayerSTTBalance, 'ether')});
@ -170,7 +176,7 @@ class Status extends Component {
submitState.etherSend = false;
this.setState({submitState});
web3.eth.sendTransaction({from: web3.eth.defaultAccount, to: this.state.relayerAddress, value: web3.utils.toWei('1', "ether")})
web3.eth.sendTransaction({from: web3.eth.defaultAccount, to: this.props.relayerAddress, value: web3.utils.toWei('1', "ether")})
.then((receipt) => {
console.log(receipt);
submitState = this.state.submitState;
@ -180,8 +186,8 @@ class Status extends Component {
}
render(){
const {classes, walletAddress, nonce} = this.props;
const {addressETHBalance, relayerAddress, relayerEthBalance, addressSTTBalance, relayerSTTBalance, submitState, block} = this.state;
const {classes, walletAddress, nonce, relayerAddress} = this.props;
const {addressETHBalance, relayerEthBalance, addressSTTBalance, relayerSTTBalance, submitState, block, isDev} = this.state;
return <Fragment>
<Card className={classes.card}>
@ -200,14 +206,14 @@ class Status extends Component {
<Typography variant="display1">
Address
</Typography>
<Button className={classes.button} color="primary" aria-label="Generate STT" onClick={this.generateSTT} disabled={submitState.generateSTT}>
{ isDev && <Button className={classes.button} color="primary" aria-label="Generate STT" onClick={this.generateSTT} disabled={submitState.generateSTT}>
<AddIcon className={classes.icon} />
1. Generate 5K STT (only on dev)
</Button>
<Button className={classes.button} color="primary" aria-label="Generate STT" onClick={this.changeSNTController}>
</Button> }
{ isDev && <Button className={classes.button} color="primary" aria-label="Generate STT" onClick={this.changeSNTController}>
<AddIcon className={classes.icon} />
2. Change SNT Controller
</Button>
</Button> }
</ListItem>
<ListItem className={classes.root}>
<ListItemIcon>
@ -247,10 +253,10 @@ class Status extends Component {
<Typography variant="display1">
Relayer
</Typography>
<Button className={classes.button} color="primary" aria-label="Add ether" onClick={this.sendEther} disabled={submitState.etherSend}>
{ isDev && <Button className={classes.button} color="primary" aria-label="Add ether" onClick={this.sendEther} disabled={submitState.etherSend}>
<AddIcon className={classes.icon} />
Send ether
</Button>
</Button> }
</ListItem>
<ListItem className={classes.root}>
<ListItemIcon>
@ -292,7 +298,8 @@ Status.propTypes = {
walletAddress: PropTypes.string,
nonce: PropTypes.string.isRequired,
nonceUpdateFunction: PropTypes.func.isRequired,
message: PropTypes.string
message: PropTypes.string,
relayerAddress: PropTypes.string
};
export default withStyles(styles)(Status);

View File

@ -51,6 +51,9 @@ class TransferSNT extends Component {
}
handleChange = name => event => {
if(name == 'relayer'){
this.props.updateRelayer(event.target.value);
}
this.setState({
[name]: event.target.value
});
@ -77,7 +80,7 @@ class TransferSNT extends Component {
try {
const accounts = await web3.eth.getAccounts();
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[2])
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[0])
.transferSNT(this.state.to, this.state.amount)
.setGas(this.state.gasPrice);
@ -105,7 +108,7 @@ class TransferSNT extends Component {
try {
const accounts = await web3.eth.getAccounts();
const s = new StatusGasRelayer.AvailableRelayers(Contracts.SNT, SNTController.options.address, accounts[2])
const s = new StatusGasRelayer.AvailableRelayers(Contracts.SNT, SNTController.options.address, accounts[0])
.setRelayersSymKeyID(skid)
.setAsymmetricKeyID(kid)
.setGas(STT.options.address, this.state.gasPrice);
@ -125,9 +128,16 @@ class TransferSNT extends Component {
const {web3, kid} = this.props;
let relayer = this.state.relayer;
if(relayer == '' && this.props.relayers.length == 1){
relayer = this.props.relayers[0];
}
let relayers = [];
for (var key in this.props.relayers) {
if (this.props.relayers.hasOwnProperty(key)) relayers.push(key);
}
if(relayer == '' && relayers.length == 1){
relayer = relayers[0];
}
this.setState({
messagingError: '',
@ -138,7 +148,7 @@ class TransferSNT extends Component {
try {
const accounts = await web3.eth.getAccounts();
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[2])
const s = new StatusGasRelayer.SNTController(SNTController.options.address, accounts[0])
.transferSNT(this.state.to, this.state.amount)
.setGas(this.state.gasPrice)
.setRelayer(relayer)
@ -156,6 +166,12 @@ class TransferSNT extends Component {
render(){
const {classes} = this.props;
let relayers = [];
for (var key in this.props.relayers) {
if (this.props.relayers.hasOwnProperty(key)) relayers.push(key);
}
return <div>
<Card className={classes.card}>
<CardContent>
@ -270,12 +286,8 @@ class TransferSNT extends Component {
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>
}
{ relayers.length > 0 ? relayers.map((r, i) => <option key={i} value={r}>Relayer #{i+1}: {r}</option>) : <option></option> }
</TextField>
<TextField
id="signature"
@ -306,7 +318,8 @@ TransferSNT.propTypes = {
kid: PropTypes.string,
skid: PropTypes.string,
clearMessages: PropTypes.func,
relayers: PropTypes.array.isRequired
updateRelayer: PropTypes.func,
relayers: PropTypes.object.isRequired
};
export default withStyles(styles)(TransferSNT);