-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathtx.js
155 lines (131 loc) · 4.14 KB
/
tx.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
var express = require('express');
var router = express.Router();
var async = require('async');
var Web3 = require('web3');
var abi = require('ethereumjs-abi');
var abiDecoder = require('abi-decoder');
router.get('/pending', function(req, res, next) {
var config = req.app.get('config');
var web3 = new Web3();
web3.setProvider(config.provider);
async.waterfall([
function(callback) {
web3.parity.pendingTransactions(function(err, result) {
callback(err, result);
});
}
], function(err, txs) {
if (err) {
return next(err);
}
res.render('tx_pending', { txs: txs });
});
});
router.get('/submit', function(req, res, next) {
res.render('tx_submit', { });
});
router.post('/submit', function(req, res, next) {
if (!req.body.txHex) {
return res.render('tx_submit', { message: "No transaction data specified"});
}
var config = req.app.get('config');
var web3 = new Web3();
web3.setProvider(config.provider);
async.waterfall([
function(callback) {
web3.eth.sendRawTransaction(req.body.txHex, function(err, result) {
callback(err, result);
});
}
], function(err, hash) {
if (err) {
res.render('tx_submit', { message: "Error submitting transaction: " + err });
} else {
res.render('tx_submit', { message: "Transaction submitted. Hash: " + hash });
}
});
});
router.get('/:tx', function(req, res, next) {
var config = req.app.get('config');
var web3 = new Web3();
web3.setProvider(config.provider);
var db = req.app.get('db');
async.waterfall([
function(callback) {
web3.eth.getTransaction(req.params.tx, function(err, result) {
callback(err, result);
});
}, function(result, callback) {
if (!result || !result.hash) {
return callback({ message: "Transaction hash not found" }, null);
}
web3.eth.getTransactionReceipt(result.hash, function(err, receipt) {
callback(err, result, receipt);
});
}, function(tx, receipt, callback) {
web3.trace.transaction(tx.hash, function(err, traces) {
callback(err, tx, receipt, traces);
});
}, function(tx, receipt, traces, callback) {
db.get(tx.to, function(err, value) {
callback(null, tx, receipt, traces, value);
});
}
], function(err, tx, receipt, traces, source) {
if (err) {
return next(err);
}
// Try to match the tx to a solidity function call if the contract source is available
if (source) {
tx.source = JSON.parse(source);
try {
var jsonAbi = JSON.parse(tx.source.abi);
abiDecoder.addABI(jsonAbi);
tx.logs = abiDecoder.decodeLogs(receipt.logs);
tx.callInfo = abiDecoder.decodeMethod(tx.input);
} catch (e) {
console.log("Error parsing ABI:", tx.source.abi, e);
}
}
tx.traces = [];
tx.failed = false;
tx.gasUsed = 0;
if (traces != null) {
traces.forEach(function(trace) {
tx.traces.push(trace);
if (trace.error) {
tx.failed = true;
tx.error = trace.error;
}
if (trace.result && trace.result.gasUsed) {
tx.gasUsed += parseInt(trace.result.gasUsed, 16);
}
});
}
// console.log(tx.traces);
res.render('tx', { tx: tx });
});
});
router.get('/raw/:tx', function(req, res, next) {
var config = req.app.get('config');
var web3 = new Web3();
web3.setProvider(config.provider);
async.waterfall([
function(callback) {
web3.eth.getTransaction(req.params.tx, function(err, result) {
callback(err, result);
});
}, function(result, callback) {
web3.trace.replayTransaction(result.hash, ["trace", "stateDiff", "vmTrace"], function(err, traces) {
callback(err, result, traces);
});
}
], function(err, tx, traces) {
if (err) {
return next(err);
}
tx.traces = traces;
res.render('tx_raw', { tx: tx });
});
});
module.exports = router;