question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript errors when creating provider in ethersv5

See original GitHub issue

✅ Prerequisites

  • Did you perform a cursory search of open issues? Is this bug already reported elsewhere?
  • Are you running the latest SDK version?
  • Are you reporting to the correct repository (magic-sdk)?

🐛 Description

Trying to create an ethersv5 Web3Provider using magic.rpcProvider as described in docs results in a Typescript error

🧩 Steps to Reproduce

  1. Install magic-sdk and ethersv5 in a typescript environment
  2. Attempt to construct a Web3Provider using magic

🤔 Expected behavior

Types to match such that no error appears

😮 Actual behavior

Typescript reports that RPCProviderModule can’t be assigned to ExternalProvider | JsonRpcFetchFunc

Screenshot_2020-08-23_15-12-38

💻 Code Sample

I’m afraid I can’t share the repo but here’s the relevant code

import React, { createContext, ReactElement, useContext } from "react";
import { Web3Provider } from "@ethersproject/providers";
import { Magic } from "magic-sdk";

interface Props {
  children: ReactElement | ReactElement[];
}

interface State {
  magic?: Magic;
}

export const MagicContext = createContext({} as State);

export function useMagicContext(): State {
  return useContext(MagicContext);
}

function MagicProvider({ children }: Props) {
  // WARNING: This requires a domain whitelist to be set up on https://dashboard.magic.link/
  // Otherwise users are vulnerable to a phishing attack on other websites
  const MAGIC_API_KEY = "pk_test_5EF8CA802033ADE6";

  const customNodeOptions = {
    rpcUrl: "http://127.0.0.1:8545",
    chainId: 31337, // networkId of buidlerEVM test network
  };

  const magic = new Magic(MAGIC_API_KEY, {
    network: customNodeOptions,
  });

  return (
    <MagicContext.Provider value={{ magic }}>{children}</MagicContext.Provider>
  );
}

export const useProvider = (): Web3Provider | null => {
  const { magic } = useMagicContext();
  return typeof magic?.rpcProvider !== "undefined"
    ? new Web3Provider(magic.rpcProvider)
    : null;
};

export default MagicProvider;

🌎 Environment

Software Version(s)
magic-sdk 2.4.8
Browser N/A
yarn 1.22.4
Operating System Linux

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
smithkicommented, Aug 15, 2021

I’m on the latest version, but I’m also getting this error. I’m a bit worried using any will result in a mysterious bug later. Is there a fix?

This one fell off the map for us a bit as it’s strictly related to typings and has a known workaround (casting to any). I know it’s not the prettiest solution, but considering that the Web3 provider interface is stabilized and standardized by the Ethereum foundation, I’m comfortable saying that the any typing shouldn’t cause you backwards compatibility issues in the future. We are open to a PR to update the typings, if you’re game!

0reactions
gigameshcommented, Aug 15, 2021

All good. thanks for the crazy fast response!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enable TS errors when ethers v5 contract object does not ...
This line in the Contract class is pulling back the power of typescript. const rainbowInstance = RainbowFactory.connect(address, provider); ...
Read more >
Error Codes - ethers
All errors in ethers are created by the Logger class, which includes a number of ... check the address and network you are...
Read more >
Documentation - Ethers.js
Provider, A Provider (in ethers) is a class which provides an abstraction for a connection to the Ethereum Network. It provides read-only access...
Read more >
I am getting a transaction was replaced error in ether js while ...
js to create a dapp.I want to input something in my database when something modifies on a blockchain.So I am using this for...
Read more >
ethers-io/Lobby - Gitter
@mormone how are you creating your provider? That error means you are connecting to a JSON-RPC backend that ia either not running or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found