/
accounts.js
298 lines (247 loc) · 6.5 KB
/
accounts.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
/**
@module Ethereum:accounts
*/
/**
The accounts collection, with some ethereum additions.
@class EthAccounts
@constructor
*/
var collection = new Mongo.Collection("ethereum_accounts", {
connection: null
});
EthAccounts = _.clone(collection);
EthAccounts._collection = collection;
if (typeof PersistentMinimongo !== "undefined")
new PersistentMinimongo(EthAccounts._collection);
/**
Updates the accounts balances, by watching for new blocks and checking the balance.
@method _watchBalance
*/
EthAccounts._watchBalance = function() {
var _this = this;
if (this.blockSubscription) {
this.blockSubscription.unsubscribe();
}
// UPDATE SIMPLE ACCOUNTS balance on each new block
this.blockSubscription = web3.eth
.subscribe("newBlockHeaders")
.on("data", function() {
_this._updateBalance();
});
};
/**
Updates the accounts balances.
@method _updateBalance
*/
EthAccounts._updateBalance = function() {
var _this = this;
_.each(EthAccounts.find({}).fetch(), function(account) {
web3.eth.getBalance(account.address, function(err, res) {
if (!err) {
if (res.toFixed) {
res = res.toFixed();
}
EthAccounts.update(account._id, {
$set: {
balance: res
}
});
}
});
});
};
/**
Updates the accounts list,
if its finds a difference between the accounts in the collection and the accounts in the accounts array.
@method _addAccounts
*/
EthAccounts._addAccounts = function() {
var _this = this;
// UPDATE normal accounts on start
web3.eth.getAccounts(function(e, accounts) {
if (!e) {
var visibleAccounts = _.pluck(EthAccounts.find().fetch(), "address");
if (
!_.isEmpty(accounts) &&
_.difference(accounts, visibleAccounts).length === 0 &&
_.difference(visibleAccounts, accounts).length === 0
)
return;
var localAccounts = EthAccounts.findAll().fetch();
// if the accounts are different, update the local ones
_.each(localAccounts, function(account) {
// needs to have the balance
if (!account.balance) return;
// set status deactivated, if it seem to be gone
if (!_.contains(accounts, account.address)) {
EthAccounts.updateAll(account._id, {
$set: {
deactivated: true
}
});
} else {
EthAccounts.updateAll(account._id, {
$unset: {
deactivated: ""
}
});
}
accounts = _.without(accounts, account.address);
});
// ADD missing accounts
var accountsCount = visibleAccounts.length + 1;
_.each(accounts, function(address) {
web3.eth.getBalance(address, function(e, balance) {
if (!e) {
if (balance.toFixed) {
balance = balance.toFixed();
}
web3.eth.getCoinbase(function(error, coinbase) {
if (error) {
console.warn("getCoinbase error: ", error);
coinbase = null; // continue with null coinbase
}
var doc = EthAccounts.findAll({
address: address
}).fetch()[0];
var insert = {
type: "account",
address: address,
balance: balance,
name:
address === coinbase
? "Main account (Etherbase)"
: "Account " + accountsCount
};
if (doc) {
EthAccounts.updateAll(doc._id, {
$set: insert
});
} else {
EthAccounts.insert(insert);
}
if (address !== coinbase) accountsCount++;
});
}
});
});
}
});
};
/**
Builds the query with the addition of "{deactivated: {$exists: false}}"
@method _addToQuery
@param {Mixed} arg
@param {Object} options
@param {Object} options.includeDeactivated If set then de-activated accounts are also included.
@return {Object} The query
*/
EthAccounts._addToQuery = function(args, options) {
var _this = this;
options = _.extend(
{
includeDeactivated: false
},
options
);
var args = Array.prototype.slice.call(args);
if (_.isString(args[0])) {
args[0] = {
_id: args[0]
};
} else if (!_.isObject(args[0])) {
args[0] = {};
}
if (!options.includeDeactivated) {
args[0] = _.extend(args[0], {
deactivated: { $exists: false }
});
}
return args;
};
/**
Find all accounts, besides the deactivated ones
@method find
@return {Object} cursor
*/
EthAccounts.find = function() {
return this._collection.find.apply(this, this._addToQuery(arguments));
};
/**
Find all accounts, including the deactivated ones
@method findAll
@return {Object} cursor
*/
EthAccounts.findAll = function() {
return this._collection.find.apply(
this,
this._addToQuery(arguments, {
includeDeactivated: true
})
);
};
/**
Find one accounts, besides the deactivated ones
@method findOne
@return {Object} cursor
*/
EthAccounts.findOne = function() {
return this._collection.findOne.apply(this, this._addToQuery(arguments));
};
/**
Update accounts, besides the deactivated ones
@method update
@return {Object} cursor
*/
EthAccounts.update = function() {
return this._collection.update.apply(this, this._addToQuery(arguments));
};
/**
Update accounts, including the deactivated ones
@method updateAll
@return {Object} cursor
*/
EthAccounts.updateAll = function() {
return this._collection.update.apply(
this,
this._addToQuery(arguments, {
includeDeactivated: true
})
);
};
/**
Update accounts, including the deactivated ones
@method upsert
@return {Object} cursor
*/
EthAccounts.upsert = function() {
return this._collection.upsert.apply(
this,
this._addToQuery(arguments, {
includeDeactivated: true
})
);
};
/**
Starts fetching and watching the accounts
@method init
*/
EthAccounts.init = function() {
var _this = this;
if (typeof web3 === "undefined") {
console.warn(
"EthAccounts couldn't find web3, please make sure to instantiate a web3 object before calling EthAccounts.init()"
);
return;
}
Tracker.nonreactive(function() {
_this._addAccounts();
_this._updateBalance();
_this._watchBalance();
// check for new accounts every 2s
Meteor.clearInterval(_this._intervalId);
_this._intervalId = Meteor.setInterval(function() {
_this._addAccounts();
}, 2000);
});
};