From 881c58c8b88b095d055d8921174aca6878674c89 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 14 Sep 2018 11:35:46 -0400 Subject: [PATCH] Obtaining relayer balance and switched method to obtain latest blocks --- test-dapp/app/components/body-identity.js | 4 ++ .../app/components/body-sntcontroller.js | 34 ++++++++++---- test-dapp/app/components/execute.js | 37 +++++++++------ test-dapp/app/components/status-identity.js | 8 +++- .../app/components/status-sntcontroller.js | 45 +++++++++++-------- test-dapp/app/components/transfersnt.js | 39 ++++++++++------ 6 files changed, 112 insertions(+), 55 deletions(-) diff --git a/test-dapp/app/components/body-identity.js b/test-dapp/app/components/body-identity.js index ff4ce00..0af7bef 100644 --- a/test-dapp/app/components/body-identity.js +++ b/test-dapp/app/components/body-identity.js @@ -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}); } diff --git a/test-dapp/app/components/body-sntcontroller.js b/test-dapp/app/components/body-sntcontroller.js index 6eba1f2..5376a42 100644 --- a/test-dapp/app/components/body-sntcontroller.js +++ b/test-dapp/app/components/body-sntcontroller.js @@ -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 - {tab === 0 && } - {tab === 1 && } + {tab === 0 && } + {tab === 1 && } - + ; } diff --git a/test-dapp/app/components/execute.js b/test-dapp/app/components/execute.js index 7e335ed..a71ecb0 100644 --- a/test-dapp/app/components/execute.js +++ b/test-dapp/app/components/execute.js @@ -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
@@ -279,12 +294,7 @@ class Execute extends Component { native: true }} > - { - this.props.relayers.length > 0 ? - this.props.relayers.map((r, i) => ) - : - - } + { relayers.length > 0 ? relayers.map((r, i) => ) : } { + this.web3BlockRead(); + } + + web3BlockRead = () => { + web3.eth.getBlock('latest').then(block => { this.setState({block: block.number}); this.readChain(); + setTimeout(this.web3BlockRead, 10000); return true; }); } diff --git a/test-dapp/app/components/status-sntcontroller.js b/test-dapp/app/components/status-sntcontroller.js index b49bb6d..8cc5973 100644 --- a/test-dapp/app/components/status-sntcontroller.js +++ b/test-dapp/app/components/status-sntcontroller.js @@ -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 @@ -200,14 +206,14 @@ class Status extends Component { Address - - } + { isDev && + } @@ -247,10 +253,10 @@ class Status extends Component { Relayer - + } @@ -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); diff --git a/test-dapp/app/components/transfersnt.js b/test-dapp/app/components/transfersnt.js index d9129c1..5881675 100644 --- a/test-dapp/app/components/transfersnt.js +++ b/test-dapp/app/components/transfersnt.js @@ -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
@@ -270,12 +286,8 @@ class TransferSNT extends Component { native: true }} > - { - this.props.relayers.length > 0 ? - this.props.relayers.map((r, i) => ) - : - - } + { relayers.length > 0 ? relayers.map((r, i) => ) : } +