Skip to content

Commit abfa6f0

Browse files
committed
Fixed mainet default value
1 parent 4dc7e1b commit abfa6f0

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

lib/init.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
"use strict";
2-
var axios = require('axios');
2+
const axios = require('axios');
33
const querystring = require('querystring');
44

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'
1110
};
1211

1312
/**
1413
* @module etherscan/api
1514
*/
1615

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+
*/
1820
module.exports = function (apiKey, chain) {
1921

2022
if (!apiKey) {
2123
apiKey = 'YourApiKeyToken';
2224
}
2325

26+
/**
27+
* @param {string} chain
28+
* @returns {string}
29+
*/
2430
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;
3133
}
3234

33-
return testUrls[chain];
35+
return TESTNET_API_URL_MAP[chain];
3436
}
3537

3638
var client = axios.create({
3739
baseURL: pickChainUrl(chain),
3840
timeout: 3000
3941
});
4042

43+
/**
44+
* @param query
45+
* @returns {Promise<any>}
46+
*/
4147
function getRequest(query) {
4248
return new Promise(function (resolve, reject) {
4349
client.get('/api?' + query)

test/index-test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
'use strict';
22
const assert = require('chai').assert;
3-
describe('index.js', function() {
4-
it('can be required', function( ){
3+
describe('index.js', function () {
4+
5+
it('can be required', function () {
56
require('../.');
67
});
7-
it('has init', function( ){
8+
9+
it('has init', function () {
810
var res = require('../.');
911
assert.ok(res.init);
1012
});
13+
14+
it('init on mainnet', function () {
15+
var client = require('../.');
16+
assert.ok(client.init(null, 'mainnet'));
17+
});
18+
1119
});

0 commit comments

Comments
 (0)