[Ethereum] How to call the smart contract method using new metamask

ethereum-wallet-dappmetamaskweb3js

I am able to connect or call the send txn method of ethereum using new metamask script. But i m not able to call the method of any of custom contract.
Can someone tell me how can i do in javascript?

Thanks

Best Answer

I am not sure what does your new metamask mean, however I can show you how to call an contract method in 2022:

( assuming you are calling a contract 's mint method )

    async function loadWeb3() {
      if (window.ethereum) {
        window.web3 = new Web3(window.ethereum);
        window.ethereum.enable();
      }
    }

    async function loadContract() {
      let abi = [] // your abi here
      let address = "0xa1b2c3d4..."  // your contract address here
      return await new window.web3.eth.Contract(abi, address);
    }
    async function getCurrentAccount() {
      const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
      return accounts[0];
    }

    async function run() {
      await loadWeb3();
      window.contract = await loadContract();
      const account = await getCurrentAccount();
      let result = await window.contract.methods.mint().send({ from: account });
    }

    if (typeof window.ethereum !== 'undefined') {
      run()
    }

refer to: http://siwei.me/blog/posts/blockchain-web3-metamask-contract

Related Topic