Skip to content

Commit

Permalink
Merge pull request #3 from AdChain/use-async-accounts
Browse files Browse the repository at this point in the history
Use async account fetch instead of sync
  • Loading branch information
jamesyoung authored Jan 24, 2017
2 parents c6756ec + 8e776bc commit 6c505a1
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 6c505a1

Please sign in to comment.