diff --git a/packages/embark-accounts-manager/src/fundAccount.js b/packages/embark-accounts-manager/src/fundAccount.js deleted file mode 100644 index 9b4be690c..000000000 --- a/packages/embark-accounts-manager/src/fundAccount.js +++ /dev/null @@ -1,75 +0,0 @@ -const async = require('async'); -const TARGET = 0x7FFFFFFFFFFFFFFF; -const ALREADY_FUNDED = 'alreadyFunded'; - -export default function fundAccount(web3, accountAddress, hexBalance, callback) { - if (!hexBalance) { - hexBalance = TARGET; - } - const targetBalance = web3.utils.toBN(hexBalance); - let accountBalance; - let coinbaseAddress; - let lastNonce; - let gasPrice; - - async.waterfall([ - function getAccountBalance(next) { - web3.eth.getBalance(accountAddress, (err, balance) => { - if (err) { - return next(err); - } - balance = web3.utils.toBN(balance); - if (balance.gte(targetBalance)) { - return next(ALREADY_FUNDED); - } - accountBalance = balance; - next(); - }); - }, - function getNeededParams(next) { - async.parallel([ - function getCoinbaseAddress(paraCb) { - web3.eth.getCoinbase() - .then((address) => { - coinbaseAddress = address; - paraCb(); - }).catch(paraCb); - }, - function getGasPrice(paraCb) { - web3.eth.getGasPrice((err, price) => { - if (err) { - return paraCb(err); - } - gasPrice = price; - paraCb(); - }); - } - ], (err, _result) => { - next(err); - }); - }, - function getNonce(next) { - web3.eth.getTransactionCount(coinbaseAddress, (err, nonce) => { - if (err) { - return next(err); - } - lastNonce = nonce; - next(); - }); - }, - function sendTransaction(next) { - web3.eth.sendTransaction({ - from: coinbaseAddress, - to: accountAddress, - value: targetBalance.sub(accountBalance), - gasPrice: gasPrice, - nonce: lastNonce - }, next); - } - ], (err) => { - if (err && err !== ALREADY_FUNDED) { - return callback(err); - } - callback(); - }); -} diff --git a/packages/embark-accounts-manager/src/fundAccount.ts b/packages/embark-accounts-manager/src/fundAccount.ts new file mode 100644 index 000000000..8736329e9 --- /dev/null +++ b/packages/embark-accounts-manager/src/fundAccount.ts @@ -0,0 +1,31 @@ +import Web3 = require("web3"); + +const TARGET = 0x7FFFFFFFFFFFFFFF; + +export default async function fundAccount(web3: Web3, accountAddress: string, coinbaseAddress: string, hexBalance: string | number | undefined) { + if (!hexBalance) { + hexBalance = TARGET; + } + const targetBalance = web3.utils.toBN(hexBalance); + + // check if account is already funded + let accountBalance = await web3.eth.getBalance(accountAddress); + accountBalance = web3.utils.toBN(accountBalance); + if (accountBalance.gte(targetBalance)) { + return; + } + + // run in parallel + const getGasPricePromise = web3.eth.getGasPrice(); + const getTxCountPromise = web3.eth.getTransactionCount(coinbaseAddress); + + const [gasPrice, lastNonce] = await Promise.all([getGasPricePromise, getTxCountPromise]); + + return web3.eth.sendTransaction({ + from: coinbaseAddress, + to: accountAddress, + value: targetBalance.sub(accountBalance), + gasPrice: gasPrice, + nonce: lastNonce + }); +} diff --git a/packages/embark-accounts-manager/src/index.ts b/packages/embark-accounts-manager/src/index.ts index 177511f91..d047a4944 100644 --- a/packages/embark-accounts-manager/src/index.ts +++ b/packages/embark-accounts-manager/src/index.ts @@ -142,16 +142,18 @@ export default class AccountsManager { this.ready = true; return; } - async.eachLimit(this.accounts, 1, (account, eachCb) => { - if (!account.address) { - return eachCb(); - } - fundAccount(web3, account.address, account.hexBalance, eachCb); - }, (err) => { - if (err) { - this.logger.error(__("Error funding accounts"), err.message || err); - } - this.ready = true; - }); + try { + const coinbase = await web3.eth.getCoinbase(); + const fundingAccounts = this.accounts.map((account) => { + if (!account.address) { + return null; + } + return fundAccount(web3, account.address, coinbase, account.hexBalance); + }); + await Promise.all(fundingAccounts); + } catch (err) { + this.logger.error(__("Error funding accounts"), err.message || err); + } + this.ready = true; } } diff --git a/packages/embark-proxy/src/proxy.js b/packages/embark-proxy/src/proxy.js index e511a7df1..10b9c69c9 100644 --- a/packages/embark-proxy/src/proxy.js +++ b/packages/embark-proxy/src/proxy.js @@ -15,6 +15,7 @@ export class Proxy { this.receipts = {}; this.transactions = {}; this.timeouts = {}; + // TODO create local events instance for the ready events that are just used locally once `events.js` is extracted in a package this.events = options.events; this.plugins = options.plugins; this.logger = options.logger;