move code to front end. funds on provider start

This commit is contained in:
Jonathan Rainville 2018-05-18 09:25:44 -04:00
parent 418c55851b
commit 6c8653ef09
5 changed files with 150 additions and 114 deletions

View File

@ -1,52 +0,0 @@
/*global web3, eth*/
(function () {
var workAccount = '<%- accountAddress %>';
// Blockchain process ends if Javascript ends
function keepAlive() {
setInterval(function () {
// Just stay alive
}, 999999);
}
var workAccountBalance = eth.getBalance(workAccount);
var TARGET = 15000000000000000000;
if (workAccountBalance >= TARGET) {
return keepAlive();
}
function getNonce() {
return web3.eth.getTransactionCount(eth.coinbase);
}
function getGasPrice() {
return web3.eth.getGasPrice();
}
function sendTransaction(nonce, gasPrice) {
web3.eth.sendTransaction({
from: eth.coinbase,
to: workAccount,
value: TARGET - workAccountBalance,
gasPrice: gasPrice,
nonce: nonce
}, function (err, _result) {
if (err) {
console.error('Error while transferring funds to user account', err);
}
});
}
try {
var nonce = getNonce();
var gasPrice = getGasPrice();
sendTransaction(nonce, gasPrice);
} catch (e) {
console.error('Error while getting nonce or gas price', e);
}
keepAlive();
})();

View File

