From 81e0c9ce772da8b23c0db7c7604417a626ac7570 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 11 May 2018 08:20:03 -0400 Subject: [PATCH] detect web3 version and get accounts correctly --- lib/contracts/code_templates/utils.js.ejs | 17 +++++++++++++++++ .../code_templates/web3-connector.js.ejs | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/contracts/code_templates/utils.js.ejs b/lib/contracts/code_templates/utils.js.ejs index b88c7b37..25bb65fd 100644 --- a/lib/contracts/code_templates/utils.js.ejs +++ b/lib/contracts/code_templates/utils.js.ejs @@ -34,3 +34,20 @@ function __reduce(arr, memo, iteratee, cb) { }); })(0, memo); }; + +function __isNewWeb3_1() { + return (typeof(web3.version) === "string"); +}; + +function __getAccounts(cb) { + if (__isNewWeb3_1()) { + web3.eth.getAccounts().then(function(accounts) { + cb(null, accounts); + }).catch(function(err) { + cb(err); + }); + return; + } + web3.eth.getAccounts(cb); +}; + diff --git a/lib/contracts/code_templates/web3-connector.js.ejs b/lib/contracts/code_templates/web3-connector.js.ejs index 4e61fb0f..be3e3351 100644 --- a/lib/contracts/code_templates/web3-connector.js.ejs +++ b/lib/contracts/code_templates/web3-connector.js.ejs @@ -16,7 +16,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { return next(null, ''); } - web3.eth.getAccounts(function(err, account) { + __getAccounts(function(err, account) { if (err) { next(null, true) } else { @@ -24,7 +24,7 @@ __reduce(<%- connectionList %>,function(prev, value, next) { } }); }, function(err, _result) { - web3.eth.getAccounts(function(err, accounts) { + __getAccounts(function(err, accounts) { web3.eth.defaultAccount = accounts[0]; <%- done %> });