How to connect ethers.js library with Rinkeby programmatically?
Asked Answered
R

3

8

According to official docs of ethers.js, this should be the way how to connect to a specific network like Rinkeby-testnet with custom data:

 const provider = ethers.getDefaultProvider(network, {
    etherscan: ETHERSCAN_API_KEY,
    infura: INFURA_API_KEY,

Also, this would be the way to get a signer in order to sign transactions:

 const signer = provider.getSigner()

However, there is now method "getSigner" available on the default provider.

TypeError: provider.getSigner is not a function

How to achieve that using ethers.js?

Regardless answered 11/6, 2021 at 19:9 Comment(0)
P
6

ethers.getDefaultProvider seems to be somehow broken or outdated. Instead, you should connect directly to a specific Provider, like this for Alchemy:

const provider = new ethers.providers.AlchemyProvider("rinkeby", apiKey)

or for Infura:

const provider = new ethers.providers.InfuraProvider("rinkeby", apiKey)

After this, it is easy to get a signer:

const signer = provider.getSigner()

or

const walletSigner = wallet.connect(provider);

You can read more about this here.

Polloch answered 11/6, 2021 at 21:35 Comment(2)
I've also been trying to get a signer from InfuraProvider, but keep getting an error "API provider does not support signing (operation="getSigner", code=UNSUPPORTED_OPERATION, version=providers/5.4.3)" Any idea why this might be happening?Tapley
@Tapley Please, check my answer below.Steno
S
8

getSigner() for InfuraProvider doesn't work, use this:

const infuraProvider = new ethers.providers.InfuraProvider(network, API_KEY);
const wallet = new ethers.Wallet(privateKey, infuraProvider);
const signer = wallet.connect(infuraProvider);
contract = new ethers.Contract(smartContractAddress, abi, signer);
Steno answered 13/9, 2021 at 5:10 Comment(0)
P
6

ethers.getDefaultProvider seems to be somehow broken or outdated. Instead, you should connect directly to a specific Provider, like this for Alchemy:

const provider = new ethers.providers.AlchemyProvider("rinkeby", apiKey)

or for Infura:

const provider = new ethers.providers.InfuraProvider("rinkeby", apiKey)

After this, it is easy to get a signer:

const signer = provider.getSigner()

or

const walletSigner = wallet.connect(provider);

You can read more about this here.

Polloch answered 11/6, 2021 at 21:35 Comment(2)
I've also been trying to get a signer from InfuraProvider, but keep getting an error "API provider does not support signing (operation="getSigner", code=UNSUPPORTED_OPERATION, version=providers/5.4.3)" Any idea why this might be happening?Tapley
@Tapley Please, check my answer below.Steno
T
5

This works for me:

const provider = new ethers.providers.JsonRpcProvider(url)
const signer = provider.getSigner()

Got to know about this from the Alchemy docs

ethers.js doc

Tawnyatawsha answered 6/2, 2022 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.