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
|
const xhr = process.browser ? require('xhr') : require('request')
const inherits = require('util').inherits
const createPayload = require('../util/create-payload.js')
module.exports = EtherScanProvider
function EtherScanProvider(opts) {
this.url = opts.url || 'http://testnet.etherscan.io/api?module=proxy&'
}
EtherScanProvider.prototype.setEngine = function(engine) {
const self = this
self.engine = engine
engine.on('block', function(block) {
self.currentBlock = block
})
}
EtherScanProvider.prototype.handleRequest = function(payload, next, end) {
const self = this
var method = payload.method
var targetUrl = self.rpcUrl + 'action=' + method
var params = payload.params
var newPayload = createPayload(payload)
xhr({
uri: targetUrl,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(newPayload),
rejectUnauthorized: false,
}, function(err, res, body) {
if (err) return end(err)
// parse response into raw account
var data
try {
data = JSON.parse(body)
if (data.error) return end(data.error)
} catch (err) {
console.error(err.stack)
return end(err)
}
// console.log('network:', payload.method, payload.params, '->', data.result)
end(null, data.result)
})
}
EtherScanProvider.prototype.emitPayload = function(payload, cb){
const self = this
self.engine.sendAsync(createPayload(payload), cb)
}
|