diff --git a/packages/web3-core-subscriptions/src/subscriptions/eth/SyncingSubscription.js b/packages/web3-core-subscriptions/src/subscriptions/eth/SyncingSubscription.js index 86df83c55fa..f003e3b5e51 100644 --- a/packages/web3-core-subscriptions/src/subscriptions/eth/SyncingSubscription.js +++ b/packages/web3-core-subscriptions/src/subscriptions/eth/SyncingSubscription.js @@ -45,23 +45,27 @@ export default class SyncingSubscription extends AbstractSubscription { * @returns {Object} */ onNewSubscriptionItem(subscriptionItem) { - const isSyncing = subscriptionItem.result.syncing; + if (typeof subscriptionItem !== 'boolean') { + const isSyncing = subscriptionItem.syncing; - if (this.isSyncing === null) { - this.isSyncing = isSyncing; - this.emit('changed', this.isSyncing); - } + if (this.isSyncing === null) { + this.isSyncing = isSyncing; + this.emit('changed', this.isSyncing); - if (this.isSyncing === true && isSyncing === false) { - this.isSyncing = isSyncing; - this.emit('changed', this.isSyncing); - } + return subscriptionItem.status; + } - if (this.isSyncing === false && isSyncing === true) { - this.isSyncing = isSyncing; - this.emit('changed', this.isSyncing); + if (this.isSyncing !== isSyncing) { + this.isSyncing = isSyncing; + this.emit('changed', this.isSyncing); + } + + return subscriptionItem.status; } - return this.formatters.outputSyncingFormatter(subscriptionItem); + this.isSyncing = subscriptionItem; + this.emit('changed', subscriptionItem); + + return subscriptionItem; } } diff --git a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js index f3685d25d7d..f0db002f676 100644 --- a/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js +++ b/packages/web3-core-subscriptions/tests/src/subscriptions/eth/SyncingSubscriptionTest.js @@ -1,9 +1,5 @@ -import {formatters} from 'web3-core-helpers'; import SyncingSubscription from '../../../../src/subscriptions/eth/SyncingSubscription'; -// Mocks -jest.mock('web3-core-helpers'); - /** * SyncingSubscription test */ @@ -11,7 +7,7 @@ describe('SyncingSubscriptionTest', () => { let syncingSubscription; beforeEach(() => { - syncingSubscription = new SyncingSubscription({}, formatters, {}); + syncingSubscription = new SyncingSubscription({}, {}, {}); }); it('constructor check', () => { @@ -22,16 +18,10 @@ describe('SyncingSubscriptionTest', () => { expect(syncingSubscription.type).toEqual('eth_subscribe'); expect(syncingSubscription.options).toEqual(null); - - expect(syncingSubscription.utils).toEqual({}); - - expect(syncingSubscription.formatters).toEqual(formatters); - - expect(syncingSubscription.moduleInstance).toEqual({}); }); - it('onNewSubscriptionItem calls outputSyncingFormatter and emits "changed" event (isSyncing: null)', (done) => { - const item = {result: {syncing: true}}; + it('calls onNewSubscriptionItem and emits the initial "changed" event', (done) => { + const item = {syncing: true}; syncingSubscription.on('changed', (subscriptionItem) => { expect(subscriptionItem).toEqual(true); @@ -40,37 +30,28 @@ describe('SyncingSubscriptionTest', () => { }); syncingSubscription.onNewSubscriptionItem(item); - - expect(formatters.outputSyncingFormatter).toHaveBeenCalledWith(item); }); - it('onNewSubscriptionItem calls outputSyncingFormatter and emits "changed" event (isSyncing: true)', (done) => { - const item = {result: {syncing: false}}; + it('calls onNewSubscriptionItem and emits the "changed" event', (done) => { + const item = {syncing: false}; syncingSubscription.on('changed', (subscriptionItem) => { - expect(subscriptionItem).toEqual(item.result.syncing); + expect(subscriptionItem).toEqual(false); done(); }); syncingSubscription.isSyncing = true; syncingSubscription.onNewSubscriptionItem(item); - - expect(formatters.outputSyncingFormatter).toHaveBeenCalledWith(item); }); - it('onNewSubscriptionItem calls outputSyncingFormatter and emits "changed" event (isSyncing: false)', (done) => { - const item = {result: {syncing: true}}; - - syncingSubscription.on('changed', (subscriptionItem) => { - expect(subscriptionItem).toEqual(item.result.syncing); - - done(); - }); - - syncingSubscription.isSyncing = false; - syncingSubscription.onNewSubscriptionItem(item); + it('calls onNewSubscriptionItem and returns the boolean', () => { + expect(syncingSubscription.onNewSubscriptionItem(true)).toEqual(true); + }); - expect(formatters.outputSyncingFormatter).toHaveBeenCalledWith(item); + it('calls onNewSubscriptionItem and returns the syncing status', () => { + expect( + syncingSubscription.onNewSubscriptionItem({status: true, syncing: true}) + ).toEqual(true); }); }); diff --git a/packages/web3-eth/types/index.d.ts b/packages/web3-eth/types/index.d.ts index 88ba45b3e34..3688d9046e5 100644 --- a/packages/web3-eth/types/index.d.ts +++ b/packages/web3-eth/types/index.d.ts @@ -163,11 +163,11 @@ export interface Method { } export interface Syncing { - startingBlock: number; - currentBlock: number; - highestBlock: number; - knownStates: number; - pulledStates: number; + StartingBlock: number; + CurrentBlock: number; + HighestBlock: number; + KnownStates: number; + PulledStates: number; } export interface BlockHeader {