@parity/api
Version:
The Parity Promise-based API library for interfacing with Ethereum over RPC
202 lines (201 loc) • 10.5 kB
JavaScript
"use strict";
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
var PubsubBase = require('../pubsubBase');
var _a = require('../../format/input'), inAddress = _a.inAddress, inBlockNumber = _a.inBlockNumber, inHex = _a.inHex, inNumber16 = _a.inNumber16, inOptions = _a.inOptions, inFilter = _a.inFilter;
var _b = require('../../format/output'), outAddress = _b.outAddress, outBlock = _b.outBlock, outNumber = _b.outNumber, outTransaction = _b.outTransaction, outSyncing = _b.outSyncing, outReceipt = _b.outReceipt, outLog = _b.outLog;
var Eth = /** @class */ (function (_super) {
__extends(Eth, _super);
function Eth(provider) {
var _this = _super.call(this, provider) || this;
_this._api = 'parity';
return _this;
}
Eth.prototype.newHeads = function (callback) {
return this.addListener('eth', 'newHeads', function (error, data) {
error ? callback(error) : callback(null, outBlock(data));
}, null);
};
Eth.prototype.syncing = function (callback) {
return this.addListener('eth', 'syncing', function (error, data) {
error ? callback(error) : callback(null, outSyncing(data));
}, null);
};
Eth.prototype.logs = function (callback) {
throw Error('not supported yet');
};
// eth API
Eth.prototype.protocolVersion = function (callback) {
return this.addListener(this._api, 'eth_protocolVersion', callback);
};
Eth.prototype.hashrate = function (callback) {
return this.addListener(this._api, 'eth_hashrate', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
});
};
Eth.prototype.coinbase = function (callback) {
return this.addListener(this._api, 'eth_coinbase', function (error, data) {
error ? callback(error) : callback(null, outAddress(data));
});
};
Eth.prototype.mining = function (callback) {
return this.addListener(this._api, 'eth_mining', callback);
};
Eth.prototype.gasPrice = function (callback) {
return this.addListener(this._api, 'eth_gasPrice', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
});
};
Eth.prototype.accounts = function (callback) {
return this.addListener(this._api, 'eth_accounts', function (error, accounts) {
error
? callback(error)
: callback(null, (accounts || []).map(outAddress));
});
};
Eth.prototype.blockNumber = function (callback) {
return this.addListener(this._api, 'eth_blockNumber', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
});
};
Eth.prototype.getBalance = function (callback, address, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_getBalance', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inAddress(address), inBlockNumber(blockNumber)]);
};
Eth.prototype.getStorageAt = function (callback, address, index, blockNumber) {
if (index === void 0) { index = 0; }
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_getStorageAt', callback, [
inAddress(address),
inNumber16(index),
inBlockNumber(blockNumber)
]);
};
Eth.prototype.getBlockByHash = function (callback, hash, full) {
if (full === void 0) { full = false; }
return this.addListener(this._api, 'eth_getBlockByHash', function (error, data) {
error ? callback(error) : callback(null, outBlock(data));
}, [inHex(hash), full]);
};
Eth.prototype.getBlockByNumber = function (callback, blockNumber, full) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
if (full === void 0) { full = false; }
return this.addListener(this._api, 'eth_getBlockByNumber', function (error, data) {
error ? callback(error) : callback(null, outBlock(data));
}, [inBlockNumber(blockNumber), full]);
};
Eth.prototype.getTransactionCount = function (callback, address, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_getTransactionCount', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inAddress(address), inBlockNumber(blockNumber)]);
};
Eth.prototype.getBlockTransactionCountByHash = function (callback, hash) {
return this.addListener(this._api, 'eth_getBlockTransactionCountByHash', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inHex(hash)]);
};
Eth.prototype.getBlockTransactionCountByNumber = function (callback, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_getBlockTransactionCountByNumber', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inBlockNumber(blockNumber)]);
};
Eth.prototype.getUncleCountByBlockHash = function (callback, hash) {
return this.addListener(this._api, 'eth_getUncleCountByBlockHash', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inHex(hash)]);
};
Eth.prototype.getUncleCountByBlockNumber = function (callback, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_getUncleCountByBlockNumber', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inBlockNumber(blockNumber)]);
};
Eth.prototype.getCode = function (callback, address, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_getCode', callback, [
inAddress(address),
inBlockNumber(blockNumber)
]);
};
Eth.prototype.call = function (callback, options, blockNumber) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
return this.addListener(this._api, 'eth_call', callback, [
inOptions(options),
inBlockNumber(blockNumber)
]);
};
Eth.prototype.estimateGas = function (callback, options) {
return this.addListener(this._api, 'eth_estimateGas', function (error, data) {
error ? callback(error) : callback(null, outNumber(data));
}, [inOptions(options)]);
};
Eth.prototype.getTransactionByHash = function (callback, hash) {
return this.addListener(this._api, 'eth_getTransactionByHash', function (error, data) {
error ? callback(error) : callback(null, outTransaction(data));
}, [inHex(hash)]);
};
Eth.prototype.getTransactionByBlockHashAndIndex = function (callback, hash, index) {
if (index === void 0) { index = 0; }
return this.addListener(this._api, 'eth_getTransactionByBlockHashAndIndex', function (error, data) {
error ? callback(error) : callback(null, outTransaction(data));
}, [inHex(hash), inNumber16(index)]);
};
Eth.prototype.getTransactionByBlockNumberAndIndex = function (callback, blockNumber, index) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
if (index === void 0) { index = 0; }
return this.addListener(this._api, 'eth_getTransactionByBlockNumberAndIndex', function (error, data) {
error ? callback(error) : callback(null, outTransaction(data));
}, [inBlockNumber(blockNumber), inNumber16(index)]);
};
Eth.prototype.getTransactionReceipt = function (callback, txhash) {
return this.addListener(this._api, 'eth_getTransactionReceipt', function (error, data) {
error ? callback(error) : callback(null, outReceipt(data));
}, [inHex(txhash)]);
};
Eth.prototype.getUncleByBlockHashAndIndex = function (callback, hash, index) {
if (index === void 0) { index = 0; }
return this.addListener(this._api, 'eth_getUncleByBlockHashAndIndex', callback, [inHex(hash), inNumber16(index)]);
};
Eth.prototype.getUncleByBlockNumberAndIndex = function (callback, blockNumber, index) {
if (blockNumber === void 0) { blockNumber = 'latest'; }
if (index === void 0) { index = 0; }
return this.addListener(this._api, 'eth_getUncleByBlockNumberAndIndex', callback, [inBlockNumber(blockNumber), inNumber16(index)]);
};
Eth.prototype.getLogs = function (callback, options) {
return this.addListener(this._api, 'eth_getLogs', function (error, logs) {
error ? callback(error) : callback(null, function (logs) { return logs.map(outLog); });
}, [inFilter(options)]);
};
Eth.prototype.getWork = function (callback) {
return this.addListener(this._api, 'eth_getWork', callback);
};
return Eth;
}(PubsubBase));
module.exports = Eth;