diff --git a/Readme.md b/Readme.md index 41f58999..85ad2791 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,8 @@ # Etherscan API +It forked from [etherscan-api 10.0.5](https://github.com/sebs/etherscan-api/tree/v10.0.5)
+The proxy URL and header have been added to avoid CORS. + ## Development of a NEXTGEN Version has started - please stand by [![npm](https://img.shields.io/npm/dt/etherscan-api.svg)](https://www.npmjs.com/package/etherscan-api) @@ -31,19 +34,25 @@ Supported: * morden * ropsten * rinkeby +* heco_mainnet +* heco_testnet +* bsc_mainnet +* bsc_testnet Latest ```javascript -// apikey, network, timeout -var api = require('etherscan-api').init('YourApiKey','rinkeby'. '3000'); +// apikey, network, timeout, proxyUrl, headers +var api = require('etherscan-api').init('YourApiKey','rinkeby'. '3000', 'https://cors-anywhere.herokuapp.com/', { + 'x-requested-with': 'me' +}); ``` ## Install ```bash - npm install etherscan-api --save + npm install https://github.com/luckyCoco3418/etherscan-api.git ``` diff --git a/dist/bundle.js b/dist/bundle.js index 062e6482..c1d28602 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -94,14 +94,17 @@ module.exports = function(getRequest, apiKey) { if (!startblock) { startblock = 0; } + queryObject.startblock = startblock; if (!endblock) { endblock = 'latest'; } + queryObject.endblock = endblock; if (!sort) { sort = 'asc'; } + queryObject.sort = sort; if (txhash) { queryObject.txhash = txhash; @@ -174,13 +177,15 @@ module.exports = function(getRequest, apiKey) { * @param {string} address - Account address * @param {string} startblock - start looking here * @param {string} endblock - end looking there - * @param {string} sort - Sort asc/desc + * @param {number} page - Page number + * @param {number} offset - Max records to return + * @param {string} sort - Sort asc/desc * @param {string} contractaddress - Address of ERC20 token contract (if not specified lists transfers for all tokens) * @example * var txlist = api.account.tokentx('0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae', '0x5F988D968cb76c34C87e6924Cc1Ef1dCd4dE75da', 1, 'latest', 'asc'); * @returns {Promise.} */ - tokentx(address, contractaddress, startblock, endblock, sort) { + tokentx(address, contractaddress, startblock, endblock, page, offset, sort) { const module = 'account'; const action = 'tokentx'; @@ -192,12 +197,20 @@ module.exports = function(getRequest, apiKey) { endblock = 'latest'; } + if (!page) { + page = 1; + } + + if (!offset) { + offset = 100; + } + if (!sort) { sort = 'asc'; } var queryObject = { - module, action, startblock, endblock, sort, address, apiKey + module, action, startblock, endblock, page, offset, sort, address, apiKey }; if (contractaddress) { @@ -282,14 +295,33 @@ 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' + homestead: 'https://api.etherscan.io', + heco_mainnet: 'https://api.hecoinfo.com', + heco_testnet: 'https://api-testnet.hecoinfo.com', + bsc_mainnet: 'https://api.bscscan.com', + bsc_testnet: 'https://api-testnet.bscscan.com', }; -module.exports = function(chain, timeout) { - var client = axios.create({ - baseURL: pickChainUrl(chain), +module.exports = function(chain, timeout, proxyUrl, headers) { + var param = { timeout: timeout - }); + }; + + var baseUrl = pickChainUrl(chain); + if (proxyUrl && 0 < proxyUrl.length) { + if (proxyUrl.charAt(proxyUrl.length - 1) == '/') { + baseUrl = proxyUrl + baseUrl; + } else { + baseUrl = proxyUrl + '/' + baseUrl; + } + } + param['baseURL'] = baseUrl; + + if (headers) { + param['headers'] = headers; + } + + var client = axios.create(param); /** * @param query @@ -301,10 +333,13 @@ module.exports = function(chain, timeout) { var data = response.data; if (data.status && data.status != 1) { - let returnMessage = 'NOTOK'; + let returnMessage = data.message ||'NOTOK'; if (data.result && typeof data.result === 'string') { returnMessage = data.result; + } else if (data.message && typeof data.message === 'string') { + returnMessage = data.message; } + return reject(returnMessage); } @@ -345,8 +380,10 @@ const account = require('./account'); * @param {string} apiKey - (optional) Your Etherscan APIkey * @param {string} chain - (optional) Testnet chain keys [ropsten, rinkeby, kovan] * @param {number} timeout - (optional) Timeout in milliseconds for requests, default 10000 + * @param {string} proxyUrl - (optional) The URL of proxy server to avoid CORS + * @param {object} headers - (optional) The object containing custom headers */ -module.exports = function(apiKey, chain, timeout) { +module.exports = function(apiKey, chain, timeout, proxyUrl, headers) { if (!apiKey) { apiKey = 'YourApiKeyToken'; @@ -357,7 +394,7 @@ module.exports = function(apiKey, chain, timeout) { timeout = 10000; } - var getRequest = require('./get-request')(chain, timeout); + var getRequest = require('./get-request')(chain, timeout, proxyUrl, headers); /** @lends module:etherscan/api */ return { diff --git a/lib/get-request.js b/lib/get-request.js index c81c37b4..dfa5756c 100644 --- a/lib/get-request.js +++ b/lib/get-request.js @@ -18,14 +18,33 @@ 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' + homestead: 'https://api.etherscan.io', + heco_mainnet: 'https://api.hecoinfo.com', + heco_testnet: 'https://api-testnet.hecoinfo.com', + bsc_mainnet: 'https://api.bscscan.com', + bsc_testnet: 'https://api-testnet.bscscan.com', }; -module.exports = function(chain, timeout) { - var client = axios.create({ - baseURL: pickChainUrl(chain), +module.exports = function(chain, timeout, proxyUrl, headers) { + var param = { timeout: timeout - }); + }; + + var baseUrl = pickChainUrl(chain); + if (proxyUrl && 0 < proxyUrl.length) { + if (proxyUrl.charAt(proxyUrl.length - 1) == '/') { + baseUrl = proxyUrl + baseUrl; + } else { + baseUrl = proxyUrl + '/' + baseUrl; + } + } + param['baseURL'] = baseUrl; + + if (headers) { + param['headers'] = headers; + } + + var client = axios.create(param); /** * @param query diff --git a/lib/init.js b/lib/init.js index 986a90e4..c4ce1325 100644 --- a/lib/init.js +++ b/lib/init.js @@ -14,8 +14,10 @@ const account = require('./account'); * @param {string} apiKey - (optional) Your Etherscan APIkey * @param {string} chain - (optional) Testnet chain keys [ropsten, rinkeby, kovan] * @param {number} timeout - (optional) Timeout in milliseconds for requests, default 10000 + * @param {string} proxyUrl - (optional) The URL of proxy server to avoid CORS + * @param {object} headers - (optional) The object containing custom headers */ -module.exports = function(apiKey, chain, timeout) { +module.exports = function(apiKey, chain, timeout, proxyUrl, headers) { if (!apiKey) { apiKey = 'YourApiKeyToken'; @@ -26,7 +28,7 @@ module.exports = function(apiKey, chain, timeout) { timeout = 10000; } - var getRequest = require('./get-request')(chain, timeout); + var getRequest = require('./get-request')(chain, timeout, proxyUrl, headers); /** @lends module:etherscan/api */ return { diff --git a/package.json b/package.json index 7934a981..30190df5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "etherscan-api", - "version": "10.0.5", + "name": "etherscan-api-e", + "version": "10.0.6", "description": "API to etherscan with a simple interface", "main": "index.js", "scripts": { @@ -8,7 +8,7 @@ "posttest": "npm run lint", "lint": "jshint lib test", "docs": "documentation build ./lib/init.js -f html -o out", - "bundle": "browserify index.js -s etherscanApi -o dist/bundle.js && git commit ./dist -m bundle && git push", + "bundle": "npx browserify index.js -s etherscanApi -o dist/bundle.js && git commit ./dist -m bundle && git push", "preversion": "npm run lint && npm run changelog", "postversion": "git push && git push --tags", "changelog": "rm ./docs/CHANGELOG.md && changelog https://github.com/sebs/etherscan-api all > ./docs/CHANGELOG.md && git add ./docs/CHANGELOG.md && git commit ./docs/CHANGELOG.md -m changelog", @@ -25,14 +25,10 @@ ], "repository": { "type": "git", - "url": "git+https://github.com/sebs/etherscan-api.git" + "url": "git+https://github.com/luckyCoco3418/etherscan-api.git" }, - "author": "", + "author": "luckycoco3418", "license": "ISC", - "bugs": { - "url": "https://github.com/sebs/etherscan-api/issues" - }, - "homepage": "https://github.com/sebs/etherscan-api#readme", "devDependencies": { "browserify": "^16.5.0", "chai": "^4.2.0",