-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
41 lines (34 loc) · 1.39 KB
/
utils.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
// Note we need to leave and reopen truffle console if we change this...
// 'require' caches the js files not sure how to easily prevent this.
// From truffle, REMEMBER TO CHANGE THE PORT! Could convert to an object...
// var utils = require("./utils.js")
var Web3 = require('web3')
// Note we must change the web3 provider port when we change the testnet port.
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8544'))
var acc = index => web3.eth.accounts[index]
var balance = acct => web3.fromWei(web3.eth.getBalance(acct), 'ether').toNumber()
var rawBalance = acct => web3.eth.getBalance(acct).toNumber()
var balances = numberOfAccounts => {
Array
.from(Array(numberOfAccounts).keys())
.forEach(index => console.log(index + " " + acc(index) + " : " + balance(acc(index))))
}
var unlockAccount = (acct, password) => web3.personal.unlockAccount(acct, password, 0);
var unlockAccounts = (numberOfAccounts, password) => {
return Array
.from(Array(numberOfAccounts).keys())
.map(index => acc(index))
.map(account => unlockAccount(account, password))
.every(unlocked => true)
}
module.exports = {
acc: acc,
balance: balance,
rawBalance: rawBalance,
balances: balances,
unlockAccount: unlockAccount,
unlockAccounts: unlockAccounts,
weiToEther: wei => web3.fromWei(wei, 'ether'),
etherToWei: ether => web3.toWei(ether, 'ether'),
gweiToWei: gwei => web3.toWei(gwei, 'gwei')
}