Merge branch 'fund_accounts' of github.com:embark-framework/embark into fund_accounts

This commit is contained in:
Iuri Matias 2018-07-18 17:48:58 +03:00
commit c1809e3752
9 changed files with 13 additions and 103 deletions

View File

@ -184,11 +184,8 @@ Blockchain.prototype.run = function() {
};
Blockchain.prototype.createFundAndUnlockAccounts = function(cb) {
console.dir('createFundAndUnlockAccounts');
let devFunds = new DevFunds(this.config);
devFunds.createFundAndUnlockAccounts((err) => {
console.dir("=====> done!");
console.dir(arguments);
cb(err);
});
};

View File

@ -14,9 +14,7 @@ class DevFunds {
this.networkId = null;
this.balance = Web3.utils.toWei("1", "ether");
if (this.blockchainConfig.account.balance) {
console.dir('[blockchain/dev_funds]: converting balance from ' + this.blockchainConfig.account.balance);
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
console.dir('[blockchain/dev_funds]: converted balance to ' + this.balance);
}
}
@ -36,7 +34,7 @@ class DevFunds {
return;
}
setInterval(function() { self._sendTx(); }, 3000);
setInterval(function() { self._sendTx(); }, 1500);
if (cb) {
cb();
}
@ -57,7 +55,6 @@ class DevFunds {
if (accounts.length > 1) {
this.accounts = accounts.slice(1);
}
console.dir('----- CURRENT ACCOUNTS ' + this.accounts);
cb();
});
}
@ -66,89 +63,54 @@ class DevFunds {
const numAccountsToCreate = numAccounts - (this.accounts.length + 1);
if (numAccountsToCreate === 0) return cb();
console.dir("creating " + numAccountsToCreate + " new accounts with password " + password);
async.timesLimit(numAccountsToCreate, 1, (_, next) => {
console.dir("--- creating new account");
this.web3.eth.personal.newAccount(password, next);
}, (err, accounts) => {
if (err) console.error(err);
console.dir("-- accounts created are ");
console.dir(accounts);
if (err) return cb(err);
this.accounts = accounts;
cb(err);
cb();
});
}
unlockAccounts(password, cb) {
console.dir('--- CURRENT ACCOUNTS ' + this.accounts);
async.each(this.accounts, (account, next) => {
console.dir('-- unlocking account ' + account + ' with password ' + password);
this.web3.eth.personal.unlockAccount(account, password).then((result) => {
console.dir('-- unlocked account ' + account + ' with password ' + password + ' and result ' + result);
next();
}).catch(next);
}, (err) => {
console.dir('-- FINISHED UNLOCKING ACCOUNTS, err= ' + err);
cb(err);
});
}, cb);
}
fundAccounts(balance, cb) {
console.dir('-- funding accounts...');
async.each(this.accounts, (account, next) => {
this.web3.eth.getBalance(account).then(currBalance => {
const remainingBalance = balance - currBalance;
console.dir("---- account " + account + " balance needed = " + remainingBalance);
if (remainingBalance <= 0) return next();
console.dir("-- funding account " + account + " with balance " + remainingBalance);
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((result) => {
console.dir('FUNDING ACCT result: ' + JSON.stringify(result));
next();
}).catch(next);
}, (err) => {
console.dir('-- FINISHED FUNDING ACCOUNTS, err= ' + err);
cb(err);
});
}, cb);
});
}
createFundAndUnlockAccounts(cb) {
async.waterfall([
(next) => {
console.dir('--- CONNECTING TO NODE');
this.connectToNode(next);
},
(next) => {
console.dir('--- CREATING THE ACCOUNTS');
this.createAccounts(this.numAccounts, this.password, next);
},
(next) => {
console.dir('--- UNLOCKING THE ACCOUNTS');
this.unlockAccounts(this.password, next);
},
(next) => {
console.dir('--- FUNDING THE ACCOUNTS');
this.regularTxs();
this.regularUnlocks();
this.fundAccounts(this.balance, next);
}
], (err) => {
console.dir(`--- COMPLETED THE ACCOUNTS (${this.accounts.join(', ')} and funded with ${this.balance} wei)`);
if (err) console.error('Error creating, unlocking, and funding accounts', JSON.stringify(err));
// this.web3.eth.getAccounts().then((accounts) => {
// let numAccts = accounts.length;
// accounts.forEach((account) => {
// this.web3.eth.getBalance(account).then((balance) => {
// console.dir('[contracts/dev_funds]: account ' + account + ' has balance of ' + balance);
// if(--numAccts === 0) cb(err);
// });
// });
// });
cb(err);
});
], cb);
}
}

View File

