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
- Install magic-sdk and ethersv5 in a typescript environment
- 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
💻 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 theany
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!All good. thanks for the crazy fast response!