diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index 337fd6d560..15f068af1d 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -404,26 +404,7 @@ class Blockchain { /** Get the balance of an address, and convert wei to ether */ getBalanceInEther (address, cb) { - address = stripHexPrefix(address) - - if (!this.executionContext.isVM()) { - return this.executionContext.web3().eth.getBalance(address, (err, res) => { - if (err) { - return cb(err) - } - cb(null, Web3.utils.fromWei(res.toString(10), 'ether')) - }) - } - if (!this.providers.vm.accounts) { - return cb('No accounts?') - } - - this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { - if (err) { - return cb('Account not found') - } - cb(null, Web3.utils.fromWei(new BN(res.balance).toString(10), 'ether')) - }) + this.getCurrentProvider().getBalanceInEther(address, cb) } pendingTransactionsCount () { diff --git a/src/blockchain/providers/injected.js b/src/blockchain/providers/injected.js index f121667c0d..0663f69903 100644 --- a/src/blockchain/providers/injected.js +++ b/src/blockchain/providers/injected.js @@ -1,3 +1,4 @@ +const Web3 = require('web3') class InjectedProvider { @@ -11,6 +12,16 @@ class InjectedProvider { resetEnvironment () { } + + getBalanceInEther (address, cb) { + address = stripHexPrefix(address) + this.executionContext.web3().eth.getBalance(address, (err, res) => { + if (err) { + return cb(err) + } + cb(null, Web3.utils.fromWei(res.toString(10), 'ether')) + }) + } } module.exports = InjectedProvider diff --git a/src/blockchain/providers/node.js b/src/blockchain/providers/node.js index 9ba7106044..a31f4e7882 100644 --- a/src/blockchain/providers/node.js +++ b/src/blockchain/providers/node.js @@ -1,3 +1,4 @@ +const Web3 = require('web3') class NodeProvider { @@ -15,6 +16,16 @@ class NodeProvider { resetEnvironment () { } + + getBalanceInEther(address, cb) { + address = stripHexPrefix(address) + this.executionContext.web3().eth.getBalance(address, (err, res) => { + if (err) { + return cb(err) + } + cb(null, Web3.utils.fromWei(res.toString(10), 'ether')) + }) + } } module.exports = NodeProvider diff --git a/src/blockchain/providers/vm.js b/src/blockchain/providers/vm.js index 7d4c817cb5..8697584f0e 100644 --- a/src/blockchain/providers/vm.js +++ b/src/blockchain/providers/vm.js @@ -1,3 +1,4 @@ +const Web3 = require('web3') const { privateToAddress, toChecksumAddress } = require('ethereumjs-util') class VMProvider { @@ -42,6 +43,16 @@ class VMProvider { this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } } + getBalanceInEther (address, cb) { + address = stripHexPrefix(address) + + this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => { + if (err) { + return cb('Account not found') + } + cb(null, Web3.utils.fromWei(new BN(res.balance).toString(10), 'ether')) + }) + } } module.exports = VMProvider