Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript errors when creating provider in ethersv5 #126

Closed
3 tasks done
TomAFrench opened this issue Aug 23, 2020 · 6 comments
Closed
3 tasks done

Typescript errors when creating provider in ethersv5 #126

TomAFrench opened this issue Aug 23, 2020 · 6 comments
Assignees

Comments

@TomAFrench
Copy link

✅ 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:https://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
@smithki
Copy link
Contributor

smithki commented Aug 24, 2020

Possible dupe or related to #83?

For now, does casting magic.rpcProvider to any fix your issue?

@TomAFrench
Copy link
Author

Ah whoops, yeah this is a duplicate. I'm using // @ts-ignore for that line at the moment.

@smithki
Copy link
Contributor

smithki commented Aug 24, 2020

Ah whoops, yeah this is a duplicate. I'm using // @ts-ignore for that line at the moment.

This works, but I recommend casting to any so that any other type errors on that line become apparent 😃

I'll try to solve the typings issues between Web3JS and Ethers soon!

@gigamesh
Copy link

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?

@smithki
Copy link
Contributor

smithki commented 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!

@gigamesh
Copy link

All good. thanks for the crazy fast response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants