-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtomo.js
53 lines (50 loc) · 2.18 KB
/
tomo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const Web3Util = require('./web3')
const BlockSignerABI = require('../contracts/abi/BlockSigner')
const contractAddress = require('../contracts/contractAddress')
const TomoValidatorABI = require('../contracts/abi/TomoValidator')
const logger = require('./logger')
let sleep = (time) => new Promise((resolve) => setTimeout(resolve, time))
let blockSigner = {
getBlockSignerContract: async () => {
let web3 = await Web3Util.getWeb3()
return new web3.eth.Contract(BlockSignerABI, contractAddress.BlockSigner)
},
getSigners: async (blockHash) => {
let bs = await blockSigner.getBlockSignerContract()
return bs.methods.getSigners(blockHash).call().catch(e => {
logger.warn('Cannot get signer of block %s. Sleep 2 seconds and try more', blockHash)
return sleep(2000).then(() => {
return blockSigner.getSigners(blockHash)
})
})
}
}
let tomoValidator = {
getValidatorContract: async () => {
let web3 = await Web3Util.getWeb3()
return new web3.eth.Contract(TomoValidatorABI, contractAddress.TomoValidator)
},
getValidatorContractWs: async () => {
let web3 = await Web3Util.getWeb3Socket()
return new web3.eth.Contract(TomoValidatorABI, contractAddress.TomoValidator)
},
getCandidateOwner: async (candidate) => {
let tc = await tomoValidator.getValidatorContract()
return tc.methods.getCandidateOwner(candidate).call().catch(e => {
logger.warn('cannot get owner of candidate %s. Sleep 2 seconds and try more', candidate)
return sleep(2000).then(() => {
return tomoValidator.getCandidateOwner(candidate)
})
})
},
getVoterCapacity: async (candidate, voter) => {
let tc = await tomoValidator.getValidatorContract()
return tc.methods.getVoterCap(candidate, voter).call().catch(e => {
logger.warn('cannot get owner of candidate %s. Sleep 2 seconds and try more', candidate)
return sleep(2000).then(() => {
return tomoValidator.getVoterCapacity(candidate, voter)
})
})
}
}
module.exports = { blockSigner, tomoValidator }