npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

web3-redux-middleware

v1.0.4

Published

Redux middleware for handling Web3.js PromiEvents

Downloads

36

Readme

Web3 Redux Middleware

PromiEvent-redux Middleware enables declarative reducers when making calls to the Ethereum network with Web3 Redux: it accepts a promiEvent or promise and dispatches pending, hashed, confirmed, receipt, fulfilled and rejected actions.

Heavily inspired by the popular redux-promise-middleware library.

Example

import { createStore, applyMiddleware } from 'redux'
import web3Redux from 'web3-redux-middleware'

const store = createStore(
  promiEventReducer,
  {},
  applyMiddleware(web3Middleware())
);

const EXAMPLE = 'EXAMPLE';

const contract = new web3.eth.Contract(abi);

// To Use just attach a PromiEvent as a payload

const promiEventActionCreator = () => ({
  type: EXAMPLE,
  payload: contract
    .methods
    .contractMethod()
    .send({
      from: `0xE71b3853e8Dbc57Bc91E7504726e8F051c977e69`,
      value: 1000000
    })
});

// The middleware will intercept the action and dispatch actions as the network events occur

function promiEventReducer(state, action) {
  // A _PENDING event will be sent right away
  switch(action.type) {
    case `${EXAMPLE}_PENDING`:
      return state;

    // Other events will be released as the network events occur
    case `${EXAMPLE}_FULFILLED`:
      return {
        ...state,
        result: action.payload
      };

    case `${EXAMPLE}_HASHED`:
      return {
        ...state,
        hash: action.payload
      };

    case `${EXAMPLE}_CONFIRMED`:
      return {
        ...state,
        confirmationObject: action.payload,
        confirmationsCount: action.payload.confirmationsCount
      };

    case `${EXAMPLE}_RECEIPT`:
      return {
        ...state,
        receipt: action.payload
      };

    // Dispatched on .catch
    case `${EXAMPLE}_REJECTED`
      return {
        ...state,
        value: action.payload,
        isError: action.error,
      };

    // Dispatched on 'error' event
    case `${EXAMPLE}_ERROR`:
      return {
        ..state,
        value: action.payload,
        isError: action.error
      }
  }
};

// Alternatively you can attatch the promise to a promiEvent to the promiEvent property
// Common use case is when you want to send some data for optimistic loading
// The data will be dispatched on the _PENDING action
const promiEventAction2 = () => ({
  type: EXAMPLE,
  payload: {
    data: 'Some Data',
    promiEvent: contract
      .methods
      .contractMethod()
      .send({
        from: `0xE71b3853e8Dbc57Bc91E7504726e8F051c977e69`,
        value: 1000000
      })
});

// Any meta data can be attatched the the meta property.  It will be dispatched on every event
const promiEventAction3 = () => ({
  type: EXAMPLE,
  meta: 'some meta data'
  payload: contract
    .methods
    .contractMethod()
    .send({
      from: `0xE71b3853e8Dbc57Bc91E7504726e8F051c977e69`,
      value: 1000000
    })
})

// to change the promise type suffixes or the delimiter just pass in a config object
const configedWeb3Middleware = web3Redux({
  promiEventTypeDelimiter: ' ',
  promiEventTypeSuffixes: {
    pending: 'WAITING',
    fulfilled: 'SUCCESS',
    rejected: 'FAIL',
    transactionHash: 'TRANSACTION_HASHED',
    confirmation: 'CONFIRMATION',
    reciept: 'RECEIPT_RECIEVED',
    error: 'FAILURE'
  }
})

Releases

Maintainers

Please reach out if you have any questions or comments!

License

Code licensed with the MIT License (MIT).