@@ -4,7 +4,6 @@ let TestLogger = require('../lib/tests/test_logger.js');
44const Web3 = require ( 'web3' ) ;
55const i18n = require ( '../lib/i18n/i18n.js' ) ;
66const constants = require ( '../lib/constants.json' ) ;
7- const Test = require ( '../lib/tests/test' ) ;
87const DevFunds = require ( '../lib/cmds/blockchain/dev_funds' ) ;
98const async = require ( 'async' ) ;
109const FakeIpcProvider = require ( './helpers/fakeIpcProvider' ) ;
@@ -52,73 +51,88 @@ describe('embark.DevFunds', function () {
5251 config . rpcPort += constants . blockchain . servicePortOnProxy ;
5352 }
5453
55- // TODO put default config
56- const test = new Test ( { loglevel : 'trace' } ) ;
54+ describe ( '#create, fund, and unlock accounts' , function ( ) {
55+ let provider = new FakeIpcProvider ( ) ;
56+ let devFunds = new DevFunds ( { blockchainConfig : config , provider : provider , logger : new TestLogger ( { } ) } ) ;
57+ const web3 = new Web3 ( provider ) ;
5758
59+ it ( 'should create correct number of accounts' , function ( done ) {
60+ provider . injectResult ( [ '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855' ] ) ; // getAccounts - return --dev account
61+ devFunds . getCurrentAccounts ( ( ) => {
5862
59- test . initWeb3Provider ( ( err ) => {
60- if ( err ) throw err ;
61- describe ( '#create, fund, and unlock accounts' , function ( ) {
62- let provider = new FakeIpcProvider ( ) ;
63- let devFunds = new DevFunds ( config , provider , new TestLogger ( { } ) ) ;
64- const web3 = new Web3 ( provider ) ;
63+ provider . injectResult ( '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' ) ; // createAccount #1
64+ provider . injectResult ( '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab' ) ; // createAccount #2
6565
66- it ( 'should create correct number of accounts' , function ( done ) {
67- provider . injectResult ( [ '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855' ] ) ; // getAccounts - return --dev account
68- devFunds . getCurrentAccounts ( ( ) => {
66+ devFunds . createAccounts ( config . account . numAccounts , 'test_password' , ( err ) => {
67+ assert . equal ( err , null ) ;
6968
70- provider . injectResult ( '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' ) ; // createAccount #1
71- provider . injectResult ( ' 0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab') ; // createAccount #2
69+ // TODO: make FakeIpcProvider smart enough to keep track of created accounts
70+ provider . injectResult ( [ '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855' , '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' , ' 0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab'] ) ;
7271
73-
74- devFunds . createAccounts ( config . account . numAccounts , 'test_password' , ( err ) => {
75- assert . equal ( err , null ) ;
76-
77- provider . injectResult ( [ '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855' , '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' , '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab' ] ) ;
78- web3 . eth . getAccounts ( ) . then ( ( accts ) => {
79- console . log ( 'got accts: ' + JSON . stringify ( accts ) ) ;
80- assert . equal ( accts . length , config . account . numAccounts ) ;
81- assert . strictEqual ( accts [ 0 ] , '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855' ) ;
82- assert . strictEqual ( accts [ 1 ] , '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe' ) ;
83- assert . strictEqual ( accts [ 2 ] , '0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb' ) ;
84- done ( ) ;
85- } ) ;
72+ web3 . eth . getAccounts ( ) . then ( ( accts ) => {
73+ assert . equal ( accts . length , config . account . numAccounts ) ;
74+ assert . strictEqual ( accts [ 0 ] , '0x47D33b27Bb249a2DBab4C0612BF9CaF4C1950855' ) ;
75+ assert . strictEqual ( accts [ 1 ] , '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe' ) ;
76+ assert . strictEqual ( accts [ 2 ] , '0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb' ) ;
77+ done ( ) ;
8678 } ) ;
8779 } ) ;
8880 } ) ;
81+ } ) ;
82+
83+ it ( 'should unlock accounts' , function ( done ) {
84+ provider . injectResult ( true ) ; // account #1 unlock result
85+ provider . injectResult ( true ) ; // account #2 unlock result
86+
87+ devFunds . unlockAccounts ( devFunds . password , ( errUnlock ) => {
88+ assert . equal ( errUnlock , null ) ;
89+ done ( ) ;
90+ } ) ;
91+ } ) ;
92+
93+ it ( 'should fund accounts' , function ( done ) {
94+
95+ provider . injectResult ( '1234567890' ) ; // account #1 balance
96+ provider . injectResult ( '1234567890' ) ; // account #2 balance
97+ // provider.injectResult('0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe'); // send tx #1
98+ // provider.injectResult('0x22F4d0A3C12E86b4b5F39B213f7e19D048276DAb'); // send tx #2
8999
90- it ( 'should fund accounts' , function ( done ) {
91- console . dir ( 'funding accounts...' ) ;
92-
93- provider . injectResult ( '1234567890' ) ; // account #1 balance
94- provider . injectResult ( '1234567890' ) ; // account #2 balance
95- provider . injectResult ( '0xfff12345678976543213456786543212345675432' ) ; // send tx #1
96- provider . injectResult ( '0xfff12345678976543213456786543212345675433' ) ; // send tx #2
97-
98- try {
99- devFunds . fundAccounts ( devFunds . balance , ( err ) => {
100- console . dir ( 'accounts funded...' ) ;
101- assert . equal ( err , null ) ;
102-
103- provider . injectResult ( [ '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855' , '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' , '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab' ] ) ;
104- web3 . eth . getAccounts ( ) . then ( ( accts ) => {
105- console . log ( 'got accts: ' + JSON . stringify ( accts ) ) ;
106-
107- const weiFromConfig = utils . getWeiBalanceFromString ( config . account . balance ) ;
108- async . each ( accts , ( acct , cb ) => {
109- provider . injectResult ( web3 . utils . numberToHex ( weiFromConfig ) ) ;
110- devFunds . web3 . eth . getBalance ( acct ) . then ( ( wei ) => {
111- assert . equal ( wei , weiFromConfig ) ;
112- cb ( ) ;
113- } ) . catch ( cb ) ;
114- } , function ( err ) { done ( ) ; } ) ;
115- } ) . catch ( ( ) => {
116- done ( ) ;
117- } ) ;
100+ devFunds . fundAccounts ( devFunds . balance , ( errFundAccounts ) => {
101+
102+ assert . equal ( errFundAccounts , null ) ;
103+
104+ // inject response for web3.eth.getAccounts
105+ // TODO: make FakeIpcProvider smart enough to keep track of created accounts
106+ provider . injectResult ( [ '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855' , '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' , '0x22f4d0a3c12e86b4b5f39b213f7e19d048276dab' ] ) ;
107+
108+ web3 . eth . getAccounts ( ) . then ( ( accts ) => {
109+
110+ const weiFromConfig = utils . getWeiBalanceFromString ( config . account . balance , web3 ) ;
111+
112+ async . each ( accts , ( acct , cb ) => {
113+
114+ // inject response for web3.eth.getBalance.
115+ // essentially, this will always return the amount we specified
116+ // in the config.
117+ // this is dodgy. really, we should be letting the FakeIpcProvider
118+ // at this point tell us how many wei we have per account (as it would
119+ // in a real node), but the FakeIpcProvider is not smart enough... yet.
120+ // TODO: make FakeIpcProvider smart enough to keep track of balances
121+ provider . injectResult ( web3 . utils . numberToHex ( weiFromConfig ) ) ;
122+
123+ web3 . eth . getBalance ( acct ) . then ( ( wei ) => {
124+ assert . equal ( wei , weiFromConfig ) ;
125+ cb ( ) ;
126+ } ) . catch ( cb ) ;
127+
128+ } , function ( errAcctsBalance ) {
129+ if ( errAcctsBalance ) throw errAcctsBalance ;
130+ done ( ) ;
118131 } ) ;
119- } catch ( errFundAccts ) {
120- throw errFundAccts ;
121- }
132+ } ) . catch ( ( errGetAccts ) => {
133+ if ( errGetAccts ) throw errGetAccts ;
134+ done ( ) ;
135+ } ) ;
122136 } ) ;
123137 } ) ;
124138 } ) ;
0 commit comments