Git Product home page Git Product logo

redux-multicall's Introduction

MultiCall

A React + Redux library for fetching, batching, and caching chain state via the MultiCall contract.

Setup

yarn add @uniswap/redux-multicall or npm install @uniswap/redux-multicall

Usage

The usage of this library is similar to RTK Query.

// Somewhere in your app
export const multicall = createMulticall({ reducerPath: 'multicall' })

// In your store's root reducer
export const rootReducer = combineReducers({
  // Other reducers
  [multicall.reducerPath]: multicall.reducer
})

To use the updater, you'll need an instance of the Uniswap Multicall2 contract:

import { abi as MulticallABI } from '@uniswap/v3-periphery/artifacts/contracts/lens/UniswapInterfaceMulticall.sol/UniswapInterfaceMulticall.json'
import { Contract } from '@ethersproject/contracts'
import { UniswapInterfaceMulticall } from './abi/types'

const multicall2Contract = new Contract(address, MulticallABI, provider) as UniswapInterfaceMulticall

For a more detailed example, see basic example app in ./integration-tests

Alpha software

The latest version of the SDK is used in production in the Uniswap Interface, but it is considered Alpha software and may contain bugs or change significantly between patch versions. If you have questions about how to use the SDK, please reach out in the #dev-chat channel of the Discord. Pull requests welcome!

redux-multicall's People

Contributors

jds-23 avatar jmrossy avatar lint-action avatar lynnshaoyu avatar moodysalem avatar stefdelec avatar tinaszheng avatar vm avatar zzmp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

redux-multicall's Issues

What determines blockPerfetch?

Currently using this package on a layer 2 Blockchain and I'm wondering what value to set for blockPerfetch as I don't know the parameters to consider.

For context, the blockchain Is Nahmii blockchain

Improve multichain support

Currently, this lib's interface (Updater + hooks) were designed with the intention of using 1 active chain at a time.
For consumers using multiple active chains, this requires inelegant solutions to use multiple copies of the hooks at once.

Explore options for supporting multiple chains in an efficient way with a consumer-friendly interface.

Improved error messaging for LOADING_CALL_STATE

Hi, been experimenting using the multicall library in conjunction with the web3-react library. Ran into some difficulties with the build version, namely that in utils/callState.ts, we have:

export function toCallState(
  callResult: CallResult | undefined,
  contractInterface: utils.Interface | undefined,
  fragment: utils.FunctionFragment | undefined,
  latestBlockNumber: number | undefined
): CallState {
  if (!callResult) return INVALID_CALL_STATE
  const { valid, data, blockNumber } = callResult
  if (!valid) return INVALID_CALL_STATE
  if (valid && !blockNumber) return LOADING_CALL_STATE
  if (!contractInterface || !fragment || !latestBlockNumber) return LOADING_CALL_STATE

LOADING_CALL_STATE in particular covers several options:

  • missing values in the initial contract call (contractinterface, fragment, latestblocknumber)
  • missing blocknumber from the redux actions

The blockNumber is particularly challenging, as it is computed from several prior hooks and related function calls (useCallDataSubscription > callKeysToCalls > toCallKey), which results in:

// result =
        return { valid: true, data, blockNumber: result?.blockNumber }

So, as a user, I might suspect I'm missing the blockNumber in the result, but it's challenging to understand exactly where the error in the contract call might be, that we can fix in order to move the call from LOADING_CALL_STATE.

Would welcome some initial feedback on the causes of the above, then would be happy to start working on a PR.

Bad setState call inside Updater

I am receiving an error when attempting to use Multicall Updater similar to the implementation in uniswap-interface

import React from 'react'
import { combineReducers, createStore } from 'redux'

import { createMulticall } from '@uniswap/redux-multicall'
import { useWeb3React } from '@web3-react/core'
import { SupportedChainId } from '../constants/chains'
import { useInterfaceMulticall } from '../hooks/useContract'
import useBlockNumber from '../contexts/BlockNumberContext'

const multicall = createMulticall()
const reducer = combineReducers({ [multicall.reducerPath]: multicall.reducer })

export default multicall

function getBlocksPerFetchForChainId(chainId): number {
  switch (chainId) {
    case SupportedChainId.RINKEBY:
    case SupportedChainId.ROPSTEN:
      return 5
    default:
      return 1
  }
}

export function MulticallUpdater() {
  const { chainId } = useWeb3React()
  const latestBlockNumber = useBlockNumber()
  const contract = useInterfaceMulticall()
  
  const listenerOptions = React.useMemo(
    () => ({
      blocksPerFetch: getBlocksPerFetchForChainId(chainId),
    }),
    [chainId]
  )

  return (
    <multicall.Updater
      chainId={chainId}
      latestBlockNumber={latestBlockNumber}
      contract={contract}
      listenerOptions={listenerOptions}
    />
  )
}
 Warning: Cannot update a component (`Updater`) while rendering a different component (`Updater`)

Stack Trace

Warning: Cannot update a component (`Updater`) while rendering a different component (`Updater`). To locate the bad setState() call inside `Updater`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at Updater
    at UpdaterContextBound
    at MulticallUpdater 
    at Updaters
    at BlockNumberProvider
    at Web3ReactProvider 
    at UserPreferencesProvider
    at Provider

Versions

node v16.8.10
react v18.2.0

Articles for react-mulitcall

I wrote a series of comprehensive articles for anyone strugling to use this library in their application
Part A - Explains what redux-multicall is and why you should use it: https://ajayimike.medium.com/a-guide-to-redux-multicall-building-a-real-time-dapp-part-a-b263b14ac
Part B - A practical guide to setting up redux-multicall for use in your dApp: https://ajayimike.medium.com/a-guide-to-redux-multicall-building-a-real-time-dapp-part-b-e92cccb0f2e6
Part C - Build a simple dApp that shows actual usage of redux-multicall for data fetching in your dApp: https://ajayimike.medium.com/a-guide-to-redux-multicall-building-a-real-time-dapp-part-c-89a9c11eba76

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.