Argument of type 'EthereumProvider' is not assignable to parameter of type 'ExternalProvider | JsonRpcFetchFunc'.
See original GitHub issueHey 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:
- Created a year ago
- Comments:10 (4 by maintainers)
Top GitHub Comments
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:
and:
The problem is that Ethers’ expected provider function signature’s
method: string
is not strictly assignable to Ganache’smethod: "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
andid
types from theirsend
andsendAsync
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
, andrequest
acceptany
for the method param.Ganache could relax its types on
sendAsync
,send
, andrequest
to acceptstring
for the method param.Ethers could use Generics:
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 @ricmooThat 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 withas 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)
Correct 127.0.0.1 works however specifying localhost does not. See issue #3491.