(Refactor) move web3.eth.sendTransaction to helpers.js
This commit is contained in:
parent
424d3839ad
commit
494aaf2766
|
@ -1,11 +1,12 @@
|
|||
import Web3 from 'web3'
|
||||
//import Web3 from 'web3'
|
||||
import React from 'react'
|
||||
import moment from 'moment'
|
||||
import { observable, action, computed } from 'mobx'
|
||||
import { inject, observer } from 'mobx-react'
|
||||
import { messages } from '../messages'
|
||||
import { constants } from '../constants'
|
||||
import { sleep } from '../helpers'
|
||||
//import { constants } from '../constants'
|
||||
//import { sleep } from '../helpers'
|
||||
import { sendTransactionByVotingKey } from '../helpers'
|
||||
import swal from 'sweetalert2'
|
||||
|
||||
const ACCEPT = 1
|
||||
|
@ -194,9 +195,40 @@ export class BallotCard extends React.Component {
|
|||
return
|
||||
}
|
||||
|
||||
const web3 = new Web3(contractsStore.web3Instance.currentProvider)
|
||||
//const web3 = new Web3(contractsStore.web3Instance.currentProvider)
|
||||
const contract = this.getContract(contractsStore, votingType)
|
||||
|
||||
sendTransactionByVotingKey(
|
||||
this.props,
|
||||
contract.address,
|
||||
contract.vote(id, choice),
|
||||
async tx => {
|
||||
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
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
/*
|
||||
web3.eth.sendTransaction(
|
||||
{
|
||||
from: contractsStore.votingKey,
|
||||
|
@ -249,6 +281,7 @@ export class BallotCard extends React.Component {
|
|||
}
|
||||
}
|
||||
)
|
||||
*/
|
||||
}
|
||||
|
||||
finalize = async e => {
|
||||
|
@ -283,9 +316,28 @@ export class BallotCard extends React.Component {
|
|||
return
|
||||
}
|
||||
|
||||
const web3 = new Web3(contractsStore.web3Instance.currentProvider)
|
||||
//const web3 = new Web3(contractsStore.web3Instance.currentProvider)
|
||||
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
|
||||
}
|
||||
swal('Congratulations!', messages.FINALIZED_SUCCESS_MSG, 'success').then(result => {
|
||||
push(`${commonStore.rootPath}`)
|
||||
})
|
||||
},
|
||||
messages.FINALIZE_FAILED_TX
|
||||
)
|
||||
|
||||
/*
|
||||
web3.eth.sendTransaction(
|
||||
{
|
||||
from: contractsStore.votingKey,
|
||||
|
@ -326,6 +378,7 @@ export class BallotCard extends React.Component {
|
|||
}
|
||||
}
|
||||
)
|
||||
*/
|
||||
}
|
||||
|
||||
repeatGetProperty = async (contractsStore, contractType, id, methodID, tryID) => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import Web3 from 'web3'
|
||||
//import Web3 from 'web3'
|
||||
import React from 'react'
|
||||
import { inject, observer } from 'mobx-react'
|
||||
import moment from 'moment'
|
||||
|
@ -10,7 +10,8 @@ import { BallotMinThresholdMetadata } from './BallotMinThresholdMetadata.jsx'
|
|||
import { BallotProxyMetadata } from './BallotProxyMetadata.jsx'
|
||||
import { messages } from '../messages'
|
||||
import { constants } from '../constants'
|
||||
import { sleep } from '../helpers'
|
||||
//import { sleep } from '../helpers'
|
||||
import { sendTransactionByVotingKey } from '../helpers'
|
||||
@inject('commonStore', 'ballotStore', 'validatorStore', 'contractsStore', 'routing', 'ballotsStore')
|
||||
@observer
|
||||
export class NewBallot extends React.Component {
|
||||
|
@ -231,7 +232,7 @@ export class NewBallot extends React.Component {
|
|||
let methodToCreateBallot
|
||||
let contractType
|
||||
let contractInstance
|
||||
let web3 = new Web3(contractsStore.web3Instance.currentProvider)
|
||||
//let web3 = new Web3(contractsStore.web3Instance.currentProvider)
|
||||
switch (ballotStore.ballotType) {
|
||||
case ballotStore.BallotType.keys:
|
||||
methodToCreateBallot = this.createBallotForKeys
|
||||
|
@ -254,6 +255,28 @@ export class NewBallot extends React.Component {
|
|||
|
||||
const startTime = this.getStartTimeUnix()
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
/*
|
||||
web3.eth.sendTransaction(
|
||||
{
|
||||
from: contractsStore.votingKey,
|
||||
|
@ -297,6 +320,7 @@ export class NewBallot extends React.Component {
|
|||
}
|
||||
}
|
||||
)
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
import Web3 from 'web3'
|
||||
import swal from 'sweetalert2'
|
||||
import { constants } from './constants'
|
||||
|
||||
var toAscii = hex => {
|
||||
var str = '',
|
||||
i = 0,
|
||||
|
@ -17,7 +21,46 @@ 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,
|
||||
sleep: sleep
|
||||
//sleep: sleep,
|
||||
sendTransactionByVotingKey: sendTransactionByVotingKey
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue