-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactis.js
395 lines (359 loc) · 14.4 KB
/
actis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
var app = angular.module("ActisApp", ["LocalStorageModule", "ngSanitize"]);
app.filter('utc', function () {
return function (val) {
var date = new Date(val * 1000);
return new Date(date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours(),
date.getUTCMinutes(),
date.getUTCSeconds());
};
}).filter('units', function ($rootScope) {
return function (val, unit) {
if (typeof val == "undefined" || val == "") {
return "";
} else {
return $rootScope.settings.web3.fromWei(val, unit).toNumber();
}
}
}).filter('unitsSimpleNumber', function ($rootScope) {
return function (val, unit) {
if (typeof val == "undefined" || val == "") {
return "";
} else {
return $rootScope.settings.web3.fromWei(val, unit);
}
}
});
function isNumberKey(evt, objInput) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode == 46 && (objInput.value.split('.').length > 1 || objInput.value.length == 0)) {
return false;
} else if (charCode != 37 && charCode != 39 && charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
var STORAGE_KEY_ACTIS_CONFIG = "actis.config";
var STORAGE_CONTRACT_PREFFIX = "contr."
app.config(function (localStorageServiceProvider) {
localStorageServiceProvider.setPrefix('actis').setStorageType('localStorage').setNotify(true, true);
});
//Init app
app.run(function ($rootScope, uiService, localStorageService) {
//load from storage configuration
$rootScope.appConfig = localStorageService.get(STORAGE_KEY_ACTIS_CONFIG);
if ($rootScope.appConfig == null) {
$rootScope.appConfig = {
peers: {}
}
$rootScope.appConfig.peers["localhost"] = {
peerName: "localhost",
peerUri: "http://127.0.0.1:8545",
blockCount: 7
};
}
$rootScope.settings = {
//selected peer
peerName: null,
web3: null,
reloadInterval: 2000,
//current value unit
unit: "wei",
units: ["wei", "kwei", "mwei", "gwei", "szabo", "finney", "ether", "kether", "mether", "gether", "tether"]
}
$rootScope.ui = {
reloadInterval: null,
blockArr: [],
networkProtocolVer: 0,
//the ethereum protocol version.
ethProtocolVer: 0,
//number of hashes per second that the node is mining with.
hashRate: 0,
//the gas price is determined by the x latest blocks median gas price.
gasPrice: 0,
//whether the node is mining or not.
mining: false,
//show progress bar
showProgress: false,
//last block
lastBlockNumber: 0
}
uiService.initUI();
});
app.service('uiService', function ($rootScope, $http, $timeout, $interval) {
var self = this;
this.initUI = function () {
$rootScope.settings.peer = $rootScope.appConfig.peers["localhost"];
$rootScope.ui.reloadInterval = $interval(function () {
self.loadData()
}, $rootScope.settings.reloadInterval);
}
this.loadData = function () {
var blockArr = $rootScope.ui.blockArr;
var web3 = $rootScope.settings.web3;
if (web3 != null) {
web3.eth.getBlockNumber(function (err, lastBlock) {
if (lastBlock > $rootScope.ui.lastBlockNumber) {
web3.eth.getBlock("latest", function (error, block) {
if (error) {
alert(error);
} else {
if (blockArr.length == 0) {
blockArr.push(block);
} else {
//add only latest block
if (blockArr[0].number != block.number) {
blockArr.unshift(block);
if (blockArr.length > $rootScope.appConfig.peers[$rootScope.settings.peerName].blockCount) {
blockArr.pop();
}
}
}
}
});
web3.eth.getHashrate(function (error, result) {
if (error) {
alert(error);
} else {
$rootScope.ui.hashRate = result;
}
});
web3.eth.getGasPrice(function (error, result) {
if (error) {
alert(error);
} else {
$rootScope.ui.gasPrice = result;
}
});
web3.eth.getMining(function (error, result) {
if (error) {
alert(error);
} else {
$rootScope.ui.mining = result;
}
});
$rootScope.ui.lastBlockNumber = lastBlock;
}
});
}
}
});
app.controller('mainController', ['$scope', '$rootScope', '$timeout', '$interval', 'uiService', 'localStorageService', function ($scope, $rootScope, $timeout, $interval, uiService, localStorageService) {
$scope.userTabs = {};
$scope.tabSelected = "";
var web3;
$scope.peer = {};
$scope.contractDetails = {};
$scope.switchPeer = function (peerName) {
$scope.peer = $rootScope.appConfig.peers[peerName];
}
$scope.deletePeer = function (peerName) {
$scope.peer = null;
delete $rootScope.appConfig.peers[peerName];
localStorageService.set(STORAGE_KEY_ACTIS_CONFIG, $rootScope.appConfig);
}
$scope.addPeer = function () {
$scope.peer = {blockCount: 7}
}
$scope.savePeer = function () {
$rootScope.appConfig.peers[$scope.peer.peerName] = $scope.peer;
localStorageService.set(STORAGE_KEY_ACTIS_CONFIG, $rootScope.appConfig);
}
$scope.connectToPeer = function (formObj) {
$scope.settingsErr = null;
$rootScope.ui.blockArr = [];
$rootScope.settings.peerName = $scope.peer.peerName;
web3 = new Web3(new Web3.providers.HttpProvider($scope.peer.peerUri));
if (web3.isConnected()) {
$rootScope.settings.web3 = web3;
$rootScope.ui.networkProtocolVer = web3.version.network;
$rootScope.ui.ethProtocolVer = web3.version.ethereum;
$('#settingsModal').modal('hide');
} else {
$scope.settingsErr = "Can't connect to peer !"
}
}
$scope.showContractsCovernance = function (address) {
var keys = localStorageService.keys();
$scope.contracts = {};
$scope.addTab("contracts", $scope.contracts);
for (index in keys) {
if (keys[index].startsWith(STORAGE_CONTRACT_PREFFIX)) {
contract = localStorageService.get(keys[index]);
$scope.contracts[contract.contractAddress] = contract;
}
}
}
/*
Contract details
*/
$scope.addContract = function (formObj) {
//trim and remove \" if code copied from abigen result (contract golang binding source)
$scope.contractDetails.contractABI = ($scope.contractDetails.contractABI + "").trim().replace(/\\/g, '');
localStorageService.set(STORAGE_CONTRACT_PREFFIX + $scope.contractDetails.contractAddress, $scope.contractDetails);
$scope.contracts[$scope.contractDetails.contractAddress] = $scope.contractDetails;
$scope.contractDetails = {contractAddress: "", contractName: "", contractVersion: ""};
}
$scope.deleteContract = function (contractAddress) {
localStorageService.remove(STORAGE_CONTRACT_PREFFIX + contractAddress);
$scope.contractDetails = {contractAddress: "", contractName: "", contractVersion: ""};
delete $scope.contracts[contractAddress];
}
$scope.editContract = function (contractAddress) {
$scope.contractDetails = $scope.contracts[contractAddress];
}
//search
$scope.searchComponent = function () {
$scope.infoSearch = ""
var searchStr = $scope.searchString.trim();
var tabId = ""
if (searchStr.startsWith("0x")) {
if (searchStr.length == 42) {
//address
$scope.openAddressTab(searchStr);
} else if (searchStr.length == 66) {
//tx ?
var tx = web3.eth.getTransaction(searchStr);
if (tx == null) {
//block
$scope.openBlockTab(searchStr);
} else {
$scope.infoSearch = "tx " + searchStr;
var txReceipt = web3.eth.getTransactionReceipt(searchStr)
$scope.addTab($scope.infoSearch, Object.assign(tx, txReceipt));
}
}
} else {
var blockNumber = parseInt(searchStr);
if (Number.isNaN(blockNumber)) {
tabId = "Can't recognize input string";
} else {
//block number
if (blockNumber > -1) {
$scope.openBlockTab(blockNumber);
} else {
tabId = "Invalid block number"
}
}
}
$scope.infoSearch = tabId;
$scope.searchString = "";
}
//blockId is hash or number
$scope.openBlockTab = function (blockId) {
$rootScope.ui.showProgress = true;
web3.eth.getBlock(blockId, true, function (error, block) {
if (error) {
$rootScope.ui.showProgress = false;
alert(error);
} else {
$scope.infoSearch = "block " + blockId;
block.extraData = web3.toAscii(block.extraData);
$scope.addTab($scope.infoSearch, block);
$rootScope.ui.showProgress = false;
var len = block.transactions.length;
var tx = null;
//is addresses as contract ?
for (var i = 0; i < len; i++) {
tx = block.transactions[i];
//to
if (tx.to == null) {
//if created contract, field "to" is null, we should use getTransactionReceipt to recognize a contract address
var txReceipt = web3.eth.getTransactionReceipt(tx.hash);
if (txReceipt.contractAddress == null) {
//contract not mined
tx.toContr = "E";
tx.to = "Err. Contract not mined !";
} else {
//we should check contract code. If contract created with error.
var code = web3.eth.getCode(txReceipt.contractAddress);
if (code == "0x") {
tx.toContr = "E";
tx.to = "Err. Contract not saved at state !";
} else {
tx.toContr = "M";
tx.to = txReceipt.contractAddress;
}
}
} else {
if (web3.eth.getCode(tx.to) == "0x") {
tx.toContr = "";
} else {
tx.toContr = "C";
}
}
}
}
});
}
$scope.openTxTab = function (txId) {
$rootScope.ui.showProgress = true;
web3.eth.getTransaction(txId, function (error, tx) {
if (error) {
$rootScope.ui.showProgress = false;
alert(error);
} else {
$scope.infoSearch = "tx " + txId;
var txReceipt = web3.eth.getTransactionReceipt(txId);
$scope.addTab($scope.infoSearch, Object.assign(tx, txReceipt));
$rootScope.ui.showProgress = false;
}
});
}
$scope.openAddressTab = function (address) {
$rootScope.ui.showProgress = true;
var addrObj = {
address: "",
balance: 0,
storageAt: "",
code: "",
txCount: 0
}
web3.eth.getBalance(address, "latest", function (error, value) {
if (error) {
$rootScope.ui.showProgress = false;
alert(error);
} else {
$scope.infoSearch = "addr " + address;
addrObj.address = address;
addrObj.balance = value;
addrObj.storageAt = web3.eth.getStorageAt(address, 0);
addrObj.code = web3.eth.getCode(address);
addrObj.txCount = web3.eth.getTransactionCount(address);
addrObj.eventArr = [];
$scope.addTab($scope.infoSearch, addrObj);
$rootScope.ui.showProgress = false;
//try get contract details from local storage
var contractDetails = localStorageService.get(STORAGE_CONTRACT_PREFFIX + address);
if (contractDetails != null) {
addrObj.contractDetails = contractDetails;
var contract = web3.eth.contract(JSON.parse(contractDetails.contractABI));
var contractInstance = contract.at(address);
var events = contractInstance.allEvents({fromBlock: 0, toBlock: 'latest'});
events.watch(function (error, result) {
addrObj.eventArr.unshift(result);
});
}
}
});
}
$scope.selectTab = function (tabId) {
$scope.tabSelected = tabId;
}
$scope.addTab = function (tabId, object) {
$scope.userTabs [tabId] = object;
$scope.tabSelected = tabId;
}
$scope.closeTab = function (tabId) {
delete $scope.userTabs [tabId];
$scope.tabSelected = Object.keys($scope.userTabs)[0];
}
$scope.closeAllTabs = function () {
$scope.userTabs = {};
}
$scope.getTabCount = function() {
return Object.keys($scope.userTabs).length;
}
}]);