mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-16 23:57:11 +00:00
make fundAccounts TS and use @emizzle's async function
This commit is contained in:
parent
24090b89ea
commit
f516dec200
@ -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();
|
||||
});
|
||||
}
|
31
packages/embark-accounts-manager/src/fundAccount.ts
Normal file
31
packages/embark-accounts-manager/src/fundAccount.ts
Normal file
@ -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
|
||||
});
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user