diff --git a/package-lock.json b/package-lock.json index 24ca69d..3f129ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -224,7 +224,7 @@ "arr-flatten": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/arr-flatten/-/arr-flatten-1.1.0.tgz", - "integrity": "sha1-NgSLv/TntH4TZkQxbJlmnqWukfE=" + "integrity": "sha512-L3hKV5R/p5o81R7O02IGnwpDmkp6E982XhtbuwSe3O4qOtMMMtodicASA1Cny2U+aCXcNpml+m4dPsvsJ3jatg==" }, "arr-union": { "version": "3.1.0", @@ -2723,7 +2723,7 @@ "cosmiconfig": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-2.2.2.tgz", - "integrity": "sha1-YXPOvVb6wELB9DkO33r2wHx8uJI=", + "integrity": "sha512-GiNXLwAFPYHy25XmTPpafYvn3CLAkJ8FLsscq78MQd1Kh0OU6Yzhn4eV2MVF4G9WEQZoWEGltatdR+ntGPMl5A==", "requires": { "is-directory": "0.3.1", "js-yaml": "3.7.0", @@ -5110,8 +5110,7 @@ }, "jsbn": { "version": "0.1.1", - "bundled": true, - "optional": true + "bundled": true }, "json-schema": { "version": "0.2.3", @@ -6088,7 +6087,7 @@ "hosted-git-info": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.5.0.tgz", - "integrity": "sha1-bWDjSzq7yDEwYsO3mO+NkBoHrzw=" + "integrity": "sha512-pNgbURSuab90KbTqvRPsseaTxOJCZBD0a7t+haSN33piP9cCM4l0CqdzAif2hUqm716UovKB2ROmiabGAKVXyg==" }, "hpack.js": { "version": "2.1.6", @@ -9371,7 +9370,7 @@ "normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", - "integrity": "sha1-EvlaMH1YNSB1oEkHuErIvpisAS8=", + "integrity": "sha512-9jjUFbTPfEy3R/ad/2oNbKtW9Hgovl5O1FvFWKkKblNXoN/Oou6+9+KKohPK13Yc3/TyunyWhJp6gvRNR/PPAw==", "requires": { "hosted-git-info": "2.5.0", "is-builtin-module": "1.0.0", @@ -9490,7 +9489,7 @@ "npmlog": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-4.1.2.tgz", - "integrity": "sha1-CKfyqL9zRgR3mp76StXMcXq7lUs=", + "integrity": "sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==", "requires": { "are-we-there-yet": "1.1.5", "console-control-strings": "1.1.0", @@ -12559,7 +12558,7 @@ "safe-buffer": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", - "integrity": "sha1-iTMSr2myEj3vcfV4iQAWce6yyFM=" + "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" }, "safe-regex": { "version": "1.1.0", @@ -16679,7 +16678,7 @@ "dependencies": { "fs-extra": { "version": "0.30.0", - "resolved": "http://registry.npmjs.org/fs-extra/-/fs-extra-0.30.0.tgz", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-0.30.0.tgz", "integrity": "sha1-8jP/zAjU2n1DLapEl3aYnbHfk/A=", "requires": { "graceful-fs": "4.1.11", @@ -16768,7 +16767,7 @@ "which": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz", - "integrity": "sha1-/wS9/AEO5UfXgL7DjhrBwnd9JTo=", + "integrity": "sha512-xcJpopdamTuY5duC/KnTTNBraPK54YwpenP4lzxU8H91GudWpFv38u0CKjclE1Wi2EH2EDz5LRcHcKbCIzqGyg==", "requires": { "isexe": "2.0.0" } diff --git a/package.json b/package.json index f6caf3c..c7156cd 100644 --- a/package.json +++ b/package.json @@ -123,9 +123,14 @@ "rules": { "no-control-regex": 0, "dependencies/no-cycles": "error", - "dependencies/no-unresolved": ["error", { - "ignore": ["web3"] - }] + "dependencies/no-unresolved": [ + "error", + { + "ignore": [ + "web3" + ] + } + ] }, "settings": { "import/resolver": { diff --git a/src/components/BallotCard.jsx b/src/components/BallotCard.jsx index fed02fd..06a65ab 100644 --- a/src/components/BallotCard.jsx +++ b/src/components/BallotCard.jsx @@ -3,6 +3,7 @@ import moment from 'moment' import { observable, action, computed } from 'mobx' import { inject, observer } from 'mobx-react' import { messages } from '../messages' +import { sendTransactionByVotingKey } from '../helpers' import swal from 'sweetalert2' const ACCEPT = 1 @@ -177,7 +178,7 @@ export class BallotCard extends React.Component { swal('Warning!', messages.ballotIsNotActiveMsg(this.timeTo.displayValue), 'warning') return } - const { commonStore, contractsStore, id, votingType, ballotsStore } = this.props + const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props const { push } = this.props.routing if (!contractsStore.isValidVotingKey) { swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning') @@ -192,40 +193,36 @@ export class BallotCard extends React.Component { } const contract = this.getContract(contractsStore, votingType) - contract - .vote(id, choice, contractsStore.votingKey) - .on('receipt', async tx => { - commonStore.hideLoading() - if (tx.status === true || tx.status === '0x1') { - const ballotInfo = await contract.getBallotInfo(id, contractsStore.votingKey) - this.totalVoters = Number(ballotInfo.totalVoters) - this.progress = Number(ballotInfo.progress) - this.isFinalized = Boolean(ballotInfo.isFinalized) - if (ballotInfo.hasOwnProperty('canBeFinalizedNow')) { - this.canBeFinalized = Boolean(ballotInfo.canBeFinalizedNow) - } else { - await this.canBeFinalizedNow() - } - this.hasAlreadyVoted = true + sendTransactionByVotingKey( + this.props, + contract.address, + contract.vote(id, choice), + async tx => { + const ballotInfo = await contract.getBallotInfo(id, contractsStore.votingKey) - ballotsStore.ballotCards[this.props.pos].props.votingState.totalVoters = this.totalVoters - ballotsStore.ballotCards[this.props.pos].props.votingState.progress = this.progress - ballotsStore.ballotCards[this.props.pos].props.votingState.isFinalized = this.isFinalized - ballotsStore.ballotCards[this.props.pos].props.votingState.canBeFinalized = this.canBeFinalized - ballotsStore.ballotCards[this.props.pos].props.votingState.hasAlreadyVoted = this.hasAlreadyVoted - - swal('Congratulations!', messages.VOTED_SUCCESS_MSG, 'success').then(result => { - push(`${commonStore.rootPath}`) - }) + this.totalVoters = Number(ballotInfo.totalVoters) + this.progress = Number(ballotInfo.progress) + this.isFinalized = Boolean(ballotInfo.isFinalized) + if (ballotInfo.hasOwnProperty('canBeFinalizedNow')) { + this.canBeFinalized = Boolean(ballotInfo.canBeFinalizedNow) } else { - swal('Warning!', messages.VOTE_FAILED_TX, 'warning') + await this.canBeFinalizedNow() } - }) - .on('error', e => { - commonStore.hideLoading() - swal('Error!', e.message, 'error') - }) + this.hasAlreadyVoted = true + + ballotsStore.ballotCards[pos].props.votingState.totalVoters = this.totalVoters + ballotsStore.ballotCards[pos].props.votingState.progress = this.progress + ballotsStore.ballotCards[pos].props.votingState.isFinalized = this.isFinalized + ballotsStore.ballotCards[pos].props.votingState.canBeFinalized = this.canBeFinalized + ballotsStore.ballotCards[pos].props.votingState.hasAlreadyVoted = this.hasAlreadyVoted + + swal('Congratulations!', messages.VOTED_SUCCESS_MSG, 'success').then(result => { + push(`${commonStore.rootPath}`) + }) + }, + messages.VOTE_FAILED_TX + ) } finalize = async e => { @@ -237,7 +234,7 @@ export class BallotCard extends React.Component { swal('Warning!', messages.ballotIsNotActiveMsg(this.timeTo.displayValue), 'warning') return } - const { commonStore, contractsStore, id, votingType, ballotsStore } = this.props + const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props const { push } = this.props.routing if (!contractsStore.isValidVotingKey) { swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning') @@ -259,28 +256,26 @@ export class BallotCard extends React.Component { swal('Warning!', messages.INVALID_FINALIZE_MSG, 'warning') return } - this.getContract(contractsStore, votingType) - .finalize(id, contractsStore.votingKey) - .on('receipt', tx => { - commonStore.hideLoading() - if (tx.status === true || tx.status === '0x1') { - this.isFinalized = true - ballotsStore.ballotCards[this.props.pos].props.votingState.isFinalized = this.isFinalized - if (this.canBeFinalized !== null) { - this.canBeFinalized = false - ballotsStore.ballotCards[this.props.pos].props.votingState.canBeFinalized = this.canBeFinalized - } - swal('Congratulations!', messages.FINALIZED_SUCCESS_MSG, 'success').then(result => { - push(`${commonStore.rootPath}`) - }) - } else { - swal('Warning!', messages.FINALIZE_FAILED_TX, 'warning') + + const contract = this.getContract(contractsStore, votingType) + + sendTransactionByVotingKey( + this.props, + contract.address, + contract.finalize(id), + async tx => { + this.isFinalized = true + ballotsStore.ballotCards[pos].props.votingState.isFinalized = this.isFinalized + if (this.canBeFinalized !== null) { + this.canBeFinalized = false + ballotsStore.ballotCards[pos].props.votingState.canBeFinalized = this.canBeFinalized } - }) - .on('error', e => { - commonStore.hideLoading() - swal('Error!', e.message, 'error') - }) + swal('Congratulations!', messages.FINALIZED_SUCCESS_MSG, 'success').then(result => { + push(`${commonStore.rootPath}`) + }) + }, + messages.FINALIZE_FAILED_TX + ) } repeatGetProperty = async (contractsStore, contractType, id, methodID, tryID) => { diff --git a/src/components/NewBallot.jsx b/src/components/NewBallot.jsx index e5d2ec3..c43c61b 100644 --- a/src/components/NewBallot.jsx +++ b/src/components/NewBallot.jsx @@ -9,6 +9,7 @@ import { BallotMinThresholdMetadata } from './BallotMinThresholdMetadata.jsx' import { BallotProxyMetadata } from './BallotProxyMetadata.jsx' import { messages } from '../messages' import { constants } from '../constants' +import { sendTransactionByVotingKey } from '../helpers' @inject('commonStore', 'ballotStore', 'validatorStore', 'contractsStore', 'routing', 'ballotsStore') @observer export class NewBallot extends React.Component { @@ -17,6 +18,13 @@ export class NewBallot extends React.Component { this.onClick = this.onClick.bind(this) } + getStartTimeUnix() { + return moment + .utc() + .add(constants.startTimeOffsetInMinutes, 'minutes') + .unix() + } + checkValidation() { const { commonStore, contractsStore, ballotStore, validatorStore } = this.props @@ -37,16 +45,21 @@ export class NewBallot extends React.Component { } const minBallotDurationInHours = constants.minBallotDurationInDays * 24 + const startTime = this.getStartTimeUnix() const minEndTime = moment .utc() .add(minBallotDurationInHours, 'hours') .format() let neededMinutes = moment(minEndTime).diff(moment(ballotStore.endTime), 'minutes') - let neededHours = Math.round(neededMinutes / 60) - let duration = minBallotDurationInHours - neededHours + let neededHours = Math.floor(neededMinutes / 60) + let duration = moment.unix(ballotStore.endTimeUnix).diff(moment.unix(startTime), 'hours') + + if (duration < 0) { + duration = 0 + } if (neededMinutes > 0) { - neededMinutes = neededHours * 60 - neededMinutes + neededMinutes = Math.abs(neededHours * 60 - neededMinutes) swal( 'Warning!', messages.SHOULD_BE_MORE_THAN_MIN_DURATION(minBallotDurationInHours, duration, neededHours, neededMinutes), @@ -148,20 +161,19 @@ export class NewBallot extends React.Component { newPayoutKey: ballotStore.ballotKeys.newPayoutKey, miningKey: ballotStore.ballotKeys.miningKey.value, ballotType: ballotStore.ballotKeys.keysBallotType, - sender: contractsStore.votingKey, memo: ballotStore.memo } - let method + let data if ( inputToMethod.ballotType === ballotStore.KeysBallotType.add && inputToMethod.affectedKeyType === ballotStore.KeyType.mining && (inputToMethod.newVotingKey || inputToMethod.newPayoutKey) ) { - method = contractsStore.votingToChangeKeys.createBallotToAddNewValidator(inputToMethod) + data = contractsStore.votingToChangeKeys.createBallotToAddNewValidator(inputToMethod) } else { - method = contractsStore.votingToChangeKeys.createBallot(inputToMethod) + data = contractsStore.votingToChangeKeys.createBallot(inputToMethod) } - return method + return data } createBallotForMinThreshold = startTime => { @@ -170,11 +182,9 @@ export class NewBallot extends React.Component { startTime: startTime, endTime: ballotStore.endTimeUnix, proposedValue: ballotStore.ballotMinThreshold.proposedValue, - sender: contractsStore.votingKey, memo: ballotStore.memo } - let method = contractsStore.votingToChangeMinThreshold.createBallot(inputToMethod) - return method + return contractsStore.votingToChangeMinThreshold.createBallot(inputToMethod) } createBallotForProxy = startTime => { @@ -184,11 +194,9 @@ export class NewBallot extends React.Component { endTime: ballotStore.endTimeUnix, proposedValue: ballotStore.ballotProxy.proposedAddress, contractType: ballotStore.ballotProxy.contractType, - sender: contractsStore.votingKey, memo: ballotStore.memo } - let method = contractsStore.votingToChangeProxy.createBallot(inputToMethod) - return method + return contractsStore.votingToChangeProxy.createBallot(inputToMethod) } onClick = async () => { @@ -221,47 +229,50 @@ export class NewBallot extends React.Component { let methodToCreateBallot let contractType + let contractInstance + //let web3 = new Web3(contractsStore.web3Instance.currentProvider) switch (ballotStore.ballotType) { case ballotStore.BallotType.keys: methodToCreateBallot = this.createBallotForKeys contractType = 'votingToChangeKeys' + contractInstance = contractsStore.votingToChangeKeys.votingToChangeKeysInstance break case ballotStore.BallotType.minThreshold: methodToCreateBallot = this.createBallotForMinThreshold contractType = 'votingToChangeMinThreshold' + contractInstance = contractsStore.votingToChangeMinThreshold.votingToChangeMinThresholdInstance break case ballotStore.BallotType.proxy: methodToCreateBallot = this.createBallotForProxy contractType = 'votingToChangeProxy' + contractInstance = contractsStore.votingToChangeProxy.votingToChangeProxyInstance break default: break } - const startTime = moment - .utc() - .add(constants.startTimeOffsetInMinutes, 'minutes') - .unix() - methodToCreateBallot(startTime) - .on('receipt', async tx => { - commonStore.hideLoading() - if (tx.status === true || tx.status === '0x1') { - const newId = Number(tx.events.BallotCreated.returnValues.id) - const card = await contractsStore.getCard(newId, contractType) - ballotsStore.ballotCards.push(card) + const startTime = this.getStartTimeUnix() - swal('Congratulations!', messages.BALLOT_CREATED_SUCCESS_MSG, 'success').then(result => { - push(`${commonStore.rootPath}`) - window.scrollTo(0, 0) - }) - } else { - swal('Warning!', messages.BALLOT_CREATE_FAILED_TX, 'warning') - } - }) - .on('error', e => { - commonStore.hideLoading() - swal('Error!', e.message, 'error') - }) + sendTransactionByVotingKey( + this.props, + contractInstance.options.address, + methodToCreateBallot(startTime), + async tx => { + const events = await contractInstance.getPastEvents('BallotCreated', { + fromBlock: tx.blockNumber, + toBlock: tx.blockNumber + }) + const newId = Number(events[0].returnValues.id) + const card = await contractsStore.getCard(newId, contractType) + ballotsStore.ballotCards.push(card) + + swal('Congratulations!', messages.BALLOT_CREATED_SUCCESS_MSG, 'success').then(result => { + push(`${commonStore.rootPath}`) + window.scrollTo(0, 0) + }) + }, + messages.BALLOT_CREATE_FAILED_TX + ) } } @@ -312,7 +323,7 @@ export class NewBallot extends React.Component { className={this.menuItemActive(ballotStore.BallotType.minThreshold)} onClick={e => ballotStore.changeBallotType(e, ballotStore.BallotType.minThreshold)} > - Consenus Threshold Ballot + Consensus Threshold Ballot
ballotStore.changeBallotType(e, ballotStore.BallotType.minThreshold)} />

Ballot to change the minimum threshold for consensus to vote for keys.

diff --git a/src/constants.js b/src/constants.js index ed9b62d..eaef903 100644 --- a/src/constants.js +++ b/src/constants.js @@ -21,6 +21,7 @@ constants.NEW_MINING_KEY = { constants.minBallotDurationInDays = 2 constants.startTimeOffsetInMinutes = 5 constants.endTimeDefaultInMinutes = 2890 +constants.getTransactionReceiptInterval = 5000 module.exports = { constants } diff --git a/src/contracts/VotingToChangeKeys.contract.js b/src/contracts/VotingToChangeKeys.contract.js index 9096165..0e2dae8 100644 --- a/src/contracts/VotingToChangeKeys.contract.js +++ b/src/contracts/VotingToChangeKeys.contract.js @@ -14,34 +14,32 @@ export default class VotingToChangeKeys { this.votingToChangeKeysInstance = new web3_10.eth.Contract(votingToChangeKeysABI, VOTING_TO_CHANGE_KEYS_ADDRESS) this.gasPrice = web3_10.utils.toWei('1', 'gwei') + this.address = VOTING_TO_CHANGE_KEYS_ADDRESS } //setters - createBallot({ startTime, endTime, affectedKey, affectedKeyType, miningKey, ballotType, sender, memo }) { + createBallot({ startTime, endTime, affectedKey, affectedKeyType, miningKey, ballotType, memo }) { let method if (this.votingToChangeKeysInstance.methods.createBallot) { method = this.votingToChangeKeysInstance.methods.createBallot } else { method = this.votingToChangeKeysInstance.methods.createVotingForKeys } - return method(startTime, endTime, affectedKey, affectedKeyType, miningKey, ballotType, memo).send({ - from: sender, - gasPrice: this.gasPrice - }) + return method(startTime, endTime, affectedKey, affectedKeyType, miningKey, ballotType, memo).encodeABI() } - createBallotToAddNewValidator({ startTime, endTime, affectedKey, newVotingKey, newPayoutKey, sender, memo }) { + createBallotToAddNewValidator({ startTime, endTime, affectedKey, newVotingKey, newPayoutKey, memo }) { return this.votingToChangeKeysInstance.methods .createBallotToAddNewValidator(startTime, endTime, affectedKey, newVotingKey, newPayoutKey, memo) - .send({ from: sender, gasPrice: this.gasPrice }) + .encodeABI() } - vote(_id, choice, sender) { - return this.votingToChangeKeysInstance.methods.vote(_id, choice).send({ from: sender, gasPrice: this.gasPrice }) + vote(_id, choice) { + return this.votingToChangeKeysInstance.methods.vote(_id, choice).encodeABI() } - finalize(_id, sender) { - return this.votingToChangeKeysInstance.methods.finalize(_id).send({ from: sender, gasPrice: this.gasPrice }) + finalize(_id) { + return this.votingToChangeKeysInstance.methods.finalize(_id).encodeABI() } //getters diff --git a/src/contracts/VotingToChangeMinThreshold.contract.js b/src/contracts/VotingToChangeMinThreshold.contract.js index 30c9288..658a9c8 100644 --- a/src/contracts/VotingToChangeMinThreshold.contract.js +++ b/src/contracts/VotingToChangeMinThreshold.contract.js @@ -17,27 +17,26 @@ export default class VotingToChangeMinThreshold { VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS ) this.gasPrice = web3_10.utils.toWei('1', 'gwei') + this.address = VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS } //setters - createBallot({ startTime, endTime, proposedValue, sender, memo }) { + createBallot({ startTime, endTime, proposedValue, memo }) { let method if (this.votingToChangeMinThresholdInstance.methods.createBallot) { method = this.votingToChangeMinThresholdInstance.methods.createBallot } else { method = this.votingToChangeMinThresholdInstance.methods.createBallotToChangeThreshold } - return method(startTime, endTime, proposedValue, memo).send({ from: sender, gasPrice: this.gasPrice }) + return method(startTime, endTime, proposedValue, memo).encodeABI() } - vote(_id, choice, sender) { - return this.votingToChangeMinThresholdInstance.methods - .vote(_id, choice) - .send({ from: sender, gasPrice: this.gasPrice }) + vote(_id, choice) { + return this.votingToChangeMinThresholdInstance.methods.vote(_id, choice).encodeABI() } - finalize(_id, sender) { - return this.votingToChangeMinThresholdInstance.methods.finalize(_id).send({ from: sender, gasPrice: this.gasPrice }) + finalize(_id) { + return this.votingToChangeMinThresholdInstance.methods.finalize(_id).encodeABI() } //getters diff --git a/src/contracts/VotingToChangeProxy.contract.js b/src/contracts/VotingToChangeProxy.contract.js index 967fcf8..91e47b7 100644 --- a/src/contracts/VotingToChangeProxy.contract.js +++ b/src/contracts/VotingToChangeProxy.contract.js @@ -14,25 +14,26 @@ export default class VotingToChangeProxy { this.votingToChangeProxyInstance = new web3_10.eth.Contract(votingToChangeProxyABI, VOTING_TO_CHANGE_PROXY_ADDRESS) this.gasPrice = web3_10.utils.toWei('1', 'gwei') + this.address = VOTING_TO_CHANGE_PROXY_ADDRESS } //setters - createBallot({ startTime, endTime, proposedValue, contractType, sender, memo }) { + createBallot({ startTime, endTime, proposedValue, contractType, memo }) { let method if (this.votingToChangeProxyInstance.methods.createBallot) { method = this.votingToChangeProxyInstance.methods.createBallot } else { method = this.votingToChangeProxyInstance.methods.createBallotToChangeProxyAddress } - return method(startTime, endTime, proposedValue, contractType, memo).send({ from: sender, gasPrice: this.gasPrice }) + return method(startTime, endTime, proposedValue, contractType, memo).encodeABI() } - vote(_id, choice, sender) { - return this.votingToChangeProxyInstance.methods.vote(_id, choice).send({ from: sender, gasPrice: this.gasPrice }) + vote(_id, choice) { + return this.votingToChangeProxyInstance.methods.vote(_id, choice).encodeABI() } - finalize(_id, sender) { - return this.votingToChangeProxyInstance.methods.finalize(_id).send({ from: sender, gasPrice: this.gasPrice }) + finalize(_id) { + return this.votingToChangeProxyInstance.methods.finalize(_id).encodeABI() } //getters diff --git a/src/helpers.js b/src/helpers.js index e0c11fc..3c51afd 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,3 +1,7 @@ +import Web3 from 'web3' +import swal from 'sweetalert2' +import { constants } from './constants' + var toAscii = hex => { var str = '', i = 0, @@ -13,6 +17,49 @@ var toAscii = hex => { return str } -module.exports = { - toAscii: toAscii +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)) +} + +function sendTransactionByVotingKey(props, to, data, cb, warning) { + const { commonStore, contractsStore } = props + const web3 = new Web3(contractsStore.web3Instance.currentProvider) + + web3.eth.sendTransaction( + { + from: contractsStore.votingKey, + to, + gasPrice: web3.utils.toWei('1', 'gwei'), + data + }, + async (error, hash) => { + if (error) { + commonStore.hideLoading() + swal('Error!', error.message, 'error') + } else { + try { + let tx + do { + await sleep(constants.getTransactionReceiptInterval) + tx = await web3.eth.getTransactionReceipt(hash) + } while (tx === null) + + commonStore.hideLoading() + if (tx.status === true || tx.status === '0x1') { + await cb(tx) + } else { + swal('Warning!', warning, 'warning') + } + } catch (e) { + commonStore.hideLoading() + swal('Error!', e.message, 'error') + } + } + } + ) +} + +module.exports = { + toAscii: toAscii, + sendTransactionByVotingKey: sendTransactionByVotingKey }