Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.

Commit 22ec55f

Browse files
committed
feature: make it possible to pass a vlient to the init function
* add a test * make pickChainUrl method availabale to the consumer * move some code around a bit to simplify adding a client
1 parent 6155075 commit 22ec55f

File tree

6 files changed

+92
-28
lines changed

6 files changed

+92
-28
lines changed

Readme.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ balance.then(function(balanceData){
2424

2525
* [Polymer3 based example](https://github.com/hiherto-elements/test-app)
2626

27+
28+
## use a own instance of axios
29+
30+
```js
31+
const axios = require('axios');
32+
const {
33+
init,
34+
pickChainUrl
35+
} = require('..');
36+
37+
38+
const chain = pickChainUrl(null);
39+
const client = axios.create({
40+
baseURL: chain,
41+
timeout: 10000
42+
});
43+
44+
var api = init('apikey', null, 10000, client);
45+
```
46+
2747
## For testnet and L2s usage
2848

2949
Supported:
@@ -41,7 +61,6 @@ Latest
4161
var api = require('etherscan-api').init('YourApiKey','rinkeby'. '3000');
4262
```
4363

44-
4564
## Install
4665

4766
```bash

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict';
22
const init = require('./lib/init');
3+
const pickChainUrl = require('./lib/pick-chain-url');
4+
35

46
module.exports = {
5-
init
7+
init,
8+
pickChainUrl
69
};

lib/get-request.js

+3-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11

2-
const axios = require('axios');
32
/**
43
* @param {string} chain
4+
* @param {number} timeout
5+
* @param {object} client
56
* @returns {string}
67
*/
7-
function pickChainUrl(chain) {
8-
if (!chain || !OTHER_API_URL_MAP[chain]) {
9-
return MAIN_API_URL;
10-
}
11-
12-
return OTHER_API_URL_MAP[chain];
13-
}
14-
158

16-
const MAIN_API_URL = 'https://api.etherscan.io';
17-
const OTHER_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',
22-
arbitrum: 'https://api.arbiscan.io',
23-
arbitrum_rinkeby: 'https://api-testnet.arbiscan.io'
24-
};
9+
module.exports = function(chain, timeout, client) {
2510

26-
module.exports = function(chain, timeout) {
27-
var client = axios.create({
28-
baseURL: pickChainUrl(chain),
29-
timeout: timeout
30-
});
3111

3212
/**
3313
* @param query

lib/init.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
"use strict";
2+
const axios = require('axios');
23
const log = require('./log');
34
const proxy = require('./proxy');
45
const stats = require('./stats');
56
const block = require('./block');
67
const transaction = require('./transaction');
78
const contract = require('./contract');
89
const account = require('./account');
10+
const pickChainUrl = require('./pick-chain-url');
911
/**
1012
* @module etherscan/api
1113
*/
1214

15+
1316
/**
1417
* @param {string} apiKey - (optional) Your Etherscan APIkey
1518
* @param {string} chain - (optional) Other chain keys [ropsten, rinkeby, kovan]
1619
* @param {number} timeout - (optional) Timeout in milliseconds for requests, default 10000
20+
* @param {object} client - optional axios client instance
1721
*/
18-
module.exports = function(apiKey, chain, timeout) {
22+
module.exports = function(apiKey, chain, timeout, client = null) {
1923

2024
if (!apiKey) {
2125
apiKey = 'YourApiKeyToken';
2226
}
2327

24-
2528
if (!timeout) {
2629
timeout = 10000;
2730
}
2831

29-
var getRequest = require('./get-request')(chain, timeout);
32+
if (!client) {
33+
client = axios.create({
34+
baseURL: pickChainUrl(chain),
35+
timeout: timeout
36+
});
37+
}
38+
39+
var getRequest = require('./get-request')(chain, timeout, client);
3040

3141
/** @lends module:etherscan/api */
3242
return {

lib/pick-chain-url.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const MAIN_API_URL = 'https://api.etherscan.io';
2+
const OTHER_API_URL_MAP = {
3+
ropsten: 'https://api-ropsten.etherscan.io',
4+
kovan: 'https://api-kovan.etherscan.io',
5+
rinkeby: 'https://api-rinkeby.etherscan.io',
6+
homestead: 'https://api.etherscan.io',
7+
arbitrum: 'https://api.arbiscan.io',
8+
arbitrum_rinkeby: 'https://api-testnet.arbiscan.io'
9+
};
10+
11+
/**
12+
* gets the correct urls of the backend
13+
* @param {string} chain
14+
* @returns Url of backend
15+
*/
16+
function pickChainUrl(chain) {
17+
if (!chain || !OTHER_API_URL_MAP[chain]) {
18+
return MAIN_API_URL;
19+
}
20+
return OTHER_API_URL_MAP[chain];
21+
}
22+
23+
24+
module.exports = pickChainUrl;

test/pass-client-test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const assert = require('chai').assert;
3+
const axios = require('axios');
4+
const {
5+
init,
6+
pickChainUrl
7+
} = require('..');
8+
9+
10+
describe('pass client', function() {
11+
12+
it('executes successfully', function(done){
13+
14+
const chain = pickChainUrl(null);
15+
const client = axios.create({
16+
baseURL: chain,
17+
timeout: 10000
18+
});
19+
20+
21+
var api = init(process.env.API_KEY, null, 10000, client);
22+
var txlist = api.account.getminedblocks('0x9dd134d14d1e65f84b706d6f205cd5b1cd03a46b');
23+
txlist.then(function(res){
24+
assert.ok(res);
25+
done();
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)