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.

Argument of type 'EthereumProvider' is not assignable to parameter of type 'ExternalProvider | JsonRpcFetchFunc'.

See original GitHub issue

Hey all.

I would love for there to be more typescript examples and more programmatic examples.

I’m trying to programmatically spin up a local blockchain with a contract for fuzz testing. I.e. I need the ability to randomly create addresses with funds and etc on demand. I do see a lot of the functions I need available, however, it’s quite a struggle to figure out how to get access to what I need to call. So some more examples would really be appreciated for the programmatic side.

However, I’m blocked right at the start trying to create an ethers Web3Provider with the error, Argument of type 'EthereumProvider' is not assignable to parameter of type 'ExternalProvider | JsonRpcFetchFunc'.

The code is as follows:

import { ethers } from "ethers";
import Ganache from "ganache";

export function start_private_chain(): ethers.providers.Web3Provider {
  const provider = Ganache.provider();
  return new ethers.providers.Web3Provider(provider);
}

Would appreciate any help here. Thanks.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
davidmurdochcommented, Jul 30, 2022

My hunch is that this is due to strictFunctionTypes enabled in your tsconfig.json (see also: strict).

Ganache’s provider types are very strict, which let’s us offer type hints like:

image

possible rpc methods

and:

image

an rpc method’s specific allowed params.

The problem is that Ethers’ expected provider function signature’s method: string is not strictly assignable to Ganache’s method: "eth_estimateGas" | "eth_sendTransaction" | ... .

This isn’t really an Ether’s problem though, nor is it a bug in Ganache. But it does make for a bad UX!

Additionally, Ether’s omits the jsonrpc and id types from their send and sendAsync function signatures (I suspect this is a bug in Ether’s types and this may need to be updated).

Possible fixes:

Ethers could relax its ExternalProvider type so that sendAsync, send, and request accept any for the method param.

  • downside is the types would now be too loose.

Ganache could relax its types on sendAsync, send, and request to accept string for the method param.

  • downside is that we loose detailed type checking

Ethers could use Generics:

export type ExternalProvider<T extends string> = {
    isMetaMask?: boolean;
    isStatus?: boolean;
    host?: string;
    path?: string;
    request?: (request: { method: T, params?: Array<any> }) => Promise<any>
}

export type JsonRpcFetchFunc<T extends string> = (method: T, params?: Array<any>) => Promise<any>;

export class Web3Provider<T extends string> extends JsonRpcProvider {
    readonly provider: ExternalProvider<T> | null = null;
    readonly jsonRpcFetchFunc: JsonRpcFetchFunc<T> | null = null;

    constructor(provider: ExternalProvider<T> | JsonRpcFetchFunc<T>, network?: Networkish) {
      //...
    }
    // ...
}

Something similar to the above would allow for the best of both worlds. It even opens up the possibility for Ethers to flow the strict type checking offered by Ganache (and maybe others?) in its own methods, like send. /cc @ricmoo

That said, there may be some other issues with the types, but the problems are only with the types, not with the run-time provider. You should be able to use it as you have now, but you’ll need to cast it with as any or (as unknown as ExternalProvider).

Let’s keep this open so the team here can discuss options and other potential solutions I haven’t thought of.

Re: docs for programmatic use… you are right; we are lacking in that department… docs are in the works! (cc @MicaiahReid)

1reaction
gluaxcommented, Aug 3, 2022

Correct 127.0.0.1 works however specifying localhost does not. See issue #3491.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type 'Web3EthereumProvider' is not assignable to ... - GitHub
I'm trying to follow the web3 integration cookbook receipt in Typescript: this.provider = new eth.providers.Web3Provider(opts.web3.
Read more >
Argument of type 'WalletConnectProvider' is not assignable to ...
This issue is due to typescripts typechecking, one hack to bypass it is to typecast provder as <any>provider in this.web3 = new ...
Read more >
Type error when passing web3provider as a prop with typescript
Type 'Web3Provider' is not assignable to type 'ExternalProvider'. Types of property 'send' are incompatible. Type '(method: string, params: ...
Read more >
Ethereum Provider API - MetaMask Docs
MetaMask injects a global API into websites visited by its users at window.ethereum . This API allows websites to request users' Ethereum ...
Read more >
Providers — ethers.js 4.0.0 documentation
If you are not running your own local Ethereum node, it is recommended that you ... and types that are commonly used as...
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