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

PubSub for parity-js #5830

Merged
merged 26 commits into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
66fc121
PubSub Integration WebSocket
kaikun213 Jun 13, 2017
f38fb4d
PubSub Provider API
kaikun213 Jun 13, 2017
1f6fd73
Parity License and fix switch statement
kaikun213 Jun 13, 2017
4df1057
Minor fix: use parameter api
kaikun213 Jun 13, 2017
fcffa39
Exclude subscriptionId return
kaikun213 Jun 13, 2017
65f8091
Unsubscribe parameters as array
kaikun213 Jun 13, 2017
e5ad5b0
secureProvider API added
kaikun213 Jun 13, 2017
1f065c5
isSecure check
kaikun213 Jun 14, 2017
82134b7
Refractor: Formatting in callback (no Promise)
kaikun213 Jun 14, 2017
87ad489
Tests for parityProvider
kaikun213 Jun 14, 2017
60d7afd
Refractor: Formatting in callback (secure API)
kaikun213 Jun 14, 2017
e2b3759
Updated transaction documentation
kaikun213 Jun 20, 2017
6a0b33b
Module instead of API-Names, Options always as array (e.g. empty)
kaikun213 Jun 22, 2017
1eb138a
Removed isSecure transport check, because APIs are configurable
kaikun213 Jun 22, 2017
de0f0a0
Refractor Provider API to single Pubsub
kaikun213 Jun 22, 2017
0476901
Modify transport layer to have single identifier for subscriptions
kaikun213 Jun 22, 2017
db5af2e
FIX: Display pubsub errors
kaikun213 Jun 23, 2017
95de48c
Discard Messages after unsubscribing
kaikun213 Jun 26, 2017
0da4dce
Fix: display error normal messages correctly
kaikun213 Jun 28, 2017
0103a7f
Simplified code, removed unnecessary pubsub methods
kaikun213 Jun 29, 2017
5a5ad4a
trace_call API 2nd argument blockNumber, first whatTrace
kaikun213 Jun 29, 2017
864cf06
Separate namespaces pubsub. eth, parity, net
kaikun213 Jul 4, 2017
951d0cc
Keep error for messages from unsubscribed topics.
kaikun213 Jul 4, 2017
c9c25ef
Fix: Unsubscribe Promise
kaikun213 Jul 5, 2017
a873d56
Add Test: Unsubscribe promise resolved
kaikun213 Jul 5, 2017
26d5043
Fix: 'error' in params
kaikun213 Jul 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
// import { LocalAccountsMiddleware } from './local';
Expand All @@ -45,10 +46,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 (process.env.NODE_ENV !== 'test') {
const middleware = this.parity
Expand All @@ -66,6 +70,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