From c660fc8048edf0209ab13dce84d6dcace5c4d374 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 31 May 2017 12:25:19 +0200 Subject: [PATCH] add getBalance + refactor --- src/app.js | 9 +++++++++ src/app/renderer.js | 7 ++++--- src/lib/helper.js | 6 ++++++ src/universal-dapp.js | 4 ++-- 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/lib/helper.js diff --git a/src/app.js b/src/app.js index ec152e7d6d..f10fe21caa 100644 --- a/src/app.js +++ b/src/app.js @@ -770,6 +770,15 @@ var run = function () { }, getAccounts: (callback) => { udapp.getAccounts(callback) + }, + getBalance: (address, callback) => { + udapp.getBalance(address, (error, balance) => { + if (error) { + callback(error) + } else { + callback(null, executionContext.web3().fromWei(balance, 'ether')) + } + }) } } var renderer = new Renderer(rendererAPI, compiler.event) diff --git a/src/app/renderer.js b/src/app/renderer.js index 06e4abe9a0..c97106950c 100644 --- a/src/app/renderer.js +++ b/src/app/renderer.js @@ -3,6 +3,7 @@ var $ = require('jquery') var utils = require('./utils') +var helper = require('../lib/helper.js') // -------------- styling ---------------------- var csjs = require('csjs-inject') @@ -61,7 +62,7 @@ function Renderer (appAPI, compilerEvent) { }) } }) - setInterval(() => { updateAccountBalances(appAPI) }, 1000) + setInterval(() => { updateAccountBalances(self, appAPI) }, 1000) } Renderer.prototype.clear = function () { @@ -345,13 +346,13 @@ Renderer.prototype.contracts = function (data, source) { $('.' + css.col2 + ' input,textarea').click(function () { this.select() }) } -function updateAccountBalances (appAPI) { +function updateAccountBalances (self, appAPI) { var accounts = $('#txorigin').children('option') accounts.each(function (index, value) { (function (acc) { appAPI.getBalance(accounts[acc].value, function (err, res) { if (!err) { - accounts[acc].innerText = accounts[acc].value.substring(0, 8) + '... (' + res.toString() + ' ether)' + accounts[acc].innerText = helper.shortenAddress(accounts[acc].value, res) } }) })(index) diff --git a/src/lib/helper.js b/src/lib/helper.js new file mode 100644 index 0000000000..4d09e04b71 --- /dev/null +++ b/src/lib/helper.js @@ -0,0 +1,6 @@ +module.exports = { + shortenAddress: function (address, etherBalance) { + var len = address.length + return address.slice(0, 5) + '...' + address.slice(len - 5, len) + (etherBalance ? ' (' + etherBalance.toString() + ' ether)' : '') + } +} diff --git a/src/universal-dapp.js b/src/universal-dapp.js index fbedf2876c..c289c2df90 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -9,6 +9,7 @@ var EventManager = require('ethereum-remix').lib.EventManager var crypto = require('crypto') var async = require('async') var TxRunner = require('./app/txRunner') +var helper = require('./lib/helper') var yo = require('yo-yo') // copy to copyToClipboard @@ -353,8 +354,7 @@ UniversalDApp.prototype.getInstanceInterface = function (contract, address, $tar var context = self.executionContext.isVM() ? 'memory' : 'blockchain' address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex') - var len = address.length - var shortAddress = address.slice(0, 5) + '...' + address.slice(len - 5, len) + var shortAddress = helper.shortenAddress(address) var title = yo`
${contract.name} at ${shortAddress} (${context})