|
| 1 | +const async = require('async'); |
| 2 | +const Web3 = require('web3'); |
| 3 | +const {getWeiBalanceFromString, buildUrl} = require('../../utils/utils.js'); |
| 4 | +const {readFileSync, dappPath} = require('../../core/fs'); |
| 5 | + |
| 6 | +class DevFunds { |
| 7 | + constructor(blockchainConfig) { |
| 8 | + // TODO: temporary to avoid nasty suprises, should be removed |
| 9 | + try { |
| 10 | + this.web3 = null; |
| 11 | + this.blockchainConfig = blockchainConfig; |
| 12 | + this.accounts = []; |
| 13 | + this.numAccounts = this.blockchainConfig.account.numAccounts || 0; |
| 14 | + this.password = readFileSync(dappPath(blockchainConfig.account.password), 'utf8').replace('\n', ''); |
| 15 | + this.web3 = new Web3(); |
| 16 | + this.networkId = null; |
| 17 | + this.balance = Web3.utils.toWei("1", "ether"); |
| 18 | + if (this.blockchainConfig.account.balance) { |
| 19 | + this.balance = getWeiBalanceFromString(this.blockchainConfig.account.balance, this.web3); |
| 20 | + } |
| 21 | + } catch(_err) { |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + _sendTx() { |
| 26 | + if (this.networkId !== 1337) { |
| 27 | + return; |
| 28 | + } |
| 29 | + this.web3.eth.sendTransaction({value: "1000000000000000", to: "0xA2817254cb8E7b6269D1689c3E0eBadbB78889d1", from: this.web3.eth.defaultAccount}); |
| 30 | + } |
| 31 | + |
| 32 | + // trigger regular txs due to a bug in geth and stuck transactions in --dev mode |
| 33 | + regularTxs(cb) { |
| 34 | + const self = this; |
| 35 | + self.web3.eth.net.getId().then((networkId) => { |
| 36 | + self.networkId = networkId; |
| 37 | + if (self.networkId !== 1337) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + setInterval(function() { self._sendTx(); }, 1500); |
| 42 | + if (cb) { |
| 43 | + cb(); |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + regularUnlocks() { |
| 49 | + const self = this; |
| 50 | + setInterval(function() { self.unlockAccounts(self.password, () => {}); }, 20000); |
| 51 | + } |
| 52 | + |
| 53 | + connectToNode(cb) { |
| 54 | + |
| 55 | + this.web3.setProvider(new Web3.providers.WebsocketProvider(buildUrl('ws', this.blockchainConfig.wsHost, this.blockchainConfig.wsPort), {headers: {Origin: "http://localhost:8000"}})); |
| 56 | + |
| 57 | + this.web3.eth.getAccounts().then((accounts) => { |
| 58 | + this.web3.eth.defaultAccount = accounts[0]; |
| 59 | + if (accounts.length > 1) { |
| 60 | + this.accounts = accounts.slice(1); |
| 61 | + } |
| 62 | + cb(); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + createAccounts(numAccounts, password, cb) { |
| 67 | + const numAccountsToCreate = numAccounts - (this.accounts.length + 1); |
| 68 | + if (numAccountsToCreate === 0) return cb(); |
| 69 | + |
| 70 | + async.timesLimit(numAccountsToCreate, 1, (_, next) => { |
| 71 | + this.web3.eth.personal.newAccount(password, next); |
| 72 | + }, (err, accounts) => { |
| 73 | + if (err) return cb(err); |
| 74 | + this.accounts = accounts; |
| 75 | + cb(); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + unlockAccounts(password, cb) { |
| 80 | + async.each(this.accounts, (account, next) => { |
| 81 | + this.web3.eth.personal.unlockAccount(account, password).then((_result) => { |
| 82 | + next(); |
| 83 | + }).catch(next); |
| 84 | + }, cb); |
| 85 | + } |
| 86 | + |
| 87 | + fundAccounts(balance, cb) { |
| 88 | + |
| 89 | + async.each(this.accounts, (account, next) => { |
| 90 | + this.web3.eth.getBalance(account).then(currBalance => { |
| 91 | + const remainingBalance = balance - currBalance; |
| 92 | + if (remainingBalance <= 0) return next(); |
| 93 | + |
| 94 | + this.web3.eth.sendTransaction({to: account, value: remainingBalance}).then((_result) => { |
| 95 | + next(); |
| 96 | + }).catch(next); |
| 97 | + }, cb); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + createFundAndUnlockAccounts(cb) { |
| 102 | + if (!this.web3) { |
| 103 | + return cb(); |
| 104 | + } |
| 105 | + async.waterfall([ |
| 106 | + (next) => { |
| 107 | + this.connectToNode(next); |
| 108 | + }, |
| 109 | + (next) => { |
| 110 | + this.createAccounts(this.numAccounts, this.password, next); |
| 111 | + }, |
| 112 | + (next) => { |
| 113 | + this.unlockAccounts(this.password, next); |
| 114 | + }, |
| 115 | + (next) => { |
| 116 | + this.regularTxs(); |
| 117 | + this.regularUnlocks(); |
| 118 | + this.fundAccounts(this.balance, next); |
| 119 | + } |
| 120 | + ], cb); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = DevFunds; |
0 commit comments