@ -30,7 +30,6 @@ class AccountParser {
if (accountConfig.balance) {
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
//hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
console.dir('[contracts/accountParser]: balance ' + accountConfig.balance + ' converted to hex ' + hexBalance);
}
if (accountConfig.privateKey) {
if (!accountConfig.privateKey.startsWith('0x')) {

View File

@ -51,7 +51,6 @@ class Blockchain {
const protocol = (this.contractsConfig.deployment.type === "rpc") ? this.contractsConfig.deployment.protocol : 'ws';
let provider;
this.web3Endpoint = utils.buildUrl(protocol, this.contractsConfig.deployment.host, this.contractsConfig.deployment.port);//`${protocol}://${this.contractsConfig.deployment.host}:${this.contractsConfig.deployment.port}`;
console.dir('[blockchain/contracts]: web3 endpoint: ' + this.web3Endpoint);
const providerOptions = {
web3: this.web3,
accountsConfig: this.contractsConfig.deployment.accounts,
@ -65,7 +64,6 @@ class Blockchain {
async.waterfall([
function checkNode(next) {
console.dir('[blockchain/contracts]: check node');
self.assertNodeConnection(true, (err) => {
if (err && self.web3StartedInProcess) {
// Already started blockchain in another node, we really have a node problem
@ -79,7 +77,6 @@ class Blockchain {
}
self.web3StartedInProcess = true;
self.startBlockchainNode(() => {
console.dir('[blockchain/contracts]: starting blockchain node');
// Need to re-initialize web3 to connect to the new blockchain node
provider.stop();
self.initWeb3(cb);
@ -87,11 +84,9 @@ class Blockchain {
});
},
function startProvider(next) {
console.dir('[blockchain/contracts]: starting web3 provider');
provider.startWeb3Provider(next);
},
function fundAccountsIfNeeded(next) {
console.dir('[blockchain/contracts]: funding accounts');
self.isWeb3Ready = true;
provider.fundAccounts(next);
}
@ -184,11 +179,7 @@ class Blockchain {
});
this.events.setCommandHandler("blockchain:gasPrice", function(cb) {
console.dir('[blockchain/contracts]: getting gas price...');
self.getGasPrice((gp) => {
console.dir('[blockchain/contracts]: got gasPrice of ' + gp);
cb(gp);
});
self.getGasPrice(cb);
});
}
@ -287,16 +278,7 @@ class Blockchain {
}
let accountConfig = self.blockchainConfig.account;
let selectedAccount = accountConfig && accountConfig.address;
console.dir('[contracts/blockchain]: setting default account of ' + (selectedAccount || accounts[0]));
self.setDefaultAccount(selectedAccount || accounts[0]);
console.dir('[contracts/blockchain]: accounts on node = ');
//let numAccts = accounts.length;
self.web3.eth.getAccounts().then((accounts) => {
accounts.forEach((account) => {
self.web3.eth.getBalance(account).then((balance) => console.dir('[contracts/blockchain]: account ' + account + ' has balance of ' + balance));
//if(--numAccts === 0) cb();
});
});
cb();
});
}

View File

@ -201,7 +201,6 @@ class ContractDeployer {
},
function getGasPriceForNetwork(next) {
self.events.request("blockchain:gasPrice", (gasPrice) => {
console.dir('[contracts/contracts]: got gasPrice of ' + gasPrice);
contract.gasPrice = contract.gasPrice || gasPrice;
next();
});

View File

@ -54,21 +54,18 @@ class ContractsManager {
self.contracts = {};
async.waterfall([
function loadContractFiles(callback) {
console.dir('[contracts/contracts]: load contract files');
self.events.request("config:contractsFiles", (contractsFiles) => {
self.contractsFiles = contractsFiles;
callback();
});
},
function loadContractConfigs(callback) {
console.dir('[contracts/contracts]: load contract configs');
self.events.request("config:contractsConfig", (contractsConfig) => {
self.contractsConfig = cloneDeep(contractsConfig);
callback();
});
},
function compileContracts(callback) {
console.dir('[contracts/contracts]: compile contracts');
self.events.emit("status", __("Compiling..."));
if (process.env.isTest && self.compiledContracts && Object.keys(self.compiledContracts).length) {
// Only compile once for tests
@ -80,7 +77,6 @@ class ContractsManager {
});
},
function prepareContractsFromConfig(callback) {
console.dir('[contracts/contracts]: prepare contracts from config');
self.events.emit("status", __("Building..."));
let className, contract;
for (className in self.contractsConfig.contracts) {
@ -94,11 +90,9 @@ class ContractsManager {
callback();
},
function getGasPriceForNetwork(callback) {
console.dir('[contracts/contracts]: gas price for network, passed in gasPrice from config: ' + self.contractsConfig.gasPrice);
return callback(null, self.contractsConfig.gasPrice);
},
function prepareContractsFromCompilation(gasPrice, callback) {
console.dir('[contracts/contracts]: using gasprice ' + gasPrice + ', prepare contracts from compilation');
let className, compiledContract, contractConfig, contract;
for (className in self.compiledContracts) {
compiledContract = self.compiledContracts[className];
@ -126,7 +120,6 @@ class ContractsManager {
callback();
},
function setDeployIntention(callback) {
console.dir('[contracts/contracts]: set deploy intention');
let className, contract;
for (className in self.contracts) {
contract = self.contracts[className];
@ -144,7 +137,6 @@ class ContractsManager {
},
/*eslint complexity: ["error", 11]*/
function dealWithSpecialConfigs(callback) {
console.dir('[contracts/contracts]: deal with special configs');
let className, contract, parentContractName, parentContract;
let dictionary = Object.keys(self.contracts);
@ -194,7 +186,6 @@ class ContractsManager {
callback();
},
function removeContractsWithNoCode(callback) {
console.dir('[contracts/contracts]: remove Contracts with no code');
let className, contract;
let dictionary = Object.keys(self.contracts);
for (className in self.contracts) {
@ -216,7 +207,6 @@ class ContractsManager {
/*eslint complexity: ["error", 16]*/
/*eslint max-depth: ["error", 16]*/
function determineDependencies(callback) {
console.dir('[contracts/contracts]: determining dependencies');
let className, contract;
for (className in self.contracts) {
contract = self.contracts[className];

View File

@ -67,7 +67,6 @@ class DeployManager {
async.waterfall([
function buildContracts(callback) {
self.events.request("contracts:build", self.deployOnlyOnConfig, (err) => {
console.dir('[contracts/deploy_manager]: done building contracts');
callback(err);
});
},
@ -83,28 +82,20 @@ class DeployManager {
// TODO: could be implemented as an event (beforeDeployAll)
function checkIsConnectedToBlockchain(callback) {
console.dir('[contracts/deploy_manager]: checking connection to blockchain');
self.blockchain.onReady(() => {
console.dir('[contracts/deploy_manager]: onReady called, asserting node connection');
self.blockchain.assertNodeConnection((err) => {
console.dir('[contracts/deploy_manager]: node connection asserted, err: ' + JSON.stringify(err));
callback(err);
});
self.blockchain.assertNodeConnection(callback);
});
},
// TODO: this can be done on the fly or as part of the initialization
function determineDefaultAccount(callback) {
console.dir('[contracts/deploy_manager]: determining default acct');
self.blockchain.determineDefaultAccount((err) => {
callback(err);
});
},
function deployAllContracts(callback) {
console.dir('[contracts/deploy_manager]: deploying all contracts');
self.deployAll(function (err) {
console.dir('[contracts/deploy_manager]: done deploying all contracts, err: ' + JSON.stringify(err));
if (!err) {
self.events.emit('contractsDeployed');
}
@ -115,7 +106,6 @@ class DeployManager {
});
},
function runAfterDeploy(callback) {
console.dir('[contracts/deploy_manager]: emitting and running actions for event "contracts:deploy:afterAll"');
self.plugins.emitAndRunActionsForEvent('contracts:deploy:afterAll', callback);
}
], function (err, _result) {

View File

@ -2,13 +2,14 @@ const async = require('async');
const TARGET = 0x7FFFFFFFFFFFFFFF;
const ALREADY_FUNDED = 'alreadyFunded';
function fundAccount(web3, accountAddress, hexBalance, nonce, callback) {
function fundAccount(web3, accountAddress, hexBalance, callback) {
if (!hexBalance) {
hexBalance = TARGET;
}
const targetBalance = (typeof hexBalance === 'string') ? parseInt(hexBalance, 16) : hexBalance;
let accountBalance;
let coinbaseAddress;
let lastNonce;
let gasPrice;
async.waterfall([
@ -51,23 +52,17 @@ function fundAccount(web3, accountAddress, hexBalance, nonce, callback) {
if (err) {
return next(err);
}
lastNonce = nonce;
next();
});
},
function sendTransaction(next) {
console.dir('[contracts/fundAccount]: sending tx ' + JSON.stringify({
from: coinbaseAddress,
to: accountAddress,
value: targetBalance - accountBalance,
gasPrice: gasPrice,
nonce: nonce
}));
web3.eth.sendTransaction({
from: coinbaseAddress,
to: accountAddress,
value: targetBalance - accountBalance,
gasPrice: gasPrice,
nonce: nonce
nonce: lastNonce
}, next);
}
], (err) => {

View File

@ -78,12 +78,8 @@ class Provider {
if (!self.isDev) {
return callback();
}
let nonce = 0;
async.each(self.accounts, (account, eachCb) => {
console.dir('[contracts/provider]: nonce is ' + nonce);
fundAccount(self.web3, account.address, account.hexBalance, nonce++, (err) => {
eachCb(err);
});
fundAccount(self.web3, account.address, account.hexBalance, eachCb);
}, callback);
}