Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
PubSub for parity-js (#5830)
Browse files Browse the repository at this point in the history
* PubSub Integration WebSocket

* PubSub Provider API

* Parity License and fix switch statement

* Minor fix: use parameter api

* Exclude subscriptionId return

* Unsubscribe parameters as array

* secureProvider API added

* isSecure check

* Refractor: Formatting in callback (no Promise)

* Tests for parityProvider

* Refractor: Formatting in callback (secure API)

* Updated transaction documentation

* Module instead of API-Names, Options always as array (e.g. empty)

'parity' instead of 'parity_subscribe' calls
params with empty array as options. If eth_subscribe includes empty array parity-core will send invalid request (eth api doesn't have options)

* Removed isSecure transport check, because APIs are configurable

* Refractor Provider API to single Pubsub

* Modify transport layer to have single identifier for subscriptions

* FIX: Display pubsub errors

* Discard Messages after unsubscribing

* Fix: display error normal messages correctly

* Simplified code, removed unnecessary pubsub methods

* trace_call API 2nd argument blockNumber, first whatTrace

https://github.com/paritytech/parity/wiki/JSONRPC-trace-module#trace_call

* Separate namespaces pubsub. eth, parity, net

* Keep error for messages from unsubscribed topics.

* Fix: Unsubscribe Promise

* Add Test: Unsubscribe promise resolved

* Fix: 'error' in params
  • Loading branch information
kaikun213 authored and jacogr committed Jul 6, 2017
1 parent cc718bb commit 349316f
Show file tree
Hide file tree
Showing 16 changed files with 1,574 additions and 16 deletions.
13 changes: 12 additions & 1 deletion js/src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Contract from './contract';

import { Db, Eth, Parity, Net, Personal, Shh, Signer, Trace, Web3 } from './rpc';
import Subscriptions from './subscriptions';
import Pubsub from './pubsub';
import util from './util';
import { isFunction } from './util/types';

Expand All @@ -46,10 +47,13 @@ export default class Api extends EventEmitter {
this._trace = new Trace(transport);
this._web3 = new Web3(transport);

if (isFunction(transport.subscribe)) {
this._pubsub = new Pubsub(transport);
}

if (allowSubscriptions) {
this._subscriptions = new Subscriptions(this);
}

// Doing a request here in test env would cause an error
if (LocalAccountsMiddleware && process.env.NODE_ENV !== 'test') {
const middleware = this.parity
Expand All @@ -67,6 +71,13 @@ export default class Api extends EventEmitter {
}
}

get pubsub () {
if (!this._pubsub) {
throw Error('Pubsub is only available with a subscribing-supported transport injected!');
}
return this._pubsub;
}

get db () {
return this._db;
}
Expand Down
227 changes: 227 additions & 0 deletions js/src/api/pubsub/eth/eth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// 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/>.
import PubsubBase from '../pubsubBase';

import { inAddress, inBlockNumber, inHex, inNumber16, inOptions, inFilter } from '../../format/input';
import { outAddress, outBlock, outNumber, outTransaction, outSyncing, outReceipt, outLog } from '../../format/output';

export default class Eth extends PubsubBase {
constructor (transport) {
super(transport);
this._api = 'parity';
}

newHeads (callback) {
return this.addListener('eth', 'newHeads', callback);
}

logs (callback) {
throw Error('not supported yet');
}

// eth API
protocolVersion (callback) {
return this.addListener(this._api, 'eth_protocolVersion', callback);
}

syncing (callback) {
return this.addListener(this._api, 'eth_syncing', (error, data) => {
error
? callback(error)
: callback(null, outSyncing(data));
});
}

hashrate (callback) {
return this.addListener(this._api, 'eth_hashrate', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
});
}

coinbase (callback) {
return this.addListener(this._api, 'eth_coinbase', (error, data) => {
error
? callback(error)
: callback(null, outAddress(data));
});
}

mining (callback) {
return this.addListener(this._api, 'eth_mining', callback);
}

gasPrice (callback) {
return this.addListener(this._api, 'eth_gasPrice', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
});
}

accounts (callback) {
return this.addListener(this._api, 'eth_accounts', (error, accounts) => {
error
? callback(error)
: callback(null, (accounts || []).map(outAddress));
});
}

blockNumber (callback) {
return this.addListener(this._api, 'eth_blockNumber', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
});
}

getBalance (callback, address, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_getBalance', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inAddress(address), inBlockNumber(blockNumber)]);
}

getStorageAt (callback, address, index = 0, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_getStorageAt', callback, [inAddress(address), inNumber16(index), inBlockNumber(blockNumber)]);
}

getBlockByHash (callback, hash, full = false) {
return this.addListener(this._api, 'eth_getBlockByHash', (error, data) => {
error
? callback(error)
: callback(null, outBlock(data));
}, [inHex(hash), full]);
}

getBlockByNumber (callback, blockNumber = 'latest', full = false) {
return this.addListener(this._api, 'eth_getBlockByNumber', (error, data) => {
error
? callback(error)
: callback(null, outBlock(data));
}, [inBlockNumber(blockNumber), full]);
}

getTransactionCount (callback, address, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_getTransactionCount', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inAddress(address), inBlockNumber(blockNumber)]);
}

getBlockTransactionCountByHash (callback, hash) {
return this.addListener(this._api, 'eth_getBlockTransactionCountByHash', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inHex(hash)]);
}

