Commit bd444d40 authored by Iuri Matias's avatar Iuri Matias

move/refactor getBalanceInEther to their own providers

parent 91ae8e36
...@@ -404,26 +404,7 @@ class Blockchain { ...@@ -404,26 +404,7 @@ class Blockchain {
/** Get the balance of an address, and convert wei to ether */ /** Get the balance of an address, and convert wei to ether */
getBalanceInEther (address, cb) { getBalanceInEther (address, cb) {
address = stripHexPrefix(address) this.getCurrentProvider().getBalanceInEther(address, cb)
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'))
})
} }
pendingTransactionsCount () { pendingTransactionsCount () {
......
const Web3 = require('web3')
class InjectedProvider { class InjectedProvider {
...@@ -11,6 +12,16 @@ class InjectedProvider { ...@@ -11,6 +12,16 @@ class InjectedProvider {
resetEnvironment () { 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 module.exports = InjectedProvider
const Web3 = require('web3')
class NodeProvider { class NodeProvider {
...@@ -15,6 +16,16 @@ class NodeProvider { ...@@ -15,6 +16,16 @@ class NodeProvider {
resetEnvironment () { 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 module.exports = NodeProvider
const Web3 = require('web3')
const { privateToAddress, toChecksumAddress } = require('ethereumjs-util') const { privateToAddress, toChecksumAddress } = require('ethereumjs-util')
class VMProvider { class VMProvider {
...@@ -42,6 +43,16 @@ class VMProvider { ...@@ -42,6 +43,16 @@ class VMProvider {
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } 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 module.exports = VMProvider
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment