Implement getBalance() for retrieving the balance

pull/1/head
Alex Beregszaszi 9 years ago
parent 2222e0b1af
commit 6731cb5f97
  1. 25
      src/universal-dapp.js

@ -41,6 +41,7 @@ UniversalDApp.prototype.addAccount = function (privateKey, balance) {
var account = new EthJSAccount();
account.balance = balance || 'f00000000000000001';
this.vm.stateManager.trie.put(address, account.serialize());
this.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 };
@ -57,6 +58,30 @@ UniversalDApp.prototype.getAccounts = function (cb) {
}
};
UniversalDApp.prototype.getBalance = function (address, cb) {
address = ethJSUtil.stripHexPrefix(address);
if (!this.vm) {
web3.eth.getBalance(address, function (err, res) {
if (err) {
cb(err);
} else {
cb(null, res.toString(10));
}
});
} else {
if (!this.accounts) return cb("No accounts?");
this.vm.stateManager.trie.get(new Buffer(address, 'hex'), function (err, res) {
if (err) {
cb("Account not found");
} else {
cb(null, new ethJSUtil.BN(new EthJSAccount(res).toString(), 16).toString(10));
}
});
}
};
UniversalDApp.prototype.render = function () {
if (this.contracts.length == 0) {
this.$el.append( this.getABIInputForm() );

Loading…
Cancel
Save