11const async = require ( 'async' ) ;
2- var Web3 = require ( 'web3' ) ;
3- var utils = require ( '../../utils/utils.js' ) ;
2+ const Web3 = require ( 'web3' ) ;
3+ const { getWeiBalanceFromString, buildUrl} = require ( '../../utils/utils.js' ) ;
4+ const { readFileSync, dappPath} = require ( '../../core/fs' ) ;
45
56class DevFunds {
67 constructor ( blockchainConfig ) {
78 this . web3 = null ;
89 this . blockchainConfig = blockchainConfig ;
910 this . accounts = [ ] ;
1011 this . numAccounts = this . blockchainConfig . account . numAccounts || 0 ;
11- //this.balance = this.blockchainConfig.account.balance || Web3.utils.toWei("1", "ether");
12+ this . password = readFileSync ( dappPath ( 'config/development/password' ) ) ;
13+ this . password = 'dev_password' ;
14+ this . existingNumAccounts = 0 ;
15+ this . web3 = new Web3 ( ) ;
1216 this . balance = Web3 . utils . toWei ( "1" , "ether" ) ;
13- this . password = "dev_password"
17+ if ( this . blockchainConfig . account . balance ) {
18+ console . dir ( '[blockchain/dev_funds]: converting balance from ' + this . blockchainConfig . account . balance ) ;
19+ this . balance = getWeiBalanceFromString ( this . blockchainConfig . account . balance , this . web3 ) ;
20+ console . dir ( '[blockchain/dev_funds]: converted balance to ' + this . balance ) ;
21+ }
1422 }
1523
1624 connectToNode ( cb ) {
17- const self = this ;
18- this . web3 = new Web3 ( ) ;
19- this . web3 . setProvider ( new Web3 . providers . WebsocketProvider ( utils . buildUrl ( 'ws' , this . blockchainConfig . wsHost , this . blockchainConfig . wsPort ) , { headers : { Origin : "http://localhost:8000" } } ) ) ;
25+
26+ this . web3 . setProvider ( new Web3 . providers . WebsocketProvider ( buildUrl ( 'ws' , this . blockchainConfig . wsHost , this . blockchainConfig . wsPort ) , { headers : { Origin : "http://localhost:8000" } } ) ) ;
27+
2028 this . web3 . eth . getAccounts ( ) . then ( ( accounts ) => {
21- self . web3 . eth . defaultAccount = accounts [ 0 ] ;
29+ this . web3 . eth . defaultAccount = accounts [ 0 ] ;
30+ this . existingNumAccounts = accounts . length ;
2231 cb ( ) ;
2332 } ) ;
2433 }
2534
2635 createAccounts ( numAccounts , password , cb ) {
27- console . dir ( "creating " + numAccounts + " with password " + password ) ;
28- const self = this ;
29- async . timesLimit ( numAccounts , 1 , ( _ , next ) => {
36+ console . dir ( "creating " + ( numAccounts - this . existingNumAccounts ) + " new accounts with password " + password ) ;
37+ async . timesLimit ( ( numAccounts - this . existingNumAccounts ) , 1 , ( _ , next ) => {
3038 console . dir ( "--- creating new account" ) ;
31- self . web3 . eth . personal . newAccount ( password , next ) ;
39+ this . web3 . eth . personal . newAccount ( password , next ) ;
3240 } , ( err , accounts ) => {
41+ if ( err ) console . error ( err ) ;
3342 console . dir ( "-- accounts created are " ) ;
3443 console . dir ( accounts ) ;
3544 this . accounts = accounts ;
@@ -38,37 +47,62 @@ class DevFunds {
3847 }
3948
4049 unlockAccounts ( password , cb ) {
41- const self = this ;
4250 async . each ( this . accounts , ( account , next ) => {
43- self . web3 . eth . personal . unlockAccount ( account , password ) . then ( ( ) => { next ( ) } ) ;
51+ console . dir ( '-- unlocking account ' + account + ' with password ' + password ) ;
52+ this . web3 . eth . personal . unlockAccount ( account , password ) . then ( ( ) => next ( ) ) . catch ( next ) ;
4453 } , cb ) ;
4554 }
4655
4756 fundAccounts ( balance , cb ) {
48- const self = this ;
57+ console . dir ( '-- funding accounts...' ) ;
58+
59+
60+
4961 async . each ( this . accounts , ( account , next ) => {
50- self . web3 . eth . sendTransaction ( { to : account , value : balance } ) . then ( ( ) => {
62+ console . dir ( "-- funding account " + account + " with balance " + balance ) ;
63+ this . web3 . eth . sendTransaction ( { to : account , value : balance } ) . then ( ( result ) => {
64+ console . dir ( 'FUNDING ACCT result: ' + JSON . stringify ( result ) ) ;
5165 next ( ) ;
52- } ) ;
53- } , cb ) ;
66+ } ) . catch ( next ) ;
67+ } , ( err ) => {
68+ console . dir ( '-- FINISHED FUNDING ACCOUNTS, err= ' + err ) ;
69+ cb ( err ) ;
70+ } ) ;
5471 }
5572
5673 createFundAndUnlockAccounts ( cb ) {
57- const self = this ;
5874 async . waterfall ( [
59- function connect ( next ) {
60- self . connectToNode ( next ) ;
61- } ,
62- function create ( next ) {
63- self . createAccounts ( self . numAccounts , self . password , next )
75+ ( next ) => {
76+ console . dir ( '--- CONNECTING TO NODE' ) ;
77+ this . connectToNode ( next ) ;
6478 } ,
65- function unlock ( next ) {
66- self . unlockAccounts ( self . password , next ) ;
79+ ( next ) => {
80+ console . dir ( '--- CREATING THE ACCOUNTS' ) ;
81+ this . createAccounts ( this . numAccounts , this . password , next ) ;
6782 } ,
68- function fund ( next ) {
69- self . fundAccounts ( self . balance , next ) ;
83+ ( next ) => {
84+ console . dir ( '--- UNLOCKING THE ACCOUNTS' ) ;
85+ this . unlockAccounts ( this . password , next ) ;
7086 } ,
71- ] , cb ) ;
87+ ( next ) => {
88+ console . dir ( '--- FUNDING THE ACCOUNTS' ) ;
89+ this . fundAccounts ( this . balance , next ) ;
90+ }
91+ ] , ( err ) => {
92+ console . trace ( `--- COMPLETED THE ACCOUNTS (${ this . accounts . join ( ', ' ) } and funded with ${ this . balance } wei)` ) ;
93+ if ( err ) console . error ( 'Error creating, unlocking, and funding accounts' , err ) ;
94+
95+ // this.web3.eth.getAccounts().then((accounts) => {
96+ // let numAccts = accounts.length;
97+ // accounts.forEach((account) => {
98+ // this.web3.eth.getBalance(account).then((balance) => {
99+ // console.dir('[contracts/dev_funds]: account ' + account + ' has balance of ' + balance);
100+ // if(--numAccts === 0) cb(err);
101+ // });
102+ // });
103+ // });
104+ cb ( err ) ;
105+ } ) ;
72106 }
73107}
74108
0 commit comments