Skip to content

Commit

Permalink
Use async account fetch instead of sync
Browse files Browse the repository at this point in the history
This allows TestRPC to run in the same node process as the loopback app
  • Loading branch information
rlisagor committed Jan 24, 2017
1 parent c6756ec commit 8e776bc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ var Web3 = require('./lib/web3');
exports.initialize = function(dataSource, cb) {
var settings = dataSource.settings;
var connector = new Web3(settings);
connector.connect(cb);

dataSource.connector = connector;
};
22 changes: 14 additions & 8 deletions lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ function Web3Connector(settings) {
this.settings = settings;

const url = settings.url || 'http://localhost:8545';
const web3 = new Web3(new Web3.providers.HttpProvider(url));

if (web3.eth.accounts[0]) {
web3.eth.defaultAccount = web3.eth.accounts[0];
} else {
throw "error : can not connect to rpc server - please check datasources.json"
}
this.web3 = web3;
this.web3 = new Web3(new Web3.providers.HttpProvider(url));
}

Web3Connector.prototype.DataAccessObject = Web3DAO;

Web3Connector.prototype.connect = function(cb) {
const web3 = this.web3;

web3.eth.getAccounts(function(err, accounts) {
if (err) {
return cb(err);
}

web3.eth.defaultAccount = accounts[0];
cb();
});
}

Web3Connector.prototype.define = function(modelData) {
var web3 = this.web3;
var model = modelData.model;
Expand Down

0 comments on commit 8e776bc

Please sign in to comment.