'How to connect to a deployed Polygon testnet contract?

I have a front-end / web3 application that connects to a deployed contract in the rinkeby ethereum networt.

That is possible with the Web3 library, and creating an instance of web3, like the code below:

web3.js

import Web3 from "web3";

window.ethereum.request({ method: "eth_requestAccounts" });

const web3 = new Web3(window.ethereum);

export default web3;

So, in another file, i connect to the deployed contract via contract address and ABI:

contract.js

import web3 from "./web3";

const address = "0x(...)fEb";
const abi = ["(...)"]
export default new web3.eth.Contract(abi, address);

Ok. Then i can call methods on my etherium contract.

But i would like to do it in the mumbai polygon testnet. I already have a deployed / verified contract, and then in the contract.js, i replaced the values, address and abi, but it doesn't work.

I saw something about magic sdk library, but the examples is kind different from this model, and i got kind confused.

So, someone have a hint about how to connect to a mumbai polygon testnet to front-end?

Thanks!



Solution 1:[1]

If you need to communicate with Polygon, you should use Matic.js and use this guide: https://medium.com/the-polygon-blog/beginner-friendly-tutorial-to-matic-js-58f7c24c9744

Answer to the question: To connect to Mumbai Polygon you use:

const polygonNodeOptions = {

  rpcUrl: 'https://rpc-mumbai.maticvigil.com/',

  chainId: 80001,

};

Solution 2:[2]

From https://mumbai.polygonscan.com/tx/xxxxxx Get the contract address from your deployed contract. which you can find in To: section

deployed contract

then

const getContract = new web3.eth.Contract(yourabi, deployedcontractAddress); const res = await getContract.methods.your-method().call();

Hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IdotMaster1
Solution 2