Skip to content

Commit 11d6980

Browse files
committed
further dev funding
1 parent f221397 commit 11d6980

File tree

10 files changed

+3054
-2940
lines changed

10 files changed

+3054
-2940
lines changed

lib/cmds/blockchain/blockchain.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ Blockchain.prototype.run = function() {
164164
data = data.toString();
165165
if (self.onReadyCallback && !self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
166166
if (self.isDev) {
167-
return self.createFundAndUnlockAccounts(() => {
167+
return self.createFundAndUnlockAccounts((err) => {
168+
if(err) console.error('Error creating, unlocking, and funding accounts', err);
168169
self.readyCalled = true;
169170
self.onReadyCallback();
170171
});
@@ -185,12 +186,12 @@ Blockchain.prototype.run = function() {
185186
Blockchain.prototype.createFundAndUnlockAccounts = function(cb) {
186187
console.dir('createFundAndUnlockAccounts');
187188
let devFunds = new DevFunds(this.config);
188-
devFunds.createFundAndUnlockAccounts(() => {
189+
devFunds.createFundAndUnlockAccounts((err) => {
189190
console.dir("=====> done!");
190191
console.dir(arguments);
191-
cb();
192+
cb(err);
192193
});
193-
}
194+
};
194195

195196
Blockchain.prototype.kill = function() {
196197
if (this.child) {

lib/cmds/blockchain/dev_funds.js

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
const 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

56
class 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

lib/contracts/accountParser.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const bip39 = require("bip39");
22
const hdkey = require('ethereumjs-wallet/hdkey');
33
const fs = require('../core/fs');
4+
const {getHexBalanceFromString} = require('../utils/utils');
45

56
class AccountParser {
67
static parseAccountsConfig(accountsConfig, web3, logger) {
@@ -21,31 +22,15 @@ class AccountParser {
2122
return accounts;
2223
}
2324

24-
static getHexBalance(balanceString, web3) {
25-
if (!balanceString) {
26-
return 0xFFFFFFFFFFFFFFFFFF;
27-
}
28-
if (web3.utils.isHexStrict(balanceString)) {
29-
return balanceString;
30-
}
31-
const match = balanceString.match(/([0-9]+) ?([a-zA-Z]*)/);
32-
if (!match) {
33-
throw new Error(__('Unrecognized balance string "%s"', balanceString));
34-
}
35-
if (!match[2]) {
36-
return web3.utils.toHex(parseInt(match[1], 10));
37-
}
38-
39-
return web3.utils.toHex(web3.utils.toWei(match[1], match[2]));
40-
}
41-
4225
static getAccount(accountConfig, web3, logger) {
4326
if (!logger) {
4427
logger = console;
4528
}
4629
let hexBalance = null;
4730
if (accountConfig.balance) {
48-
hexBalance = AccountParser.getHexBalance(accountConfig.balance, web3);
31+
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
32+
//hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
33+
console.dir('[contracts/accountParser]: balance ' + accountConfig.balance + ' converted to hex ' + hexBalance);
4934
}
5035
if (accountConfig.privateKey) {
5136
if (!accountConfig.privateKey.startsWith('0x')) {

lib/contracts/blockchain.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Blockchain {
5050
const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws';
5151
let provider;
5252
this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`;
53-
53+
console.dir('[blockchain/contracts]: web3 endpoint: ' + this.web3Endpoint);
5454
const providerOptions = {
5555
web3: this.web3,
5656
accountsConfig: this.contractsConfig.deployment.accounts,
@@ -64,6 +64,7 @@ class Blockchain {
6464

6565
async.waterfall([
6666
function checkNode(next) {
67+
console.dir('[blockchain/contracts]: check node');
6768
self.assertNodeConnection(true, (err) => {
6869
if (err && self.web3StartedInProcess) {
6970
// Already started blockchain in another node, we really have a node problem
@@ -77,16 +78,19 @@ class Blockchain {
7778
}
7879
self.web3StartedInProcess = true;
7980
self.startBlockchainNode(() => {
81+
console.dir('[blockchain/contracts]: starting blockchain node');
8082
// Need to re-initialize web3 to connect to the new blockchain node
8183
provider.stop();
8284
self.initWeb3(cb);
8385
});
8486
});
8587
},
8688
function startProvider(next) {
89+
console.dir('[blockchain/contracts]: starting web3 provider');
8790
provider.startWeb3Provider(next);
8891
},
8992
function fundAccountsIfNeeded(next) {
93+
console.dir('[blockchain/contracts]: funding accounts');
9094
provider.fundAccounts(next);
9195
}
9296
], (err) => {
@@ -178,7 +182,11 @@ class Blockchain {
178182
});
179183

180184
this.events.setCommandHandler("blockchain:gasPrice", function(cb) {
181-
self.getGasPrice(cb);
185+
console.dir('[blockchain/contracts]: getting gas price...');
186+
self.getGasPrice((gp) => {
187+
console.dir('[blockchain/contracts]: got gasPrice of ' + gp);
188+
cb(gp);
189+
});
182190
});
183191

184192
}
@@ -277,7 +285,16 @@ class Blockchain {
277285
}
278286
let accountConfig = self.blockchainConfig.account;
279287
let selectedAccount = accountConfig && accountConfig.address;
288+
console.dir('[contracts/blockchain]: setting default account of ' + (selectedAccount || accounts[0]));
280289
self.setDefaultAccount(selectedAccount || accounts[0]);
290+
console.dir('[contracts/blockchain]: accounts on node = ');
291+
//let numAccts = accounts.length;
292+
self.web3.eth.getAccounts().then((accounts) => {
293+
accounts.forEach((account) => {
294+
self.web3.eth.getBalance(account).then((balance) => console.dir('[contracts/blockchain]: account ' + account + ' has balance of ' + balance));
295+
//if(--numAccts === 0) cb();
296+
});
297+
});
281298
cb();
282299
});
283300
}

lib/contracts/contracts.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,21 @@ class ContractsManager {
5454
self.contracts = {};
5555
async.waterfall([
5656
function loadContractFiles(callback) {
57+
console.dir('[contracts/contracts]: load contract files');
5758
self.events.request("config:contractsFiles", (contractsFiles) => {
5859
self.contractsFiles = contractsFiles;
5960
callback();
6061
});
6162
},
6263
function loadContractConfigs(callback) {
64+
console.dir('[contracts/contracts]: load contract configs');
6365
self.events.request("config:contractsConfig", (contractsConfig) => {
6466
self.contractsConfig = cloneDeep(contractsConfig);
6567
callback();
6668
});
6769
},
6870
function compileContracts(callback) {
71+
console.dir('[contracts/contracts]: compile contracts');
6972
self.events.emit("status", __("Compiling..."));
7073
if (process.env.isTest && self.compiledContracts && Object.keys(self.compiledContracts).length) {
7174
// Only compile once for tests
@@ -77,6 +80,7 @@ class ContractsManager {
7780
});
7881
},
7982
function prepareContractsFromConfig(callback) {
83+
console.dir('[contracts/contracts]: prepare contracts from config');
8084
self.events.emit("status", __("Building..."));
8185
let className, contract;
8286
for (className in self.contractsConfig.contracts) {
@@ -90,12 +94,17 @@ class ContractsManager {
9094
callback();
9195
},
9296
function getGasPriceForNetwork(callback) {
97+
console.dir('[contracts/contracts]: gas price for network, passed in gasPrice from config: ' + self.contractsConfig.gasPrice);
9398
if (self.contractsConfig.gasPrice) {
9499
return callback(null, self.contractsConfig.gasPrice);
95100
}
96-
self.events.request("blockchain:gasPrice", callback);
101+
self.events.request("blockchain:gasPrice", (gasprice) => {
102+
console.dir('[contracts/contracts]: got gasPrice of ' + gasprice);
103+
callback(gasprice);
104+
});
97105
},
98106
function prepareContractsFromCompilation(gasPrice, callback) {
107+
console.dir('[contracts/contracts]: using gasprice ' + gasPrice + ', prepare contracts from compilation');
99108
let className, compiledContract, contractConfig, contract;
100109
for (className in self.compiledContracts) {
101110
compiledContract = self.compiledContracts[className];
@@ -123,6 +132,7 @@ class ContractsManager {
123132
callback();
124133
},
125134
function setDeployIntention(callback) {
135+
console.dir('[contracts/contracts]: set deploy intention');
126136
let className, contract;
127137
for (className in self.contracts) {
128138
contract = self.contracts[className];
@@ -140,6 +150,7 @@ class ContractsManager {
140150
},
141151
/*eslint complexity: ["error", 11]*/
142152
function dealWithSpecialConfigs(callback) {
153+
console.dir('[contracts/contracts]: deal with special configs');
143154
let className, contract, parentContractName, parentContract;
144155
let dictionary = Object.keys(self.contracts);
145156

@@ -189,6 +200,7 @@ class ContractsManager {
189200
callback();
190201
},
191202
function removeContractsWithNoCode(callback) {
203+
console.dir('[contracts/contracts]: remove Contracts with no code');
192204
let className, contract;
193205
let dictionary = Object.keys(self.contracts);
194206
for (className in self.contracts) {
@@ -210,6 +222,7 @@ class ContractsManager {
210222
/*eslint complexity: ["error", 16]*/
211223
/*eslint max-depth: ["error", 16]*/
212224
function determineDependencies(callback) {
225+
console.dir('[contracts/contracts]: determining dependencies');
213226
let className, contract;
214227
for (className in self.contracts) {
215228
contract = self.contracts[className];

lib/contracts/deploy_manager.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class DeployManager {
6767
async.waterfall([
6868
function buildContracts(callback) {
6969
self.events.request("contracts:build", self.deployOnlyOnConfig, (err) => {
70+
console.dir('[contracts/deploy_manager]: done building contracts');
7071
callback(err);
7172
});
7273
},
@@ -82,22 +83,28 @@ class DeployManager {
8283

8384
// TODO: could be implemented as an event (beforeDeployAll)
8485
function checkIsConnectedToBlockchain(callback) {
86+
console.dir('[contracts/deploy_manager]: checking connection to blockchain');
8587
self.blockchain.onReady(() => {
88+
console.dir('[contracts/deploy_manager]: onReady called, asserting node connection');
8689
self.blockchain.assertNodeConnection((err) => {
90+
console.dir('[contracts/deploy_manager]: node connection asserted, err: ' + JSON.stringify(err));
8791
callback(err);
8892
});
8993
});
9094
},
9195

9296
// TODO: this can be done on the fly or as part of the initialization
9397
function determineDefaultAccount(callback) {
98+
console.dir('[contracts/deploy_manager]: determining default acct');
9499
self.blockchain.determineDefaultAccount((err) => {
95100
callback(err);
96101
});
97102
},
98103

99104
function deployAllContracts(callback) {
105+
console.dir('[contracts/deploy_manager]: deploying all contracts');
100106
self.deployAll(function (err) {
107+
console.dir('[contracts/deploy_manager]: done deploying all contracts, err: ' + JSON.stringify(err));
101108
if (!err) {
102109
self.events.emit('contractsDeployed');
103110
}
@@ -108,6 +115,7 @@ class DeployManager {
108115
});
109116
},
110117
function runAfterDeploy(callback) {
118+
console.dir('[contracts/deploy_manager]: emitting and running actions for event "contracts:deploy:afterAll"');
111119
self.plugins.emitAndRunActionsForEvent('contracts:deploy:afterAll', callback);
112120
}
113121
], function (err, _result) {

0 commit comments

Comments
 (0)