2018-07-13 12:56:59 +00:00
|
|
|
const async = require('async');
|
2018-07-16 16:48:32 +00:00
|
|
|
const Web3 = require('web3');
|
2018-07-18 14:47:53 +00:00
|
|
|
const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js');
|
|
|
|
const {readFileSync, dappPath} = require('../../core/fs');
|
2018-07-13 12:56:59 +00:00
|
|
|
|
|
|
|
class DevFunds {
|
|
|
|
constructor(blockchainConfig) {
|
|
|
|
this.web3 = null;
|
|
|
|
this.blockchainConfig = blockchainConfig;
|
|
|
|
this.accounts = [];
|
|
|
|
this.numAccounts = this.blockchainConfig.account.numAccounts || 0;
|
2018-07-16 19:19:01 +00:00
|
|
|
this.password = readFileSync(dappPath('config/development/password'), 'utf8').replace('\n', '');
|
2018-07-16 16:48:32 +00:00
|
|
|
this.web3 = new Web3();
|
2018-07-18 10:29:02 +00:00
|
|
|
this.networkId = null;
|
2018-07-13 12:56:59 +00:00
|
|
|
this.balance = Web3.utils.toWei("1", "ether");
|
2018-07-17 08:57:31 +00:00
|
|
|
if (this.blockchainConfig.account.balance) {
|
2018-07-16 16:48:32 +00:00
|
|
|
this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3);
|
|
|
|
}
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 10:29:02 +00:00
|
|
|
_sendTx() {
|
|
|
|
if (this.networkId !== 1337) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.web3.eth.sendTransaction({value: "1000000000000000", to: "0xA2817254cb8E7b6269D1689c3E0eBadbB78889d1", from: this.web3.eth.defaultAccount});
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger regular txs due to a bug in geth and stuck transactions in --dev mode
|
|
|
|
regularTxs(cb) {
|
|
|
|
const self = this;
|
|
|
|
self.web3.eth.net.getId().then((networkId) => {
|
|
|
|
self.networkId = networkId;
|
|
|
|
if (self.networkId !== 1337) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:08:32 +00:00
|
|
|
setInterval(function() { self._sendTx(); }, 1500);
|
2018-07-18 10:29:02 +00:00
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:45:18 +00:00
|
|
|
regularUnlocks() {
|
|
|
|
const self = this;
|
2018-07-18 14:47:53 +00:00
|
|
|
setInterval(function() { self.unlockAccounts(self.password, () => {}); }, 20000);
|
2018-07-18 10:45:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 12:56:59 +00:00
|
|
|
connectToNode(cb) {
|
2018-07-17 08:57:31 +00:00
|
|
|
|
2018-07-18 14:47:53 +00:00
|
|
|
this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}}));
|
2018-07-17 08:57:31 +00:00
|
|
|
|
2018-07-13 12:56:59 +00:00
|
|
|
this.web3.eth.getAccounts().then((accounts) => {
|
2018-07-16 16:48:32 +00:00
|
|
|
this.web3.eth.defaultAccount = accounts[0];
|
2018-07-17 08:57:31 +00:00
|
|
|
if (accounts.length > 1) {
|
|
|
|
this.accounts = accounts.slice(1);
|
|
|
|
}
|
2018-07-13 12:56:59 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createAccounts(numAccounts, password, cb) {
|
2018-07-17 08:57:31 +00:00
|
|
|
const numAccountsToCreate = numAccounts - (this.accounts.length + 1);
|
|
|
|
if (numAccountsToCreate === 0) return cb();
|
|
|
|
|
|
|
|
async.timesLimit(numAccountsToCreate, 1, (_, next) => {
|
2018-07-16 16:48:32 +00:00
|
|
|
this.web3.eth.personal.newAccount(password, next);
|
2018-07-13 12:56:59 +00:00
|
|
|
}, (err, accounts) => {
|
2018-07-18 12:08:32 +00:00
|
|
|
if (err) return cb(err);
|
2018-07-13 12:56:59 +00:00
|
|
|
this.accounts = accounts;
|
2018-07-18 12:08:32 +00:00
|
|
|
cb();
|
2018-07-17 08:57:31 +00:00
|
|
|
});
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unlockAccounts(password, cb) {
|
|
|
|
async.each(this.accounts, (account, next) => {
|
2018-07-17 08:57:31 +00:00
|
|
|
this.web3.eth.personal.unlockAccount(account, password).then((result) => {
|
|
|
|
next();
|
|
|
|
}).catch(next);
|
2018-07-18 12:08:32 +00:00
|
|
|
}, cb);
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fundAccounts(balance, cb) {
|
2018-07-17 08:57:31 +00:00
|
|
|
|
2018-07-13 12:56:59 +00:00
|
|
|
async.each(this.accounts, (account, next) => {
|
2018-07-17 08:57:31 +00:00
|
|
|
this.web3.eth.getBalance(account).then(currBalance => {
|
|
|
|
const remainingBalance = balance - currBalance;
|
|
|
|
if (remainingBalance <= 0) return next();
|
|
|
|
|
|
|
|
this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((result) => {
|
|
|
|
next();
|
|
|
|
}).catch(next);
|
2018-07-18 12:08:32 +00:00
|
|
|
}, cb);
|
2018-07-16 16:48:32 +00:00
|
|
|
});
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createFundAndUnlockAccounts(cb) {
|
|
|
|
async.waterfall([
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
this.connectToNode(next);
|
2018-07-13 12:56:59 +00:00
|
|
|
},
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
this.createAccounts(this.numAccounts, this.password, next);
|
2018-07-13 12:56:59 +00:00
|
|
|
},
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
|
|
|
this.unlockAccounts(this.password, next);
|
2018-07-13 12:56:59 +00:00
|
|
|
},
|
2018-07-16 16:48:32 +00:00
|
|
|
(next) => {
|
2018-07-18 10:29:02 +00:00
|
|
|
this.regularTxs();
|
2018-07-18 10:45:18 +00:00
|
|
|
this.regularUnlocks();
|
2018-07-16 16:48:32 +00:00
|
|
|
this.fundAccounts(this.balance, next);
|
|
|
|
}
|
2018-07-18 12:08:32 +00:00
|
|
|
], cb);
|
2018-07-13 12:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DevFunds;
|