getBlockTransactionCountByNumber (callback, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_getBlockTransactionCountByNumber', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inBlockNumber(blockNumber)]);
}

getUncleCountByBlockHash (callback, hash) {
return this.addListener(this._api, 'eth_getUncleCountByBlockHash', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inHex(hash)]);
}

getUncleCountByBlockNumber (callback, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_getUncleCountByBlockNumber', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inBlockNumber(blockNumber)]);
}

getCode (callback, address, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_getCode', callback, [inAddress(address), inBlockNumber(blockNumber)]);
}

call (callback, options, blockNumber = 'latest') {
return this.addListener(this._api, 'eth_call', callback, [inOptions(options), inBlockNumber(blockNumber)]);
}

estimateGas (callback, options) {
return this.addListener(this._api, 'eth_estimateGas', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
}, [inOptions(options)]);
}

getTransactionByHash (callback, hash) {
return this.addListener(this._api, 'eth_getTransactionByHash', (error, data) => {
error
? callback(error)
: callback(null, outTransaction(data));
}, [inHex(hash)]);
}

getTransactionByBlockHashAndIndex (callback, hash, index = 0) {
return this.addListener(this._api, 'eth_getTransactionByBlockHashAndIndex', (error, data) => {
error
? callback(error)
: callback(null, outTransaction(data));
}, [inHex(hash), inNumber16(index)]);
}

getTransactionByBlockNumberAndIndex (callback, blockNumber = 'latest', index = 0) {
return this.addListener(this._api, 'eth_getTransactionByBlockNumberAndIndex', (error, data) => {
error
? callback(error)
: callback(null, outTransaction(data));
}, [inBlockNumber(blockNumber), inNumber16(index)]);
}

getTransactionReceipt (callback, txhash) {
return this.addListener(this._api, 'eth_getTransactionReceipt', (error, data) => {
error
? callback(error)
: callback(null, outReceipt(data));
}, [inHex(txhash)]);
}

getUncleByBlockHashAndIndex (callback, hash, index = 0) {
return this.addListener(this._api, 'eth_getUncleByBlockHashAndIndex', callback, [inHex(hash), inNumber16(index)]);
}

getUncleByBlockNumberAndIndex (callback, blockNumber = 'latest', index = 0) {
return this.addListener(this._api, 'eth_getUncleByBlockNumberAndIndex', callback, [inBlockNumber(blockNumber), inNumber16(index)]);
}

getLogs (callback, options) {
return this.addListener(this._api, 'eth_getLogs', (error, logs) => {
error
? callback(error)
: callback(null, (logs) => logs.map(outLog));
}, [inFilter(options)]);
}

getWork (callback) {
return this.addListener(this._api, 'eth_getWork', callback);
}
}
16 changes: 16 additions & 0 deletions js/src/api/pubsub/eth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// 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/>.
export default from './eth';
16 changes: 16 additions & 0 deletions js/src/api/pubsub/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// 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/>.
export default from './pubsub';
16 changes: 16 additions & 0 deletions js/src/api/pubsub/net/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// 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/>.
export default from './net';
42 changes: 42 additions & 0 deletions js/src/api/pubsub/net/net.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// 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/>.
import PubsubBase from '../pubsubBase';

import { outNumber } from '../../format/output';

export default class Net extends PubsubBase {
constructor (transport) {
super(transport);
this._api = 'parity';
}

// net API
version (callback) {
return this.addListener(this._api, 'net_version', callback);
}

peerCount (callback) {
return this.addListener(this._api, 'net_peerCount', (error, data) => {
error
? callback(error)
: callback(null, outNumber(data));
});
}

listening (callback) {
return this.addListener(this._api, 'net_listening', callback);
}
}
16 changes: 16 additions & 0 deletions js/src/api/pubsub/parity/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// 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/>.
export default from './parity';
Loading

0 comments on commit 349316f

Please sign in to comment.