forked from sebs/etherscan-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-request.js
More file actions
65 lines (53 loc) · 1.47 KB
/
get-request.js
File metadata and controls
65 lines (53 loc) · 1.47 KB
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
const axios = require('axios');
/**
* @param {string} chain
* @returns {string}
*/
function pickChainUrl(chain) {
if (!chain || !TESTNET_API_URL_MAP[chain]) {
return MAIN_API_URL;
}
return TESTNET_API_URL_MAP[chain];
}
const MAIN_API_URL = 'https://api.etherscan.io';
const TESTNET_API_URL_MAP = {
ropsten: 'https://api-ropsten.etherscan.io',
kovan: 'https://api-kovan.etherscan.io',
rinkeby: 'https://api-rinkeby.etherscan.io',
homestead: 'https://api.etherscan.io'
};
module.exports = function(chain, timeout) {
var client = axios.create({
baseURL: pickChainUrl(chain),
timeout: timeout
});
/**
* @param query
* @returns {Promise<any>}
*/
function getRequest(query) {
return new Promise(function(resolve, reject) {
client.get('/api?' + query).then(function(response) {
var data = response.data;
if (data.status && data.status != 1) {
let returnMessage = 'NOTOK';
if (data.result && typeof data.result === 'string') {
returnMessage = data.result;
}
return reject(returnMessage);
}
if (data.error) {
var message = data.error;
if(typeof data.error === 'object' && data.error.message){
message = data.error.message;
}
return reject(new Error(message));
}
resolve(data);
}).catch(function(error) {
return reject(new Error(error));
});
});
}
return getRequest;
};