etherscan
Version:
Node.js library for communicating with the Etherscan API.
82 lines (69 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _requestPromiseNative = require('request-promise-native');
var _requestPromiseNative2 = _interopRequireDefault(_requestPromiseNative);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class Etherscan {
constructor(apiKey) {
this._apiKey = apiKey;
this._apiUrl = 'https://api.etherscan.io/api';
}
getEtherBalance(options) {
return this._moduleAccount({
action: 'balance',
address: options.address,
tag: options.tag || 'latest'
});
}
getEtherBalanceMulti(options) {
return this._moduleAccount({
action: 'balancemulti',
address: options.address,
tag: options.tag || 'latest'
});
}
getTxList(options) {
return this._moduleAccount({
action: 'txlist',
address: options.address,
startblock: options.startBlock,
endblock: options.endBlock,
sort: options.sort
});
}
getTxListInternal(options) {
return this._moduleAccount({
action: 'txlistinternal',
address: options.address,
startblock: options.startBlock,
endblock: options.endBlock,
sort: options.sort
});
}
_moduleAccount(params) {
return this._query(Object.assign({}, params, {
module: 'account'
}));
}
async _query(params) {
if (this._apiKey) {
params.apikey = this._apiKey;
}
const data = await (0, _requestPromiseNative2.default)(this._apiUrl, {
method: 'POST',
qsStringifyOptions: {
arrayFormat: 'repeat'
},
form: params,
json: true
});
if (data.status !== '1') {
return Promise.reject(`API returned result "${data.result}"`);
}
return data.result;
}
}
exports.default = Etherscan;
module.exports = exports['default'];