|
1 | 1 | "use strict"; |
2 | | -var axios = require('axios'); |
| 2 | +const axios = require('axios'); |
3 | 3 | const querystring = require('querystring'); |
4 | 4 |
|
5 | | -const url = 'https://api.etherscan.io'; |
6 | | -const testUrls = { |
7 | | - // old default defaults to rinkeb |
8 | | - 'ropsten': 'https://ropsten.etherscan.io', |
9 | | - 'kovan': 'https://kovan.etherscan.io', |
10 | | - 'rinkeby': 'https://rinkeby.etherscan.io' |
| 5 | +const MAIN_API_URL = 'https://api.etherscan.io'; |
| 6 | +const TESTNET_API_URL_MAP = { |
| 7 | + ropsten: 'https://ropsten.etherscan.io', |
| 8 | + kovan: 'https://kovan.etherscan.io', |
| 9 | + rinkeby: 'https://rinkeby.etherscan.io' |
11 | 10 | }; |
12 | 11 |
|
13 | 12 | /** |
14 | 13 | * @module etherscan/api |
15 | 14 | */ |
16 | 15 |
|
17 | | -// chain is for testnet (ropsten, rinkeby, kovan) |
| 16 | +/** |
| 17 | + * @param {string} apiKey - (optional) Your Etherscan APIkey |
| 18 | + * @param {string} chain - (optional) Testnet chain keys [ropsten, rinkeby, kovan] |
| 19 | + */ |
18 | 20 | module.exports = function (apiKey, chain) { |
19 | 21 |
|
20 | 22 | if (!apiKey) { |
21 | 23 | apiKey = 'YourApiKeyToken'; |
22 | 24 | } |
23 | 25 |
|
| 26 | + /** |
| 27 | + * @param {string} chain |
| 28 | + * @returns {string} |
| 29 | + */ |
24 | 30 | function pickChainUrl(chain) { |
25 | | - if (!chain) { |
26 | | - return url; |
27 | | - } |
28 | | - |
29 | | - if (!testUrls[chain]) { |
30 | | - throw Error('Chain missing ' + chain); |
| 31 | + if (!chain || !TESTNET_API_URL_MAP[chain]) { |
| 32 | + return MAIN_API_URL; |
31 | 33 | } |
32 | 34 |
|
33 | | - return testUrls[chain]; |
| 35 | + return TESTNET_API_URL_MAP[chain]; |
34 | 36 | } |
35 | 37 |
|
36 | 38 | var client = axios.create({ |
37 | 39 | baseURL: pickChainUrl(chain), |
38 | 40 | timeout: 3000 |
39 | 41 | }); |
40 | 42 |
|
| 43 | + /** |
| 44 | + * @param query |
| 45 | + * @returns {Promise<any>} |
| 46 | + */ |
41 | 47 | function getRequest(query) { |
42 | 48 | return new Promise(function (resolve, reject) { |
43 | 49 | client.get('/api?' + query) |
|
0 commit comments