Merge pull request #29 from ethereum/patch/account-balance

Implement getBalance() for retrieving the account balance
pull/1/head
chriseth 9 years ago
commit ed460f09c6
  1. 1
      package.json
  2. 30
      src/universal-dapp.js

@ -13,7 +13,6 @@
"merkle-patricia-tree": "^2.1.2", "merkle-patricia-tree": "^2.1.2",
"ethereumjs-util": "^4.4.0", "ethereumjs-util": "^4.4.0",
"ethereumjs-tx": "^1.1.1", "ethereumjs-tx": "^1.1.1",
"ethereumjs-account": "^2.0.2",
"ethereumjs-abi": "^0.6.1", "ethereumjs-abi": "^0.6.1",
"web3": "^0.15.3", "web3": "^0.15.3",
"jquery": "^2.2.0", "jquery": "^2.2.0",

@ -3,7 +3,6 @@ var EthJSVM = require('ethereumjs-vm');
var Trie = require('merkle-patricia-tree'); var Trie = require('merkle-patricia-tree');
var ethJSUtil = require('ethereumjs-util'); var ethJSUtil = require('ethereumjs-util');
var EthJSTX = require('ethereumjs-tx'); var EthJSTX = require('ethereumjs-tx');
var EthJSAccount = require('ethereumjs-account');
var ethJSABI = require('ethereumjs-abi'); var ethJSABI = require('ethereumjs-abi');
var EthJSBlock = require('ethereumjs-block'); var EthJSBlock = require('ethereumjs-block');
var web3 = require('./web3-adapter.js'); var web3 = require('./web3-adapter.js');
@ -39,9 +38,8 @@ UniversalDApp.prototype.addAccount = function (privateKey, balance) {
privateKey = new Buffer(privateKey, 'hex') privateKey = new Buffer(privateKey, 'hex')
var address = ethJSUtil.privateToAddress(privateKey); var address = ethJSUtil.privateToAddress(privateKey);
var account = new EthJSAccount(); // FIXME: we don't care about the callback, but we should still make this proper
account.balance = balance || 'f00000000000000001'; this.vm.stateManager.putAccountBalance(address, balance || 'f00000000000000001', function cb() {} );
this.vm.stateManager.trie.put(address, account.serialize());
this.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 }; this.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 };
} }
@ -57,6 +55,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.getAccountBalance(new Buffer(address, 'hex'), function (err, res) {
if (err) {
cb("Account not found");
} else {
cb(null, new ethJSUtil.BN(res).toString(10));
}
});
}
};
UniversalDApp.prototype.render = function () { UniversalDApp.prototype.render = function () {
if (this.contracts.length == 0) { if (this.contracts.length == 0) {
this.$el.append( this.getABIInputForm() ); this.$el.append( this.getABIInputForm() );

Loading…
Cancel
Save