forked from waffle-iron/QuorumNetworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fundingHandler.js
60 lines (53 loc) · 1.47 KB
/
fundingHandler.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
54
55
56
57
58
59
60
let whisper = require('./Communication/whisperNetwork.js')
let util = require('./util.js')
let processedAccounts = []
function accountDiff(arrayA, arrayB){
let arrayC = []
for(let i in arrayA){
let itemA = arrayA[i]
let found = false
for(let j in arrayB){
let itemB = arrayB[j]
if(itemA === itemB){
found = true
}
}
if(found === false){
arrayC.push(itemA)
}
}
return arrayC
}
function lookAtBalances(result, cb){
if(util.IsWeb3RPCConnectionAlive(result.web3RPC)){
let thresholdBalance = 0.1
let commWeb3RPC = result.communicationNetwork.web3RPC
let web3RPC = result.web3RPC
let accounts = accountDiff(web3RPC.eth.accounts, processedAccounts)
for(let i in accounts){
let account = accounts[i]
let balance = web3RPC.fromWei(web3RPC.eth.getBalance(account).toString(), 'ether')
// if balance is below threshold, request topup
if(balance < thresholdBalance){
whisper.RequestSomeEther(commWeb3RPC, account, function(){
processedAccounts.push(account)
})
}
}
cb(true)
} else {
cb(false)
}
}
function monitorAccountBalances(result, cb){
let web3RPC = result.web3RPC
let intervalID = setInterval(function(){
lookAtBalances(result, function(connectionAlive){
if(connectionAlive == false){
clearInterval(intervalID)
}
})
}, 5*1000)
cb(null, result)
}
exports.MonitorAccountBalances = monitorAccountBalances