move/refactor getBalanceInEther to their own providers

pull/1/head
Iuri Matias 5 years ago
parent 91ae8e364f
commit bd444d408b
  1. 21
      src/blockchain/blockchain.js
  2. 11
      src/blockchain/providers/injected.js
  3. 11
      src/blockchain/providers/node.js
  4. 11
      src/blockchain/providers/vm.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 () {

@ -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

@ -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

@ -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

Loading…
Cancel
Save