1
+ const axios = require ( "axios" ) ;
2
+ const EtherscanApiStatusError = require ( "./etherscanApiStatusError" ) ;
1
3
2
- const axios = require ( 'axios' ) ;
3
4
/**
4
5
* @param {string } chain
5
6
* @returns {string }
@@ -12,55 +13,60 @@ function pickChainUrl(chain) {
12
13
return TESTNET_API_URL_MAP [ chain ] ;
13
14
}
14
15
15
-
16
- const MAIN_API_URL = 'https://api.etherscan.io' ;
16
+ const MAIN_API_URL = "https://api.etherscan.io" ;
17
17
const TESTNET_API_URL_MAP = {
18
- ropsten : ' https://api-ropsten.etherscan.io' ,
19
- kovan : ' https://api-kovan.etherscan.io' ,
20
- rinkeby : ' https://api-rinkeby.etherscan.io' ,
21
- homestead : ' https://api.etherscan.io'
18
+ ropsten : " https://api-ropsten.etherscan.io" ,
19
+ kovan : " https://api-kovan.etherscan.io" ,
20
+ rinkeby : " https://api-rinkeby.etherscan.io" ,
21
+ homestead : " https://api.etherscan.io" ,
22
22
} ;
23
23
24
- module . exports = function ( chain , timeout ) {
24
+ // Axios automatically throws an error if statusCode is 4xx/5xx
25
+ module . exports = function ( chain , timeout ) {
25
26
var client = axios . create ( {
26
27
baseURL : pickChainUrl ( chain ) ,
27
- timeout : timeout
28
+ timeout : timeout ,
28
29
} ) ;
29
30
30
31
/**
31
32
* @param query
32
33
* @returns {Promise<any> }
33
34
*/
34
35
function getRequest ( query ) {
35
- return new Promise ( function ( resolve , reject ) {
36
- client . get ( '/api?' + query ) . then ( function ( response ) {
37
- var data = response . data ;
36
+ return new Promise ( function ( resolve , reject ) {
37
+ client
38
+ . get ( "/api?" + query )
39
+ . then ( function ( response ) {
40
+ var data = response . data ;
41
+ if ( data . status && data . status != 1 ) {
42
+ let returnMessage = data . message || "NOTOK" ;
43
+ if (
44
+ data . hasOwnProperty ( "result" ) &&
45
+ typeof data . result === "string"
46
+ ) {
47
+ returnMessage = data . result ;
48
+ } else if ( data . message && typeof data . message === "string" ) {
49
+ returnMessage = data . message ;
50
+ }
38
51
39
- if ( data . status && data . status != 1 ) {
40
- let returnMessage = data . message || 'NOTOK' ;
41
- if ( data . result && typeof data . result === 'string' ) {
42
- returnMessage = data . result ;
43
- } else if ( data . message && typeof data . message === 'string' ) {
44
- returnMessage = data . message ;
52
+ return reject ( new EtherscanApiStatusError ( returnMessage ) ) ;
45
53
}
46
54
47
- return reject ( returnMessage ) ;
48
- }
55
+ if ( data . error ) {
56
+ var message = data . error ;
49
57
50
- if ( data . error ) {
51
- var message = data . error ;
58
+ if ( typeof data . error === "object" && data . error . message ) {
59
+ message = data . error . message ;
60
+ }
52
61
53
- if ( typeof data . error === 'object' && data . error . message ) {
54
- message = data . error . message ;
62
+ return reject ( new Error ( message ) ) ;
55
63
}
56
64
57
- return reject ( new Error ( message ) ) ;
58
- }
59
-
60
- resolve ( data ) ;
61
- } ) . catch ( function ( error ) {
62
- return reject ( new Error ( error ) ) ;
63
- } ) ;
65
+ resolve ( data ) ;
66
+ } )
67
+ . catch ( function ( error ) {
68
+ return reject ( new Error ( error ) ) ;
69
+ } ) ;
64
70
} ) ;
65
71
}
66
72
0 commit comments