I am trying to interact with my smart contract for my nextjs app. I cannot interact with the contract because my alchemy provider is not working.

Attempt 1:

const provider = new ethers.providers.AlchemyProvider(
    "sepolia",
    process.env.ALCHEMY_API_KEY
);

Error: unsupported network (argument="network", value={"name":"sepolia","chainId":11155111}, code=INVALID_ARGUMENT, version=providers/5.7.2) Attempt 2:

const alchemyProvider = new ethers.providers.JsonRpcProvider(process.env.ALCHEMY_URL);
export const contract = new ethers.Contract(
    contractAddress,
    contractABI,
    provider
);

Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.7.2)


Attempt 1 immediately breaks. Attempt 2 breaks when I try to run a function from the contract. I can console.log() the contract and it is correct. No matter what I put as the parameter it will still initialize the contract.

4

There are 4 best solutions below

1
Dev Shah On

It is not happening with sepolia mate, try goerli. worked for me

1
karlrez On

I'm not using nextjs, but I got it working on my React app like this:

import { ethers } from "ethers";

const provider = new ethers.AlchemyProvider("sepolia", process.env.ALCHEMY_API_KEY)
0
Alireza Sattari On

The Sepolia network, which is a testnet for the Ethereum 2.0 beacon chain. However, as of now the AlchemyProvider does not support this network, as it only supports the mainnet and some common testnets such as Rinkeby, Ropsten, Kovan and Goerli.

https://docs.alchemy.com/docs/ethers-js-provider

0
Eric On

You need ethers v6 to support sepolia testnet.

e.g. in package.json:

"ethers": "^6.8.1",

And the api changed bit, you call ethers.AlchemyProvider() to get the alchemy provider, e.g:

const alchemyProvider = new ethers.AlchemyProvider(network = "sepolia", "your_api_key");

Doc: https://docs.ethers.org/v6/api/providers/