Sync Middleware
Web3-Redux comes with a built-in Sync ta model which serves as a form of dynamic middleware that can be added, removed, and customized. There are three types of syncs, BlockSync which each can trigger actions upon receiving updates to a new block, new event, or new transaction.
Sync middleware can be useful when looking to dispatch your own custom Redux action as a result of some blockchain update. They are also used as the building blocks for the Contract Call sync.
Base Sync
All sync middleware shares the following parameters:
id
: This must be a unique id that defines the middleware and can be used to remove it in the future.actions
: These actions are dispatched whenever the sync middleware is triggered.
Bock Sync
This middleware listens for Block/CREATE actions, and if block.number % matchBlockNumberModulo == 0
, it will dispatch its actions
. A matchBlockNumberModulo
value of 1
would imply dispatching on every block.
Sync.create({ id: '1', type: 'Block', matchBlockNumberModulo: 1, actions });
Reference: BlockSync
Event Sync
This middleware listens for ContractEvent/CREATE actions, and if an event matches its matchAddress
, matchName
, and matchReturnValues
, will dispatch its actions
.
The following example filters for Transfer
events:
Sync.create({ id: '1', type: 'Event', matchAddress: address, matchName: 'Transfer', actions });
An event sync middleware is NOT the same as an event subscription. Sync middleware simply watches the local redux store and dispatches additional actions. This does not create a new Web3 subscription.
Reference: EventSync
Transaction Sync
This middleware listens for Transaction/CREATE actions, and if an transaction matches its matchFrom
and matchTo
parameteres, will dispatch its actions
.
The following example filters for tranaction from a particular sender:
Sync.create({ id: '1', type: 'Transaction', matchFrom: account, actions }));
Reference: TransactionSync
Sync
Type union of Block/Event/Transaction Syncs.
Reference: Sync