-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
205 lines (174 loc) · 4.83 KB
/
app.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
// from config.js: KNOWN_ADDRESSES, DEFAULT_SOURCE, TOKEN, MAX_RESULTS
address = DEFAULT_ADDRESS
total_txs = [];
// verify token is valid
ready = true;
if (typeof TOKEN === 'undefined' || TOKEN === ''){
ready = false;
}
class GNode {
constructor(item) {
this.id = item
this.title = item
this.shape = 'image'
if (KNOWN_ADDRESSES[item]) {
this.label = KNOWN_ADDRESSES[item]['title'];
this.image = `./assets/${KNOWN_ADDRESSES[item]['type']}.png`
} else {
this.label = item.slice(0,5);
this.image = `./assets/unknown.png`
}
}
}
class GEdge {
constructor(item) {
this.id = item.hash
this.from = item.from
this.to = item.to
this.title = item.hash
this.arrowhead = 'normal'
}
}
function get_nodes(txs){
nodesSet = new Set();
txs.forEach((item, index)=>{
nodesSet.add(item.from);
nodesSet.add(item.to);
})
return nodesSet;
}
async function getFromEtherScan(action, address) {
res = await fetch(`https://api.etherscan.io/api?module=account&sort=asc&apikey=${TOKEN}&action=${action}&address=${address}`)
rv = await res.json();
return rv;
}
function toggle_info_display(on, off){
$("#"+on).show();
$("#"+off).hide();
}
function wei_to_eth(wei){
return wei/1000000000000000000;
}
function chunkify(array){
var i,j,chunk = MAX_RESULTS;
rv = [];
for (i=0,j=array.length; i<j; i+=chunk) {
rv.push(array.slice(i,i+chunk));
}
return rv
}
async function update_table(source, balance, txs, tx){
if (tx == null){
toggle_info_display("address_display", "tx_display");
$("#address_table").empty();
$("#txs_table").empty();
$("#address_table").append("<tr><td>Address</td><td>" + source + "</td></tr>");
$("#address_table").append("<tr><td>Balance</td><td>" + wei_to_eth(parseInt(balance)) + "</td></tr>");
for (var i = 0; i < txs.length; i++){
$("#txs_table").append(`<tr><td>${txs[i]['hash']}</td></tr>`);
}
} else {
toggle_info_display("tx_display", "address_display");
$("#tx_table").empty();
for (var i = 0; i < txs.length; i++){
tx_data = txs[i];
if (tx_data['hash'] == tx){
for (var key in tx_data){
$("#tx_table").append(`<tr><td class="four wide">${key}</td><td>${tx_data[key]}</td></tr>`);
}
break;
}
}
}
}
async function generate_graph(nodes, edges){
var container = document.getElementById("network_graph");
var data = {
nodes: new vis.DataSet(nodes),
edges: new vis.DataSet(edges),
};
var options = {
nodes: {font: { color: "#eeeeee" }},
interaction: {
hover: true,
navigationButtons: true,
},
physics: {
barnesHut: { gravitationalConstant: -30000 },
stabilization: { iterations: 2500 },
},
edges: {
length: 300,
arrows: 'middle'
},
layout: {
randomSeed: undefined,
improvedLayout:true,
clusterThreshold: 150,
}
};
network = new vis.Network(container, data, options);
network.once('afterDrawing', () => {
container.style.height = '100vh'
})
// to create click events
network.on("click", async function (params) {
if (params.nodes.length > 0){
address = params.nodes[0];
populate(address);
}
else if (params.edges.length > 0){
txs_call = await getFromEtherScan('txlist',address);
txs = txs_call.result.slice(0, MAX_RESULTS);
update_table(null,null,txs, params.edges[0])
}
});
}
async function populate(address, tx=null) {
txs_call = await getFromEtherScan('txlist',address);
txs = txs_call.result.slice(0, MAX_RESULTS);
total_txs = chunkify(txs_call.result);
// TODO - continue here the results paging issue
if (total_txs.length > 1){
$("#more_results").show();
}
balance_call = await getFromEtherScan('balance',address);
balance = balance_call.result;
update_table(address, balance, txs, tx);
nodes = []
get_nodes(txs).forEach((item)=>{
nodes.push(new GNode(item))
})
edges = []
txs.forEach((item)=>{
edges.push(new GEdge(item));
})
generate_graph(nodes, edges);
}
jQuery(function(){
if (ready !== true){
$("#network_graph").hide();
$("#info_display").hide();
$("#source_form").after(`
<div class="ui warning message">
<div class="header">
No valid TOKEN defined in config.js
</div>
Visit https://etherscan.io/myapikey to generate a token.
</div>
`)
}
else{
$("#source_form").on("submit", function(){
event.preventDefault();
$("#info_display").show();
populate($("#source_address").val());
});
if (address === ''){
$("#info_display").hide();
} else{
$("#more_results").hide();
populate(address);
}
}
});