-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEthereumController.js
425 lines (330 loc) · 10.8 KB
/
EthereumController.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const Web3 = require('web3');
const EthereumTx = require('ethereumjs-tx').Transaction;
const Service = require('../common/services')
const Utils = require('../common/utils')
const isHexPrefixed = require('is-hex-prefixed');
const stripHexPrefix = require('strip-hex-prefix');
const { validationResult } = require('express-validator');
if (process.env.ETH_RPC_DRIVER == 'infura') {
var web3 = new Web3(new Web3.providers.HttpProvider(process.env.ETH_INFURA_RPC_URL));
}else{
var web3 = new Web3(new Web3.providers.HttpProvider(process.env.ETH_CUSTOM_RPC_URL));
}
exports.createAccount = (req, res, next) => {
const result = validationResult(req);
if (!result.isEmpty()) {
res.status(400).json({
errors: result.array()
});
res.end();
}
try{
const account = web3.eth.accounts.create();
const private_key = account.privateKey; //Get the private key from the object
const passphrase = req.body.password;
const keystore = web3.eth.accounts.encrypt(private_key, passphrase); //Get the keystore
res.status(200).json({
address: account.address,
privateKey: account.privateKey,
keystore: keystore,
})
res.end();
}
catch(err){
console.log("Err creating account",err)
res.status(500).json({
message:"Err creating account: "+err.message,
data:err
});
res.end();
}
}
exports.unlockAccount = (req, res, next) => {
const result = validationResult(req);
if (!result.isEmpty()) {
res.status(400).json({
errors: result.array()
});
res.end();
}
try{
const password = req.body.password;
const keystore = req.body.keystore;
const account = web3.eth.accounts.decrypt(keystore, password);
res.status(200).json(account);
res.end();
}
catch(err){
console.log("Err unlocking account",err)
res.status(500).json({
message:"Err unlocking account: "+err.message,
data:err
});
res.end();
}
}
exports.getInfo = (req, res, next) => {
try{
const info = {
symbol: 'ETH',
name: 'Ethereum',
decimals: 18,
};
res.status(200).json(info);
res.end();
}
catch(err){
console.log("Err getting token info",err)
res.status(500).json({
message:"Err getting token info: "+err.message,
data:err
});
res.end();
}
}
exports.getBalance = async (req, res, next) => {
const result = validationResult(req);
if (!result.isEmpty()) {
res.status(400).json({
errors: result.array()
});
res.end();
}
try{
const address = req.body.address;
const balanceWei = await web3.eth.getBalance(address)
const balance = web3.utils.fromWei(balanceWei, 'ether')
res.status(200).json({
amount: balance,
});
res.end();
}
catch(err){
console.log("Err getting balance",err)
res.status(500).json({
message:"Err getting balance: "+err.message,
data:err
});
res.end();
}
}
exports.getGas = async (req, res, next) => {
let type = req.body.type;
try{
let gasPrices = await Service.getCurrentGasPrices();
let block = await web3.eth.getBlock("latest");
let prices = await gasPrices;
let oneGwei = 0.000000001;
let gasLimit = (type == 'token')? 60000 : 21000;
let lowPriceToEth = prices.low * oneGwei * gasLimit;
let mediumPriceToEth = prices.medium * oneGwei * gasLimit;
let highPriceToEth = prices.high * oneGwei * gasLimit;
let pricesWithEth = {
low: {
amount: prices.low,
eth: lowPriceToEth.toPrecision(4)
},
medium: {
amount: prices.medium,
eth: mediumPriceToEth.toPrecision(4)
},
high: {
amount: prices.high,
eth: highPriceToEth.toPrecision(4)
}
}
res.status(200).json({
prices: pricesWithEth, limit: gasLimit
});
res.end();
}
catch(err){
console.log("Err getting gas",err)
res.status(500).json({
message:"Err getting gas: "+err.message,
data:err
});
res.end();
}
}
exports.transferTo = async (req, res, next) => {
const result = validationResult(req);
if (!result.isEmpty()) {
res.status(400).json({
errors: result.array()
});
res.end();
}
// private key of token holder
let privateKey = req.body.private_key;
// Who holds the token now?
const from_address = req.body.from_address;
// Who are we trying to send this token to?
const to_address = req.body.to_address;
let amount = req.body.amount;
// Gas price specifies the amount of ether you are willing to pay for each unit of gas (Gwei)
let gasPrice = req.body.gas_price;
//The maximum amount of units of gas you are will to send
let gasLimit = req.body.gas_limit;
if (gasLimit == null) {
gasLimit = 21000; //2204 * gasPrice + 21000;
}else{
gasLimit = gasLimit;//2204 * gasPrice + gasLimit;
}
if(isHexPrefixed(privateKey)){
privateKey = stripHexPrefix(privateKey);
}
try{
// Determine the nonce
var count = await web3.eth.getTransactionCount(from_address);
//console.log(`num transactions so far: ${count}`);
// How many tokens do I have before sending?
var balance = await web3.eth.getBalance(from_address);
//console.log(`Balance before send: ${web3.utils.fromWei(balance, 'ether')} ETH\n------------------------`);
if (web3.utils.fromWei(balance, 'ether') < parseFloat(amount)) {
throw new Error("Insufficient Balance");
}
// I chose gas price and gas limit based on what ethereum wallet was recommending for a similar transaction. You may need to change the gas price!
// Use Gwei for the unit of gas price
let gasPrices = await Service.getCurrentGasPrices();
if (gasPrice == null) {
var gasPriceGwei = web3.utils.toWei(gasPrices.standard,'gwei');
}else{
var gasPriceGwei = web3.utils.toWei(gasPrice,'gwei');
}
// Chain ID for Main Net
var chainId = 1;
var rawTransaction = {
"from": from_address,
"nonce": "0x" + count.toString(16),
"gasPrice": web3.utils.toHex(gasPriceGwei),
"gasLimit": web3.utils.toHex(gasLimit),
"to": to_address,
"value": web3.utils.toHex(web3.utils.toWei(amount, 'ether')),
"chainId": chainId
};
//console.log(`Raw of Transaction: \n${JSON.stringify(rawTransaction, null, '\t')}\n------------------------`);
// The private key for from_address in .env
var privKey = Buffer.from(privateKey, 'hex');
var transaction = new EthereumTx(rawTransaction);
transaction.sign(privKey);
var serializedTx = transaction.serialize();
// Comment out these four lines if you don't really want to send the TX right now
//console.log(`Attempting to send signed tx: ${serializedTx.toString('hex')}\n------------------------`);
var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
// The receipt info of transaction, Uncomment for debug
//console.log(`Receipt info: \n${JSON.stringify(receipt, null, '\t')}\n------------------------`);
// The balance may not be updated yet, but let's check
balance = await web3.eth.getBalance(from_address);
//console.log(`Balance after send: ${web3.utils.fromWei(balance, 'ether')} ETH`);
res.status(200).json({
balance: web3.utils.fromWei(balance, 'ether'),
receipt: receipt,
});
res.end();
}
catch(err){
console.log("Err signing transaction",err)
res.status(500).json({
message:"Err signing transaction: "+err.message,
data:err
});
res.end();
}
}
// exports.transactions = async (req, res, next) => {
// const address = req.body.address;
// const sent_transactions = [];
// const received_transactions = [];
// const currentBlock = await web3.eth.getBlockNumber();
// console.log(currentBlock)
// const n = await web3.eth.getTransactionCount(address, currentBlock);
// const bal = await web3.eth.getBalance(address, currentBlock);
// console.log(n, web3.utils.fromWei(bal, 'ether'))
// for (var i=currentBlock; i >= 0 && (n > 0 || bal > 0); --i) {
// try {
// const block = await web3.eth.getBlock(i, true);
// if (block && block.transactions) {
// block.transactions.forEach(function(e) {
// if (address == e.from) {
// if (e.from != e.to)
// bal = bal.plus(e.value);
// sent_transactions.push({
// txn: i, from: e.from, to: e.to, amount: e.value.toString(10)
// });
// //console.log(i, e.from, e.to, e.value.toString(10));
// --n;
// }
// if (address == e.to) {
// if (e.from != e.to)
// bal = bal.minus(e.value);
// received_transactions.push({
// txn: i, from: e.from, to: e.to, amount: e.value.toString(10)
// });
// --n;
// //console.log(i, e.from, e.to, e.value.toString(10));
// }
// });
// }
// } catch (e) {
// console.error("Error in block " + i, e);
// }
// }
// res.json({
// status:200,
// transactions: {sent: await sent_transactions,received: await received_transactions},
// });
// res.end();
// }
exports.transactions = async (req, res, next) => {
const result = validationResult(req);
if (!result.isEmpty()) {
res.status(400).json({
errors: result.array()
});
res.end();
}
const address = req.body.address;
let startBlockNumber = req.body.startblock;
let endBlockNumber = req.body.endblock;
let transactions = [];
if (endBlockNumber == null) {
endBlockNumber = await web3.eth.getBlockNumber();
//console.log("Using endBlockNumber: " + endBlockNumber);
}
if (startBlockNumber == null) {
startBlockNumber = endBlockNumber - 50;
//console.log("Using startBlockNumber: " + startBlockNumber);
}
//console.log("Searching for transactions to/from account \"" + address + "\" within blocks " + startBlockNumber + " and " + endBlockNumber);
for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 50 == 0) {
console.log("Searching block " + i);
}
var block = await web3.eth.getBlock(i, true);
if (block != null && block.transactions != null) {
block.transactions.forEach( function(e) {
if (address == "*" || address == e.from || address == e.to) {
transactions.push({
txhash : e.hash,
nonce : e.nonce,
blockHash : e.blockHash,
blockNumber : e.blockNumber,
transactionIndex: e.transactionIndex,
from : e.from,
to : e.to,
value : e.value,
time : block.timestamp,
gasPrice : e.gasPrice,
gas : e.gas,
input : e.input
});
}
})
}
}
res.status(200).json({
transactions: await transactions,
});
res.end();
}