How to use web3-core-subscriptions - 10 common examples

To help you get started, we’ve selected a few web3-core-subscriptions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vechain / thorify / src / extend / methods.ts View on Github external
name: 'getChainTag',
                call: 'eth_getChainTag',
                params: 0,
            }),
            new web3.extend.Method({
                name: 'getPastLogs',
                call: 'eth_getLogs',
                params: 1,
                inputFormatter: [web3.extend.formatters.inputLogFormatter],
                outputFormatter: web3.extend.formatters.outputLogFormatter,
            }),
        ],
    })

    // subscriptions
    const subs = new Subscriptions({
        name: 'subscribe',
        type: 'eth',
        subscriptions: {
            newBlockHeaders: {
                subscriptionName: 'newHeads',
                params: 1,
                inputFormatter: [inputBlockFilterFormatter],
                subscriptionHandler(subscriptionMsg: any) {
                    if (subscriptionMsg.error) {
                        this.emit('error', subscriptionMsg.error)
                         // web3-core-subscriptions/subscription sets a default value for this.callback
                        this.callback(subscriptionMsg.error, null, this)
                    } else {
                        const result = web3.extend.formatters.outputBlockFormatter(subscriptionMsg.data)
                        if (result.removed) {
                            this.emit('changed', result)
github ethereum / web3.js / packages / web3-shh / src / index.js View on Github external
var _this = this;

    // sets _requestmanager
    core.packageInit(this, arguments);

    // overwrite setProvider
    var setProvider = this.setProvider;
    this.setProvider = function () {
        setProvider.apply(_this, arguments);
        _this.net.setProvider.apply(_this, arguments);
    };

    this.net = new Net(this.currentProvider);

    [
        new Subscriptions({
            name: 'subscribe',
            type: 'shh',
            subscriptions: {
                'messages': {
                    params: 1
                    // inputFormatter: [formatters.inputPostFormatter],
                    // outputFormatter: formatters.outputPostFormatter
                }
            }
        }),

        new Method({
            name: 'getVersion',
            call: 'shh_version',
            params: 0
        }),
github ethereum / web3.js / packages / web3-eth-contract / src / index.js View on Github external
Contract.prototype._on = function(){
    var subOptions = this._generateEventOptions.apply(this, arguments);


    // prevent the event "newListener" and "removeListener" from being overwritten
    this._checkListener('newListener', subOptions.event.name, subOptions.callback);
    this._checkListener('removeListener', subOptions.event.name, subOptions.callback);

    // TODO check if listener already exists? and reuse subscription if options are the same.

    // create new subscription
    var subscription = new Subscription({
        subscription: {
            params: 1,
            inputFormatter: [formatters.inputLogFormatter],
            outputFormatter: this._decodeEventABI.bind(subOptions.event),
            // DUBLICATE, also in web3-eth
            subscriptionHandler: function (output) {
                if(output.removed) {
                    this.emit('changed', output);
                } else {
                    this.emit('data', output);
                }

                if (_.isFunction(this.callback)) {
                    this.callback(null, output, this);
                }
            }
github vechain / thorify / src / extend / contracts.ts View on Github external
if (subOptions.params.topics) {
            for (const [index, value] of subOptions.params.topics.entries()) {
                if (value === null) {
                    continue
                }
                if (typeof value === 'string') {
                    filterOptions['t' + index as 't0' | 't1' | 't2' | 't3' | 't4'] = value
                } else {
                    throw new Error('[thorify] Array filter option is not supported in thor, must be null or bytes32 string')
                }
            }
        }

        const decodeEventABI = this._decodeEventABI.bind(subOptions.event)
        const subscription = new Subscription({
            subscription: {
                params: 1,
                inputFormatter: [inputLogFilterFormatter],
                subscriptionHandler(subscriptionMsg: any) {
                    if (subscriptionMsg.error) {
                        this.emit('error', subscriptionMsg.error)
                        // web3-core-subscriptions/subscription sets a default value for this.callback
                        this.callback(subscriptionMsg.error, null, this)
                    } else {
                        const result = decodeEventABI(subscriptionMsg.data)
                        if (result.removed) {
                            this.emit('changed', result)
                        } else {
                            this.emit('data', result)
                        }
                        // web3-core-subscriptions/subscription sets a default value for this.callback
github ethereum / web3.js / packages / web3-eth / src / index.js View on Github external
}),
        new Method({
            name: 'getWork',
            call: 'eth_getWork',
            params: 0
        }),
        new Method({
            name: 'getPastLogs',
            call: 'eth_getLogs',
            params: 1,
            inputFormatter: [formatter.inputLogFormatter],
            outputFormatter: formatter.outputLogFormatter
        }),

        // subscriptions
        new Subscriptions({
            name: 'subscribe',
            type: 'eth',
            subscriptions: {
                'newBlockHeaders': {
                    // TODO rename on RPC side?
                    subscriptionName: 'newHeads', // replace subscription with this name
                    params: 0,
                    outputFormatter: formatter.outputBlockFormatter
                },
                'pendingTransactions': {
                    subscriptionName: 'newPendingTransactions', // replace subscription with this name
                    params: 0
                },
                'logs': {
                    params: 1,
                    inputFormatter: [formatter.inputLogFormatter],
github poanetwork / bridge-ui / lib / web3-eth / index.js View on Github external
}),
        new Method({
            name: 'getWork',
            call: 'eth_getWork',
            params: 0
        }),
        new Method({
            name: 'getPastLogs',
            call: 'eth_getLogs',
            params: 1,
            inputFormatter: [formatter.inputLogFormatter],
            outputFormatter: formatter.outputLogFormatter
        }),

        // subscriptions
        new Subscriptions({
            name: 'subscribe',
            type: 'eth',
            subscriptions: {
                'newBlockHeaders': {
                    // TODO rename on RPC side?
                    subscriptionName: 'newHeads', // replace subscription with this name
                    params: 0,
                    outputFormatter: formatter.outputBlockFormatter
                },
                'pendingTransactions': {
                    subscriptionName: 'newPendingTransactions', // replace subscription with this name
                    params: 0
                },
                'logs': {
                    params: 1,
                    inputFormatter: [formatter.inputLogFormatter],
github jpmorganchase / quorum.js / lib / index.js View on Github external
property: 'eth',
    methods: [{
      name: 'getBlock',
      call: blockCall,
      params: 2,
      inputFormatter: [web3.extend.formatters.inputBlockNumberFormatter, function (val) {
        return !!val;
      }],
      outputFormatter: quorumOutputBlockFormatter
    }]
  });

  // Creating a modified 'newBlockHeaders' subscription to replace the output formatter in the case of Quorum
  var Subscriptions = require('web3-core-subscriptions').subscriptions;

  const patchedSub = new Subscriptions({
    name: 'subscribe',
    type: 'eth',
    subscriptions: {
      'newBlockHeaders': {
        subscriptionName: 'newHeads',
        params: 0,
        outputFormatter: quorumOutputBlockFormatter
      }
    }
  });

  let _quorumSubObject = {};
  patchedSub.setRequestManager(web3._requestManager);
  patchedSub.attachToObject(_quorumSubObject);
github poanetwork / tokenbridge / ui / lib / web3-eth / index.js View on Github external
}),
        new Method({
            name: 'getWork',
            call: 'eth_getWork',
            params: 0
        }),
        new Method({
            name: 'getPastLogs',
            call: 'eth_getLogs',
            params: 1,
            inputFormatter: [formatter.inputLogFormatter],
            outputFormatter: formatter.outputLogFormatter
        }),

        // subscriptions
        new Subscriptions({
            name: 'subscribe',
            type: 'eth',
            subscriptions: {
                'newBlockHeaders': {
                    // TODO rename on RPC side?
                    subscriptionName: 'newHeads', // replace subscription with this name
                    params: 0,
                    outputFormatter: formatter.outputBlockFormatter
                },
                'pendingTransactions': {
                    subscriptionName: 'newPendingTransactions', // replace subscription with this name
                    params: 0
                },
                'logs': {
                    params: 1,
                    inputFormatter: [formatter.inputLogFormatter],
github ethereum / web3.js / modules / ethereum / src / contract / factories / MethodFactory.js View on Github external
createTransactionObserver(moduleInstance) {
        if (moduleInstance.currentProvider.supportsSubscriptions()) {
            return new SocketTransactionObserver(
                moduleInstance.currentProvider,
                this.getTimeout(moduleInstance),
                moduleInstance.transactionConfirmationBlocks,
                new GetTransactionReceiptMethod(this.utils, this.formatters, moduleInstance),
                new NewHeadsSubscription(this.utils, this.formatters, moduleInstance)
            );
        }

        return new HttpTransactionObserver(
            moduleInstance.currentProvider,
            this.getTimeout(moduleInstance),
            moduleInstance.transactionConfirmationBlocks,
            new GetTransactionReceiptMethod(this.utils, this.formatters, moduleInstance),
            new GetBlockByNumberMethod(this.utils, this.formatters, moduleInstance)
        );
    }
}
github ethereum / web3.js / lib / json-rpc / factories / AbstractMethodFactory.js View on Github external
createTransactionObserver(moduleInstance) {
        if (moduleInstance.currentProvider.supportsSubscriptions()) {
            return new SocketTransactionObserver(
                moduleInstance.currentProvider,
                this.getTimeout(moduleInstance),
                moduleInstance.transactionConfirmationBlocks,
                new GetTransactionReceiptMethod(this.utils, this.formatters, moduleInstance),
                new NewHeadsSubscription(this.utils, this.formatters, moduleInstance)
            );
        }

        return new HttpTransactionObserver(
            moduleInstance.currentProvider,
            this.getTimeout(moduleInstance),
            moduleInstance.transactionConfirmationBlocks,
            new GetTransactionReceiptMethod(this.utils, this.formatters, moduleInstance),
            new GetBlockByNumberMethod(this.utils, this.formatters, moduleInstance)
        );
    }
}