UNPKG

@parity/api

Version:

The Parity Promise-based API library for interfacing with Ethereum over RPC

269 lines (268 loc) 9.56 kB
"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 EventEmitter = require('eventemitter3'); var Contract = require('./contract'); var Providers = require('./provider'); var Transports = require('./transport'); var _a = require('./rpc'), Db = _a.Db, Eth = _a.Eth, Parity = _a.Parity, Net = _a.Net, Personal = _a.Personal, Private = _a.Private, Shell = _a.Shell, Shh = _a.Shh, Signer = _a.Signer, Trace = _a.Trace, Web3 = _a.Web3; var Subscriptions = require('./subscriptions'); var Pubsub = require('./pubsub'); var util = require('./util'); var isFunction = require('./util/types').isFunction; var Api = /** @class */ (function (_super) { __extends(Api, _super); function Api(provider, allowSubscriptions, middlewareClass) { if (allowSubscriptions === void 0) { allowSubscriptions = true; } var _this = _super.call(this) || this; if (!provider) { throw new Error('Provider needs to be supplied to Api instance'); } if (isFunction(provider.sendAsync)) { provider = new Providers.Current(provider); } else if (!isFunction(provider.send)) { console.warn(new Error('deprecated: Api needs provider with send() function, old-style Transport found instead'), provider); } _this._provider = new Providers.PromiseProvider(provider); _this._provider.on('connected', function () { return _this.emit('connected'); }); _this._provider.on('connecting', function () { return _this.emit('connecting'); }); _this._provider.on('disconnected', function () { return _this.emit('disconnected'); }); _this._db = new Db(_this._provider); _this._eth = new Eth(_this._provider); _this._net = new Net(_this._provider); _this._parity = new Parity(_this._provider); _this._personal = new Personal(_this._provider); _this._private = new Private(_this._provider); _this._shell = new Shell(_this._provider); _this._shh = new Shh(_this._provider); _this._signer = new Signer(_this._provider); _this._trace = new Trace(_this._provider); _this._web3 = new Web3(_this._provider); // FIXME: Remove, convert to shell if (middlewareClass) { var middleware = _this.parity .nodeKind() .then(function (nodeKind) { if (nodeKind.availability === 'public') { return middlewareClass; } return null; }) .catch(function () { return null; }); provider.addMiddleware(middleware); } if (provider && isFunction(provider.subscribe)) { _this._pubsub = new Pubsub(provider); } if (allowSubscriptions) { _this._subscriptions = new Subscriptions(_this); } return _this; } Object.defineProperty(Api.prototype, "isConnected", { get: function () { var isConnected = this.provider.isConnected; return isConnected || typeof isConnected === 'undefined'; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "isPubSub", { get: function () { return !!this._pubsub; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "pubsub", { get: function () { if (!this.isPubSub) { throw Error('Pubsub is only available with a subscribing-supported transport injected!'); } return this._pubsub; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "db", { get: function () { return this._db; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "eth", { get: function () { return this._eth; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "parity", { get: function () { return this._parity; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "net", { get: function () { return this._net; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "personal", { get: function () { return this._personal; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "private", { get: function () { return this._private; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "provider", { get: function () { return this._provider.provider; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "shell", { get: function () { return this._shell; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "shh", { get: function () { return this._shh; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "signer", { get: function () { return this._signer; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "trace", { get: function () { return this._trace; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "transport", { get: function () { return this.provider; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "web3", { get: function () { return this._web3; }, enumerable: true, configurable: true }); Object.defineProperty(Api.prototype, "util", { get: function () { return util; }, enumerable: true, configurable: true }); Api.prototype.newContract = function (abi, address) { return new Contract(this, abi).at(address); }; Api.prototype.subscribe = function (subscriptionName, callback) { if (!this._subscriptions) { return Promise.resolve(1); } return this._subscriptions.subscribe(subscriptionName, callback); }; Api.prototype.unsubscribe = function (subscriptionId) { if (!this._subscriptions) { return Promise.resolve(true); } return this._subscriptions.unsubscribe(subscriptionId); }; Api.prototype.pollMethod = function (method, input, validate) { var _this = this; var _a = method.split('_'), _group = _a[0], endpoint = _a[1]; var group = "_" + _group; return new Promise(function (resolve, reject) { var timeout = function () { _this[group][endpoint](input) .then(function (result) { if (validate ? validate(result) : result) { resolve(result); } else { setTimeout(timeout, 500); } }) .catch(function (error) { // Don't print if the request is rejected: that's ok if (error.type !== 'REQUEST_REJECTED') { console.error('pollMethod', error); } reject(error); }); }; timeout(); }); }; return Api; }(EventEmitter)); Api.util = util; Api.Provider = { Current: Providers.Current, Ipc: Providers.Ipc, Http: Providers.Http, PostMessage: Providers.PostMessage, SendAsync: Providers.SendAsync, Ws: Providers.Ws, WsSecure: Providers.WsSecure }; // NOTE: kept for backwards compatibility Api.Transport = { Http: Transports.Http, Ws: Transports.Ws, WsSecure: Transports.WsSecure }; module.exports = Api;