@ -1,7 +1,4 @@
const async = require('async');
const Web3 = require('web3');
const AccountParser = require('../../contracts/accountParser');
const fs = require('../../core/fs');
// TODO: make all of this async
class GethCommands {
@ -209,25 +206,6 @@ class GethCommands {
}
callback(null, "");
},
function fundAccount(callback) {
if (self.isDev && self.config.accountsConfig && self.config.accountsConfig.length) {
const accounts = AccountParser.parseAccountsConfig(self.config.accountsConfig, new Web3());
if (accounts.length) {
const fundAccountTemplate = require('./fundAccout.js.ejs');
const code = fundAccountTemplate({accountAddress: accounts[0].address});
const filePath = `.embark/${self.env}/js/fundAccount.js`;
fs.writeFile(fs.dappPath(filePath), code, (err) => {
if (err) {
console.error('Failed to created the script to fund the account');
return callback(null, '');
}
callback(null, filePath);
});
return;
}
}
callback(null, "");
},
function isDev(callback) {
if (self.isDev) {
return callback(null, '--dev');

View File

@ -0,0 +1,72 @@
const async = require('async');
const TARGET = 15000000000000000000;
const ALREADY_FUNDED = 'alreadyFunded';
function fundAccount(web3, accountAddress, callback) {
let accountBalance;
let coinbaseAddress;
let lastNonce;
let gasPrice;
async.waterfall([
function getAccountBalance(next) {
web3.eth.getBalance(accountAddress, (err, balance) => {
if (err) {
return next(err);
}
if (balance >= TARGET) {
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: TARGET - accountBalance,
gasPrice: gasPrice,
nonce: lastNonce
}, next);
}
], (err) => {
if (err && err !== ALREADY_FUNDED) {
return callback(err);
}
callback();
});
}
module.exports = fundAccount;

View File

@ -1,38 +1,63 @@
const ProviderEngine = require('web3-provider-engine');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js');
const async = require('async');
const AccountParser = require('./accountParser');
const fundAccount = require('./fundAccount');
const NO_ACCOUNTS = 'noAccounts';
class Provider {
constructor(options) {
const self = this;
this.web3 = options.web3;
this.accountsConfig = options.accountsConfig;
this.web3Endpoint = options.web3Endpoint;
this.logger = options.logger;
this.engine = new ProviderEngine();
this.asyncMethods = {};
}
this.engine.addProvider(new RpcSubprovider({
rpcUrl: options.web3Endpoint
startProvider(callback) {
const self = this;
self.engine.addProvider(new RpcSubprovider({
rpcUrl: self.web3Endpoint
}));
this.accounts = AccountParser.parseAccountsConfig(this.accountsConfig, this.web3, this.logger);
this.addresses = [];
if (this.accounts.length) {
this.accounts.forEach(account => {
this.addresses.push(account.address);
this.web3.eth.accounts.wallet.add(account);
});
this.asyncMethods = {
eth_accounts: self.eth_accounts.bind(this)
};
}
// network connectivity error
this.engine.on('error', (err) => {
self.engine.on('error', (err) => {
// report connectivity errors
this.logger.error(err.stack);
self.logger.error(err.stack);
});
self.engine.start();
self.web3.setProvider(self);
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
self.addresses = [];
async.waterfall([
function fundAccounts(next) {
if (!self.accounts.length) {
return next(NO_ACCOUNTS);
}
async.each(self.accounts, (account, eachCb) => {
fundAccount(self.web3, account.address, eachCb);
}, next);
},
function populateWeb3Wallet(next) {
self.accounts.forEach(account => {
self.addresses.push(account.address);
self.web3.eth.accounts.wallet.add(account);
});
self.asyncMethods = {
eth_accounts: self.eth_accounts.bind(self)
};
next();
}
], function (err) {
if (err && err !== NO_ACCOUNTS) {
self.logger.error((err));
}
callback();
});
this.engine.start();
}
eth_accounts(payload, cb) {

View File

@ -275,6 +275,7 @@ class Engine {
web3Service(options) {
let self = this;
this.web3 = options.web3;
let provider;
if (this.web3 === undefined) {
this.web3 = new Web3();
if (this.config.contractsConfig.deployment.type === "rpc") {
@ -285,43 +286,55 @@ class Engine {
logger: this.logger,
web3Endpoint
};
this.web3.setProvider(new Provider(providerOptions));
provider = new Provider(providerOptions);
} else {
throw new Error("contracts config error: unknown deployment type " + this.config.contractsConfig.deployment.type);
}
}
self.servicesMonitor.addCheck('Ethereum', function (cb) {
if (self.web3.currentProvider === undefined) {
return cb({name: __("No Blockchain node found"), status: 'off'});
async.waterfall([
function (next) {
if (!provider) {
return next();
}
provider.startProvider(next);
}
self.web3.eth.getAccounts(function(err, _accounts) {
if (err) {
], function (err) {
if (err) {
console.error(err);
}
self.servicesMonitor.addCheck('Ethereum', function (cb) {
if (self.web3.currentProvider === undefined) {
return cb({name: __("No Blockchain node found"), status: 'off'});
}
// TODO: web3_clientVersion method is currently not implemented in web3.js 1.0
self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, version) => {
self.web3.eth.getAccounts(function(err, _accounts) {
if (err) {
return cb({name: __("Ethereum node (version unknown)"), status: 'on'});
return cb({name: __("No Blockchain node found"), status: 'off'});
}
if (version.indexOf("/") < 0) {
return cb({name: version, status: 'on'});
}
let nodeName = version.split("/")[0];
let versionNumber = version.split("/")[1].split("-")[0];
let name = nodeName + " " + versionNumber + " (Ethereum)";
return cb({name: name, status: 'on'});
// TODO: web3_clientVersion method is currently not implemented in web3.js 1.0
self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, version) => {
if (err) {
return cb({name: __("Ethereum node (version unknown)"), status: 'on'});
}
if (version.indexOf("/") < 0) {
return cb({name: version, status: 'on'});
}
let nodeName = version.split("/")[0];
let versionNumber = version.split("/")[1].split("-")[0];
let name = nodeName + " " + versionNumber + " (Ethereum)";
return cb({name: name, status: 'on'});
});
});
});
});
this.registerModule('whisper', {
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor),
communicationConfig: this.config.communicationConfig,
web3: this.web3
self.registerModule('whisper', {
addCheck: self.servicesMonitor.addCheck.bind(self.servicesMonitor),
communicationConfig: self.config.communicationConfig,
web3: self.web3
});
});
}