How to connect to a deployed Polygon testnet contract?

Viewed 1023

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?

2 Answers

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.

Related